diff --git a/examples/render-mandoc/docs/download.1 b/examples/render-mandoc/docs/download.1 index f4398bb2..76e19da7 100644 --- a/examples/render-mandoc/docs/download.1 +++ b/examples/render-mandoc/docs/download.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 3.2 .\" -.TH "download" "1" "November 2025" "Version 0.1.0" "Sample application" +.TH "download" "1" "December 2025" "Version 0.1.0" "Sample application" .SH NAME \f[B]download\f[R] \- Sample application .SH SYNOPSIS diff --git a/examples/render-mandoc/docs/download.md b/examples/render-mandoc/docs/download.md index d1dfb470..552fac15 100644 --- a/examples/render-mandoc/docs/download.md +++ b/examples/render-mandoc/docs/download.md @@ -1,6 +1,6 @@ % download(1) Version 0.1.0 | Sample application % Lana Lang -% November 2025 +% December 2025 NAME ================================================== diff --git a/lib/bashly/concerns/renderable.rb b/lib/bashly/concerns/renderable.rb index 56ae490b..ebbc5750 100644 --- a/lib/bashly/concerns/renderable.rb +++ b/lib/bashly/concerns/renderable.rb @@ -51,6 +51,13 @@ def user_file_exist?(file) File.exist? user_file_path(file) end + # Returns a wrapped, indented and sanitized string + # Designed to place help and example messages inside bash's 'printf' + def user_string(text, indent: 0) + wrap = Settings.word_wrap - indent + text.wrap(wrap).indent(indent).sanitize_for_print + end + private def view_path(view) diff --git a/lib/bashly/extensions/string.rb b/lib/bashly/extensions/string.rb index 7c7c20f2..ea066729 100644 --- a/lib/bashly/extensions/string.rb +++ b/lib/bashly/extensions/string.rb @@ -33,7 +33,7 @@ def to_path tr(' ', '/').downcase end - def wrap(length = 80) + def wrap(length) strip! split("\n").collect! do |line| if line.length > length diff --git a/lib/bashly/libraries/settings/settings.yml b/lib/bashly/libraries/settings/settings.yml index 5f05de2b..92af221b 100644 --- a/lib/bashly/libraries/settings/settings.yml +++ b/lib/bashly/libraries/settings/settings.yml @@ -84,6 +84,9 @@ strict: false # (every 2 leading spaces will be converted to a tab character) tab_indent: false +# Set the character width used to wrap help and example messages +word_wrap: 80 + # Choose a post-processor for the generated script: # formatter: internal # Use Bashly’s built-in formatter (removes extra newlines) # formatter: external # Run the external command `shfmt --case-indent --indent 2` diff --git a/lib/bashly/settings.rb b/lib/bashly/settings.rb index c07d0014..6bb0b354 100644 --- a/lib/bashly/settings.rb +++ b/lib/bashly/settings.rb @@ -27,7 +27,8 @@ class << self :tab_indent, :target_dir, :usage_colors, - :var_aliases + :var_aliases, + :word_wrap ) def commands_dir @@ -173,6 +174,10 @@ def var_aliases @var_aliases ||= get :var_aliases end + def word_wrap + @word_wrap ||= get :word_wrap + end + private def get(key) diff --git a/lib/bashly/views/argument/usage.gtx b/lib/bashly/views/argument/usage.gtx index 1b8d992c..2c2d46b8 100644 --- a/lib/bashly/views/argument/usage.gtx +++ b/lib/bashly/views/argument/usage.gtx @@ -1,7 +1,7 @@ = view_marker > printf " %s\n" "{{ label.color(:arg) }}" -> printf "{{ help.wrap(76).indent(4).sanitize_for_print }}\n" +> printf "{{ user_string help, indent: 4 }}\n" if allowed > printf " %s\n" "{{ strings[:allowed] % { values: allowed.join(', ') } }}" diff --git a/lib/bashly/views/command/examples_on_error.gtx b/lib/bashly/views/command/examples_on_error.gtx index 66999f50..222a83d0 100644 --- a/lib/bashly/views/command/examples_on_error.gtx +++ b/lib/bashly/views/command/examples_on_error.gtx @@ -3,6 +3,6 @@ if Settings.show_examples_on_error && examples > printf "{{ strings[:examples_caption_on_error] }}\n" >&2 examples.each do |example| - > printf "{{ example.wrap(78).indent(2).sanitize_for_print }}\n" >&2 + > printf "{{ user_string example, indent: 2 }}\n" >&2 end end diff --git a/lib/bashly/views/command/usage.gtx b/lib/bashly/views/command/usage.gtx index b87e9510..57db51a6 100644 --- a/lib/bashly/views/command/usage.gtx +++ b/lib/bashly/views/command/usage.gtx @@ -9,7 +9,7 @@ else = help_header_override.indent 4 else > printf "{{ full_name }}\n\n" - > printf "{{ help.wrap(78).indent(2).sanitize_for_print }}\n\n" + > printf "{{ user_string help, indent: 2 }}\n\n" end > else > printf "{{ caption_string.sanitize_for_print }}\n\n" diff --git a/lib/bashly/views/command/usage_args.gtx b/lib/bashly/views/command/usage_args.gtx index 60f5b498..6927450f 100644 --- a/lib/bashly/views/command/usage_args.gtx +++ b/lib/bashly/views/command/usage_args.gtx @@ -12,7 +12,7 @@ end if catch_all.help > > echo " {{ catch_all.label }}" - > printf "{{ catch_all.help.wrap(76).indent(4).sanitize_for_print }}\n" + > printf "{{ user_string catch_all.help, indent: 4 }}\n" > echo end diff --git a/lib/bashly/views/command/usage_examples.gtx b/lib/bashly/views/command/usage_examples.gtx index 9a15953a..e0b93223 100644 --- a/lib/bashly/views/command/usage_examples.gtx +++ b/lib/bashly/views/command/usage_examples.gtx @@ -3,7 +3,7 @@ > printf "%s\n" "{{ strings[:examples].color(:caption) }}" examples.each do |example| - > printf "{{ example.wrap(78).indent(2).sanitize_for_print }}\n" + > printf "{{ user_string example, indent: 2 }}\n" end > echo diff --git a/lib/bashly/views/environment_variable/usage.gtx b/lib/bashly/views/environment_variable/usage.gtx index 3e92f893..57573af2 100644 --- a/lib/bashly/views/environment_variable/usage.gtx +++ b/lib/bashly/views/environment_variable/usage.gtx @@ -1,7 +1,7 @@ = view_marker > printf " %s\n" "{{ usage_string(extended: true).color(:environment_variable) }}" -> printf "{{ help.wrap(76).indent(4).sanitize_for_print }}\n" +> printf "{{ user_string help, indent: 4 }}\n" if allowed > printf " %s\n" "{{ strings[:allowed] % { values: allowed.join(', ') } }}" diff --git a/lib/bashly/views/flag/usage.gtx b/lib/bashly/views/flag/usage.gtx index 25d684c8..a13ce1de 100644 --- a/lib/bashly/views/flag/usage.gtx +++ b/lib/bashly/views/flag/usage.gtx @@ -1,7 +1,7 @@ = view_marker > printf " %s\n" "{{ usage_string(extended: true).color(:flag) }}" -> printf "{{ help.wrap(76).indent(4).sanitize_for_print }}\n" +> printf "{{ user_string help, indent: 4 }}\n" if allowed > printf " %s\n" "{{ strings[:allowed] % { values: allowed.join(', ') } }}" diff --git a/schemas/settings.json b/schemas/settings.json index 057bab36..1dfc8f55 100644 --- a/schemas/settings.json +++ b/schemas/settings.json @@ -132,6 +132,12 @@ "type": "boolean", "default": false }, + "word_wrap": { + "title": "word wrap", + "description": "Configure the character width used to wrap help and example messages\nhttps://bashly.dev/usage/settings/#word_wrap", + "type": "integer", + "default": 80 + }, "compact_short_flags": { "title": "compact short flags", "description": "Whether to expand -abc to -a -b -c in the input line\nhttps://bashly.dev/usage/settings/#compact_short_flags", diff --git a/support/schema/settings.yml b/support/schema/settings.yml index d8ef047c..c1845a7e 100644 --- a/support/schema/settings.yml +++ b/support/schema/settings.yml @@ -111,6 +111,13 @@ properties: https://bashly.dev/usage/settings/#tab_indent type: boolean default: false + word_wrap: + title: word wrap + description: |- + Configure the character width used to wrap help and example messages + https://bashly.dev/usage/settings/#word_wrap + type: integer + default: 80 compact_short_flags: title: compact short flags description: |-