diff --git a/src/arg.sh b/src/arg.sh index f01fd40..466ae79 100644 --- a/src/arg.sh +++ b/src/arg.sh @@ -18,7 +18,13 @@ arg::interpolate() { local -r arg="$1" local -r value="$2" - sed "s|<${arg}>|\"${value}\"|g" + local -r words="$(echo "$value" | wc -w | xargs)" + + if [[ $words > 1 ]]; then + sed "s|<${arg}>|\"${value}\"|g" + else + sed "s|<${arg}>|${value}|g" + fi } arg::next() { diff --git a/src/misc.sh b/src/misc.sh index 27b8a70..669fcd7 100644 --- a/src/misc.sh +++ b/src/misc.sh @@ -9,18 +9,21 @@ command_exists() { type "$1" &> /dev/null } +platform::existing_command() { + for cmd in "$@"; do + if command_exists "$cmd"; then + echo "$cmd" + return 0 + fi + done + return 1 +} + echoerr() { echo "$@" 1>&2 } url::open() { - if command_exists xdg-open; then - xdg-open "$@" - elif command_exists open; then - open "$@" - elif command_exists google-chrome; then - google-chrome "$@" - elif command_exists firefox; then - firefox "$@" - fi + local -r cmd="$(platform::existing_command "${BROWSER:-}" xdg-open open google-chrome firefox)" + "$cmd" "$@" } \ No newline at end of file diff --git a/test/arg_test.sh b/test/arg_test.sh new file mode 100644 index 0000000..6998cc0 --- /dev/null +++ b/test/arg_test.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +interpolation_one_word() { + echo "curl http://mysite.com//profile" \ + | arg::interpolate "user" "john" \ + | test::equals "curl http://mysite.com/john/profile" +} + +interpolation_multiple_words() { + echo "cp " \ + | arg::interpolate "file" "C:/Program Files/app/foo.exe" \ + | arg::interpolate "new_file" "/mnt/c/Users/john/foo.exe" \ + | test::equals 'cp "C:/Program Files/app/foo.exe" /mnt/c/Users/john/foo.exe' +} + +test::set_suite "arg" +test::run "if there's only one word, interpolation doesn't include quotes" interpolation_one_word +test::run "if there are multiple words, interpolation includes quotes" interpolation_multiple_words diff --git a/test/platform_test.sh b/test/platform_test.sh new file mode 100644 index 0000000..6443d8b --- /dev/null +++ b/test/platform_test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +existing() { + platform::existing_command oasida fngo ni awk aoisdn oafm \ + | test::equals awk +} + +test::set_suite "platform" +test::run "existing_command" existing