Skip to content

Commit b87e6a2

Browse files
committed
fix text and date funcs; fix tests; fix colors
1 parent 490ff46 commit b87e6a2

9 files changed

+274
-102
lines changed

Makefile.inc

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Makefile.inc
2-
# Copyright 2006-2014 Alan K. Stebbens <[email protected]>
2+
# Copyright 2006-2022 Alan K. Stebbens <[email protected]>
33
#
44
# $Id$
55
#
6-
# Makefile for managing sets of files
6+
# Makefile for managing sets of files
77
#
88
# bins = list of "binaries" (could be scripts)
99
# bindirs = list of directories into which the binaries can be installed
@@ -24,21 +24,21 @@ disthost ?= $(distuser)@$$DISTHOST
2424
distpath ?= .
2525

2626
# These should be defined in the "parent" Makefile
27-
# bindirs
28-
# bins
29-
# libdirs
30-
# libs
27+
# bindirs
28+
# bins
29+
# libdirs
30+
# libs
3131
# incdirs
3232
# incs
3333
# cssdirs
3434
# cssfiles
3535
# tmpldirs
3636
# tmpls
37-
# tests
38-
# subdirs
37+
# tests
38+
# subdirs
3939

4040
# This is a list of files which are in development, and which should not be installed
41-
# indev
41+
# indev
4242

4343
# if the target is "diff" or "difflist", use -k to keep going even if "diff" errors
4444

@@ -55,10 +55,11 @@ allfiles = $(bins) $(libs) $(incs) $(cssfiles) Makefile RCS
5555

5656
afile = dummy
5757

58-
.PHONY: subdirs default help status diff diffs difflist
58+
.PHONY: subdirs default help status diff diffs difflist
5959
.PHONY: fetch install install-bins install-libs install-files zip putzip getzip $(subdirs)
6060

6161
default: help
62+
.DEFAULT_GOAL = help
6263
help:
6364
@echo "You can make these things:"
6465
@echo " help"
@@ -67,7 +68,7 @@ help:
6768
@echo " difflist -- show different files installed versions"
6869
@echo " install -- install"
6970
@echo " fetch -- fetch the installed file(s)"
70-
@echo " test -- run tests"
71+
@echo " tests -- run tests"
7172
@echo " zip -- Create a zip file "
7273
@echo " putzip -- distribute zipfile to yoda"
7374
@echo " getzip -- fetch zipfile from yoda"
@@ -143,7 +144,7 @@ action_if_exists = \
143144
if [[ -f $(1) ]]; then \
144145
$(2) ; \
145146
fi
146-
147+
147148

148149
# These are the known actions
149150
# diff_file

arg-utils.sh

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#
33
# handy functions for flexibly managing function arguments in bash scripts
44
#
5-
# Copyright 2006-2018 Alan K. Stebbens <[email protected]>
5+
# Copyright 2006-2022 Alan K. Stebbens <[email protected]>
66
#
77

8-
ARG_UTILS_VERSION="arg-utils.sh v2.2"
8+
ARG_UTILS_VERSION="arg-utils.sh v2.4"
99

1010
[[ "$ARG_UTILS_SH" = "$ARG_UTILS_VERSION" ]] && return
1111
ARG_UTILS_SH="$ARG_UTILS_VERSION"
@@ -160,7 +160,7 @@ __arg_or_input() {
160160
else
161161
arg="$1"
162162
fi
163-
echo "$arg"
163+
echo -n "$arg"
164164
}
165165

166166

@@ -183,7 +183,7 @@ __args_or_input() {
183183
done
184184
echo "${args[@]}"
185185
else
186-
echo "$@"
186+
echo -n "$@"
187187
fi
188188
}
189189

