Skip to content

Commit

Permalink
feat(whiskers): add --list-flavors and --list-accents (#219)
Browse files Browse the repository at this point in the history
Co-authored-by: backwardspy <[email protected]>
  • Loading branch information
uncenter and backwardspy committed May 27, 2024
1 parent a869d85 commit 2a53432
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 39 deletions.
10 changes: 8 additions & 2 deletions whiskers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ Options:
--dry-run
Dry run, don't write anything to disk

-l, --list-functions
--list-functions
List all Tera filters and functions

--list-flavors
List the Catppuccin flavors

--list-accents
List the Catppuccin accent colors

-o, --output-format <OUTPUT_FORMAT>
Output format of --list-functions
[default: json]
[possible values: json, yaml, markdown, markdown-table]
[possible values: json, yaml, markdown, markdown-table, plain]

-h, --help
Print help (see a summary with '-h')
Expand Down
14 changes: 12 additions & 2 deletions whiskers/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ type ValueMap = HashMap<String, serde_json::Value>;

#[derive(Parser, Debug)]
#[command(version, about)]
#[allow(clippy::struct_excessive_bools)] // not a problem for cli flags
pub struct Args {
/// Path to the template file, or - for stdin
#[arg(required_unless_present = "list_functions")]
#[arg(required_unless_present_any = ["list_functions", "list_flavors", "list_accents"])]
pub template: Option<FileOrStdin>,

/// Render a single flavor instead of all four
Expand Down Expand Up @@ -40,9 +41,17 @@ pub struct Args {
pub dry_run: bool,

/// List all Tera filters and functions
#[arg(short, long)]
#[arg(long)]
pub list_functions: bool,

/// List the Catppuccin flavors
#[arg(long)]
pub list_flavors: bool,

/// List the Catppuccin accent colors
#[arg(long)]
pub list_accents: bool,

/// Output format of --list-functions
#[arg(short, long, default_value = "json")]
pub output_format: OutputFormat,
Expand Down Expand Up @@ -103,6 +112,7 @@ pub enum OutputFormat {
Yaml,
Markdown,
MarkdownTable,
Plain,
}

fn json_map<T>(s: &str) -> Result<T, Error>
Expand Down
125 changes: 105 additions & 20 deletions whiskers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ fn main() -> anyhow::Result<()> {
return Ok(());
}

if args.list_flavors {
list_flavors(args.output_format);
return Ok(());
}

if args.list_accents {
list_accents(args.output_format);
return Ok(());
}

let template = args
.template
.as_ref()
Expand Down Expand Up @@ -220,34 +230,109 @@ fn override_matrix(

#[allow(clippy::too_many_lines)]
fn list_functions(format: OutputFormat) {
match format {
OutputFormat::Json | OutputFormat::Yaml => {
let output = serde_json::json!({
"functions": templating::all_functions(),
"filters": templating::all_filters()
});
println!(
"{}",
println!(
"{}",
match format {
OutputFormat::Json | OutputFormat::Yaml => {
let output = serde_json::json!({
"functions": templating::all_functions(),
"filters": templating::all_filters()
});

if matches!(format, OutputFormat::Json) {
serde_json::to_string_pretty(&output).expect("output is guaranteed to be valid")
} else {
serde_yaml::to_string(&output).expect("output is guaranteed to be valid")
}
);
}
OutputFormat::Markdown => {
markdown::display_functions_as_list()
}
OutputFormat::MarkdownTable => {
markdown::display_functions_as_table()
}
OutputFormat::Plain => {
let mut list = templating::all_filters()
.iter()
.map(|f| f.name.clone())
.collect::<Vec<String>>();

list.extend(templating::all_functions().iter().map(|f| f.name.clone()));

list.join("\n")
}
}
OutputFormat::Markdown => {
println!(
"{}",
markdown::format_filters_and_functions(markdown::Format::List)
);
);
}

fn list_flavors(format: OutputFormat) {
let format = match format {
OutputFormat::Markdown | OutputFormat::MarkdownTable => {
eprintln!("warning: Markdown output is not yet supported for listing flavors, reverting to `plain`");
OutputFormat::Plain
}
OutputFormat::MarkdownTable => {
println!(
"{}",
markdown::format_filters_and_functions(markdown::Format::Table)
);
other => other,
};

let output = catppuccin::PALETTE
.all_flavors()
.map(catppuccin::Flavor::identifier);

println!(
"{}",
match format {
OutputFormat::Json | OutputFormat::Yaml => {
let output = serde_json::json!(output);

if matches!(format, OutputFormat::Json) {
serde_json::to_string_pretty(&output).expect("output is guaranteed to be valid")
} else {
serde_yaml::to_string(&output).expect("output is guaranteed to be valid")
}
}
OutputFormat::Plain => {
output.join("\n")
}
_ => todo!(),
}
}
);
}

fn list_accents(format: OutputFormat) {
let format = match format {
OutputFormat::Markdown | OutputFormat::MarkdownTable => {
eprintln!("warning: Markdown output is not yet supported for listing accents, reverting to `plain`");
OutputFormat::Plain
}
other => other,
};

let output = catppuccin::PALETTE
.latte
.colors
.all_colors()
.into_iter()
.filter_map(|c| if c.accent { Some(c.identifier()) } else { None })
.collect::<Vec<_>>();

println!(
"{}",
match format {
OutputFormat::Json | OutputFormat::Yaml => {
let output = serde_json::json!(output);

if matches!(format, OutputFormat::Json) {
serde_json::to_string_pretty(&output).expect("output is guaranteed to be valid")
} else {
serde_yaml::to_string(&output).expect("output is guaranteed to be valid")
}
}
OutputFormat::Plain => {
output.join("\n")
}
_ => todo!(),
}
);
}

fn template_name(template: &clap_stdin::FileOrStdin) -> String {
Expand Down
17 changes: 2 additions & 15 deletions whiskers/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@ use itertools::Itertools as _;

use crate::templating;

#[derive(Clone, Copy)]
pub enum Format {
List,
Table,
}

#[must_use]
pub fn format_filters_and_functions(format: Format) -> String {
match format {
Format::List => list_format(),
Format::Table => table_format(),
}
}

fn list_format() -> String {
pub fn display_functions_as_list() -> String {
let mut result = String::new();
result.push_str("## Functions\n\n");
for function in templating::all_functions() {
Expand Down Expand Up @@ -72,7 +59,7 @@ fn list_format() -> String {
result
}

fn table_format() -> String {
pub fn display_functions_as_table() -> String {
let mut result = String::new();
result.push_str("### Functions\n\n");
result.push_str("| Name | Description | Examples |\n");
Expand Down

0 comments on commit 2a53432

Please sign in to comment.