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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
** Copyright (c) 2020 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
** 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.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains code used to query terminal info
*/
#include "config.h"
#include "terminal.h"
#include <assert.h>
#ifdef _WIN32
# include <windows.h>
#else
#ifdef __EXTENSIONS__
#include <termio.h>
#endif
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#endif
#if INTERFACE
/*
** Terminal size defined in terms of columns and lines.
*/
struct TerminalSize {
unsigned int nColumns; /* Number of characters on a single line */
unsigned int nLines; /* Number of lines */
};
#endif
/* Get the current terminal size by calling a system service.
**
** Return 1 on success. This sets the size parameters to the values returned by
** the system call, when such is supported; set the size to zero otherwise.
** Return 0 on the system service call failure.
**
** Under Linux/bash the size info is also available from env $LINES, $COLUMNS.
** Or it can be queried using tput `echo -e "lines\ncols"|tput -S`.
** Technically, this info could be cached, but then we'd need to handle
** SIGWINCH signal to requery the terminal on resize event.
*/
int terminal_get_size(TerminalSize *t){
memset(t, 0, sizeof(*t));
#if defined(TIOCGSIZE)
{
struct ttysize ts;
if( ioctl(STDIN_FILENO, TIOCGSIZE, &ts)>=0
|| ioctl(STDOUT_FILENO, TIOCGSIZE, &ts)>=0
|| ioctl(STDERR_FILENO, TIOCGSIZE, &ts)>=0
){
t->nColumns = ts.ts_cols;
t->nLines = ts.ts_lines;
return 1;
}
return 0;
}
#elif defined(TIOCGWINSZ)
{
struct winsize ws;
if( ioctl(STDIN_FILENO, TIOCGWINSZ, &ws)>=0
|| ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)>=0
|| ioctl(STDERR_FILENO, TIOCGWINSZ, &ws)>=0
){
t->nColumns = ws.ws_col;
t->nLines = ws.ws_row;
return 1;
}
return 0;
}
#elif defined(_WIN32)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)
|| GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)
|| GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbi)
){
t->nColumns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
t->nLines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return 1;
}
return 0;
}
#else
return 1;
#endif
}
/*
** Return the terminal's current width in columns when available, otherwise
** return the specified default value.
*/
unsigned int terminal_get_width(unsigned int nDefault){
TerminalSize ts;
if( terminal_get_size(&ts) ){
return ts.nColumns;
}
return nDefault;
}
/*
** Return the terminal's current height in lines when available, otherwise
** return the specified default value.
*/
unsigned int terminal_get_height(unsigned int nDefault){
TerminalSize ts;
if( terminal_get_size(&ts) ){
return ts.nLines;
}
return nDefault;
}
/*
** Return true if it is reasonable is emit VT100 escape codes.
*/
int terminal_is_vt100(void){
char *zNoColor;
#ifdef _WIN32
if( !win32_terminal_is_vt100(1) ) return 0;
#endif /* _WIN32 */
if( !fossil_isatty(1) ) return 0;
zNoColor =fossil_getenv("NO_COLOR");
if( zNoColor==0 ) return 1;
if( zNoColor[0]==0 ) return 1;
if( is_false(zNoColor) ) return 1;
return 0;
}
#ifdef _WIN32
/*
** Return true if the Windows console supports VT100 escape codes.
**
** Support for VT100 escape codes is enabled by default in Windows Terminal
** on Windows 10 and Windows 11, and disabled by default in Legacy Consoles
** and on older versions of Windows. Programs can turn on VT100 support for
** Legacy Consoles using the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.
**
** NOTE: If this function needs to be called in more complex scenarios with
** reassigned stdout and stderr streams, the following CRT calls are useful
** to translate from CRT streams to file descriptors and to Win32 handles:
**
** HANDLE hOutputHandle = (HANDLE)_get_osfhandle(_fileno(<FILE*>));
*/
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
int win32_terminal_is_vt100(int fd){
HANDLE hConsole = NULL;
DWORD dwConsoleMode;
switch( fd ){
case 1:
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
break;
case 2:
hConsole = GetStdHandle(STD_ERROR_HANDLE);
break;
}
if( GetConsoleMode(hConsole,&dwConsoleMode) ){
return (dwConsoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)!=0;
}
return 0;
}
#endif /* _WIN32 */
|