Small Rust scripts
at main 141 lines 3.2 kB view raw
1#!/usr/bin/env -S cargo +nightly -Zscript 2 3--- 4[package] 5edition = "2024" 6[dependencies] 7argh = "0.1" 8owo-colors = "4" 9--- 10 11use argh::FromArgs; 12use owo_colors::{AnsiColors, OwoColorize, Rgb, XtermColors}; 13 14#[derive(FromArgs)] 15#[argh(description = "Display terminal color capabilities")] 16struct App { 17 #[argh(switch, description = "show only basic 16 ANSI colors.")] 18 basic: bool, 19 20 #[argh(switch, description = "show 256 color palette.")] 21 palette: bool, 22 23 #[argh(switch, description = "show true color (24-bit) gradient.")] 24 truecolor: bool, 25} 26 27const BLOCK: &str = " "; 28 29fn main() { 30 let app: App = argh::from_env(); 31 let show_all = !app.basic && !app.palette && !app.truecolor; 32 33 if show_all || app.basic { 34 print_basic_colors(); 35 } 36 if show_all || app.palette { 37 print_256_palette(); 38 } 39 if show_all || app.truecolor { 40 print_truecolor(); 41 } 42} 43 44fn print_basic_colors() { 45 use AnsiColors::*; 46 const COLORS: [AnsiColors; 8] = [Black, Red, Green, Yellow, Blue, Magenta, Cyan, White]; 47 48 println!("ANSI 16 Colors:\n"); 49 50 print!(" Normal: "); 51 for color in COLORS { 52 print!("{}", BLOCK.on_color(color)); 53 } 54 println!(); 55 56 print!(" Bright: "); 57 for color in COLORS { 58 print!("{}", BLOCK.on_color(color).bold()); 59 } 60 println!("\n"); 61} 62 63fn print_256_palette() { 64 println!("256 Color Palette:\n"); 65 66 // Standard 0-15 67 print!(" "); 68 for i in 0..16u8 { 69 print!("{}", BLOCK.on_color(XtermColors::from(i))); 70 } 71 println!("\n"); 72 73 // Color cube 16-231 74 for row in 0..6u8 { 75 print!(" "); 76 for green in 0..6u8 { 77 for red in 0..6u8 { 78 let color = 16 + row * 36 + green * 6 + red; 79 print!("{}", BLOCK.on_color(XtermColors::from(color))); 80 } 81 print!(" "); 82 } 83 println!(); 84 } 85 println!(); 86 87 // Grayscale 232-255 88 print!(" "); 89 for i in 232..=255u8 { 90 print!("{}", BLOCK.on_color(XtermColors::from(i))); 91 } 92 println!("\n"); 93} 94 95fn print_truecolor() { 96 println!("True Color (24-bit) Test:\n"); 97 98 // Red gradient 99 print!(" "); 100 for i in 0..64u8 { 101 print!("{}", " ".on_color(Rgb(i * 4, 0, 0))); 102 } 103 println!(); 104 105 // Green gradient 106 print!(" "); 107 for i in 0..64u8 { 108 print!("{}", " ".on_color(Rgb(0, i * 4, 0))); 109 } 110 println!(); 111 112 // Blue gradient 113 print!(" "); 114 for i in 0..64u8 { 115 print!("{}", " ".on_color(Rgb(0, 0, i * 4))); 116 } 117 println!(); 118 119 // Rainbow 120 print!(" "); 121 for i in 0..64 { 122 let (r, g, b) = hsv_to_rgb(i as f32 / 64.0 * 360.0); 123 print!("{}", " ".on_color(Rgb(r, g, b))); 124 } 125 println!("\n"); 126 127 println!(" If gradients appear smooth, your terminal supports true color.\n"); 128} 129 130fn hsv_to_rgb(h: f32) -> (u8, u8, u8) { 131 let x = 1.0 - ((h / 60.0) % 2.0 - 1.0).abs(); 132 let (r, g, b) = match h as u32 { 133 0..=59 => (1.0, x, 0.0), 134 60..=119 => (x, 1.0, 0.0), 135 120..=179 => (0.0, 1.0, x), 136 180..=239 => (0.0, x, 1.0), 137 240..=299 => (x, 0.0, 1.0), 138 _ => (1.0, 0.0, x), 139 }; 140 ((r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8) 141}