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