diff --git a/build.rs b/build.rs index 8ae490926..f42c5d56a 100644 --- a/build.rs +++ b/build.rs @@ -34,24 +34,12 @@ fn gen_presets_hook(mut file: &File) -> SdResult<()> { .and_then(|v| v.strip_suffix(".toml")) .expect("Failed to process filename"); presets.push_str(format!("print::Preset(\"{name}\"),\n").as_str()); - match_arms.push_str( - format!( - r#" -"{name}" => {{ - let stdout = io::stdout(); - let mut stdout = stdout.lock(); - let _ = stdout.write_all(include_bytes!(r"{full_path}")); -}} -"# - ) - .as_str(), - ); + match_arms.push_str(format!(r#""{name}" => include_bytes!(r"{full_path}"),"#).as_str()); } writeln!( file, r#" -use std::io::{{self, Write}}; use crate::print; pub fn get_preset_list<'a>() -> &'a [print::Preset] {{ @@ -60,10 +48,10 @@ pub fn get_preset_list<'a>() -> &'a [print::Preset] {{ ] }} -pub fn print_preset_content(name: &str) {{ +pub fn get_preset_content(name: &str) -> &[u8] {{ match name {{ {match_arms} - _ => {{}} + _ => unreachable!(), }} }} "# diff --git a/docs/presets/bracketed-segments.md b/docs/presets/bracketed-segments.md index 921f58bdb..ebfb966d2 100644 --- a/docs/presets/bracketed-segments.md +++ b/docs/presets/bracketed-segments.md @@ -10,7 +10,7 @@ in brackets instead of using the default Starship wording ("via", "on", etc.). ### Configuration ```sh -starship preset bracketed-segments > ~/.config/starship.toml +starship preset bracketed-segments -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/bracketed-segments.toml) diff --git a/docs/presets/nerd-font.md b/docs/presets/nerd-font.md index f523d01c6..26472871c 100644 --- a/docs/presets/nerd-font.md +++ b/docs/presets/nerd-font.md @@ -13,7 +13,7 @@ This preset changes the symbols for each module to use Nerd Font symbols. ### Configuration ```sh -starship preset nerd-font-symbols > ~/.config/starship.toml +starship preset nerd-font-symbols -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/nerd-font-symbols.toml) diff --git a/docs/presets/no-empty-icons.md b/docs/presets/no-empty-icons.md index 7be6867d1..aa4a211fd 100644 --- a/docs/presets/no-empty-icons.md +++ b/docs/presets/no-empty-icons.md @@ -9,7 +9,7 @@ If toolset files are identified the toolset icon is displayed. If the toolset is ### Configuration ```sh -starship preset no-empty-icons > ~/.config/starship.toml +starship preset no-empty-icons -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/no-empty-icons.toml) diff --git a/docs/presets/no-nerd-font.md b/docs/presets/no-nerd-font.md index 7c97a6089..2d20b0d46 100644 --- a/docs/presets/no-nerd-font.md +++ b/docs/presets/no-nerd-font.md @@ -12,7 +12,7 @@ This preset will become the default preset in a future release of starship. ### Configuration ```sh -starship preset no-nerd-font > ~/.config/starship.toml +starship preset no-nerd-font -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/no-nerd-font.toml) diff --git a/docs/presets/no-runtimes.md b/docs/presets/no-runtimes.md index a8b326cc6..c0805d111 100644 --- a/docs/presets/no-runtimes.md +++ b/docs/presets/no-runtimes.md @@ -9,7 +9,7 @@ This preset hides the version of language runtimes. If you work in containers or ### Configuration ```sh -starship preset no-runtime-versions > ~/.config/starship.toml +starship preset no-runtime-versions -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/no-runtime-versions.toml) diff --git a/docs/presets/pastel-powerline.md b/docs/presets/pastel-powerline.md index 72c8d0009..9a54bf235 100644 --- a/docs/presets/pastel-powerline.md +++ b/docs/presets/pastel-powerline.md @@ -14,7 +14,7 @@ It also shows how path substitution works in starship. ### Configuration ```sh -starship preset pastel-powerline > ~/.config/starship.toml +starship preset pastel-powerline -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/pastel-powerline.toml) diff --git a/docs/presets/plain-text.md b/docs/presets/plain-text.md index 9f9e45924..82d77ab9b 100644 --- a/docs/presets/plain-text.md +++ b/docs/presets/plain-text.md @@ -10,7 +10,7 @@ don't have access to Unicode. ### Configuration ```sh -starship preset plain-text-symbols > ~/.config/starship.toml +starship preset plain-text-symbols -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/plain-text-symbols.toml) diff --git a/docs/presets/pure-preset.md b/docs/presets/pure-preset.md index ae1e85fc4..b75a00565 100644 --- a/docs/presets/pure-preset.md +++ b/docs/presets/pure-preset.md @@ -9,7 +9,7 @@ This preset emulates the look and behavior of [Pure](https://github.com/sindreso ### Configuration ```sh -starship preset pure-preset > ~/.config/starship.toml +starship preset pure-preset -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/pure-preset.toml) diff --git a/docs/presets/tokyo-night.md b/docs/presets/tokyo-night.md index 33f517491..1bfa22ce5 100644 --- a/docs/presets/tokyo-night.md +++ b/docs/presets/tokyo-night.md @@ -13,7 +13,7 @@ This preset is inspired by [tokyo-night-vscode-theme](https://github.com/enkia/t ### Configuration ```sh -starship preset tokyo-night > ~/.config/starship.toml +starship preset tokyo-night -o ~/.config/starship.toml ``` [Click to download TOML](/presets/toml/tokyo-night.toml) diff --git a/src/main.rs b/src/main.rs index 17312f86c..dc01666b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use clap::crate_authors; use std::io; +use std::path::PathBuf; use std::thread::available_parallelism; use std::time::SystemTime; @@ -68,6 +69,9 @@ enum Commands { /// The name of preset to be printed #[clap(required_unless_present("list"), value_enum)] name: Option, + /// Output the preset to a file instead of stdout + #[clap(short, long, conflicts_with = "list")] + output: Option, /// List out all preset names #[clap(short, long)] list: bool, @@ -202,7 +206,7 @@ fn main() { print::module(&module_name, properties); } } - Commands::Preset { name, list } => print::preset_command(name, list), + Commands::Preset { name, list, output } => print::preset_command(name, output, list), Commands::Config { name, value } => { if let Some(name) = name { if let Some(value) = value { diff --git a/src/print.rs b/src/print.rs index f34c8fe59..0263b1c12 100644 --- a/src/print.rs +++ b/src/print.rs @@ -4,6 +4,7 @@ use rayon::prelude::*; use std::collections::BTreeSet; use std::fmt::{Debug, Write as FmtWrite}; use std::io::{self, Write}; +use std::path::PathBuf; use std::time::Duration; use terminal_size::terminal_size; use unicode_segmentation::UnicodeSegmentation; @@ -464,13 +465,22 @@ impl ValueEnum for Preset { } } -pub fn preset_command(name: Option, list: bool) { +pub fn preset_command(name: Option, output: Option, list: bool) { if list { println!("{}", preset_list()); return; } let variant = name.expect("name argument must be specified"); - shadow::print_preset_content(variant.0); + let content = shadow::get_preset_content(variant.0); + if let Some(output) = output { + if let Err(err) = std::fs::write(output, content) { + eprintln!("Error writing preset to file: {err}"); + std::process::exit(1); + } + } else if let Err(err) = std::io::stdout().write_all(content) { + eprintln!("Error writing preset to stdout: {err}"); + std::process::exit(1); + } } fn preset_list() -> String { @@ -485,6 +495,7 @@ mod test { use super::*; use crate::config::StarshipConfig; use crate::test::default_context; + use crate::utils; #[test] fn main_prompt() { @@ -595,10 +606,23 @@ mod test { #[test] fn preset_command_does_not_panic_on_correct_inputs() { - preset_command(None, true); + preset_command(None, None, true); Preset::value_variants() .iter() - .for_each(|v| preset_command(Some(v.clone()), false)); + .for_each(|v| preset_command(Some(v.clone()), None, false)); + } + + #[test] + fn preset_command_output_to_file() -> std::io::Result<()> { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("preset.toml"); + preset_command(Some(Preset("nerd-font-symbols")), Some(path.clone()), false); + + let actual = utils::read_file(&path)?; + let expected = include_str!("../docs/.vuepress/public/presets/toml/nerd-font-symbols.toml"); + assert_eq!(actual, expected); + + dir.close() } #[test]