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
17#include "shamcmdopt/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 shamcmdopt {
30
31 bool is_a_tty() {
32#if __has_include(<unistd.h>)
33 return isatty(fileno(stdout));
34#else
35 return true;
36#endif
37 }
38
42
43 void set_tty_columns(u32 columns) { tty_forced_width = columns; }
44
53 std::pair<u32, u32> get_tty_dim() {
54 // If forced width is set, return it
55 if (tty_forced_width > 0) {
56 return {10, tty_forced_width};
57 }
58
59 // If the current terminal is not a tty, return a default value
60 if (!is_a_tty()) {
61 return {10, 100};
62 }
63
64 // If the terminal is a tty, query its size
65#if __has_include(<sys/ioctl.h>) && __has_include(<unistd.h>)
66 struct winsize w;
67 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
68
69 // If the terminal size is invalid, return a default value
70 if (w.ws_col == 0 || w.ws_row == 0) {
71 return {10, 100};
72 }
73 return {w.ws_row, w.ws_col};
74#else
75 // If the terminal is a tty but we don't have a way to query its size,
76 // return a default value
77 return {10, 100};
78#endif
79 }
80
81 u32 get_tty_columns() { return get_tty_dim().second; }
82 u32 get_tty_lines() { return get_tty_dim().first; }
83
84} // namespace shamcmdopt
std::uint32_t u32
32 bit unsigned integer
namespace for cli utilities
Definition ci_env.hpp:19
void set_tty_columns(u32 columns)
Set the forced width of the terminal.
Definition tty.cpp:43
bool is_a_tty()
Test if current terminal is a tty.
Definition tty.cpp:31
std::pair< u32, u32 > get_tty_dim()
Get the number of columns and lines of the current terminal.
Definition tty.cpp:53
u32 tty_forced_width
Definition tty.cpp:41
u32 get_tty_lines()
Get the number of lines of the current terminal.
Definition tty.cpp:82
u32 get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:81
This file contains tty info getters.