Skip to content

Commit 85eb031

Browse files
authored
Merge pull request #280 from bash-my-aws/refactor-skim-stdin-again
Refactor skim-stdin
2 parents 759bd23 + c06bf56 commit 85eb031

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/shared-functions

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ skim-stdin() {
1111
#
1212
# Implementation of `pipe-skimming` pattern.
1313
#
14-
# Typical usage within Bash-my-AWS:
15-
#
16-
# - local asg_names=$(skim-stdin "$@") # Append to arg list
17-
# - local asg_names=$(skim-stdin) # Only draw from STDIN
18-
#
1914
# $ stacks | skim-stdin foo bar
2015
# foo bar huginn mastodon grafana
2116
#
2217
# $ stacks
2318
# huginn CREATE_COMPLETE 2020-01-11T06:18:46.905Z NEVER_UPDATED NOT_NESTED
2419
# mastodon CREATE_COMPLETE 2020-01-11T06:19:31.958Z NEVER_UPDATED NOT_NESTED
2520
# grafana CREATE_COMPLETE 2020-01-11T06:19:47.001Z NEVER_UPDATED NOT_NESTED
21+
#
22+
# Typical usage within Bash-my-AWS functions:
23+
#
24+
# local asg_names=$(skim-stdin "$@") # Append to arg list
25+
# local asg_names=$(skim-stdin) # Only draw from STDIN
26+
27+
local skimmed_stdin="$([[ -t 0 ]] || awk 'ORS=" " { print $1 }')"
28+
29+
printf -- '%s %s' "$*" "$skimmed_stdin" |
30+
awk '{$1=$1;print}' # trim leading/trailing spaces
2631

27-
(
28-
printf -- "$*" # Print all args
29-
printf " " # Print a space
30-
[[ -t 0 ]] || awk 'ORS=" " { print $1 }' # Print first token of each line of STDIN
31-
) | awk '{$1=$1;print}' # Trim leading/trailing spaces
3232
}
3333

3434

0 commit comments

Comments
 (0)