color! macro

pull/14/head
dvkt 4 years ago
parent b8a38efe30
commit adc3d8fca4

@ -1,5 +1,14 @@
use std::fmt;
/// Shortcut to produce a String of a certain color that resets.
/// Example:
/// let s = color!(Red, "Red string");
macro_rules! color {
($color:ident, $s:expr) => {
format!("{}{}{}", color::$color, $s, color::Reset)
};
}
// Create a color:: struct that can be used with format!.
macro_rules! define_color {
($name:ident, $code:literal) => {

@ -2,8 +2,9 @@
#[macro_use]
pub mod utils;
pub mod bookmarks;
#[macro_use]
pub mod color;
pub mod bookmarks;
pub mod config;
pub mod gopher;
pub mod help;

@ -1,5 +1,8 @@
use crate::gopher::{self, Type};
use crate::ui::{Action, Key, View, MAX_COLS, SCROLL_LINES};
use crate::{
color,
ui::{Action, Key, View, MAX_COLS, SCROLL_LINES},
};
use std::fmt;
use termion::{clear, cursor};
@ -144,17 +147,6 @@ impl Menu {
fn render_lines(&self) -> String {
let mut out = String::new();
macro_rules! push {
($c:expr, $e:expr) => {{
out.push_str("\x1b[");
out.push_str($c);
out.push('m');
out.push_str(&$e);
out.push_str("\x1b[0m");
}};
}
let iter = self.lines.iter().skip(self.scroll).take(self.rows() - 1);
let indent = self.indent();
let left_margin = " ".repeat(indent);
@ -186,17 +178,19 @@ impl Menu {
} else {
line.name.to_string()
};
match line.typ {
Type::Text => push!("96", name),
Type::Menu => push!("94", name),
Type::Info => push!("93", name),
Type::HTML => push!("92", name),
Type::Error => push!("91", name),
Type::Telnet => push!("4;97;90", name),
typ if typ.is_download() => push!("4;97", name),
typ if !typ.is_supported() => push!("107;91", name),
_ => push!("0", name),
}
// color the line
out.push_str(&match line.typ {
Type::Text => color!(White, name),
Type::Menu => color!(Blue, name),
Type::Info => color!(Yellow, name),
Type::HTML => color!(Green, name),
Type::Error => color!(Red, name),
Type::Telnet => color!(Grey, name),
typ if typ.is_download() => color!(Underline, color!(White, name)),
typ if !typ.is_supported() => color!(Red, color!(WhiteBG, name)),
_ => name,
});
// clear rest of line
out.push_str(&format!("{}", clear::UntilNewline));

Loading…
Cancel
Save