From bec267bec786cfe283f02807bd2fdab733b7c56d Mon Sep 17 00:00:00 2001 From: Robert Attard Date: Sat, 8 Jun 2024 22:40:13 -0400 Subject: [PATCH 1/3] remove string_map utility function --- src/glint.gleam | 51 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/glint.gleam b/src/glint.gleam index 732b821..ec455c8 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -99,7 +99,7 @@ pub fn as_module(glint: Glint(a)) -> Glint(a) { /// Adjusts the indent width used to indent content under the usage, flags, /// and subcommands headings. -/// +/// /// Default: 4. /// pub fn with_indent_width(glint: Glint(a), width: Int) -> Glint(a) { @@ -107,7 +107,7 @@ pub fn with_indent_width(glint: Glint(a), width: Int) -> Glint(a) { } /// Adjusts the output width at which help text will wrap onto a new line. -/// +/// /// Default: 80. /// pub fn with_max_output_width(glint: Glint(a), width: Int) -> Glint(a) { @@ -115,7 +115,7 @@ pub fn with_max_output_width(glint: Glint(a), width: Int) -> Glint(a) { } /// Adjusts the minimum width of the column containing flag and command names. -/// +/// /// Default: 20. /// pub fn with_min_first_column_width(glint: Glint(a), width: Int) -> Glint(a) { @@ -123,7 +123,7 @@ pub fn with_min_first_column_width(glint: Glint(a), width: Int) -> Glint(a) { } /// Adjusts the size of the gap between columns in the help output. -/// +/// /// Default: 2. /// pub fn with_column_gap(glint: Glint(a), gap: Int) -> Glint(a) { @@ -778,11 +778,14 @@ fn build_subcommands_help( // -- HELP - FUNCTIONS - STRINGIFIERS -- fn app_help_to_string(help: AppHelp) -> String { + let command = case help.command.meta.name { + "" -> "" + s -> "Command: " <> s + } + [ - help.config.description - |> option.unwrap(""), - help.command.meta.name - |> string_map(string.append("Command: ", _)), + option.unwrap(help.config.description, ""), + command, string.join( utils.wordwrap( help.command.meta.description, @@ -843,12 +846,14 @@ fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String { } let flags = flags_help_to_usage_string(help.flags) - let subcommands = + let subcommands = case list.map(help.subcommands, fn(sc) { sc.name }) |> list.sort(string.compare) |> string.join(" | ") - |> string_map(string.append("( ", _)) - |> string_map(string.append(_, " )")) + { + "" -> "" + subcommands -> "( " <> subcommands <> " )" + } let named_args = help.named_args @@ -868,16 +873,13 @@ fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String { } <> "\n" <> string.repeat(" ", config.indent_width) - <> utils.wordwrap( - app_name - <> string_map(help.meta.name, string.append(" ", _)) - <> string_map(subcommands, string.append(" ", _)) - <> string_map(named_args, string.append(" ", _)) - <> string_map(unnamed_args, string.append(" ", _)) - <> string_map(flags, string.append(" ", _)), - max_usage_width, - ) - |> string.join("\n" <> string.repeat(" ", config.indent_width * 2)) + <> { + [app_name, help.meta.name, subcommands, named_args, unnamed_args, flags] + |> list.filter(is_not_empty) + |> string.join(" ") + |> utils.wordwrap(max_usage_width) + |> string.join("\n" <> string.repeat(" ", config.indent_width * 2)) + } } // -- HELP - FUNCTIONS - STRINGIFIERS - FLAGS -- @@ -1009,13 +1011,6 @@ fn subcommand_help_to_string( name <> description } -fn string_map(s: String, f: fn(String) -> String) -> String { - case s { - "" -> "" - _ -> f(s) - } -} - // ----- FLAGS ----- /// FlagEntry inputs must start with this prefix From 118cfa8139e84f383ce4b96230b26ec3b7fa3cd4 Mon Sep 17 00:00:00 2001 From: Robert Attard Date: Tue, 11 Jun 2024 14:51:39 -0400 Subject: [PATCH 2/3] refactor app help description wrapping --- src/glint.gleam | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/glint.gleam b/src/glint.gleam index ec455c8..e633e0e 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -783,16 +783,15 @@ fn app_help_to_string(help: AppHelp) -> String { s -> "Command: " <> s } + let command_description = + help.command.meta.description + |> utils.wordwrap(help.config.max_output_width) + |> string.join("\n") + [ option.unwrap(help.config.description, ""), command, - string.join( - utils.wordwrap( - help.command.meta.description, - help.config.max_output_width, - ), - "\n", - ), + command_description, command_help_to_usage_string(help.command, help.config), flags_help_to_string(help.command.flags, help.config), subcommands_help_to_string(help.command.subcommands, help.config), @@ -867,19 +866,20 @@ fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String { // The max width of the usage accounts for the constant indent let max_usage_width = config.max_output_width - config.indent_width - case config.pretty_help { - None -> usage_heading - Some(pretty) -> heading_style(usage_heading, pretty.usage) - } - <> "\n" - <> string.repeat(" ", config.indent_width) - <> { + let content = [app_name, help.meta.name, subcommands, named_args, unnamed_args, flags] |> list.filter(is_not_empty) |> string.join(" ") |> utils.wordwrap(max_usage_width) |> string.join("\n" <> string.repeat(" ", config.indent_width * 2)) + + case config.pretty_help { + None -> usage_heading + Some(pretty) -> heading_style(usage_heading, pretty.usage) } + <> "\n" + <> string.repeat(" ", config.indent_width) + <> content } // -- HELP - FUNCTIONS - STRINGIFIERS - FLAGS -- From 27078885ff56d3f586f26d7315da52bded6bf5e7 Mon Sep 17 00:00:00 2001 From: Robert Attard Date: Tue, 11 Jun 2024 16:50:03 -0400 Subject: [PATCH 3/3] remove snowflake usage of stringbuilder --- src/glint.gleam | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/glint.gleam b/src/glint.gleam index e633e0e..da23237 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -8,7 +8,6 @@ import gleam/list import gleam/option.{type Option, None, Some} import gleam/result import gleam/string -import gleam/string_builder as sb import gleam_community/ansi import gleam_community/colour.{type Colour} import glint/constraint @@ -814,14 +813,12 @@ fn flags_help_to_usage_strings(help: List(FlagHelp)) -> List(String) { /// fn flags_help_to_usage_string(help: List(FlagHelp)) -> String { use <- bool.guard(help == [], "") + let content = + help + |> flags_help_to_usage_strings + |> string.join(" ") - help - |> flags_help_to_usage_strings - |> list.intersperse(" ") - |> sb.from_strings() - |> sb.prepend(prefix: "[ ") - |> sb.append(suffix: " ]") - |> sb.to_string + "[ " <> content <> " ]" } /// convert an ArgsCount to a string for usage text