Skip to main content

workflow_terminal/
clear.rs

1//!
2//! Clear line helper (ANSI escape codes to clear the terminal line)
3//!
4
5use std::fmt;
6
7/// Renders the ANSI escape sequence that erases the current terminal line
8/// and returns the cursor to the start of the line.
9pub struct ClearLine;
10
11impl fmt::Display for ClearLine {
12    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13        write!(f, "\x1B[2K\r")
14    }
15}
16
17impl AsRef<[u8]> for ClearLine {
18    fn as_ref(&self) -> &'static [u8] {
19        "\x1B[2K\r".as_bytes()
20    }
21}
22
23impl AsRef<str> for ClearLine {
24    fn as_ref(&self) -> &'static str {
25        "\x1B[2K\r"
26    }
27}
28
29/// Renders the ANSI escape sequence that clears the entire terminal screen.
30pub struct ClearScreen;
31
32impl fmt::Display for ClearScreen {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        write!(f, "\x1B[2J")
35    }
36}
37
38impl AsRef<[u8]> for ClearScreen {
39    fn as_ref(&self) -> &'static [u8] {
40        "\x1B[2J".as_bytes()
41    }
42}
43
44impl AsRef<str> for ClearScreen {
45    fn as_ref(&self) -> &'static str {
46        "\x1B[2J"
47    }
48}