diff --git a/Makefile b/Makefile index 18ac8f8..4d3a05b 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,10 @@ test/shunt.sh: cd test && curl -L https://raw.githubusercontent.com/odb/shunt/latest/install.sh | bash -s latest local test: test/shunt.sh .PHONY + # Check 'source' for supported shells + $(shell which bash) -c 'source ./shml.sh && color-bar' + $(shell which zsh) -c 'source ./shml.sh && color-bar' + # Execute test suite ./test/shunt.sh --verbose ./test/*_tests.sh test/%: .PHONY diff --git a/README.md b/README.md index 2978d6e..9af486b 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,6 @@ $ bundle install **Generate and serve:** ``` -$ bundle exec jekyll s +$ bundle exec jekyll s --watch ``` Then go to: `http://localhost:4000/shml/` diff --git a/docs/_config.yml b/docs/_config.yml index 562d412..9cdd336 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -26,4 +26,4 @@ baseurl: /shml/ # paginate: 1 # Custom vars -version: 1.0.4 +version: 1.1.0 diff --git a/docs/_includes/confirm.md b/docs/_includes/confirm.md new file mode 100644 index 0000000..384a2b5 --- /dev/null +++ b/docs/_includes/confirm.md @@ -0,0 +1,41 @@ +
+ + +# Confirm +Confirm allows you to simply ask end-users questions. + +### Usage + +`confirm QUESTION [SUCCESS_FUNCTION] [FAILURE_FUNCTION]` + +### Code Samples + +```bash +on_human() { + dialog "What is your name?" "on_dialog" +} + +on_dialog() { + echo "$(color green "Hello, $1! $(emoji beer)")" +} + +on_not_human() { + confirm "$(color red "Do you come in peace?")" "on_friend" "on_enemy" +} + +on_friend() { + echo "$(emoji '=)') $(emoji beer)" +} + +on_enemy() { + echo "$(emoji rage) $(emoji poop)" +} + +confirm "$(color blue "Are you human?")" "on_human" "on_not_human" +exit 0 +``` + +### Results +
+ +
diff --git a/docs/_includes/index-sidebar.html b/docs/_includes/index-sidebar.html index 68aa141..28d028e 100644 --- a/docs/_includes/index-sidebar.html +++ b/docs/_includes/index-sidebar.html @@ -29,8 +29,9 @@ Icons Emojis Color Bar + Confirm GitHub - © 2013-{{ site.time | date: '%Y' }} MaxCDN | v{{ site.version }} + © 2013-{{ site.time | date: '%Y' }}
ol' dirty bashnerds | v{{ site.version }}
diff --git a/docs/_includes/sidebar.html b/docs/_includes/sidebar.html index 66acbe1..ead4da6 100644 --- a/docs/_includes/sidebar.html +++ b/docs/_includes/sidebar.html @@ -29,8 +29,9 @@ Icons Emojis Color Bar + Confirm GitHub - © 2013-{{ site.time | date: '%Y' }} MaxCDN | v{{ site.version }} + © 2013-{{ site.time | date: '%Y' }}
ol' dirty bashnerds | v{{ site.version }}
diff --git a/docs/_site/404.html b/docs/_site/404.html index a78260a..f34820a 100644 --- a/docs/_site/404.html +++ b/docs/_site/404.html @@ -69,9 +69,10 @@ Icons Emojis Color Bar + Confirm GitHub - © 2013-2018 MaxCDN | v1.0.4 + © 2013-2018
ol' dirty bashnerds | v1.1.0
diff --git a/docs/_site/getting-started/index.html b/docs/_site/getting-started/index.html index 7846f9f..de850ac 100644 --- a/docs/_site/getting-started/index.html +++ b/docs/_site/getting-started/index.html @@ -69,9 +69,10 @@ Icons Emojis Color Bar + Confirm GitHub - © 2013-2018 MaxCDN | v1.0.4 + © 2013-2018
ol' dirty bashnerds | v1.1.0
@@ -686,6 +687,48 @@

Results


+

+ +

Confirm

+

Confirm allows you to simply ask end-users questions.

+ +

Usage

+ +

confirm QUESTION [SUCCESS_FUNCTION] [FAILURE_FUNCTION]

+ +

Code Samples

+ +
on_human() {
+  dialog "What is your name?" "on_dialog"
+}
+
+on_dialog() {
+  echo "$(color green "Hello, $1! $(emoji beer)")"
+}
+
+on_not_human() {
+  confirm "$(color red "Do you come in peace?")" "on_friend" "on_enemy"
+}
+
+on_friend() {
+  echo "$(emoji '=)') $(emoji beer)"
+}
+
+on_enemy() {
+  echo "$(emoji rage) $(emoji poop)"
+}
+
+confirm "$(color blue "Are you human?")" "on_human" "on_not_human"
+exit 0
+
+ +

