Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ColorDisplay trait for printing objects to the terminal #926

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ impl Color {
}
}

pub(crate) fn fmt(fmt: &Formatter) -> Self {
if fmt.alternate() {
Self::always()
} else {
Self::never()
}
}

pub(crate) fn auto() -> Self {
Self {
use_color: UseColor::Auto,
Expand Down
20 changes: 20 additions & 0 deletions src/color_display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::common::*;

pub(crate) trait ColorDisplay {
fn color_display<'a>(&'a self, color: Color) -> Wrapper<'a>
where
Self: Sized,
{
Wrapper(self, color)
}

fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result;
}

pub(crate) struct Wrapper<'a>(&'a dyn ColorDisplay, Color);

impl<'a> Display for Wrapper<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.fmt(f, self.1)
}
}
4 changes: 2 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub(crate) use crate::{load_dotenv::load_dotenv, output::output, unindent::unind

// traits
pub(crate) use crate::{
command_ext::CommandExt, keyed::Keyed, ordinal::Ordinal, platform_interface::PlatformInterface,
range_ext::RangeExt,
color_display::ColorDisplay, command_ext::CommandExt, keyed::Keyed, ordinal::Ordinal,
platform_interface::PlatformInterface, range_ext::RangeExt,
};

// structs and enums
Expand Down
44 changes: 17 additions & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,6 @@ impl<'src> Error<'src> {
message: message.into(),
}
}

pub(crate) fn write(&self, w: &mut dyn Write, color: Color) -> io::Result<()> {
let color = color.stderr();

if color.active() {
writeln!(
w,
"{}: {}{:#}{}",
color.error().paint("error"),
color.message().prefix(),
self,
color.message().suffix()
)?;
} else {
writeln!(w, "error: {}", self)?;
}

if let Some(token) = self.context() {
token.write_context(w, color.error())?;
writeln!(w)?;
}

Ok(())
}
}

impl<'src> From<CompileError<'src>> for Error<'src> {
Expand All @@ -210,10 +186,17 @@ impl<'src> From<SearchError> for Error<'src> {
}
}

