-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor-samples
More file actions
executable file
·357 lines (304 loc) · 13.6 KB
/
color-samples
File metadata and controls
executable file
·357 lines (304 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# Define ANSI escape codes for formatting
NORMAL="\e[0m"
BOLD="\e[1m"
DIM="\e[2m" # Often looks like normal or bold depending on terminal
ITALIC="\e[3m" # Support varies between terminals
UNDERLINE="\e[4m"
BLINK="\e[5m" # Often not supported or shown as background change
REVERSE="\e[7m" # Swaps foreground and background
HIDDEN="\e[8m" # Text is invisible
STRIKETHROUGH="\e[9m" # Support varies between terminals
# Define ANSI escape codes for standard colors (foreground and background)
declare -A standard_colors_fg=(
[black]="30" [red]="31" [green]="32" [yellow]="33"
[blue]="34" [magenta]="35" [cyan]="36" [white]="37"
)
declare -A standard_colors_bg=(
[black]="40" [red]="41" [green]="42" [yellow]="43"
[blue]="44" [magenta]="45" [cyan]="46" [white]="47"
)
# Define ANSI escape codes for bright colors (foreground and background)
declare -A bright_colors_fg=(
[black]="90" [red]="91" [green]="92" [yellow]="93"
[blue]="94" [magenta]="95" [cyan]="96" [white]="97"
)
declare -A bright_colors_bg=(
[black]="100" [red]="101" [green]="102" [yellow]="103"
[blue]="104" [magenta]="105" [cyan]="106" [white]="107"
)
# Map format names to their codes
declare -A ALL_FORMATS=(
[Normal]="$NORMAL" [Bold]="$BOLD" [Dim]="$DIM" [Italic]="$ITALIC"
[Underline]="$UNDERLINE" [Blink]="$BLINK" [Reverse]="$REVERSE"
[Hidden]="$HIDDEN" [Strikethrough]="$STRIKETHROUGH"
# Add aliases if needed, e.g. [Default]="$NORMAL"
)
DEFAULT_COLUMNS=("Normal" "Bold" "Italic" "Underline")
# Sample text
SAMPLE_TEXT="Sample Text"
SELECTED_COLUMNS=()
USE_COLUMNS=false
# Define column width for columnar display
COLUMN_WIDTH=15
# Check if the first argument starts with --column or --columns
if [[ "$1" =~ ^--col(umn|umns) ]]; then
USE_COLUMNS=true
COLUMN_LIST_STR=""
# Check if there's an '=' sign for --columns=list format
if [[ "$1" == *=* ]]; then
COLUMN_LIST_STR="${1#*=}"
# Check if $2 exists and is not another option for --columns list format
elif [[ -n "$2" && ! "$2" =~ ^- ]]; then
COLUMN_LIST_STR="$2"
shift # Consume the list argument ($2)
else
# No list provided after --columns, use default
COLUMN_LIST_STR=$(
IFS=:
echo "${DEFAULT_COLUMNS[*]}"
)
fi
shift # Consume the --column(s) argument ($1)
# Parse the list string into the array
IFS=':' read -r -a SELECTED_COLUMNS <<<"$COLUMN_LIST_STR"
# Validate selected columns and handle empty list case
VALID_COLUMNS=()
INVALID_COLUMNS=()
if [[ ${#SELECTED_COLUMNS[@]} -eq 0 || "$COLUMN_LIST_STR" == "" ]]; then
# If parsing resulted in empty array or empty string, use default
SELECTED_COLUMNS=("${DEFAULT_COLUMNS[@]}")
VALID_COLUMNS=("${DEFAULT_COLUMNS[@]}")
else
for col in "${SELECTED_COLUMNS[@]}"; do
if [[ -v ALL_FORMATS["$col"] ]]; then
VALID_COLUMNS+=("$col")
else
INVALID_COLUMNS+=("$col")
fi
done
SELECTED_COLUMNS=("${VALID_COLUMNS[@]}") # Use only valid columns
if [[ ${#SELECTED_COLUMNS[@]} -eq 0 ]]; then # If no valid columns remain
echo "Warning: No valid columns specified. Using default." >&2
SELECTED_COLUMNS=("${DEFAULT_COLUMNS[@]}")
fi
if [[ ${#INVALID_COLUMNS[@]} -gt 0 ]]; then
echo "Warning: Ignoring invalid column names: ${INVALID_COLUMNS[*]}" >&2
fi
fi
else
# Not using columns or some other argument was provided first
:
fi
# Function to print samples in single-line format
print_single_line() {
local format_code="$1"
local format_name="$2"
local fg_code="$3"
local bg_code="$4"
local fg_name="$5"
local bg_name="$6"
local color_type="$7" # "Standard" or "Bright"
local fg_info="${color_type} ${fg_name}"
local bg_info="${color_type} ${bg_name}"
local info=""
if [[ -z "$fg_code" && -z "$bg_code" ]]; then
info="(BG default, FG default)"
elif [[ -z "$fg_code" ]]; then
info="(BG ${bg_info}, FG default)"
elif [[ -z "$bg_code" ]]; then
info="(BG default, FG ${fg_info})"
else
info="(BG ${bg_info}, FG ${fg_info})"
fi
echo -e "${format_code}\e[${fg_code};${bg_code}m${SAMPLE_TEXT}${NORMAL} ${info}"
}
if [[ "$USE_COLUMNS" == true ]]; then
VISIBLE_TEXT_LENGTH=${#SAMPLE_TEXT}
PADDING_SPACES=$((COLUMN_WIDTH - VISIBLE_TEXT_LENGTH))
NUM_COLUMNS=${#SELECTED_COLUMNS[@]}
echo "--- Columnar Display of Standard Colors and Basic Formats ---"
# Print header row dynamically
HEADER_FORMAT=""
for ((i = 0; i < NUM_COLUMNS; i++)); do
HEADER_FORMAT+="%-*s"
done
HEADER_FORMAT+="%s\n" # For the Color Setting column
# Prepare arguments for printf: width followed by column name for each column
printf_args=()
for col_name in "${SELECTED_COLUMNS[@]}"; do
printf_args+=("$COLUMN_WIDTH" "$col_name")
done
printf_args+=("Color Setting") # Add the last column name
# Print header with correct arguments for dynamic width
printf "$HEADER_FORMAT" "${printf_args[@]}"
# Print separator line dynamically
for ((i = 0; i < NUM_COLUMNS; i++)); do
printf "%0.s-" $(seq 1 $COLUMN_WIDTH)
done
printf "%0.s-" $(seq 1 40) # Separator for the info column
printf "\n"
# Add "default" to the list of names for iteration
standard_fg_names=("default" "${!standard_colors_fg[@]}")
standard_bg_names=("default" "${!standard_colors_bg[@]}")
for bg_name in "${standard_bg_names[@]}"; do
bg_code="${standard_colors_bg[$bg_name]}" # This will be empty if bg_name is "default"
for fg_name in "${standard_fg_names[@]}"; do
fg_code="${standard_colors_fg[$fg_name]}" # This will be empty if fg_name is "default"
# Handle 'default' display name
display_fg_name="${fg_name}"
display_bg_name="${bg_name}"
[[ "$fg_name" == "default" ]] && display_fg_name="default"
[[ "$bg_name" == "default" ]] && display_bg_name="default"
info="(BG standard-${display_bg_name}, FG standard-${display_fg_name})"
# Construct escape sequence, handling empty codes
esc_seq=""
[[ -n "$fg_code" ]] && esc_seq="${fg_code}"
[[ -n "$bg_code" ]] && {
[[ -n "$esc_seq" ]] && esc_seq+=";"
esc_seq+="${bg_code}"
}
# Print selected format columns with padding
for format_name in "${SELECTED_COLUMNS[@]}"; do
format_code="${ALL_FORMATS[$format_name]}"
# Special handling for Hidden text
text_to_print="$SAMPLE_TEXT"
[[ "$format_name" == "Hidden" ]] && text_to_print="Hidden Text"
# Calculate padding needed for this specific text
current_padding=$((COLUMN_WIDTH - ${#text_to_print}))
[[ $current_padding -lt 0 ]] && current_padding=0 # Ensure padding isn't negative
padding_str=$(printf "%*s" $current_padding "")
# Print formatted text and padding together
printf "%b%s" "${format_code}\e[${esc_seq}m${text_to_print}${NORMAL}" "$padding_str"
done
# Print Info column and newline
printf "%s\n" "$info"
done
done
# Bright Color Combinations
echo -e "\n--- Columnar Display of Bright Colors and Basic Formats ---"
# Print header row dynamically
HEADER_FORMAT=""
for ((i = 0; i < NUM_COLUMNS; i++)); do
HEADER_FORMAT+="%-*s"
done
HEADER_FORMAT+="%s\n" # For the Color Setting column
# Prepare arguments for printf: width followed by column name for each column
printf_args=()
for col_name in "${SELECTED_COLUMNS[@]}"; do
printf_args+=("$COLUMN_WIDTH" "$col_name")
done
printf_args+=("Color Setting") # Add the last column name
# Print header with correct arguments for dynamic width
printf "$HEADER_FORMAT" "${printf_args[@]}"
# Print separator line dynamically
for ((i = 0; i < NUM_COLUMNS; i++)); do
printf "%0.s-" $(seq 1 $COLUMN_WIDTH)
done
printf "%0.s-" $(seq 1 40) # Separator for the info column
printf "\n"
bright_fg_names=("default" "${!bright_colors_fg[@]}")
bright_bg_names=("default" "${!bright_colors_bg[@]}")
for bg_name in "${bright_bg_names[@]}"; do
bg_code="${bright_colors_bg[$bg_name]}" # This will be empty if bg_name is "default"
for fg_name in "${bright_fg_names[@]}"; do
fg_code="${bright_colors_fg[$fg_name]}" # This will be empty if fg_name is "default"
# Handle 'default' display name
display_fg_name="${fg_name}"
display_bg_name="${bg_name}"
[[ "$fg_name" == "default" ]] && display_fg_name="default"
[[ "$bg_name" == "default" ]] && display_bg_name="default"
info="(BG bright-${display_bg_name}, FG bright-${display_fg_name})"
# Construct escape sequence, handling empty codes
esc_seq=""
[[ -n "$fg_code" ]] && esc_seq="${fg_code}"
[[ -n "$bg_code" ]] && {
[[ -n "$esc_seq" ]] && esc_seq+=";"
esc_seq+="${bg_code}"
}
# Print selected format columns with padding
for format_name in "${SELECTED_COLUMNS[@]}"; do
format_code="${ALL_FORMATS[$format_name]}"
# Special handling for Hidden text
text_to_print="$SAMPLE_TEXT"
[[ "$format_name" == "Hidden" ]] && text_to_print="Hidden Text"
# Calculate padding needed for this specific text
current_padding=$((COLUMN_WIDTH - ${#text_to_print}))
[[ $current_padding -lt 0 ]] && current_padding=0 # Ensure padding isn't negative
padding_str=$(printf "%*s" $current_padding "")
# Print formatted text and padding together
printf "%b%s" "${format_code}\e[${esc_seq}m${text_to_print}${NORMAL}" "$padding_str"
done
# Print Info column and newline
printf "%s\n" "$info"
done
done
else
# Original single-line output
echo "--- Standard Colors and Basic Formats ---"
# Iterate through formats
for format_name in "Normal" "Bold" "Italic" "Underline"; do
format_code=""
case "$format_name" in
"Normal") format_code="${NORMAL}" ;;
"Bold") format_code="${BOLD}" ;;
"Italic") format_code="${ITALIC}" ;;
"Underline") format_code="${UNDERLINE}" ;;
esac
echo -e "\n--- Format: $format_name ---"
# Standard Foreground Colors (default background)
for fg_name in "${!standard_colors_fg[@]}"; do
fg_code="${standard_colors_fg[$fg_name]}"
print_single_line "$format_code" "$format_name" "$fg_code" "" "$fg_name" "default" "Standard"
done
# Standard Background Colors (default foreground)
echo -e "\n"
for bg_name in "${!standard_colors_bg[@]}"; do
bg_code="${standard_colors_bg[$bg_name]}"
print_single_line "$format_code" "$format_name" "" "$bg_code" "default" "$bg_name" "Standard"
done
# Standard Foreground and Background Combinations
echo -e "\n"
for fg_name in "${!standard_colors_fg[@]}"; do
fg_code="${standard_colors_fg[$fg_name]}"
for bg_name in "${!standard_colors_bg[@]}"; do
bg_code="${standard_colors_bg[$bg_name]}"
print_single_line "$format_code" "$format_name" "$fg_code" "$bg_code" "$fg_name" "$bg_name" "Standard"
done
done
done
# Add other formats section for single-line output
echo -e "\n--- Other Formats (default colors) ---"
echo -e "${DIM}${SAMPLE_TEXT}${NORMAL} (Dim, BG default, FG default)"
echo -e "${BLINK}${SAMPLE_TEXT}${NORMAL} (Blink, BG default, FG default)"
echo -e "${REVERSE}${SAMPLE_TEXT}${NORMAL} (Reverse, BG default, FG default)"
echo -e "${HIDDEN}Hidden Text${NORMAL} (Hidden, BG default, FG default)"
echo -e "${STRIKETHROUGH}${SAMPLE_TEXT}${NORMAL} (Strikethrough, BG default, FG default)"
echo -e "\n--- Bright Colors ---"
# Bright Foreground Colors (default background)
echo -e "\n--- Bright Foreground Colors ---"
for fg_name in "${!bright_colors_fg[@]}"; do
fg_code="${bright_colors_fg[$fg_name]}"
print_single_line "" "Normal" "$fg_code" "" "bright-${fg_name}" "default" "Bright"
done
# Bright Background Colors (default foreground)
echo -e "\n--- Bright Background Colors ---"
for bg_name in "${!bright_colors_bg[@]}"; do
bg_code="${bright_colors_bg[$bg_name]}"
print_single_line "" "Normal" "" "$bg_code" "default" "bright-${bg_name}" "Bright"
done
# Bright Foreground and Background Combinations
echo -e "\n--- Bright Foreground and Background Combinations ---"
for fg_name in "${!bright_colors_fg[@]}"; do
fg_code="${bright_colors_fg[$fg_name]}"
for bg_name in "${!bright_colors_bg[@]}"; do
bg_code="${bright_colors_bg[$bg_name]}"
print_single_line "" "Normal" "$fg_code" "$bg_code" "bright-${fg_name}" "bright-${bg_name}" "Bright"
done
done
# Removed the duplicate "Other Formats" section from here as it was added earlier
echo -e "\n--- Combined Formats (default colors unless specified) ---"
echo -e "${BOLD}${UNDERLINE}${SAMPLE_TEXT}${NORMAL} (Bold, Underlined, BG default, FG default)"
echo -e "${ITALIC}${BOLD}\e[31m${SAMPLE_TEXT}${NORMAL} (Bold, Italic, BG default, Red FG)" # Combine with color
echo -e "\n--- End of Samples ---"
fi