Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

confirm / dialog #32

Merged
merged 13 commits into from
Apr 18, 2018
Merged
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/`
2 changes: 1 addition & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ baseurl: /shml/
# paginate: 1

# Custom vars
version: 1.0.4
version: 1.1.0
41 changes: 41 additions & 0 deletions docs/_includes/confirm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<hr>
<a name="Confirm"></a>

# 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
<center>
<img src="../public/images/confirm-demo.png">
</center>
3 changes: 2 additions & 1 deletion docs/_includes/index-sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Icons">Icons</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Emojis">Emojis</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Color-Bar">Color Bar</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Confirm">Confirm</a>
<a class="sidebar-nav-item" href="https://github.com/MaxCDN/shml" target="_blank">GitHub</a>
</nav>
<span class="sidebar-nav">&copy; 2013-{{ site.time | date: '%Y' }} <a href="http://www.maxcdn.com/open-source?utm_source=SHML&utm_medium=footer-link">MaxCDN</a> | v{{ site.version }}</span>
<span class="sidebar-nav">&copy; 2013-{{ site.time | date: '%Y' }} <br> <a href="https://github.com/odb/">ol' dirty bashnerds</a> | v{{ site.version }}</span>
</div>
</div>
3 changes: 2 additions & 1 deletion docs/_includes/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
<a class="sidebar-nav-item sidebar-nav-indent" href="#Icons">Icons</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Emojis">Emojis</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Color-Bar">Color Bar</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Confirm">Confirm</a>
<a class="sidebar-nav-item" href="https://github.com/MaxCDN/shml" target="_blank">GitHub</a>
</nav>
<span class="sidebar-nav">&copy; 2013-{{ site.time | date: '%Y' }} <a href="http://www.maxcdn.com/open-source?utm_source=SHML&utm_medium=footer-link">MaxCDN</a> | v{{ site.version }}</span>
<span class="sidebar-nav">&copy; 2013-{{ site.time | date: '%Y' }} <br> <a href="https://github.com/odb/">ol' dirty bashnerds</a> | v{{ site.version }}</span>
</div>
</div>
3 changes: 2 additions & 1 deletion docs/_site/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@
<a class="sidebar-nav-item sidebar-nav-indent" href="#Icons">Icons</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Emojis">Emojis</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Color-Bar">Color Bar</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Confirm">Confirm</a>
<a class="sidebar-nav-item" href="https://github.com/MaxCDN/shml" target="_blank">GitHub</a>
</nav>
<span class="sidebar-nav">&copy; 2013-2018 <a href="http://www.maxcdn.com/open-source?utm_source=SHML&utm_medium=footer-link">MaxCDN</a> | v1.0.4</span>
<span class="sidebar-nav">&copy; 2013-2018 <br> <a href="https://github.com/odb/">ol' dirty bashnerds</a> | v1.1.0</span>
</div>
</div>

Expand Down
45 changes: 44 additions & 1 deletion docs/_site/getting-started/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@
<a class="sidebar-nav-item sidebar-nav-indent" href="#Icons">Icons</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Emojis">Emojis</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Color-Bar">Color Bar</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="#Confirm">Confirm</a>
<a class="sidebar-nav-item" href="https://github.com/MaxCDN/shml" target="_blank">GitHub</a>
</nav>
<span class="sidebar-nav">&copy; 2013-2018 <a href="http://www.maxcdn.com/open-source?utm_source=SHML&utm_medium=footer-link">MaxCDN</a> | v1.0.4</span>
<span class="sidebar-nav">&copy; 2013-2018 <br> <a href="https://github.com/odb/">ol' dirty bashnerds</a> | v1.1.0</span>
</div>
</div>

Expand Down Expand Up @@ -686,6 +687,48 @@ <h3 id="results-5">Results</h3>

<hr />

<p><a name="Confirm"></a></p>

<h1 id="confirm">Confirm</h1>
<p>Confirm allows you to simply ask end-users questions.</p>

<h3 id="usage">Usage</h3>

<p><code class="highlighter-rouge">confirm QUESTION [SUCCESS_FUNCTION] [FAILURE_FUNCTION]</code></p>

<h3 id="code-samples-6">Code Samples</h3>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>on_human<span class="o">()</span> <span class="o">{</span>
dialog <span class="s2">"What is your name?"</span> <span class="s2">"on_dialog"</span>
<span class="o">}</span>

on_dialog<span class="o">()</span> <span class="o">{</span>
<span class="nb">echo</span> <span class="s2">"</span><span class="k">$(</span>color green <span class="s2">"Hello, </span><span class="nv">$1</span><span class="s2">! </span><span class="k">$(</span>emoji beer<span class="k">)</span><span class="s2">"</span><span class="k">)</span><span class="s2">"</span>
<span class="o">}</span>

on_not_human<span class="o">()</span> <span class="o">{</span>
confirm <span class="s2">"</span><span class="k">$(</span>color red <span class="s2">"Do you come in peace?"</span><span class="k">)</span><span class="s2">"</span> <span class="s2">"on_friend"</span> <span class="s2">"on_enemy"</span>
<span class="o">}</span>

on_friend<span class="o">()</span> <span class="o">{</span>
<span class="nb">echo</span> <span class="s2">"</span><span class="k">$(</span>emoji <span class="s1">'=)'</span><span class="k">)</span><span class="s2"> </span><span class="k">$(</span>emoji beer<span class="k">)</span><span class="s2">"</span>
<span class="o">}</span>

on_enemy<span class="o">()</span> <span class="o">{</span>
<span class="nb">echo</span> <span class="s2">"</span><span class="k">$(</span>emoji rage<span class="k">)</span><span class="s2"> </span><span class="k">$(</span>emoji poop<span class="k">)</span><span class="s2">"</span>
<span class="o">}</span>

confirm <span class="s2">"</span><span class="k">$(</span>color blue <span class="s2">"Are you human?"</span><span class="k">)</span><span class="s2">"</span> <span class="s2">"on_human"</span> <span class="s2">"on_not_human"</span>
<span class="nb">exit </span>0
</code></pre></div></div>

<h3 id="results-6">Results</h3>
<center>
<img src="../public/images/confirm-demo.png" />
</center>

<hr />

<div align="center">
<small>
Designed and built by <a href="https://twitter.com/jdorfman">@jdorfman</a> and <a href="https://twitter.com/mervinej">mervinej</a>.<br />
Expand Down
5 changes: 3 additions & 2 deletions docs/_site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Icons">Icons</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Emojis">Emojis</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Color-Bar">Color Bar</a>
<a class="sidebar-nav-item sidebar-nav-indent" href="getting-started/#Confirm">Confirm</a>
<a class="sidebar-nav-item" href="https://github.com/MaxCDN/shml" target="_blank">GitHub</a>
</nav>
<span class="sidebar-nav">&copy; 2013-2018 <a href="http://www.maxcdn.com/open-source?utm_source=SHML&utm_medium=footer-link">MaxCDN</a> | v1.0.4</span>
<span class="sidebar-nav">&copy; 2013-2018 <br> <a href="https://github.com/odb/">ol' dirty bashnerds</a> | v1.1.0</span>
</div>
</div>

Expand Down Expand Up @@ -104,7 +105,7 @@ <h3 id="1-liner-install">1-liner Install</h3>
</code></pre></div></div>

<div align="center">
<small>Currently v1.0.4
<small>Currently v1.1.0
<br /><a href="getting-started/">Please be responsible &amp; RTFM</a>
</small>
<br /><br />
Expand Down
Binary file added docs/_site/public/images/confirm-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

<hr>

Expand Down
Binary file added docs/public/images/confirm-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions examples/confirm_dialog.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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/"
}
97 changes: 87 additions & 10 deletions shml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
##
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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'
Expand All @@ -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

Expand Down
Loading