Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
tty.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
17#include "sham/term/tty.hpp"
18#include <cstdio>
19#include <utility>
20
21#if __has_include(<unistd.h>)
22 #include <unistd.h>
23#endif
24
25#if __has_include(<sys/ioctl.h>)
26 #include <sys/ioctl.h>
27#endif
28
29namespace {
30
33 bool compute_is_a_tty() {
34#if __has_include(<unistd.h>)
35 return isatty(fileno(stdout));
36#else
37 return true;
38#endif
39 }
40
41} // namespace
42
43namespace sham::term {
44
45 bool is_a_tty() {
46 static bool cached = compute_is_a_tty();
47 return cached;
48 }
49
53
54 void set_tty_columns(int columns) { tty_forced_width = columns; }
55
64 std::pair<int, int> get_tty_dim() {
65 // If forced width is set, return it
66 if (tty_forced_width > 0) {
67 return {10, tty_forced_width};
68 }
69
70 // If the current terminal is not a tty, return a default value
71 if (!is_a_tty()) {
72 return {10, 100};
73 }
74
75 // If the terminal is a tty, query its size
76#if __has_include(<sys/ioctl.h>) && __has_include(<unistd.h>)
77 struct winsize w;
78 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
79
80 // If the terminal size is invalid, return a default value
81 if (w.ws_col == 0 || w.ws_row == 0) {
82 return {10, 100};
83 }
84 return {w.ws_row, w.ws_col};
85#else
86 // If the terminal is a tty but we don't have a way to query its size,
87 // return a default value
88 return {10, 100};
89#endif
90 }
91
92 int get_tty_columns() { return get_tty_dim().second; }
93 int get_tty_lines() { return get_tty_dim().first; }
94
96 bool utf8_supported = false;
97
98 void set_support_utf8(bool support) { utf8_supported = support; }
99 bool support_utf8() { return utf8_supported; }
100
101} // namespace sham::term
int tty_forced_width
Definition tty.cpp:52
bool utf8_supported
Whether the current terminal/locale supports UTF-8 output, as set by set_support_utf8.
Definition tty.cpp:96
std::pair< int, int > get_tty_dim()
Get the number of columns and lines of the current terminal.
Definition tty.cpp:64
This file contains tty info getters.
int get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:92
void set_tty_columns(int columns)
Set the forced width of the terminal.
Definition tty.cpp:54
void set_support_utf8(bool support)
Set whether the current terminal/locale supports UTF-8 output.
Definition tty.cpp:98
bool support_utf8()
Query whether the current terminal/locale supports UTF-8 output.
Definition tty.cpp:99
int get_tty_lines()
Get the number of lines of the current terminal.
Definition tty.cpp:93
bool is_a_tty()
Test if current terminal is a tty.
Definition tty.cpp:45