Skip to content

Commit f4c8086

Browse files
committed
- Add support for color usage elements
1 parent 9d7224d commit f4c8086

File tree

16 files changed

+54
-21
lines changed

16 files changed

+54
-21
lines changed

examples/custom-strings/src/bashly-strings.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Usage captions
2-
usage: "== Usage ==\\n"
3-
options: "== Options ==\\n"
4-
arguments: "== Arguments ==\\n"
5-
commands: "== Commands ==\\n"
2+
usage: "== Usage ==\n"
3+
options: "== Options ==\n"
4+
arguments: "== Arguments ==\n"
5+
commands: "== Commands ==\n"
66

77
# Fixed flags help text
88
help_flag_text: Show this helpful help

lib/bashly/extensions/string.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ def expand_tabs(tabstop = 2)
3737
"\t" * (::Regexp.last_match(1).size / tabstop)
3838
end
3939
end
40+
41+
def color(marker)
42+
color = Bashly::Settings.usage_colors.dig marker.to_s
43+
return self unless color
44+
45+
text, spaces = match(/(.*?)(\s*)$/).captures
46+
%[$(#{color} "#{text}")#{spaces}]
47+
end
4048
end

lib/bashly/settings.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class << self
44
include AssetHelper
55

66
attr_writer :compact_short_flags, :lib_dir, :partials_extension,
7-
:source_dir, :strict, :tab_indent, :target_dir
7+
:source_dir, :strict, :tab_indent, :target_dir, :usage_colors
88

99
def compact_short_flags
1010
@compact_short_flags ||= get :compact_short_flags
@@ -50,6 +50,10 @@ def target_dir
5050
@target_dir ||= get :target_dir
5151
end
5252

53+
def usage_colors
54+
@usage_colors ||= get :usage_colors
55+
end
56+
5357
private
5458

5559
def get(key)

lib/bashly/templates/settings.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ env: development
3333

3434
# The extension to use when reading/writing partial script snippets.
3535
partials_extension: sh
36+
37+
# Display various usage elements in color by providing the name of the color
38+
# function. The value for each property is a name of a function that is
39+
# available in your script, for example: `green` or `bold`.
40+
# You can run `bashly add colors` to add a standard colors library.
41+
# This option cannot be set via environment variables.
42+
usage_colors:
43+
caption: ~
44+
command: ~
45+
arg: ~
46+
flag: ~
47+
environment_variable: ~

lib/bashly/views/argument/usage.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= view_marker
22

3-
> echo " {{ label }}"
3+
> printf " %s\n" "{{ label.color(:arg) }}"
44
> printf "{{ help.wrap(76).indent(4).sanitize_for_print }}\n"
55

66
if allowed

lib/bashly/views/command/long_usage.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
= view_marker
22

33
> if [[ -n $long_usage ]]; then
4-
> printf "{{ strings[:options] }}\n"
4+
> printf "%s\n" "{{ strings[:options].color(:caption) }}"
55
>
66
= render(:usage_fixed_flags).indent 2
77
= render(:usage_flags).indent 2 if flags.any?

lib/bashly/views/command/usage.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if alt&.any?
2525
end
2626

2727
>
28-
> printf "{{ strings[:usage] }}\n"
28+
> printf "%s\n" "{{ strings[:usage].color(:caption) }}"
2929
> printf " {{ usage_string }}\n"
3030

3131
if commands.any?

lib/bashly/views/command/usage_args.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= view_marker
22

3-
> printf "{{ strings[:arguments] }}\n"
3+
> printf "%s\n" "{{ strings[:arguments].color(:caption) }}"
44

55
if args.any?
66
>

lib/bashly/views/command/usage_commands.gtx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
maxlen = command_help_data.values.map(&:keys).flatten.map(&:size).max
44

55
command_help_data.each do |group, commands|
6-
> printf "{{ group }}\n"
6+
> printf "%s\n" "{{ group.color(:caption) }}"
77

88
commands.each do |command, info|
99
if info[:help_only]
10-
> [[ -n $long_usage ]] && echo " {{ command.ljust maxlen }} {{ info[:summary] }}"
10+
> [[ -n $long_usage ]] && printf " %s {{ info[:summary] }}\n" "{{ command.ljust(maxlen).color(:command) }}"
1111
else
12-
> echo " {{ command.ljust maxlen }} {{ info[:summary] }}"
12+
> printf " %s {{ info[:summary] }}\n" "{{ command.ljust(maxlen).color(:command) }}"
1313
end
1414
end
1515

lib/bashly/views/command/usage_environment_variables.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= view_marker
22

3-
> printf "{{ strings[:environment_variables] }}\n"
3+
> printf "%s\n" "{{ strings[:environment_variables].color(:caption) }}"
44
>
55

66
environment_variables.reject(&:private).each do |env_var|

0 commit comments

Comments
 (0)