use super::traits::WindowLike;
use ncurses::*;
use unicode_width::UnicodeWidthStr;
pub enum ColorPair {
Statusbar,
Default,
Titlebar,
Internal,
Highlight,
ExitWindow,
InfoWindow,
ErrorWindow,
}
impl ColorPair {
pub fn raw(&self) -> i16 {
match self {
ColorPair::Statusbar => 1,
ColorPair::Default => 2,
ColorPair::Titlebar => 3,
ColorPair::Internal => 4,
ColorPair::Highlight => 5,
ColorPair::ExitWindow => 6,
ColorPair::InfoWindow => 7,
ColorPair::ErrorWindow => 8,
}
}
}
pub(super) fn initialize() {
initscr();
raw();
setlocale(LcCategory::all, "");
keypad(stdscr(), true);
noecho();
curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE);
cbreak();
start_color();
use_default_colors();
if !(COLORS() >= 255) {
init_pair(ColorPair::Statusbar.raw(), COLOR_CYAN, COLOR_BLACK);
init_pair(ColorPair::Titlebar.raw(), COLOR_GREEN, COLOR_BLACK);
init_pair(ColorPair::Default.raw(), COLOR_WHITE, COLOR_BLACK);
init_pair(ColorPair::Internal.raw(), COLOR_BLACK, COLOR_WHITE);
init_pair(ColorPair::Highlight.raw(), COLOR_WHITE, COLOR_BLUE);
init_pair(ColorPair::ExitWindow.raw(), COLOR_WHITE, COLOR_RED);
init_pair(ColorPair::InfoWindow.raw(), COLOR_WHITE, COLOR_GREEN);
init_pair(ColorPair::ErrorWindow.raw(), COLOR_WHITE, COLOR_RED);
} else {
init_pair(ColorPair::Statusbar.raw(), 194, 240);
init_pair(ColorPair::Titlebar.raw(), 195, 240);
init_pair(ColorPair::Default.raw(), 231, 235);
init_pair(ColorPair::Internal.raw(), 235, 253);
init_pair(ColorPair::Highlight.raw(), 253, 33);
init_pair(ColorPair::ExitWindow.raw(), 194, 202);
init_pair(ColorPair::InfoWindow.raw(), 194, 64);
init_pair(ColorPair::ErrorWindow.raw(), 194, 202);
}
color_set(ColorPair::Default.raw());
timeout(100);
}
pub(super) fn recreate_window<W>(w: &mut W)
where
W: WindowLike,
{
w.del();
if let Some(w) = w.raw() {
delwin(w);
}
w.create();
}
pub(super) fn create_window(lines: i32, cols: i32, y: i32, x: i32, colorpair: i16) -> WINDOW {
let w = newwin(lines, cols, y, x);
wcolor_set(w, colorpair);
wclear(w);
wbkgd(w, COLOR_PAIR(colorpair));
w
}
pub trait StringExt {
fn unicode_truncate(&self, cols: i32) -> String;
}
impl StringExt for &str {
fn unicode_truncate(&self, cols: i32) -> String {
unicode_truncate(self, cols)
}
}
impl StringExt for String {
fn unicode_truncate(&self, cols: i32) -> String {
unicode_truncate(self, cols)
}
}
fn unicode_truncate(value: &str, cols: i32) -> String {
let mut s = value.to_string();
let mut truncated = false;
while s.width() > (cols - 8) as usize {
s.pop();
truncated = true;
}
if truncated {
s.push_str("...");
}
s
}