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
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5-cli/tty.hpp"
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <cstdlib>
namespace libdnf5::cli::tty {
int get_width() {
// Use a custom "FORCE_COLUMNS" variable for testing purposes.
// "COLUMNS" is overwritten in a sub-shell and that makes testing more difficult
char * columns = std::getenv("FORCE_COLUMNS");
if (columns != nullptr) {
try {
return std::stoi(columns);
} catch (std::invalid_argument & ex) {
} catch (std::out_of_range & ex) {
}
}
struct winsize size;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) {
return size.ws_col;
}
return 80;
}
bool is_interactive() {
// Use a custom "DNF5_FORCE_INTERACTIVE" variable for testing purposes.
// "interactivity" depends on stdout configuration which is hard to control sometimes
char * force_interactive = std::getenv("DNF5_FORCE_INTERACTIVE");
if (force_interactive != nullptr) {
try {
// Convert to an int which is then converted to bool,
// so when defined accept 0 as FALSE and non 0 as TRUE
return std::stoi(force_interactive);
} catch (std::invalid_argument & ex) {
} catch (std::out_of_range & ex) {
}
}
return isatty(fileno(stdout)) == 1;
}
static ColoringEnabled coloring = ColoringEnabled::AUTO;
void coloring_enable(ColoringEnabled value) {
coloring = value;
}
bool is_coloring_enabled() {
return coloring == ColoringEnabled::AUTO ? is_interactive() : coloring == ColoringEnabled::ALWAYS;
}
#define TTY_COMMAND(name, escape_code) \
std::ostream & name(std::ostream & stream) { \
if (is_interactive()) { \
stream << escape_code; \
} \
return stream; \
}
// tty::reset
TTY_COMMAND(reset, "\033[0m")
// tty::bold
TTY_COMMAND(bold, "\033[1m")
// tty::underline
TTY_COMMAND(underline, "\033[4m")
// tty::blink
TTY_COMMAND(blink, "\033[5m")
// tty::black
TTY_COMMAND(black, "\033[30m")
// tty::red
TTY_COMMAND(red, "\033[31m")
// tty::green
TTY_COMMAND(green, "\033[32m")
// tty::yellow
TTY_COMMAND(yellow, "\033[33m")
// tty::blue
TTY_COMMAND(blue, "\033[34m")
// tty::magenta
TTY_COMMAND(magenta, "\033[35m")
// tty::cyan
TTY_COMMAND(cyan, "\033[36m")
// tty::white
TTY_COMMAND(white, "\033[37m")
// tty::clear_line
TTY_COMMAND(clear_line, "\033[2K")
// tty::clear_to_end
TTY_COMMAND(clear_to_end, "\033[0J")
// tty::cursor_up
TTY_COMMAND(cursor_up, "\x1b[A")
// tty::cursor_down
TTY_COMMAND(cursor_down, "\x1b[B")
// tty::cursor_hide
TTY_COMMAND(cursor_hide, "\x1b[?25l")
// tty::cursor_show
TTY_COMMAND(cursor_show, "\x1b[?25h")
} // namespace libdnf5::cli::tty
|