Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
terminal.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024, Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024, Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
11#pragma once
12
13#ifndef _WIN32
14# include <unistd.h>
15
16# include <sys/ioctl.h>
17#else
18# include <io.h>
19# include <stdio.h>
20#endif
21
22#include <sharg/platform.hpp>
23
24namespace sharg::detail
25{
26
33inline bool stdin_is_terminal()
34{
35#ifndef _WIN32
36 return isatty(STDIN_FILENO);
37#else
38 return _isatty(_fileno(stdin));
39#endif
40}
41
48inline bool stdout_is_terminal()
49{
50#ifndef _WIN32
51 return isatty(STDOUT_FILENO);
52#else
53 return _isatty(_fileno(stdout));
54#endif
55}
56
63inline bool stderr_is_terminal()
64{
65#ifndef _WIN32
66 return isatty(STDERR_FILENO);
67#else
68 return _isatty(_fileno(stderr));
69#endif
70}
71
72// ----------------------------------------------------------------------------
73// Function get_terminal_size()
74// ----------------------------------------------------------------------------
75
86inline unsigned get_terminal_width()
87{
88#ifndef _WIN32
89
90 struct winsize w;
91 w.ws_row = 0;
92 w.ws_col = 0;
93
94 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
95
96 return w.ws_col;
97#else
98 return 80; // Not implemented for Windows yet. For inspiration, see https://stackoverflow.com/a/12642749.
99#endif
100}
101
102} // namespace sharg::detail
bool stdout_is_terminal()
Check whether the standard output is interactive.
Definition terminal.hpp:48
bool stderr_is_terminal()
Check whether the standard error output is interactive.
Definition terminal.hpp:63
bool stdin_is_terminal()
Check whether the standard input is interactive.
Definition terminal.hpp:33
unsigned get_terminal_width()
Retrieve size of terminal.
Definition terminal.hpp:86
Provides platform and dependency checks.
Hide me