From f3c3930d9efb2fa7cd64171d3cb1ad27fae406a3 Mon Sep 17 00:00:00 2001 From: FujiApple Date: Thu, 17 Oct 2024 23:23:36 +0800 Subject: [PATCH] feat(tui): add `--print-locales` to print all available locales --- crates/trippy-tui/src/config.rs | 4 ++++ crates/trippy-tui/src/config/cmd.rs | 6 +++++- crates/trippy-tui/src/lib.rs | 1 + crates/trippy-tui/src/locale.rs | 5 +++++ crates/trippy-tui/src/print.rs | 7 +++++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/trippy-tui/src/config.rs b/crates/trippy-tui/src/config.rs index 727e95d2..04d15a45 100644 --- a/crates/trippy-tui/src/config.rs +++ b/crates/trippy-tui/src/config.rs @@ -256,6 +256,8 @@ pub enum TrippyAction { PrintShellCompletions(Shell), /// Generate a man page and exit. PrintManPage, + /// Print all available locales and exit. + PrintLocales, } impl TrippyAction { @@ -270,6 +272,8 @@ impl TrippyAction { Self::PrintShellCompletions(shell) } else if args.generate_man { Self::PrintManPage + } else if args.print_locales { + Self::PrintLocales } else { Self::Trippy(TrippyConfig::from(args, privilege, pid)?) }) diff --git a/crates/trippy-tui/src/config/cmd.rs b/crates/trippy-tui/src/config/cmd.rs index 647714fa..4b8ae3e4 100644 --- a/crates/trippy-tui/src/config/cmd.rs +++ b/crates/trippy-tui/src/config/cmd.rs @@ -19,7 +19,7 @@ use std::time::Duration; #[command(name = "trip", author, version, about, long_about = None, arg_required_else_help(true), styles=Styles::styled())] pub struct Args { /// A space delimited list of hostnames and IPs to trace - #[arg(required_unless_present_any(["print_tui_theme_items", "print_tui_binding_commands", "print_config_template", "generate", "generate_man"]))] + #[arg(required_unless_present_any(["print_tui_theme_items", "print_tui_binding_commands", "print_config_template", "generate", "generate_man", "print_locales"]))] pub targets: Vec, /// Config file @@ -260,6 +260,10 @@ pub struct Args { #[arg(long)] pub print_config_template: bool, + /// Print all locales and exit + #[arg(long)] + pub print_locales: bool, + /// The debug log format [default: pretty] #[arg(long)] pub log_format: Option, diff --git a/crates/trippy-tui/src/lib.rs b/crates/trippy-tui/src/lib.rs index c381c397..32835ab9 100644 --- a/crates/trippy-tui/src/lib.rs +++ b/crates/trippy-tui/src/lib.rs @@ -35,6 +35,7 @@ pub fn trippy() -> anyhow::Result<()> { TrippyAction::PrintConfigTemplate => print::print_config_template(), TrippyAction::PrintManPage => print::print_man_page()?, TrippyAction::PrintShellCompletions(shell) => print::print_shell_completions(shell)?, + TrippyAction::PrintLocales => print::print_locales(), } Ok(()) } diff --git a/crates/trippy-tui/src/locale.rs b/crates/trippy-tui/src/locale.rs index 08f5cd11..5739fd0f 100644 --- a/crates/trippy-tui/src/locale.rs +++ b/crates/trippy-tui/src/locale.rs @@ -21,6 +21,11 @@ pub fn locale() -> String { rust_i18n::locale().to_string() } +/// Get all available locales. +pub fn available_locales() -> Vec<&'static str> { + rust_i18n::available_locales!() +} + fn set_locale_inner(locale: &str) { let all_locales = rust_i18n::available_locales!(); if all_locales.contains(&locale) { diff --git a/crates/trippy-tui/src/print.rs b/crates/trippy-tui/src/print.rs index 943821de..ce14aa2c 100644 --- a/crates/trippy-tui/src/print.rs +++ b/crates/trippy-tui/src/print.rs @@ -1,6 +1,8 @@ use crate::config::{Args, TuiCommandItem, TuiThemeItem}; +use crate::locale::available_locales; use clap::CommandFactory; use clap_complete::Shell; +use itertools::Itertools; use std::process; use strum::VariantNames; @@ -29,6 +31,11 @@ pub fn print_man_page() -> anyhow::Result<()> { process::exit(0); } +pub fn print_locales() { + println!("TUI locales: {}", available_locales().iter().join(", ")); + process::exit(0); +} + fn tui_theme_items() -> String { format!( "TUI theme color items: {}",