Skip to content

Commit

Permalink
move help_content_to_wrapped_string function to glint/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Jun 16, 2024
1 parent 2b08fb8 commit e82946b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
41 changes: 8 additions & 33 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -929,43 +929,16 @@ fn flag_help_to_string_with_description(
longest_flag_length: Int,
config: Config,
) -> #(String, Bool) {
help_content_to_wrapped_string(
utils.help_content_to_wrapped_string(
flag_help_to_string(help),
help.meta.description,
longest_flag_length,
config,
config.min_first_column_width,
config.max_output_width,
config.indent_width,
)
}

fn help_content_to_wrapped_string(
left: String,
right: String,
left_length: Int,
config: Config,
) -> #(String, Bool) {
let left_formatted = string.pad_right(left, left_length, " ")

let right_width =
config.max_output_width
|> int.subtract(left_length + config.indent_width)
|> int.max(config.min_first_column_width)

let lines = utils.wordwrap(right, right_width)

let wrapped = case lines {
[] | [_] -> False
_ -> True
}

let right_formatted =
string.join(
lines,
"\n" <> string.repeat(" ", config.indent_width + left_length),
)

#(left_formatted <> right_formatted, wrapped)
}

// -- HELP - FUNCTIONS - STRINGIFIERS - SUBCOMMANDS --

/// generate the styled help text for a list of subcommands
Expand Down Expand Up @@ -1005,11 +978,13 @@ fn subcommand_help_to_string(
longest_subcommand_length: Int,
config: Config,
) -> #(String, Bool) {
help_content_to_wrapped_string(
utils.help_content_to_wrapped_string(
help.name,
help.description,
longest_subcommand_length,
config,
config.min_first_column_width,
config.max_output_width,
config.indent_width,
)
}

Expand Down
28 changes: 28 additions & 0 deletions src/glint/internal/utils.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,31 @@ pub fn to_spaced_indented_string(

content |> list.sort(string.compare) |> string.join(joiner)
}

pub fn help_content_to_wrapped_string(
left: String,
right: String,
left_length: Int,
min_left_width: Int,
max_output_width: Int,
indent_width: Int,
) -> #(String, Bool) {
let left_formatted = string.pad_right(left, left_length, " ")

let right_width =
max_output_width
|> int.subtract(left_length + indent_width)
|> int.max(min_left_width)

let lines = wordwrap(right, right_width)

let wrapped = case lines {
[] | [_] -> False
_ -> True
}

let right_formatted =
string.join(lines, "\n" <> string.repeat(" ", indent_width + left_length))

#(left_formatted <> right_formatted, wrapped)
}

0 comments on commit e82946b

Please sign in to comment.