Permalink
Browse files

More work on API.

  • Loading branch information...
wayneeseguin committed Aug 30, 2011
1 parent 9d2e359 commit 966f245b5f7f02eb2d32f0b7585bb1fbcf6af3af
@@ -9,9 +9,7 @@ date()
while (( $# > 0 ))
do
- _token="$1"
- shift
-
+ _token="$1" && shift
case "${_token}" in
(step)
_step="${1:-}"
@@ -232,8 +232,7 @@ extensions_installed()
while (( $# > 0 ))
do
- _token="$1"
- shift
+ _token="$1" && shift
case "${_token}" in
(sets|exts)
_paths+=("${sm_path}/${_token}")
@@ -6,31 +6,65 @@
# Allows manipulation of multiline values and keyss containing - and \.
#
-# Read keys from given file($1)
-hash_keys()
+hash()
{
- local file="${1:-}" valid_keys='^[^ ^=]+'
- shift || fail "Cannot read keys from hash file; Filename not given."
- echo -n $(grep -E "${valid_keys}=" $file | grep -Eo "${valid_keys}")
-}
+ local _token _file _keys _valid_keys='^[^ ^=]+'
-# Read value from given file($1) for given key($2), and assign it to a variable($3).
-hash_read()
-{
- local file="${1:-}" key="${2:-}" variable="${3:-}" valid_keys='^[^ ^=]+'
+ while (( $# ))
+ do
+ _token="$1" && shift
+ case "${_token}" in
+ (from)
+ ;;
+ (key)
+ _key="$1"
+ shift || fail "Key name must follow keyword 'key'"
+ ;;
+ (variable)
+ _variable="$1"
+ shift || fail "Variable name must follow keyword 'variable'"
+ ;;
+ (*)
+ if [[ -z "${_command}" ]]
+ then
+ _command="${_token}"
+ else
+ _keys+=("$1")
+ fi
+ ;;
+ esac
+ done
- shift || fail "Cannot read values from hash file; Filename was not given."
- shift || fail "Cannot read values from hash file; Key name was not given."
- shift || fail "Cannot read values from hash file; Variable was not given."
- eval "${variable}=$(
- awk \
- -v key="^${key}=" \
- -v valid_keys="${valid_keys}=" \
- '{
- if ($0 ~ valid_keys) found=0;
- if ($0 ~ key) {found=1; gsub(key,"");};
- if (found == 1) print $0
- }' \
- $file
- )"
+ case "${_command}" in
+ (keys)
+ [[ -n "${_file}" ]] || fail " Filename not given."
+ echo -n $(grep -E "${_valid_keys}=" "${_file}" | grep -Eo "${_valid_keys}")
+ ;;
+ (read)
+ # Read value from given file($1) for given key($2), and assign it to a variable($3).
+ [[ -n "${_file}" ]] || fail "Filename was not given (from '{filename}')."
+ [[ -n "${_key}" ]] || fail "Key name was not given (key '{key}')."
+
+ if [[ -n "${_variable}" ]]
+ then
+ eval "${variable}=$(
+ awk \
+ -v key="^${key}=" \
+ -v valid_keys="${valid_keys}=" \
+ '{
+ if ($0 ~ valid_keys) found=0;
+ if ($0 ~ key) {found=1; gsub(key,"");};
+ if (found == 1) print $0
+ }' \"$file\"
+ )"
+ else
+ awk -v key="^${key}=" -v valid_keys="${_valid_keys}=" '{
+ if ($0 ~ valid_keys) { found=0; }
+ if ($0 ~ key) { found=1; gsub(key,""); };
+ if (found == 1) print $0
+ }' "${_file}"
+ fi
+ ;;
+ esac
}
+
@@ -47,7 +47,7 @@ show_help()
local _help_command _token
while (( $# > 0 )) ; do
- _token="$1" ; shift
+ _token="$1" && shift
case "${_token}" in
desc*|description)
_help_command="description"
@@ -1,26 +1,36 @@
#!/bin/sh
-# ## shell\_is\_interactive()
-#
-# Test if the current shell is an interactive shell.
-#
-# Usage Examples:
-#
-# if shell_is_interactive
-# then
-# ${PAGER} somefile
-# else
-# cat -v somefile
-# fi
-#
-shell_is_interactive()
+shell()
{
- [[ -t 0 ]]
+ while (( $# ))
+ do
+ _token="$1" && shift
+ case "${_token}" in
+ (is)
+ _command=is
+ _subcommand="${1}"
+ shift || fail "A command must follow keyword 'is'"
+ case "${_subcommand}" in
+ (interactive)
+ [[ -t 0 ]]
+ ;;
+ (*)
+ fail "Unknown command following keyword 'is'; shell is <interactive>"
+ ;;
+ esac
+ (*)
+ [[ -z "${_command}" ]]
+ ;;
+ esac
+ done
+
}
-if shell_is_interactive
+if shell is interactive
then
# Interactive functions will be loaded here, only if shell is interactive.
- # TODO: Add BDSM interactive functions :)
+ # TODO: Add interactive functionality + interactive sm hooks loading for each
+ # module
true
fi
+
Oops, something went wrong.

0 comments on commit 966f245

Please sign in to comment.