Skip to content

Commit 631c628

Browse files
committed
feat: Add 'should_output_color' and 'get_pkg_info' fns
1 parent b513718 commit 631c628

File tree

5 files changed

+94
-25
lines changed

5 files changed

+94
-25
lines changed

docs/api.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Core functions for any Bash program
1515
* [core.err_clear()](#coreerr_clear)
1616
* [core.err_exists()](#coreerr_exists)
1717
* [core.stacktrace_print()](#corestacktrace_print)
18+
* [core.should_output_color()](#coreshould_output_color)
19+
* [core.get_package_info()](#coreget_package_info)
1820

1921
### core.init()
2022

@@ -143,3 +145,21 @@ core.trap_add 'err_handler' ERR
143145

144146
_Function has no arguments._
145147

148+
### core.should_output_color()
149+
150+
Determine if color should be printed. Note that this doesn't
151+
use tput because simple environment variable checking heuristics suffice
152+
153+
### core.get_package_info()
154+
155+
Gets information from a particular package. If the key does not exist, then the value
156+
is an empty string
157+
158+
#### Arguments
159+
160+
* **$1** (string): The `$BASALT_PACKAGE_DIR` of the caller
161+
162+
#### Variables set
163+
164+
* **REPLY** (string): The full path to the directory
165+

pkg/src/public/bash-core.sh

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -309,41 +309,58 @@ core.stacktrace_print() {
309309
fi
310310
} >&2
311311

312-
# @description Determine if color should be printed
313-
# @internal
314-
core.should_color_output() {
315-
# TODO: 'COLORTERM'
316-
312+
# @description Determine if color should be printed. Note that this doesn't
313+
# use tput because simple environment variable checking heuristics suffice
314+
core.should_output_color() {
317315
# https://no-color.org
318-
if [[ -v NO_COLOR ]]; then
316+
if [ ${NO_COLOR+x} ]; then
319317
return 1
320318
fi
321319

322-
# 0 => 2 colors
323-
# 1 => 16 colors
324-
# 2 => 256 colors
325-
# 3 => 16,777,216 colors
326-
if [[ -v FORCE_COLOR ]]; then
320+
# FIXME
321+
# # 0 => 2 colors
322+
# # 1 => 16 colors
323+
# # 2 => 256 colors
324+
# # 3 => 16,777,216 colors
325+
# if [[ -v FORCE_COLOR ]]; then
326+
# return 0
327+
# fi
328+
329+
if [ "$COLORTERM" = "truecolor" ] || [ "$COLORTERM" = "24bit" ]; then
327330
return 0
328331
fi
329332

330-
if [[ $TERM == dumb ]]; then
333+
if [ "$TERM" = 'dumb' ]; then
334+
return 1
335+
fi
336+
337+
if [ -t 0 ]; then
331338
return 0
332339
fi
340+
341+
return 1
333342
}
334343

335-
# @description Get version of the package, from the point of the callsite. In other words, it
336-
# returns the version of the package that has the file containing the direct caller of this
337-
# @set REPLY The full path to the directory
338-
# @internal
339-
core.get_package_dir() {
344+
# @description Gets information from a particular package. If the key does not exist, then the value
345+
# is an empty string
346+
# @arg $1 string The `$BASALT_PACKAGE_DIR` of the caller
347+
# @set REPLY string The full path to the directory
348+
core.get_package_info() {
349+
unset REPLY; REPLY=
350+
local basalt_package_dir="$1"
351+
local key_name="$2"
340352

341-
# local start_dir="${1:-"${BASH_SOURCE[1]}"}"
342-
343-
# while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
344-
# if ! cd ..; then
345-
# return 1
346-
# fi
347-
# done
348-
:
353+
local toml_file="$basalt_package_dir/basalt.toml"
354+
355+
if [ ! -f "$toml_file" ]; then
356+
printf '%s\n' "Error: core.get_package_data: File '$toml_file' could not be found"
357+
fi
358+
359+
local regex="^[ \t]*${key_name}[ \t]*=[ \t]*['\"](.*)['\"]"
360+
while IFS= read -r line || [ -n "$line" ]; do
361+
if [[ $line =~ $regex ]]; then
362+
REPLY=${BASH_REMATCH[1]}
363+
break
364+
fi
365+
done < "$toml_file"; unset -v line
349366
}

tests/misc.bats

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bats
2+
3+
load './util/init.sh'
4+
5+
@test "core.get_package_info version works" {
6+
core.get_package_info "$BATS_TEST_DIRNAME/testdata/info1" 'version'
7+
8+
assert [ "$REPLY" = '0.4.0' ]
9+
}
10+
11+
@test "core.get_package_info version works 2" {
12+
core.get_package_info "$BATS_TEST_DIRNAME/testdata/info2" 'version2'
13+
14+
assert [ "$REPLY" = '0.4.0' ]
15+
}
16+
17+
@test "core.should_output_color works" {
18+
unset NO_COLOR COLORTERM TERM
19+
20+
NO_COLOR= run core.should_output_color
21+
assert_failure
22+
23+
COLORTERM='truecolor' run core.should_output_color
24+
assert_success
25+
26+
TERM='dumb' run core.should_output_color
27+
assert_failure
28+
}

tests/testdata/info1/basalt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[package]
2+
version = '0.4.0'

tests/testdata/info2/basalt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[package]
2+
version2 = "0.4.0"

0 commit comments

Comments
 (0)