Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove string_map utility function #42

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 35 additions & 43 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,31 +98,31 @@ 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) {
Glint(..glint, config: Config(..glint.config, indent_width: width))
}

/// 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) {
Glint(..glint, config: Config(..glint.config, max_output_width: width))
}

/// 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) {
Glint(..glint, config: Config(..glint.config, min_first_column_width: width))
}

/// 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) {
Expand Down Expand Up @@ -778,18 +777,20 @@ 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
}

let command_description =
help.command.meta.description
|> utils.wordwrap(help.config.max_output_width)
|> string.join("\n")

[
help.config.description
|> option.unwrap(""),
help.command.meta.name
|> string_map(string.append("Command: ", _)),
string.join(
utils.wordwrap(
help.command.meta.description,
help.config.max_output_width,
),
"\n",
),
option.unwrap(help.config.description, ""),
command,
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),
Expand All @@ -812,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
Expand All @@ -843,12 +842,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
Expand All @@ -862,22 +863,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

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)
<> 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))
<> content
}

// -- HELP - FUNCTIONS - STRINGIFIERS - FLAGS --
Expand Down Expand Up @@ -1009,13 +1008,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
Expand Down
Loading