kibi/
ansi_escape.rs

1// SPDX-FileCopyrightText: 2020 Ilaï Deutel & Kibi Contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! # ANSI Escape sequences
6
7/// Switches to the main buffer.
8pub(crate) const USE_MAIN_SCREEN: &str = "\x1b[?1049l";
9
10/// Switches to a new alternate screen buffer.
11pub(crate) const USE_ALTERNATE_SCREEN: &str = "\x1b[?1049h";
12
13/// Reset the formatting
14pub(crate) const RESET: &str = "\x1b[m";
15
16/// White background: invert foreground and background color
17pub(crate) const WBG: &str = "\x1b[7m";
18
19/// Move the cursor to 1:1
20pub(crate) const MOVE_CURSOR_TO_START: &str = "\x1b[H";
21
22/// DECTCTEM: Make the cursor invisible
23pub(crate) const HIDE_CURSOR: &str = "\x1b[?25l";
24/// DECTCTEM: Make the cursor visible
25pub(crate) const SHOW_CURSOR: &str = "\x1b[?25h";
26
27/// Clear line right of the current position of the cursor
28pub(crate) const CLEAR_LINE_RIGHT_OF_CURSOR: &str = "\x1b[K";
29
30/// Report the cursor position to the application.
31pub(crate) const DEVICE_STATUS_REPORT: &str = "\x1b[6n";
32
33/// Reposition the cursor to the end of the window
34pub(crate) const REPOSITION_CURSOR_END: &str = "\x1b[999C\x1b[999B";
35
36pub(crate) fn push_colored(buffer: &mut String, color: &str, message: &str, use_color: bool) {
37    for s in &[if use_color { color } else { "" }, message, if use_color { RESET } else { "" }] {
38        buffer.push_str(s);
39    }
40}