Coverage Report

Created: 2025-04-22 06:17

/src/neomutt/gui/terminal.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file
3
 * Set the terminal title/icon
4
 *
5
 * @authors
6
 * Copyright (C) 2018-2023 Richard Russon <rich@flatcap.org>
7
 * Copyright (C) 2020 Pietro Cerutti <gahr@gahr.ch>
8
 *
9
 * @copyright
10
 * This program is free software: you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License as published by the Free Software
12
 * Foundation, either version 2 of the License, or (at your option) any later
13
 * version.
14
 *
15
 * This program is distributed in the hope that it will be useful, but WITHOUT
16
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18
 * details.
19
 *
20
 * You should have received a copy of the GNU General Public License along with
21
 * this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
/**
25
 * @page gui_terminal Set the terminal title/icon
26
 *
27
 * Set the terminal title/icon
28
 */
29
30
#include "config.h"
31
#include <stdbool.h>
32
#include <stdio.h>
33
#include "mutt/lib.h"
34
#include "terminal.h"
35
#include "mutt_curses.h"
36
#ifdef HAVE_NCURSESW_NCURSES_H
37
#include <ncursesw/term.h>
38
#elif defined(HAVE_NCURSES_NCURSES_H)
39
#include <ncurses/term.h>
40
#endif
41
42
bool TsSupported; ///< Terminal Setting is supported
43
44
/* de facto standard escapes for tsl/fsl */
45
/// TSL: to_status_line - Sent before the terminal title
46
static const char *TSL = "\033]0;"; // Escape
47
/// FSL: from_status_line - Sent after the terminal title
48
static const char *FSL = "\007"; // Ctrl-G (BEL)
49
50
/**
51
 * mutt_tigetstr - Get terminal capabilities
52
 * @param name Name of capability
53
 * @retval str Capability
54
 *
55
 * @note Do not free the returned string
56
 */
57
const char *mutt_tigetstr(const char *name)
58
0
{
59
0
  char *cap = tigetstr(name);
60
0
  if (!cap || (cap == (char *) -1) || (*cap == '\0'))
61
0
    return NULL;
62
63
0
  return cap;
64
0
}
65
66
/**
67
 * mutt_ts_capability - Check terminal capabilities
68
 * @retval true Terminal is capable of having its title/icon set
69
 *
70
 * @note This must happen after the terminfo has been initialised.
71
 */
72
bool mutt_ts_capability(void)
73
0
{
74
0
  static const char *known[] = {
75
0
    "color-xterm", "cygwin", "eterm",  "kterm", "nxterm",
76
0
    "putty",       "rxvt",   "screen", "xterm", NULL,
77
0
  };
78
79
0
#ifdef HAVE_USE_EXTENDED_NAMES
80
  /* If tsl is set, then terminfo says that status lines work. */
81
0
  const char *tcaps = mutt_tigetstr("tsl");
82
0
  if (tcaps)
83
0
  {
84
    /* update the static definitions of tsl/fsl from terminfo */
85
0
    TSL = tcaps;
86
87
0
    tcaps = mutt_tigetstr("fsl");
88
0
    if (tcaps)
89
0
      FSL = tcaps;
90
91
0
    return true;
92
0
  }
93
94
  /* If XT (boolean) is set, then this terminal supports the standard escape. */
95
  /* Beware: tigetflag returns -1 if XT is invalid or not a boolean. */
96
0
  int tcapi = tigetflag("XT");
97
0
  if (tcapi == 1)
98
0
    return true;
99
0
#endif
100
101
  /* Check term types that are known to support the standard escape without
102
   * necessarily asserting it in terminfo. */
103
0
  const char *term = mutt_str_getenv("TERM");
104
0
  for (const char **termp = known; *termp; termp++)
105
0
  {
106
0
    if (term && !mutt_istr_startswith(term, *termp))
107
0
      return true;
108
0
  }
109
110
0
  return false;
111
0
}
112
113
/**
114
 * mutt_ts_status - Set the text of the terminal title bar
115
 * @param str Text to set
116
 *
117
 * @note To clear the text, set the title to a single space.
118
 */
119
void mutt_ts_status(char *str)
120
0
{
121
0
  if (!str || (*str == '\0'))
122
0
    return;
123
124
0
  fprintf(stderr, "%s%s%s", TSL, str, FSL);
125
0
}
126
127
/**
128
 * mutt_ts_icon - Set the icon in the terminal title bar
129
 * @param str Icon string
130
 *
131
 * @note To clear the icon, set it to a single space.
132
 */
133
void mutt_ts_icon(char *str)
134
0
{
135
0
  if (!str || (*str == '\0'))
136
0
    return;
137
138
  /* icon setting is not supported in terminfo, so hardcode the escape */
139
0
  fprintf(stderr, "\033]1;%s\007", str); // Escape
140
0
}