Results

+
+ +
+ +
+
Designed and built by @jdorfman and mervinej.
diff --git a/docs/_site/index.html b/docs/_site/index.html index 49a3abb..3f014f9 100644 --- a/docs/_site/index.html +++ b/docs/_site/index.html @@ -69,9 +69,10 @@ Icons Emojis Color Bar + Confirm GitHub - © 2013-2018 MaxCDN | v1.0.4 + © 2013-2018
ol' dirty bashnerds | v1.1.0
@@ -104,7 +105,7 @@

1-liner Install

-Currently v1.0.4 +Currently v1.1.0
Please be responsible & RTFM


diff --git a/docs/_site/public/images/confirm-demo.png b/docs/_site/public/images/confirm-demo.png new file mode 100644 index 0000000..7bc37d2 Binary files /dev/null and b/docs/_site/public/images/confirm-demo.png differ diff --git a/docs/getting-started.md b/docs/getting-started.md index 66bb2d9..bb21472 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -18,6 +18,7 @@ To use SHML you must have unix-like (Darwin/OS X, Linux, etc.) POSIX-compliant o {% include icons.md %} {% include emojis.md %} {% include color-bar.md %} +{% include confirm.md %}
diff --git a/docs/public/images/confirm-demo.png b/docs/public/images/confirm-demo.png new file mode 100644 index 0000000..7bc37d2 Binary files /dev/null and b/docs/public/images/confirm-demo.png differ diff --git a/examples/confirm_dialog.sh b/examples/confirm_dialog.sh new file mode 100644 index 0000000..d241e6f --- /dev/null +++ b/examples/confirm_dialog.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +source shml.sh +on_human() { + dialog "What is your name?" "on_dialog" +} + +on_dialog() { + echo "$(color green "Hello, $1! $(emoji beer)")" +} + +on_not_human() { + confirm "$(color red "Do you come in peace?")" "on_friend" "on_enemy" +} + +on_friend() { + echo "$(emoji '=)') $(emoji beer)" +} + +on_enemy() { + echo "$(emoji rage) $(emoji poop)" +} + +confirm "$(color blue "Are you human?")" "on_human" "on_not_human" +exit 0 diff --git a/package.json b/package.json index 9b2e697..9ea67eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "shml", - "version": "1.0.4", + "version": "1.1.0", "description": "SHML is a shell framework for faster and easier script development.", "bin": { "shml": "shml.sh", @@ -15,18 +15,19 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/MaxCDN/shml.git" + "url": "git+https://github.com/odb/shml.git" }, "keywords": [ "sh", "color", "bash", + "zsh", "linux", "darwin" ], "license": "MIT", "bugs": { - "url": "https://github.com/MaxCDN/shml/issues" + "url": "https://github.com/odb/shml/issues" }, - "homepage": "http://maxcdn.github.io/shml/" + "homepage": "http://shml.xyz/" } diff --git a/shml.sh b/shml.sh index 34fdf9b..ad4b6e5 100755 --- a/shml.sh +++ b/shml.sh @@ -3,14 +3,14 @@ #SHML:START #************************************************# # SHML - Shell Markup Language Framework -# v1.0.4 +# v1.1.0 # (MIT) # by Justin Dorfman - @jdorfman # && Joshua Mervine - @mervinej # -# https://maxcdn.github.io/shml/ +# http://shml.xyz #************************************************# -SHML_VERSION="1.0.4" +SHML_VERSION="1.1.0" # Progress Bar ## @@ -90,6 +90,43 @@ progress() { test "$(which tput)" && $__ncursor && tput cnorm } +# Confirm / Dialog +## +__default_confirm_success_input="y Y yes Yes YES ok OK Ok okay Okay OKAY k K continue Continue CONTINUE proceed Proceed PROCEED success Success SUCCESS successful Successful SUCCESSFUL good Good GOOD" +confirm() { + [[ -z $1 ]] && return 127 + + [[ -z $SHML_CONFIRM_SUCCESS ]] && SHML_CONFIRM_SUCCESS=$__default_confirm_success_input + + echo -ne "$1 " + local found=false + while read __input; do + for str in $(echo $SHML_CONFIRM_SUCCESS); do + [[ "$str" == "$__input" ]] && found=true + done + break + done + + if $found; then + [[ ! -z $2 ]] && eval $2 + return 0 + else + [[ ! -z $3 ]] && eval $3 + return 1 + fi +} + +dialog() { + [[ -z $1 ]] && return 127 + [[ -z $2 ]] && return 127 + + echo -en "$1 " + while read __input; do + eval "$2 $__input" + break + done +} + # Foreground (Text) ## fgcolor() { @@ -357,7 +394,7 @@ function e { # Usage / Examples ## -if [ "$0" == "$BASH_SOURCE" ]; then +if [ "$0" = "$BASH_SOURCE" ]; then if [[ $@ =~ .*-v.* ]]; then echo "shml version ${SHML_VERSION}" @@ -581,22 +618,21 @@ $(i $I)$(bar black yellow black yellow black yellow) $(a bold "Section 8: $(color red "[EXPERIMENTAL]") Progress Bar") $(hr '-') -$(i $I)Usage: progress [title] [steps] [char] +$(i $I)Usage: progress [TITLE] [STEPS] [CHAR] $(i $I) - 'title' defines the progress bar title $(i $I) - 'steps' defines the number of steps for the progress bar to act upon $(i $I) - 'char' defines the character to be displayed in the progress bar +$(i $I)Example: + $(i $I)echo "\$\(color green\)" $(i $I)for i in \$(seq 0 10); do echo \$i; sleep .25; done | progress $(i $I)echo "\$\(color end\)" -$(color green) -$(i $I)Example [#################### ] (50%) -$(color end) +$(color green "$(i $I)Example [#################### ] (50%)") -$(i $I)'progress' supports overriding default values by setting the following -$(i $I)variables: +$(i $I)'progress' supports overriding default values by setting the following variables: $(i $I) - SHML_PROGRESS_WIDTH - width of progress bar, default 60 $(i $I) - SHML_PROGRESS_BREAK - put a new line at the end of the output, default 'true' @@ -605,6 +641,47 @@ $(i $I) - SHML_PGOGRESS_NOCURSOR - hide the cursor, default 'true' $(i $I)NOTE: These variables $(a bold 'must') be defined before sourcing 'shml'! +$(a bold "Section 9: $(color red "[EXPERIMENTAL]") Confirm") +$(hr '-') + +$(i $I)Ask a yes or no question and handle results. + +$(i $I)Usage: confirm QUESTION [SUCCESS_FUNCTION] [FAILURE_FUNCTION] + +$(i $I)Supports the following as affirmitive responses by default: + +$(for r in `echo "$__default_confirm_success_input"`; do echo "$(i $I) - '$r'"; done) + +$(i $I)Default affirmtive responses can be overwritten by setting 'SHML_CONFIRM_SUCCESS'. + +$(i $I)Example: + +$(i $I)function on_success() { +$(i $I) echo \"yay\" +$(i $I)} + +$(i $I)function on_failure() { +$(i $I) echo \"boo\" +$(i $I)} + +$(i $I)confirm \"CREAM?\" \"on_success\" \"on_failure\" + + +$(a bold "Section 9: $(color red "[EXPERIMENTAL]") Dialog") +$(hr '-') + +$(i $I)Asks a question and passes the answer to a response handler function. + +$(i $I)Usage: dialog QUESTION [RESPONSE_FUNCTION] + +$(i $I)Example: + +$(i $I)function on_response() { +$(i $I) echo \"hello $1\" +$(i $I)} + +$(i $I)dialog \"What is your name?\" \"on_response\" + " | less -r fi diff --git a/test/confirm_tests.sh b/test/confirm_tests.sh new file mode 100644 index 0000000..80dc45c --- /dev/null +++ b/test/confirm_tests.sh @@ -0,0 +1,40 @@ +source ./shml.sh + +test_callback() { + echo -ne '' # noop +} +function run_tests { + # Confirm + ## + for __good in $(echo $__default_confirm_success_input); do + echo "$__good" | confirm "question" > /dev/null + result=$? + assert_equal "0" "$result" "$__good should confirm" + done + + for __bad in $(echo "bad no N No"); do + echo "$__bad" | confirm "question" > /dev/null + result=$? + assert_equal "1" "$result" "$__bad should not confirm" + done + + # test success callback + output=$(echo "y" | confirm "question:" "__on_success__") + assert_equal "question: success" "$output" "should print question and success" + + # test failure callback + output=$(echo "n" | confirm "question:" "__on_success__" "__on_failure__") + assert_equal "question: failure" "$output" "should print question and failure" + + unset result + unset output +} + +# callbacks +__on_success__() { + echo -n "success" +} + +__on_failure__() { + echo -n "failure" +} diff --git a/test/dialog_tests.sh b/test/dialog_tests.sh new file mode 100644 index 0000000..1cdff25 --- /dev/null +++ b/test/dialog_tests.sh @@ -0,0 +1,15 @@ +source ./shml.sh + +__test_callback__() { + #noop + echo "$1" +} + +function run_tests { + # Dialog + ## + output=$(echo "foo" | dialog "question" "__test_callback__") + assert_equal "question foo" "$output" "should print input" + + unset output +}