@@ -198,12 +198,15 @@ args_or_stdin() {
198198

199199
__args_or_stdin() {
200200
if [[ $# -gt 0 ]] ; then
201-
echo "$*"
201+
echo -n "$*"
202202
else
203203
cat
204204
fi
205205
}
206206

207+
arg_or_stdin() { __args_or_stdin "$1" ; }
208+
__arg_or_stdin() { __args_or_stdin "$1" ; }
209+
207210

208211
# __append_arg ARG
209212
# __append_args ARGS
@@ -216,7 +219,7 @@ __args_or_stdin() {
216219
__append_arg() {
217220
local -a data
218221
read -a data
219-
echo "${data[@]}" "$@"
222+
echo -n "${data[@]}" "$@"
220223
}
221224
__append_args() { __append_arg "$@" ; }
222225

date-utils.sh

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# bash
22
# date-utils.sh -- date management utility for bash
33
#
4-
# Copyright 2009-2015 Alan K. Stebbens <[email protected]>
4+
# Copyright 2009-2022 Alan K. Stebbens <[email protected]>
55

6-
DATE_UTILS_VERSION="date-utils.sh v2.2"
6+
DATE_UTILS_VERSION="date-utils.sh v2.3"
77
[[ "$DATE_UTILS_SH" = "$DATE_UTILS_VERSION" ]] && return
88
DATE_UTILS_SH="$DATE_UTILS_VERSION"
99

@@ -134,7 +134,7 @@ date_to_days_since_epoch DATESTRING - the number of days since Epoch
134134
135135
date_to_seconds - the number of seconds since Epoch
136136
137-
Both `date_to_days_since_epoch` and `date_to_seconds` are provided for
137+
Both `date_to_days_since_epoch` and `date_to_seconds` are provided for
138138
each usage with and for Unix `tm` values.
139139
140140
today - today's date
@@ -155,7 +155,7 @@ date format, given by DATE_FORMAT. If DATE_FORMAT is not defined, the format
155155
156156
Most of the date format codes are performed by a function, which are listed
157157
here for completeness. Typically, they would be invoked via `date_format CODE
158-
DATESTRING`. All of these `df_...` functions accept no arguments, taking
158+
DATESTRING`. All of these `df_...` functions accept no arguments, taking
159159
the date component values they need from the variables set by `date_parse`.
160160
Specifically, `year`, `month`, and `day`.
161161
@@ -708,15 +708,9 @@ date_to_seconds() {
708708
echo $(( days * $SECONDS_PER_DAY ))
709709
}
710710

711-
today() { date +'%F' ; }
712-
713-
tomorrow() {
714-
get_date_x_days_since 1 `today`
715-
}
716-
717-
yesterday() {
718-
get_date_x_days_before 1 `today`
719-
}
711+
today() { date +'%F' ; }
712+
tomorrow() { get_date_x_days_since 1 `today` ; }
713+
yesterday() { get_date_x_days_before 1 `today` ; }
720714

721715
# date_format FORMAT
722716
# date_format [FORMAT] [DATESTRING]
@@ -948,15 +942,15 @@ get_date_5_years_before() { date_adjust "$1" - 5y ; }
948942
get_date_x_years_after() {
949943
local offset="${1:?Missing offset!}"
950944
shift
951-
date_add "$@" $offset years
945+
date_add "$*" $offset years
952946
}
953947

954948
get_date_x_years_since() { get_date_x_years_after "$@" ; }
955949

956950
get_date_x_years_before() {
957951
local offset="${1:?Missing offset!}"
958952
shift
959-
date_sub "$@" $offset years
953+
date_sub "$*" $offset years
960954
}
961955

962956
# get_date_last_quarter_end YYYY-MM-DD

generate-prompt-colors

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# run this script to create "prompt-colors.sh", which contains
77
# standard prompt color definitions, and a "reset_prompt_colors"
88
# function to unset them all.
9+
#
10+
# Copywrite 2006-2022 Alan K. Stebbens <[email protected]>
911

1012
PROG="${0##*/}"
1113
THIS_DIR="${0%/*}"
@@ -101,14 +103,14 @@ generate_prompt_color_names() {
101103
else
102104
cv="${1}"
103105
fi
104-
echo "\[\033[${cv}m\]"
106+
echo "\$'\033[${cv}m'"
105107
}
106108

107109
# def_color NAME ATTRCODE COLORCODE
108110
assign_color_name() {
109111
local color_name="$1"
110112
local code="`prompt_color_string $2 $3`"
111-
local def="$color_name=\"$code\""
113+
local def="$color_name=$code"
112114
votalk "+ $def"
113115
echo " $def"
114116
add_list color_name_list $color_name

test-all

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Run all tests with the name "test-*-*.sh"
55
#
6-
# Copyright 2015, Alan K. Stebbens <[email protected]>
6+
# Copyright 2015-2022, Alan K. Stebbens <[email protected]>
77

88
lines="--------------"
99
for test in test-*-*.sh ; do

test-date-utils.sh

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# Copyright 2006-2014, Alan K. Stebbens <[email protected]>
2+
# Copyright 2006-2022, Alan K. Stebbens <[email protected]>
33
#
44
# test date-utils.sh
55
#
@@ -349,6 +349,30 @@ test_23_days_between() {
349349
end_test
350350
}
351351

352+
# run_test_data_and_compare FUNCTION
353+
run_test_data_and_compare() {
354+
local func="${1:?'Missing function'}"
355+
local x input_date format expected_result actual_result target_date tdate
356+
for(( x=0; x < ${#test_data[*]}; x+=3 )) ; do
357+
input_date=${test_data[$x]}
358+
format=${test_data[$x+1]}
359+
expected_result=${test_data[$x+2]}
360+
date_parse "$input_date"
361+
actual_result=`$func "$format" "$input_date"`
362+
check_equal "$expected_result" "$actual_result" "date_format on '$format': '$expected_result' does not equal '$actual_result' "
363+
done
364+
}
365+
366+
test_30_date_format() {
367+
start_test
368+
test_data=( 2018-02-08 "-%e-%d-" "- 2-08-"
369+
2018-02-09 "-%e-%d-" "- 2-09-"
370+
2018-10-10 "-%e-%d-" "-10-10-"
371+
)
372+
run_test_data_and_compare date_format
373+
end_test
374+
}
375+
352376

353377
init_tests "$@"
354378
run_tests

test-text-utils.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ do_filter_test() {
4343

4444
if (( howmany > 1 )); then
4545
# test pipe with no arg call next
46-
out="`echo \"$input\" | $func`"
46+
out="`echo -n \"$input\" | $func`"
4747
check_and_show_results "$output" "$out" "$error in pipe"
4848
fi
4949
}

0 commit comments

Comments
 (0)