use our own color lib

pull/14/head
dvkt 4 years ago
parent 9a12de70d2
commit b8a38efe30

@ -0,0 +1,44 @@
use std::fmt;
// Create a color:: struct that can be used with format!.
macro_rules! define_color {
($name:ident, $code:literal) => {
pub struct $name;
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\x1b[{}m", $code)
}
}
};
}
define_color!(Reset, 0);
define_color!(Bold, 1);
define_color!(Underline, 4);
define_color!(Grey, 90);
define_color!(Red, 91);
define_color!(Green, 92);
define_color!(Yellow, 93);
define_color!(Blue, 94);
define_color!(Magenta, 95);
define_color!(Cyan, 96);
define_color!(White, 97);
define_color!(Black, 30);
define_color!(DarkRed, 31);
define_color!(DarkGreen, 32);
define_color!(DarkYellow, 33);
define_color!(DarkBlue, 34);
define_color!(DarkMagenta, 35);
define_color!(DarkCyan, 36);
define_color!(DarkWhite, 37);
define_color!(BlackBG, 40);
define_color!(RedBG, 41);
define_color!(GreenBG, 42);
define_color!(YellowBG, 43);
define_color!(BlueBG, 44);
define_color!(MagentaBG, 45);
define_color!(CyanBG, 46);
define_color!(WhiteBG, 47);

@ -3,6 +3,7 @@
#[macro_use]
pub mod utils;
pub mod bookmarks;
pub mod color;
pub mod config;
pub mod gopher;
pub mod help;

@ -4,7 +4,7 @@ pub use self::action::Action;
pub use self::view::View;
use crate::{
bookmarks,
bookmarks, color,
gopher::{self, Type},
help, history,
menu::Menu,
@ -20,7 +20,6 @@ use std::{
time::Duration,
};
use termion::{
color,
input::TermRead,
raw::{IntoRawMode, RawTerminal},
screen::AlternateScreen,
@ -118,12 +117,7 @@ impl UI {
self.status.clear();
}
if let Err(e) = self.process_action(action) {
self.set_status(format!(
"{}{}{}",
color::Fg(color::LightRed),
e,
termion::cursor::Hide
));
self.set_status(format!("{}{}{}", color::Red, e, termion::cursor::Hide));
}
}
@ -258,7 +252,7 @@ impl UI {
label,
".".repeat(i),
termion::clear::UntilNewline,
color::Fg(color::Reset),
color::Reset,
termion::cursor::Show,
);
stdout().flush();
@ -304,8 +298,8 @@ impl UI {
return Some(format!(
"{}{}{}{}{}",
termion::cursor::Goto(self.cols() - 3, self.rows()),
color::Fg(color::Black),
color::Bg(color::Green),
color::Black,
color::GreenBG,
"TLS",
"\x1b[0m"
));
@ -315,14 +309,13 @@ impl UI {
fn render_status(&self) -> String {
format!(
"{}{}{}{}{}{}{}",
"{}{}{}{}{}{}",
termion::cursor::Hide,
termion::cursor::Goto(1, self.rows()),
termion::clear::CurrentLine,
self.status,
self.render_tls_status().unwrap_or_else(|| "".into()),
color::Fg(color::Reset),
color::Bg(color::Reset),
color::Reset,
)
}
@ -345,7 +338,7 @@ impl UI {
write!(
out,
"{}{}{}{} [Y/n]: {}",
color::Fg(color::Reset),
color::Reset,
termion::cursor::Goto(1, rows),
termion::clear::CurrentLine,
question,
@ -373,7 +366,7 @@ impl UI {
write!(
out,
"{}{}{}{}{}{}",
color::Fg(color::Reset),
color::Reset,
termion::cursor::Goto(1, rows),
termion::clear::CurrentLine,
prompt,
@ -577,13 +570,7 @@ impl Default for UI {
impl Drop for UI {
fn drop(&mut self) {
let mut out = self.out.borrow_mut();
write!(
out,
"{}{}{}",
color::Fg(color::Reset),
color::Bg(color::Reset),
termion::cursor::Show,
);
write!(out, "{}{}", color::Reset, termion::cursor::Show,);
out.flush();
}
}

Loading…
Cancel
Save