Skip to content

Commit

Permalink
Add termtheme executable
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Sep 3, 2024
1 parent 6eabe6b commit e49dcf7
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 7 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
if: matrix.rust-version != 'stable'
run: rustup override set '${{matrix.rust-version}}'
- name: Build
run: cargo build --all-features
run: cargo build --workspace --all-features
docs:
name: Docs
runs-on: ubuntu-latest
Expand All @@ -56,7 +56,7 @@ jobs:
- name: Check format
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy --all-targets --all-features -- --deny warnings
run: cargo clippy --workspace --all-targets --all-features -- --deny warnings
- uses: EmbarkStudios/cargo-deny-action@v1
test:
name: Test
Expand All @@ -67,13 +67,16 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run tests
run: cargo test --all-features
run: cargo test --workspace --all-features
test_package:
name: Test Package
runs-on: ubuntu-latest
strategy:
matrix:
package: [terminal-colorsaurus, termtheme]
steps:
- uses: actions/checkout@v4
- name: Package
run: cargo package -p terminal-colorsaurus
run: cargo package -p ${{matrix.package}}
- name: Test Package
run: (cd target/package/terminal-colorsaurus-*/ && cargo test)
run: (cd target/package/${{matrix.package}}-*/ && cargo test)
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ use_debug = "warn"
all-features = true

[workspace]
members = ["examples/*"]
members = ["crates/*","examples/*"]
11 changes: 11 additions & 0 deletions crates/termtheme/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "termtheme"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
anstyle = "1.0.8"
anstyle-query = "1.1.1"
clap = { version = "4.5.16", features = ["derive"] }
terminal-colorsaurus = { version = "0.4.4", path = "../../" }
95 changes: 95 additions & 0 deletions crates/termtheme/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use anstyle::{AnsiColor, Style};
use clap::Parser;
use std::{
fmt::{self, Display},
io::{self, stdout, IsTerminal},
process::exit,
};
use terminal_colorsaurus::{color_scheme, ColorScheme, QueryOptions};

fn main() {
let args = Args::parse();
if !stdout().is_terminal() && !args.force {
display_error("stdout is not connected to a terminal");
display_help(
"use '--force' if you're sure that no other process is trying to write to the terminal",
);
exit(1);
}
match color_scheme(QueryOptions::default()) {
Ok(s) => display_theme(s, !args.no_newline),
Err(e) => {
display_error(e);
exit(1);
}
}
}

fn display_theme(color_scheme: ColorScheme, newline: bool) {
if newline {
println!("{}", DisplayName(color_scheme))
} else {
print!("{}", DisplayName(color_scheme))
}
}

fn display_error(e: impl Display) {
if use_colors(&io::stderr()) {
let style = Style::new().bold().fg_color(Some(AnsiColor::Red.into()));
eprintln!("{style}error:{style:#} {e}");
} else {
eprintln!("error: {e}");
}
}

fn display_help(e: impl Display) {
if use_colors(&io::stderr()) {
let style = Style::new()
.bold()
.fg_color(Some(AnsiColor::BrightBlue.into()));
eprintln!("{style}tip:{style:#} {e}");
} else {
eprintln!("tip: {e}");
}
}

struct DisplayName(ColorScheme);

impl fmt::Display for DisplayName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
ColorScheme::Dark => f.write_str("dark"),
ColorScheme::Light => f.write_str("light"),
}
}
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Do not output a newline.
#[arg(short = 'n')]
no_newline: bool,
/// Always query the terminal even when stdout is redirected.
#[arg(short = 'f', long)]
force: bool,
}

trait Stream: io::Write + io::IsTerminal {}

impl<T> Stream for T where T: io::Write + io::IsTerminal {}

// Copied from <https://github.com/rust-cli/anstyle/blob/4b8b9c59901ad5c08191303b59645c0139240acb/crates/anstream/src/auto.rs#L187>
// which is licensed under Apache 2.0 or MIT.
fn use_colors(raw: &dyn Stream) -> bool {
let clicolor = anstyle_query::clicolor();
let clicolor_enabled = clicolor.unwrap_or(false);
let clicolor_disabled = !clicolor.unwrap_or(true);
if anstyle_query::no_color() {
false
} else if anstyle_query::clicolor_force() {
true
} else if clicolor_disabled {
false
} else { raw.is_terminal() && (anstyle_query::term_supports_color() || clicolor_enabled || anstyle_query::is_ci()) }
}
1 change: 0 additions & 1 deletion examples/benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn main() -> Result<()> {
.with_style(ProgressStyle::default_bar().progress_chars("██░"));

let measurements = (0..args.iterations)
.into_iter()
.map(|_| bench())
.inspect(|_| bar.inc(1))
.collect::<Result<Vec<_>>>()?;
Expand Down

0 comments on commit e49dcf7

Please sign in to comment.