summaryrefslogtreecommitdiffstats
path: root/src/term.rs
blob: 7ec366c6da6ac8db91191d722c5b63cd871555ec (plain)
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
use unicode_width::UnicodeWidthStr;

use std::io::{Stdout, Write};
use termion;
use termion::screen::AlternateScreen;

pub trait ScreenExt: Write {
    fn cursor_hide(&mut self) {
        write!(self, "{}", termion::cursor::Hide).unwrap();
    }
    fn cursor_show(&mut self) {
        write!(self, "{}", termion::cursor::Show).unwrap();
    }
    fn reset(&mut self) {
        write!(self, "{}", termion::style::Reset).unwrap();
    }
}

impl ScreenExt for AlternateScreen<Box<Stdout>> {}

pub fn xsize() -> usize {
    let (xsize, _) = termion::terminal_size().unwrap();
    xsize as usize
}

pub fn ysize() -> usize {
    let (_, ysize) = termion::terminal_size().unwrap();
    ysize as usize
}

pub fn sized_string(string: &str, xsize: u16) -> String {
    let lenstr: String = string.chars().fold("".into(), |acc, ch| {
        if acc.width() + 1 >= xsize as usize {
            acc
        } else {
            acc + &ch.to_string()
        }
    });
    lenstr
}

// Do these as constants

pub fn highlight_color() -> String {
    format!(
        "{}",
        termion::color::Fg(termion::color::LightGreen),
        //termion::color::Bg(termion::color::Black)
    )
}

pub fn normal_color() -> String {
    format!(
        "{}",
        termion::color::Fg(termion::color::LightBlue),
        //termion::color::Bg(termion::color::Black)
    )
}

pub fn from_lscolor(color: &lscolors::Color) -> String {
    match color {
        lscolors::Color::Black => format!("{}", termion::color::Fg(termion::color::Black)),
        lscolors::Color::Red => format!("{}", termion::color::Fg(termion::color::Red)),
        lscolors::Color::Green => format!("{}", termion::color::Fg(termion::color::Green)),
        lscolors::Color::Yellow => format!("{}", termion::color::Fg(termion::color::Yellow)),
        lscolors::Color::Blue => format!("{}", termion::color::Fg(termion::color::Blue)),
        lscolors::Color::Magenta => format!("{}", termion::color::Fg(termion::color::Magenta)),
        lscolors::Color::Cyan => format!("{}", termion::color::Fg(termion::color::Cyan)),
        lscolors::Color::White => format!("{}", termion::color::Fg(termion::color::White)),
        _ => format!("{}", normal_color()),
    }
}

pub fn cursor_left(n: usize) -> String {
    format!("{}", termion::cursor::Left(n as u16))
}

pub fn gotoy(y: usize) -> String {
    format!("{}", termion::cursor::Goto(1, y as u16))
}

pub fn goto_xy(x: u16, y: u16) -> String {
    format!("{}", termion::cursor::Goto(x, y))
}

// pub fn move_top() -> String {
//     gotoy(1)
// }

pub fn move_bottom() -> String {
    gotoy(ysize())
}

pub fn reset() -> String {
    format!("{}", termion::style::Reset)
}

pub fn invert() -> String {
    format!("{}", termion::style::Invert)
}

pub fn header_color() -> String {
    format!(
        "{}{}",
        termion::color::Fg(termion::color::White),
        termion::color::Bg(termion::color::Blue)
    )
}

pub fn status_bg() -> String {
    format!("{}", termion::color::Bg(termion::color::LightBlue))
}