Skip to content

Commit

Permalink
Colors on light terminals (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored Jan 29, 2025
1 parent 48efe41 commit 8fc71a3
Show file tree
Hide file tree
Showing 48 changed files with 467 additions and 533 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ bitflags = "2.6"
renamore = {version="0.3.2", features = ["always-fallback"]}
anes = "0.2.0"
geozero = {version="0.14.0", features=["with-wkb"]}
terminal-light = "1.7.0"

[dependencies.bzip2]
version = "*"
Expand Down
2 changes: 1 addition & 1 deletion src/analyze/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn print(explain: &Analysis) {

fn print_buffer(buffer: &Buffer, title: impl fmt::Display) {
let mut markup = String::with_capacity(buffer.text.len());
let styler = Styler::dark_256();
let styler = Styler::new();
highlight::edgeql(&mut markup, &buffer.text, &styler);

let mut out = String::with_capacity(markup.len());
Expand Down
6 changes: 3 additions & 3 deletions src/analyze/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ pub fn print_title(title: impl fmt::Display, width: usize) {
let filler = max(width.saturating_sub(twidth) / 2, 5);
println!(
"{} {} {}",
format_args!("{0:─^filler$}", "").fade(),
title.emphasize(),
format_args!("{0:─^filler$}", "").fade(),
format_args!("{0:─^filler$}", "").to_string().muted(),
title.to_string().emphasized(),
format_args!("{0:─^filler$}", "").to_string().muted(),
);
}

Expand Down
28 changes: 14 additions & 14 deletions src/analyze/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn print_debug_plan(explain: &Analysis) {
let mut header = Vec::with_capacity(9);
header.push(Box::new("") as Box<_>);
cost_header(&mut header, &explain.arguments);
header.push(Box::new("Plan Info".emphasize()) as Box<_>);
header.push(Box::new("Plan Info".emphasized()) as Box<_>);

let marker = NarrowMarker::new(node);

Expand Down Expand Up @@ -84,11 +84,11 @@ pub fn print_shape(explain: &Analysis) {
let mut header = Vec::with_capacity(8);
header.push(Box::new("") as Box<_>);
cost_header(&mut header, &explain.arguments);
header.push(Box::new("Relations".emphasize()) as Box<_>);
header.push(Box::new("Relations".emphasized()) as Box<_>);

let mut root = Vec::with_capacity(3);
let context = explain.context(&shape.contexts);
root.push(Box::new(format!("{context}{}", "root".fade())) as Box<_>);
root.push(Box::new(format!("{context}{}", "root".muted())) as Box<_>);
cost_columns(&mut root, &shape.cost, &explain.arguments);
root.push(Box::new(table::WordList(Relations(&shape.relations))));

Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn print_expanded_tree(explain: &Analysis) {
let mut header = Vec::with_capacity(9);
header.push(Box::new("") as Box<_>);
cost_header(&mut header, &explain.arguments);
header.push(Box::new("Plan Info".emphasize()) as Box<_>);
header.push(Box::new("Plan Info".emphasized()) as Box<_>);

let marker = NarrowMarker::new(node);
let mut rows = vec![header];
Expand Down Expand Up @@ -440,7 +440,7 @@ impl table::Contents for Border {
}
fn render(&self, _width: usize, height: usize, f: &mut fmt::Formatter) -> fmt::Result {
for _ in 0..height {
writeln!(f, "{}", "│".emphasize())?;
writeln!(f, "{}", "│".emphasized())?;
}
Ok(())
}
Expand All @@ -449,15 +449,15 @@ impl table::Contents for Border {
fn cost_header(header: &mut Vec<Box<dyn table::Contents + '_>>, args: &Arguments) {
header.push(Box::new(Border) as Box<_>);
if args.execute {
header.push(Box::new(table::Right("Time".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Cost".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Loops".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Rows".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Width".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Time".emphasized())) as Box<_>);
header.push(Box::new(table::Right("Cost".emphasized())) as Box<_>);
header.push(Box::new(table::Right("Loops".emphasized())) as Box<_>);
header.push(Box::new(table::Right("Rows".emphasized())) as Box<_>);
header.push(Box::new(table::Right("Width".emphasized())) as Box<_>);
} else {
header.push(Box::new(table::Right("Cost".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Plan Rows".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Width".emphasize())) as Box<_>);
header.push(Box::new(table::Right("Cost".emphasized())) as Box<_>);
header.push(Box::new(table::Right("Plan Rows".emphasized())) as Box<_>);
header.push(Box::new(table::Right("Width".emphasized())) as Box<_>);
}
header.push(Box::new(Border) as Box<_>);
}
Expand Down Expand Up @@ -553,6 +553,6 @@ impl fmt::Display for NodeTitle<'_> {

impl<T: fmt::Display> fmt::Display for Property<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}={}", self.0.fade(), self.1)
write!(f, "{}={}", self.0.muted(), self.1)
}
}
5 changes: 2 additions & 3 deletions src/branch/rebase.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use colorful::Colorful;

use crate::branch::connections::get_connection_to_modify;
use crate::branch::context::Context;
use crate::commands::Options;
Expand All @@ -8,6 +6,7 @@ use crate::migrations::rebase::{
do_rebase, get_diverging_migrations, write_rebased_migration_files,
};
use crate::portable::project;
use crate::print::Highlight;
use crate::{migrations, print};
use uuid::Uuid;

Expand Down Expand Up @@ -146,7 +145,7 @@ async fn rename_temp_to_source(
}

async fn clone_target_branch(branch: &str, connection: &mut Connection) -> anyhow::Result<String> {
eprintln!("Cloning target branch '{}' for rebase...", branch.green());
eprintln!("Cloning target branch '{}' for rebase...", branch.success());

let temp_branch_name = Uuid::new_v4().to_string();

Expand Down
16 changes: 14 additions & 2 deletions src/cli/logo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@ pub fn print_logo(allow_animation: bool, small: bool) {
.max()
.unwrap();

let is_light = terminal_light::luma().map_or(false, |x| x > 0.6);
let primary = if is_light {
Color::DarkMagenta
} else {
Color::Magenta
};
let secondary = if is_light {
Color::DarkYellow
} else {
Color::Yellow
};

let normal = |c| {
write_ansi!(ResetAttributes);
write_ansi!(SetAttribute(Attribute::Bold));
if c == '$' || c == '█' || c == '▄' || c == '▀' {
write_ansi!(SetForegroundColor(Color::Magenta));
write_ansi!(SetForegroundColor(primary));
} else {
write_ansi!(SetForegroundColor(Color::Yellow));
write_ansi!(SetForegroundColor(secondary));
}
write_ansi!(SetAttribute(Attribute::Bold));
};
Expand Down
5 changes: 4 additions & 1 deletion src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ fn _main(options: &CliUpgrade, path: PathBuf) -> anyhow::Result<()> {
.run()?;
fs::remove_file(&tmp_path).ok();
if !options.quiet {
msg!("Upgraded to version {}", pkg.version.emphasize());
msg!(
"Upgraded to version {}",
pkg.version.to_string().emphasized()
);
}
Ok(())
}
5 changes: 2 additions & 3 deletions src/cloud/secret_keys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Context;
use colorful::Colorful;

use crate::branding::BRANDING_CLOUD;
use crate::cloud::client::CloudClient;
Expand Down Expand Up @@ -147,9 +146,9 @@ pub async fn _do_create(c: &options::CreateSecretKey, client: &CloudClient) -> a
" secret key is printed below. \
Be sure to copy and store it securely, as you will \
not be able to see it again.\n"
.green()
.success()
);
msg!("{}", sk.emphasize());
msg!("{}", sk.emphasized());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/backslash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ pub async fn execute(

let options = Options {
command_line: false,
styler: Some(Styler::dark_256()),
styler: Some(Styler::new()),
conn_params: prompt.conn_params.clone(),
};
match cmd {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn init_command_opts(options: &Options) -> Result<commands::Options, anyhow::Err
Ok(commands::Options {
command_line: true,
styler: if std::io::stdout().is_terminal() {
Some(Styler::dark_256())
Some(Styler::new())
} else {
None
},
Expand Down
13 changes: 4 additions & 9 deletions src/error_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use std::str;
use codespan_reporting::diagnostic::{Diagnostic, Label, LabelStyle};
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term::emit;
use colorful::core::color_string::CString;
use colorful::Colorful;
use const_format::concatcp;
use gel_protocol::annotations::Warning;
use termcolor::{ColorChoice, StandardStream};

use gel_errors::{Error, InternalServerError};

use crate::branding::BRANDING_CLI_CMD;
use crate::print::{self, msg};
use crate::print::{self, msg, Highlight};

pub fn print_query_error(
err: &Error,
Expand Down Expand Up @@ -112,12 +110,9 @@ pub fn print_query_warning(
}

fn print_query_warning_plain(warning: &Warning) {
let marker = concatcp!(BRANDING_CLI_CMD, " warning:");
let marker = if print::use_color() {
marker.bold().yellow()
} else {
CString::new(marker)
};
let marker = concatcp!(BRANDING_CLI_CMD, " warning:")
.emphasized()
.warning();

msg!("{marker} {warning}");
}
24 changes: 8 additions & 16 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::str;
use std::time::Instant;

use anyhow::Context;
use colorful::Colorful;
use is_terminal::IsTerminal;
use terminal_size::{terminal_size, Width};
use tokio::io::{stdout, AsyncWriteExt};
Expand Down Expand Up @@ -159,10 +158,12 @@ pub async fn _main(options: Options, mut state: repl::State, cfg: Config) -> any
if let Some(config_path) = &cfg.file_name {
msg!(
"{}",
format_args!("Applied {} configuration file", config_path.display(),).fade()
format_args!("Applied {} configuration file", config_path.display(),)
.to_string()
.muted()
);
}
msg!("{}", r#"Type \help for help, \quit to quit."#.light_gray());
msg!("{}", r#"Type \help for help, \quit to quit."#.muted());
state.set_history_limit(state.history_limit).await?;
match _interactive_main(&options, &mut state).await {
Ok(()) => Ok(()),
Expand Down Expand Up @@ -377,10 +378,7 @@ async fn execute_query(
let mut index = 0;
while let Some(row) = items.next().await.transpose()? {
if index == 0 && state.print_stats == Detailed {
eprintln!(
"{}",
format!("First row: {:?}", start.elapsed()).dark_gray()
);
eprintln!("{}", format!("First row: {:?}", start.elapsed()).muted());
}
if let Some(limit) = state.implicit_limit {
if index >= limit {
Expand Down Expand Up @@ -432,10 +430,7 @@ async fn execute_query(
let mut index = 0;
while let Some(row) = items.next().await.transpose()? {
if index == 0 && state.print_stats == Detailed {
eprintln!(
"{}",
format!("First row: {:?}", start.elapsed()).dark_gray()
);
eprintln!("{}", format!("First row: {:?}", start.elapsed()).muted());
}
index += 1;
let text = match row {
Expand Down Expand Up @@ -471,10 +466,7 @@ async fn execute_query(
let mut index = 0;
while let Some(row) = items.next().await.transpose()? {
if index == 0 && state.print_stats == Detailed {
eprintln!(
"{}",
format!("First row: {:?}", start.elapsed()).dark_gray()
);
eprintln!("{}", format!("First row: {:?}", start.elapsed()).muted());
}
let mut text = match row {
Value::Str(s) => s,
Expand Down Expand Up @@ -524,7 +516,7 @@ async fn execute_query(
"Query time (including output formatting): {:?}",
start.elapsed() - input_duration
)
.dark_gray()
.muted()
);
}
state.last_error = None;
Expand Down
Loading

0 comments on commit 8fc71a3

Please sign in to comment.