fix(preset): add output-flag to avoid encoding issues (#4926)

This commit is contained in:
David Knaack 2023-03-03 11:46:16 +01:00 committed by GitHub
parent 4c12a7878b
commit 5e78226a3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 45 additions and 29 deletions

View File

@ -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!(),
}}
}}
"#

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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<print::Preset>,
/// Output the preset to a file instead of stdout
#[clap(short, long, conflicts_with = "list")]
output: Option<PathBuf>,
/// 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 {

View File

@ -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<Preset>, list: bool) {
pub fn preset_command(name: Option<Preset>, output: Option<PathBuf>, 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]