Merge pull request #365 from GreasySlug/feature/color

Color changes due to features flag and use debug colors defined as constants
This commit is contained in:
Slug 2023-01-26 14:56:32 +09:00 committed by GitHub
commit dde998480b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 64 deletions

View file

@ -356,11 +356,11 @@ macro_rules! debug_enum_assert {
macro_rules! debug_info {
($output:ident) => {{
#[allow(unused_imports)]
use $crate::style::{CYAN, RESET};
use $crate::style::{colors::DEBUG, RESET};
write!(
$output,
"[{}DEBUG{}] {}:{:04}: ",
CYAN,
DEBUG,
RESET,
file!(),
line!()
@ -369,8 +369,8 @@ macro_rules! debug_info {
}};
() => {{
#[allow(unused_imports)]
use $crate::style::{CYAN, RESET};
print!("[{}DEBUG{}] {}:{:04}: ", CYAN, RESET, file!(), line!());
use $crate::style::{colors::DEBUG, RESET};
print!("[{}DEBUG{}] {}:{:04}: ", DEBUG, RESET, file!(), line!());
}};
}
@ -386,25 +386,25 @@ macro_rules! debug_info {
#[macro_export]
macro_rules! log {
(info $($arg: tt)*) => {{
$crate::log!(c GREEN, $($arg)*);
$crate::log!(c DEBUG_MAIN, $($arg)*);
}};
(err $($arg: tt)*) => {{
$crate::log!(c RED, $($arg)*);
$crate::log!(c DEBUG_ERROR, $($arg)*);
}};
(info_f $output:ident, $($arg: tt)*) => {{
$crate::log!(f+c $output, GREEN, $($arg)*);
$crate::log!(f+c $output, DEBUG_MAIN, $($arg)*);
}};
(err_f $output:ident, $($arg: tt)*) => {{
$crate::log!(f+c $output, RED, $($arg)*);
$crate::log!(f+c $output, DEBUG_ERROR, $($arg)*);
}};
(f $output: ident, $($arg: tt)*) => {{
if cfg!(feature = "debug") {
#[allow(unused_imports)]
use $crate::color::{RESET, GREEN, RED};
use $crate::color::{RESET, colors::DEBUG_MAIN, colors::DEBUG_ERROR};
$crate::debug_info!($output);
write!($output, $($arg)*).unwrap();
write!($output, "{}", RESET).unwrap(); // color color anyway
@ -415,7 +415,7 @@ macro_rules! log {
(c $color:ident, $($arg: tt)*) => {{
if cfg!(feature = "debug") {
#[allow(unused_imports)]
use $crate::style::{RESET, GREEN, RED};
use $crate::style::{RESET, colors::DEBUG_MAIN, colors::DEBUG_ERROR};
$crate::debug_info!();
print!("{}", $color);
println!($($arg)*);
@ -426,7 +426,7 @@ macro_rules! log {
(f+c $output:ident, $color:ident, $($arg: tt)*) => {{
if cfg!(feature = "debug") {
#[allow(unused_imports)]
use $crate::style::{RESET, GREEN};
use $crate::style::{RESET, colors::DEBUG_MAIN};
$crate::debug_info!($output);
write!($output, "{}{}{}", $color, $($arg)*, RESET).unwrap();
write!($output, $($arg)*).unwrap();

View file

@ -1,29 +1,44 @@
use self::colors::*;
use std::borrow::Cow;
pub const ATTR_RESET: &str = "\x1b[0m";
pub const BOLD: &str = "\x1b[1m";
pub const UNDERLINE: &str = "\x1b[4m";
pub const REVERSED: &str = "\x1b[7m";
pub const RESET: &str = "\x1b[m";
// Escape sequences change the color of the terminal
pub const RESET: &str = "\x1b[m";
pub const BLACK: &str = "\x1b[30m";
pub const BLUE: &str = "\x1b[94m";
pub const CYAN: &str = "\x1b[96m";
pub const GRAY: &str = "\x1b[37m";
pub const GREEN: &str = "\x1b[92m";
pub const MAGENTA: &str = "\x1b[95m";
pub const RED: &str = "\x1b[91m";
pub const WHITE: &str = "\x1b[97m";
pub const YELLOW: &str = "\x1b[93m";
#[cfg(not(feature = "pretty"))]
pub mod colors {
pub const BLACK: &str = "\x1b[30m";
pub const BLUE: &str = "\x1b[94m";
pub const CYAN: &str = "\x1b[96m";
pub const GRAY: &str = "\x1b[37m";
pub const GREEN: &str = "\x1b[92m";
pub const MAGENTA: &str = "\x1b[95m";
pub const RED: &str = "\x1b[91m";
pub const WHITE: &str = "\x1b[97m";
pub const YELLOW: &str = "\x1b[93m";
pub const DEBUG_MAIN: &str = GREEN;
pub const DEBUG: &str = CYAN;
pub const DEBUG_ERROR: &str = RED;
}
// custom colors when use `pretty`
pub const CUSTOM_RED: &str = "\x1b[38;2;255;76;76m";
pub const CUSTOM_BLUE: &str = "\x1b[38;2;76;76;255m";
pub const CUSTOM_GRAY: &str = "\x1b[38;2;231;231;235m";
pub const CUSTOM_CYAN: &str = "\x1b[38;2;76;255;255m";
pub const CUSTOM_MAGENTA: &str = "\x1b[38;2;165;76;255m";
pub const CUSTOM_GREEN: &str = "\x1b[38;2;76;255;76m";
pub const CUSTOM_YELLOW: &str = "\x1b[38;2;255;255;76m";
#[cfg(feature = "pretty")]
pub mod colors {
pub const BLACK: &str = "\x1b[30m";
pub const BLUE: &str = "\x1b[38;2;76;76;255m";
pub const CYAN: &str = "\x1b[38;2;76;255;255m";
pub const GRAY: &str = "\x1b[38;2;231;231;235m";
pub const GREEN: &str = "\x1b[38;2;76;255;76m";
pub const MAGENTA: &str = "\x1b[38;2;165;76;255m";
pub const RED: &str = "\x1b[38;2;255;76;76m";
pub const WHITE: &str = "\x1b[97m";
pub const YELLOW: &str = "\x1b[38;2;255;255;76m";
pub const DEBUG_MAIN: &str = BLUE;
pub const DEBUG: &str = MAGENTA;
pub const DEBUG_ERROR: &str = CYAN;
}
pub fn remove_style(s: &str) -> String {
s.replace(RED, "")
@ -53,13 +68,6 @@ pub enum Color {
Red,
White,
Yellow,
CustomRed,
CustomBlue,
CustomGray,
CustomCyan,
CustomMagenta,
CustomGreen,
CustomYellow,
}
impl Color {
@ -75,13 +83,6 @@ impl Color {
Color::Red => RED,
Color::Yellow => YELLOW,
Color::White => WHITE,
Color::CustomRed => CUSTOM_RED,
Color::CustomBlue => CUSTOM_BLUE,
Color::CustomGray => CUSTOM_GRAY,
Color::CustomCyan => CUSTOM_CYAN,
Color::CustomMagenta => CUSTOM_MAGENTA,
Color::CustomGreen => CUSTOM_GREEN,
Color::CustomYellow => CUSTOM_YELLOW,
}
}
}
@ -115,7 +116,6 @@ pub struct ThemeColors {
pub accent: Color,
}
#[cfg(not(feature = "pretty"))]
pub const COLORS: ThemeColors = ThemeColors {
error: Color::Red,
warning: Color::Yellow,
@ -125,16 +125,6 @@ pub const COLORS: ThemeColors = ThemeColors {
accent: Color::White,
};
#[cfg(feature = "pretty")]
pub const COLORS: ThemeColors = ThemeColors {
error: Color::CustomRed,
warning: Color::CustomYellow,
exception: Color::CustomMagenta,
gutter: Color::CustomCyan,
hint: Color::CustomGreen,
accent: Color::CustomGray,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Characters {
hat: char, // error
@ -524,9 +514,6 @@ mod tests {
println!("{BLUE}Hello{RESET}, {GREEN}World{RESET}");
println!("{MAGENTA}Hello{RESET}, {BLACK}World{RESET}");
println!("{GRAY}Hello{RESET}, {WHITE}World{RESET}");
println!("{CUSTOM_BLUE}Hello{RESET}, {CUSTOM_CYAN}World{RESET}");
println!("{CUSTOM_GRAY}Hello{RESET}, {CUSTOM_GREEN}World{RESET}");
println!("{CUSTOM_MAGENTA}Hello{RESET}, {CUSTOM_RED}World{RESET}");
}
#[test]

View file

@ -2,6 +2,7 @@
use std::option::Option; // conflicting to Type::Option
use erg_common::error::{Location, MultiErrorDisplay};
use erg_common::style::colors::DEBUG_ERROR;
use crate::ty::constructors::{and, not, or, poly};
use crate::ty::free::{Constraint, FreeKind};
@ -131,7 +132,7 @@ impl Context {
|| self.nominal_supertype_of(lhs, rhs)
}
};
log!("answer: {lhs} {RED}:>{RESET} {rhs} == {res}");
log!("answer: {lhs} {DEBUG_ERROR}:>{RESET} {rhs} == {res}");
res
}

View file

@ -4,6 +4,7 @@ use erg_common::config::ErgConfig;
use erg_common::dict::Dict;
use erg_common::error::Location;
use erg_common::set::Set;
use erg_common::style::colors::DEBUG_MAIN;
use erg_common::traits::{Locational, Stream};
use erg_common::vis::Visibility;
use erg_common::Str;
@ -78,7 +79,7 @@ impl OwnershipChecker {
self.check_expr(chunk, Ownership::Owned, true);
}
log!(
"{GREEN}[DEBUG] the ownership checking process has completed, found errors: {}{RESET}",
"{DEBUG_MAIN}[DEBUG] the ownership checking process has completed, found errors: {}{RESET}",
self.errs.len()
);
if self.errs.is_empty() {

View file

@ -33,7 +33,7 @@ macro_rules! debug_call_info {
($self: ident) => {
$self.level += 1;
log!(
c GREEN,
c DEBUG_MAIN,
"\n{} ({}) entered {}, cur: {}",
"".repeat(($self.level as f32 / 4.0).floor() as usize),
$self.level,
@ -48,7 +48,7 @@ macro_rules! debug_exit_info {
($self: ident) => {
$self.level -= 1;
log!(
c GREEN,
c DEBUG_MAIN,
"\n{} ({}) exit {}, cur: {}",
"".repeat(($self.level as f32 / 4.0).floor() as usize),
$self.level,
@ -228,7 +228,7 @@ impl Parser {
pub(crate) fn stack_dec(&mut self, fn_name: &str) {
self.level -= 1;
log!(
c GREEN,
c DEBUG_MAIN,
"\n{} ({}) exit {}, cur: {}",
"".repeat((self.level as f32 / 4.0).floor() as usize),
self.level,

View file

@ -5,7 +5,7 @@ use erg_common::config::{DummyStdin, ErgConfig, Input};
use erg_common::error::MultiErrorDisplay;
use erg_common::python_util::PythonVersion;
use erg_common::spawn::exec_new_thread;
use erg_common::style::{GREEN, RESET};
use erg_common::style::{colors::DEBUG_MAIN, RESET};
use erg_common::traits::{ExitStatus, Runnable, Stream};
use erg_compiler::error::CompileErrors;
@ -107,7 +107,7 @@ fn set_cfg(mut cfg: ErgConfig) -> ErgConfig {
/// The test is intend to run only on 3.11 for fast execution.
/// To execute on other versions, change the version and magic number.
fn _exec_file(file_path: &'static str) -> Result<i32, CompileErrors> {
println!("{GREEN}[test] exec {file_path}{RESET}");
println!("{DEBUG_MAIN}[test] exec {file_path}{RESET}");
let cfg = ErgConfig::with_main_path(PathBuf::from(file_path));
let mut vm = DummyVM::new(set_cfg(cfg));
vm.exec()
@ -115,7 +115,7 @@ fn _exec_file(file_path: &'static str) -> Result<i32, CompileErrors> {
/// WARN: You must quit REPL manually (use `:exit`, `:quit` or call something shutdowns the interpreter)
pub fn _exec_repl(name: &'static str, lines: Vec<String>) -> Result<ExitStatus, CompileErrors> {
println!("{GREEN}[test] exec dummy REPL: {lines:?}{RESET}");
println!("{DEBUG_MAIN}[test] exec dummy REPL: {lines:?}{RESET}");
let cfg = ErgConfig {
input: Input::DummyREPL(DummyStdin::new(name.to_string(), lines)),
quiet_repl: true,