use std::env;
use std::process;
use thag_styling::{
auto_help, get_verbosity, help_system::check_help_and_exit, is_konsole, is_mintty,
set_verbosity_from_env, vprtln, ColorInitStrategy, PaletteSync, TermAttributes, Theme, V,
};
fn main() {
let help = auto_help!();
check_help_and_exit(&help);
set_verbosity_from_env();
if is_konsole() {
eprintln!(
r"KDE Konsole terminal type detected. Konsole does not support the OSC 4 ANSI escape sequence that this tool uses.
Instead you can use `thag_gen_terminal_themes` to generate a Konsole theme."
);
}
if is_mintty() {
eprintln!(
r"Mintty terminal type detected. Mintty does not support the OSC 4 ANSI escape sequence that this tool uses.
Instead you can use `thag_gen_terminal_themes` to generate a Mintty theme."
);
}
let args: Vec<String> = env::args().collect();
TermAttributes::get_or_init_with_strategy(&ColorInitStrategy::Default);
match args.get(1).map(String::as_str) {
Some("apply") => {
let theme_name = args.get(2);
if let Some(name) = theme_name {
apply_theme(name);
} else {
eprintln!("β Theme name required");
print_usage();
process::exit(1);
}
}
Some("preview") => {
let theme_name = args.get(2);
if let Some(name) = theme_name {
preview_theme(name);
} else {
eprintln!("β Theme name required");
print_usage();
process::exit(1);
}
}
Some("reset") => {
reset_palette();
}
Some("demo") => {
demo_palette();
}
Some("list") => {
list_themes();
}
_ => {
print_usage();
}
}
}
fn apply_theme(theme_name: &str) {
vprtln!(V::N, "π¨ Loading theme: {}", theme_name);
let mut theme = match Theme::get_builtin(theme_name) {
Ok(theme) => theme,
Err(e) => {
eprintln!("β Failed to load theme '{}': {}", theme_name, e);
vprtln!(
V::N,
"π‘ Try running 'thag_sync_palette list' to see available themes"
);
process::exit(1);
}
};
if let Err(e) = theme.load_base_colors() {
vprtln!(V::V, "β οΈ Could not load base colors: {}", e);
vprtln!(V::V, "Falling back to role-based ANSI mapping");
}
vprtln!(V::N, "π Description: {}", theme.description);
vprtln!(V::N, "π Applying palette...");
if let Err(e) = PaletteSync::apply_theme(&theme) {
eprintln!("β Failed to apply theme: {}", e);
process::exit(1);
}
if get_verbosity() >= V::Normal {
println!("β
Theme applied successfully!");
println!("π To reset colors, run: thag_sync_palette reset");
println!("\nπ Color demonstration:");
PaletteSync::show_background_info(&theme);
PaletteSync::demonstrate_palette();
}
}
fn preview_theme(theme_name: &str) {
vprtln!(V::N, "π¨ Previewing theme: {}", theme_name);
let mut theme = match Theme::get_builtin(theme_name) {
Ok(theme) => theme,
Err(e) => {
eprintln!("β Failed to load theme '{}': {}", theme_name, e);
vprtln!(
V::N,
"π‘ Try running 'thag_sync_palette list' to see available themes"
);
process::exit(1);
}
};
if let Err(e) = theme.load_base_colors() {
vprtln!(V::V, "β οΈ Could not load base colors: {}", e);
vprtln!(V::V, "Falling back to role-based ANSI mapping");
}
if let Err(e) = PaletteSync::preview_theme(&theme) {
eprintln!("β Failed to preview theme: {}", e);
process::exit(1);
}
if get_verbosity() >= V::Normal {
println!("\nβΈοΈ Preview applied! Press Enter to see color demo, or Ctrl+C to exit...");
let mut input = String::new();
let _ = std::io::stdin().read_line(&mut input);
PaletteSync::show_background_info(&theme);
PaletteSync::demonstrate_palette();
println!(
"\nπ To make this permanent, run: thag_sync_palette apply {}",
theme_name
);
println!("π To reset colors, run: thag_sync_palette reset");
}
}
fn reset_palette() {
vprtln!(V::N, "π Resetting terminal palette to defaults...");
if let Err(e) = PaletteSync::reset_palette() {
eprintln!("β Failed to reset palette: {}", e);
process::exit(1);
}
vprtln!(V::N, "β
Terminal palette reset successfully!");
}
fn demo_palette() {
PaletteSync::demonstrate_palette();
}
fn list_themes() {
println!("π¨ Available built-in themes:");
println!();
let themes = Theme::list_builtin();
for theme_name in themes {
if let Ok(theme) = Theme::get_builtin(&theme_name) {
println!(" π¦ {} - {}", theme_name, theme.description);
} else {
println!(" π¦ {}", theme_name);
}
}
println!();
println!("π‘ Use any theme name with the 'apply' or 'preview' command");
}
fn print_usage() {
println!("π¨ Thag Terminal Palette Synchronization");
println!();
println!("Sync your terminal's color palette with thag_styling themes using OSC sequences");
println!();
println!("Usage:");
println!(" thag_sync_palette apply <theme> Apply theme to terminal palette");
println!(" thag_sync_palette preview <theme> Preview theme temporarily");
println!(" thag_sync_palette reset Reset palette to defaults");
println!(" thag_sync_palette demo Show current palette");
println!(" thag_sync_palette list List available themes");
println!(" thag_sync_palette help Show this help message");
println!();
println!("Examples:");
println!(" thag_sync_palette apply thag-botticelli-birth-of-venus-dark");
println!(" thag_sync_palette preview thag-dark");
println!(" thag_sync_palette reset");
println!();
println!("π‘ This tool uses OSC sequences to update your terminal's color palette");
println!(" in real-time. Most modern terminals support this feature.");
println!();
println!("π Supported terminals: WezTerm, Alacritty, iTerm2, Kitty, Windows Terminal,");
println!(" Gnome Terminal, and most other modern terminal emulators.");
}