impl<'src> Display for Error<'src> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
impl<'src> ColorDisplay for Error<'src> {
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
use Error::*;

write!(
f,
"{}: {}",
color.error().paint("error"),
color.message().prefix()
)?;

match self {
ArgumentCountMismatch {
recipe,
Expand Down Expand Up @@ -254,7 +237,7 @@ impl<'src> Display for Error<'src> {
}
write!(f, "\nusage:\n just {}", recipe)?;
for param in parameters {
write!(f, " {}", param)?;
write!(f, " {}", param.color_display(color))?;
}
},
Backtick { output_error, .. } => match output_error {
Expand Down Expand Up @@ -619,6 +602,13 @@ impl<'src> Display for Error<'src> {
},
}

write!(f, "{}", color.message().suffix())?;

if let Some(token) = self.context() {
writeln!(f)?;
write!(f, "{}", token.color_display(color.error()))?;
}

Ok(())
}
}
21 changes: 15 additions & 6 deletions src/interrupt_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ impl InterruptHandler {
match INSTANCE.lock() {
Ok(guard) => guard,
Err(poison_error) => {
eprintln!("{}", Error::Internal {
message: format!("interrupt handler mutex poisoned: {}", poison_error),
});
eprintln!(
"{}",
Error::Internal {
message: format!("interrupt handler mutex poisoned: {}", poison_error),
}
.color_display(Color::auto().stderr())
);
std::process::exit(EXIT_FAILURE);
},
}
Expand Down Expand Up @@ -58,9 +62,14 @@ impl InterruptHandler {
pub(crate) fn unblock(&mut self) {
if self.blocks == 0 {
if self.verbosity.loud() {
eprintln!("{}", Error::Internal {
message: "attempted to unblock interrupt handler, but handler was not blocked".to_owned(),
});
eprintln!(
"{}",
Error::Internal {
message: "attempted to unblock interrupt handler, but handler was not blocked"
.to_owned(),
}
.color_display(Color::auto().stderr())
);
}
std::process::exit(EXIT_FAILURE);
}
Expand Down
2 changes: 1 addition & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'src> Display for Item<'src> {
Item::Alias(alias) => write!(f, "{}", alias),
Item::Assignment(assignment) => write!(f, "{}", assignment),
Item::Comment(comment) => write!(f, "{}", comment),
Item::Recipe(recipe) => write!(f, "{}", recipe),
Item::Recipe(recipe) => write!(f, "{}", recipe.color_display(Color::never())),
Item::Set(set) => write!(f, "{}", set),
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ impl<'src> Justfile<'src> {
}
}

impl<'src> Display for Justfile<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
impl<'src> ColorDisplay for Justfile<'src> {
fn fmt(&self, f: &mut Formatter, color: Color) -> Result<(), fmt::Error> {
let mut items = self.recipes.len() + self.assignments.len() + self.aliases.len();
for (name, assignment) in &self.assignments {
if assignment.export {
Expand All @@ -396,7 +396,7 @@ impl<'src> Display for Justfile<'src> {
}
}
for recipe in self.recipes.values() {
write!(f, "{}", recipe)?;
write!(f, "{}", recipe.color_display(color))?;
items -= 1;
if items != 0 {
write!(f, "\n\n")?;
Expand Down Expand Up @@ -683,11 +683,11 @@ mod tests {

fn test(input: &str, expected: &str) {
let justfile = compile(input);
let actual = format!("{:#}", justfile);
let actual = format!("{}", justfile.color_display(Color::never()));
assert_eq!(actual, expected);
println!("Re-parsing...");
let reparsed = compile(&actual);
let redumped = format!("{:#}", reparsed);
let redumped = format!("{}", reparsed.color_display(Color::never()));
assert_eq!(redumped, actual);
}

Expand Down
12 changes: 4 additions & 8 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2298,17 +2298,13 @@ mod tests {
} if message == "Lexer presumed character `-`"
);

let mut cursor = Cursor::new(Vec::new());

Error::Compile { compile_error }
.write(&mut cursor, Color::never())
.unwrap();

assert_eq!(
str::from_utf8(&cursor.into_inner()).unwrap(),
Error::Compile { compile_error }
.color_display(Color::never())
.to_string(),
"error: Internal error, this may indicate a bug in just: \
Lexer presumed character `-`\nconsider filing an issue: \
https://github.com/casey/just/issues/new\n |\n1 | !\n | ^\n"
https://github.com/casey/just/issues/new\n |\n1 | !\n | ^"
);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mod assignment_resolver;
mod ast;
mod binding;
mod color;
mod color_display;
mod command_ext;
mod common;
mod compile_error;
Expand Down
7 changes: 4 additions & 3 deletions src/load_dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ pub(crate) fn load_dotenv(
.map(|val| val.as_os_str().to_str() == Some("1"))
.unwrap_or(false)
{
Warning::DotenvLoad
.write(&mut io::stderr(), config.color.stderr())
.ok();
eprintln!(
"{}",
Warning::DotenvLoad.color_display(config.color.stderr())
);
}

let iter = dotenv::from_path_iter(&path)?;
Expand Down
5 changes: 2 additions & 3 deletions src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ pub(crate) struct Parameter<'src> {
pub(crate) export: bool,
}

impl<'src> Display for Parameter<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
let color = Color::fmt(f);
impl<'src> ColorDisplay for Parameter<'src> {
fn fmt(&self, f: &mut Formatter, color: Color) -> Result<(), fmt::Error> {
if self.export {
write!(f, "$")?;
}
Expand Down
6 changes: 3 additions & 3 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ impl<'src, D> Keyed<'src> for Recipe<'src, D> {
}
}

impl<'src, D: Display> Display for Recipe<'src, D> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
impl<'src, D: Display> ColorDisplay for Recipe<'src, D> {
fn fmt(&self, f: &mut Formatter, color: Color) -> Result<(), fmt::Error> {
if let Some(doc) = self.doc {
writeln!(f, "# {}", doc)?;
}
Expand All @@ -322,7 +322,7 @@ impl<'src, D: Display> Display for Recipe<'src, D> {
}

for parameter in &self.parameters {
write!(f, " {}", parameter)?;
write!(f, " {}", parameter.color_display(color))?;
}
write!(f, ":")?;

Expand Down
2 changes: 1 addition & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn run() -> Result<(), i32> {
})
.map_err(|error| {
if !verbosity.quiet() {
error.write(&mut io::stderr(), color).ok();
eprintln!("{}", error.color_display(color));
}
error.code().unwrap_or(EXIT_FAILURE)
})
Expand Down
34 changes: 16 additions & 18 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ impl Subcommand {

if config.verbosity.loud() {
for warning in &justfile.warnings {
warning.write(&mut io::stderr(), config.color.stderr()).ok();
eprintln!("{}", warning.color_display(config.color.stderr()));
}
}

match self {
Choose { overrides, chooser } =>
Self::choose(&config, justfile, &search, overrides, chooser.as_deref())?,
Command { overrides, .. } => justfile.run(&config, &search, overrides, &[])?,
Self::choose(config, justfile, &search, overrides, chooser.as_deref())?,
Command { overrides, .. } => justfile.run(config, &search, overrides, &[])?,
Dump => Self::dump(ast),
Evaluate { overrides, .. } => justfile.run(&config, &search, overrides, &[])?,
Format => Self::format(&config, ast, &search)?,
List => Self::list(&config, justfile),
Evaluate { overrides, .. } => justfile.run(config, &search, overrides, &[])?,
Format => Self::format(config, ast, &search)?,
List => Self::list(config, justfile),
Run {
arguments,
overrides,
} => justfile.run(&config, &search, overrides, arguments)?,
Show { ref name } => Self::show(&name, justfile)?,
Summary => Self::summary(&config, justfile),
} => justfile.run(config, &search, overrides, arguments)?,
Show { ref name } => Self::show(config, &name, justfile)?,
Summary => Self::summary(config, justfile),
Variables => Self::variables(justfile),
Completions { .. } | Edit | Init => unreachable!(),
}
Expand Down Expand Up @@ -308,7 +308,9 @@ impl Subcommand {
let mut line_width = UnicodeWidthStr::width(*name);

for parameter in &recipe.parameters {
line_width += UnicodeWidthStr::width(format!(" {}", parameter).as_str());
line_width += UnicodeWidthStr::width(
format!(" {}", parameter.color_display(Color::never())).as_str(),
);
}

if line_width <= 30 {
Expand All @@ -331,11 +333,7 @@ impl Subcommand {
{
print!("{}{}", config.list_prefix, name);
for parameter in &recipe.parameters {
if config.color.stdout().active() {
print!(" {:#}", parameter);
} else {
print!(" {}", parameter);
}
print!(" {}", parameter.color_display(config.color.stdout()));
}

// Declaring this outside of the nested loops will probably be more efficient,
Expand Down Expand Up @@ -365,14 +363,14 @@ impl Subcommand {
}
}

fn show<'src>(name: &str, justfile: Justfile<'src>) -> Result<(), Error<'src>> {
fn show<'src>(config: &Config, name: &str, justfile: Justfile<'src>) -> Result<(), Error<'src>> {
if let Some(alias) = justfile.get_alias(name) {
let recipe = justfile.get_recipe(alias.target.name.lexeme()).unwrap();
println!("{}", alias);
println!("{}", recipe);
println!("{}", recipe.color_display(config.color.stdout()));
Ok(())
} else if let Some(recipe) = justfile.get_recipe(name) {
println!("{}", recipe);
println!("{}", recipe.color_display(config.color.stdout()));
Ok(())
} else {
Err(Error::UnknownRecipes {
Expand Down
14 changes: 8 additions & 6 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ impl<'src> Token<'src> {
pub(crate) fn error(&self, kind: CompileErrorKind<'src>) -> CompileError<'src> {
CompileError { token: *self, kind }
}
}

pub(crate) fn write_context(&self, w: &mut dyn Write, color: Color) -> io::Result<()> {
impl<'src> ColorDisplay for Token<'src> {
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
let width = if self.length == 0 { 1 } else { self.length };

let line_number = self.line.ordinal();
Expand Down Expand Up @@ -50,11 +52,11 @@ impl<'src> Token<'src> {
i += c.len_utf8();
}
let line_number_width = line_number.to_string().len();
writeln!(w, "{0:1$} |", "", line_number_width)?;
writeln!(w, "{} | {}", line_number, space_line)?;
write!(w, "{0:1$} |", "", line_number_width)?;
writeln!(f, "{0:1$} |", "", line_number_width)?;
writeln!(f, "{} | {}", line_number, space_line)?;
write!(f, "{0:1$} |", "", line_number_width)?;
write!(
w,
f,
" {0:1$}{2}{3:^<4$}{5}",
"",
space_column,
Expand All @@ -67,7 +69,7 @@ impl<'src> Token<'src> {
None =>
if self.offset != self.src.len() {
write!(
w,
f,
"internal error: Error has invalid line number: {}",
line_number
)?;
Expand Down
Loading