Skip to content

Commit 6537143

Browse files
authored
Merge pull request #322 from DannyBen/add/usage-colors
Add support for color usage elements
2 parents 9d7224d + 0788c8c commit 6537143

File tree

33 files changed

+488
-34
lines changed

33 files changed

+488
-34
lines changed

Runfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ end
5959

6060
help "Append the content of bashly.yml to all example READMEs"
6161
action :examples do
62+
# Disable color output (for the color examples)
63+
ENV['NO_COLOR']='1'
64+
6265
# Patch the PATH to allow the extensible example to run properly
6366
ENV['PATH']="#{Dir.pwd}/examples/extensible:#{ENV['PATH']}"
6467
Example.all.each do |example|

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
3939

4040
## Customization
4141

42+
- [colors-usage](colors-usage#readme) - adding colors to the usage text
4243
- [command-groups](command-groups#readme) - grouping sub-commands in logical sections
4344
- [custom-strings](custom-strings#readme) - configuring the script's error and usage texts
4445
- [custom-includes](custom-includes#readme) - adding and organizing your custom functions

examples/colors-usage/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli

examples/colors-usage/README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# Colors Usage Example
2+
3+
This example demonstrates how to apply colors to various usage elements.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
$ bashly add colors
10+
$ bashly add settings
11+
# ... now edit settings.yml to match the example ...
12+
$ bashly generate
13+
```
14+
15+
The `bashly add colors` command, simply created the [src/lib/colors.sh]
16+
(src/lib/colors.sh) file, with useful color functions that will be
17+
automatically included in the generated script.
18+
19+
The manually modified `settings.yml` file specifies which color function is
20+
used for which usage element.
21+
22+
Note that the output displayed in this README is not colored.
23+
24+
<!-- include: settings.yml -->
25+
26+
-----
27+
28+
## `bashly.yml`
29+
30+
```yaml
31+
name: cli
32+
help: Sample application
33+
version: 0.1.0
34+
35+
environment_variables:
36+
- name: api_key
37+
help: Set your API key
38+
39+
commands:
40+
- name: download
41+
alias: d
42+
help: Download a file
43+
44+
args:
45+
- name: source
46+
required: true
47+
help: URL to download from
48+
- name: target
49+
help: "Target filename (default: same as source)"
50+
51+
flags:
52+
- long: --force
53+
short: -f
54+
help: Overwrite existing files
55+
56+
examples:
57+
- cli download example.com
58+
- cli download example.com ./output -f
59+
60+
environment_variables:
61+
- name: default_target_location
62+
help: Set the default location to download to
63+
64+
- name: upload
65+
alias: u
66+
help: Upload a file
67+
args:
68+
- name: source
69+
required: true
70+
help: File to upload
71+
72+
flags:
73+
- long: --user
74+
short: -u
75+
arg: user
76+
help: Username to use for logging in
77+
required: true
78+
- long: --password
79+
short: -p
80+
arg: password
81+
help: Password to use for logging in
82+
```
83+
84+
## `settings.yml`
85+
86+
```yaml
87+
# Display various usage elements in color by providing the name of the color
88+
# function. The value for each property is a name of a function that is
89+
# available in your script, for example: `green` or `bold`.
90+
# You can run `bashly add colors` to add a standard colors library.
91+
# This option cannot be set via environment variables.
92+
usage_colors:
93+
caption: bold
94+
command: green
95+
arg: blue
96+
flag: magenta
97+
environment_variable: cyan
98+
99+
```
100+
101+
102+
## Generated script output
103+
104+
### `$ ./cli`
105+
106+
```shell
107+
cli - Sample application
108+
109+
Usage:
110+
cli COMMAND
111+
cli [COMMAND] --help | -h
112+
cli --version | -v
113+
114+
Commands:
115+
download Download a file
116+
upload Upload a file
117+
118+
119+
120+
```
121+
122+
### `$ ./cli -h`
123+
124+
```shell
125+
cli - Sample application
126+
127+
Usage:
128+
cli COMMAND
129+
cli [COMMAND] --help | -h
130+
cli --version | -v
131+
132+
Commands:
133+
download Download a file
134+
upload Upload a file
135+
136+
Options:
137+
--help, -h
138+
Show this help
139+
140+
--version, -v
141+
Show version number
142+
143+
Environment Variables:
144+
API_KEY
145+
Set your API key
146+
147+
148+
149+
```
150+
151+
### `$ ./cli download -h`
152+
153+
```shell
154+
cli download - Download a file
155+
156+
Alias: d
157+
158+
Usage:
159+
cli download SOURCE [TARGET] [OPTIONS]
160+
cli download --help | -h
161+
162+
Options:
163+
--help, -h
164+
Show this help
165+
166+
--force, -f
167+
Overwrite existing files
168+
169+
Arguments:
170+
SOURCE
171+
URL to download from
172+
173+
TARGET
174+
Target filename (default: same as source)
175+
176+
Environment Variables:
177+
DEFAULT_TARGET_LOCATION
178+
Set the default location to download to
179+
180+
Examples:
181+
cli download example.com
182+
cli download example.com ./output -f
183+
184+
185+
186+
```
187+
188+
189+

examples/colors-usage/settings.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Display various usage elements in color by providing the name of the color
2+
# function. The value for each property is a name of a function that is
3+
# available in your script, for example: `green` or `bold`.
4+
# You can run `bashly add colors` to add a standard colors library.
5+
# This option cannot be set via environment variables.
6+
usage_colors:
7+
caption: bold
8+
command: green
9+
arg: blue
10+
flag: magenta
11+
environment_variable: cyan
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
environment_variables:
6+
- name: api_key
7+
help: Set your API key
8+
9+
commands:
10+
- name: download
11+
alias: d
12+
help: Download a file
13+
14+
args:
15+
- name: source
16+
required: true
17+
help: URL to download from
18+
- name: target
19+
help: "Target filename (default: same as source)"
20+
21+
flags:
22+
- long: --force
23+
short: -f
24+
help: Overwrite existing files
25+
26+
examples:
27+
- cli download example.com
28+
- cli download example.com ./output -f
29+
30+
environment_variables:
31+
- name: default_target_location
32+
help: Set the default location to download to
33+
34+
- name: upload
35+
alias: u
36+
help: Upload a file
37+
args:
38+
- name: source
39+
required: true
40+
help: File to upload
41+
42+
flags:
43+
- long: --user
44+
short: -u
45+
arg: user
46+
help: Username to use for logging in
47+
required: true
48+
- long: --password
49+
short: -p
50+
arg: password
51+
help: Password to use for logging in
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/download_command.sh'"
2+
echo "# code for 'cli download' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Color functions [@bashly-upgrade colors]
2+
## This file is a part of Bashly standard library
3+
##
4+
## Usage:
5+
## Use any of the functions below to color or format a portion of a string.
6+
##
7+
## echo "before $(red this is red) after"
8+
## echo "before $(green_bold this is green_bold) after"
9+
##
10+
## Color output will be disabled if `NO_COLOR` environment variable is set
11+
## in compliance with https://no-color.org/
12+
##
13+
print_in_color() {
14+
local color="$1"
15+
shift
16+
if [[ -z ${NO_COLOR+x} ]]; then
17+
printf "$color%b\e[0m\n" "$*"
18+
else
19+
printf "%b\n" "$*"
20+
fi
21+
}
22+
23+
red() { print_in_color "\e[31m" "$*"; }
24+
green() { print_in_color "\e[32m" "$*"; }
25+
yellow() { print_in_color "\e[33m" "$*"; }
26+
blue() { print_in_color "\e[34m" "$*"; }
27+
magenta() { print_in_color "\e[35m" "$*"; }
28+
cyan() { print_in_color "\e[36m" "$*"; }
29+
bold() { print_in_color "\e[1m" "$*"; }
30+
underlined() { print_in_color "\e[4m" "$*"; }
31+
red_bold() { print_in_color "\e[1;31m" "$*"; }
32+
green_bold() { print_in_color "\e[1;32m" "$*"; }
33+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
35+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
38+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
39+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/upload_command.sh'"
2+
echo "# code for 'cli upload' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args

0 commit comments

Comments
 (0)