Skip to content

Commit

Permalink
first init
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Feb 16, 2020
0 parents commit 42f744c
Show file tree
Hide file tree
Showing 7 changed files with 1,021 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.idea
.DS_Store
servers.db
servers.db.*
14 changes: 14 additions & 0 deletions README.md
@@ -0,0 +1,14 @@
# SSH Man

A shell script to manage all your ssh config. works nicely with `fzf`.


# Usage

```
$ s
```

# Install

comming soon
302 changes: 302 additions & 0 deletions lib/ask/list.sh
@@ -0,0 +1,302 @@
#!/bin/bash

#ref: https://github.com/tanhauhau/Inquirer.sh

# store the current set options
OLD_SET=$-
set -e

arrow="$(echo -e '\xe2\x9d\xaf')"
checked="$(echo -e '\xe2\x97\x89')"
unchecked="$(echo -e '\xe2\x97\xaf')"

black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
bold="$(tput bold)"
normal="$(tput sgr0)"
dim=$'\e[2m'

print() {
echo "$1"
tput el
}

join() {
local IFS=$'\n'
local _join_list
eval _join_list=( '"${'${1}'[@]}"' )
local first=true
for item in ${_join_list[@]}; do
if [ "$first" = true ]; then
printf "%s" "$item"
first=false
else
printf "${2-, }%s" "$item"
fi
done
}

function gen_env_from_options() {
local IFS=$'\n'
local _indices
local _env_names
local _checkbox_selected
eval _indices=( '"${'${1}'[@]}"' )
eval _env_names=( '"${'${2}'[@]}"' )

for i in $(gen_index ${#_env_names[@]}); do
_checkbox_selected[$i]=false
done

for i in ${_indices[@]}; do
_checkbox_selected[$i]=true
done

for i in $(gen_index ${#_env_names[@]}); do
printf "%s=%s\n" "${_env_names[$i]}" "${_checkbox_selected[$i]}"
done
}

on_default() {
true;
}

on_keypress() {
local OLD_IFS
local IFS
local key
OLD_IFS=$IFS
local on_up=${1:-on_default}
local on_down=${2:-on_default}
local on_space=${3:-on_default}
local on_enter=${4:-on_default}
local on_left=${5:-on_default}
local on_right=${6:-on_default}
local on_ascii=${7:-on_default}
local on_backspace=${8:-on_default}
_break_keypress=false
while IFS="" read -rsn1 key; do
case "$key" in
$'\x1b')
read -rsn1 key
if [[ "$key" == "[" ]]; then
read -rsn1 key
case "$key" in
'A') eval $on_up;;
'B') eval $on_down;;
'D') eval $on_left;;
'C') eval $on_right;;
esac
fi
;;
' ') eval $on_space ' ';;
[a-z0-9A-Z\!\#\$\&\+\,\-\.\/\;\=\?\@\[\]\^\_\{\}\~]) eval $on_ascii $key;;
$'\x7f') eval $on_backspace $key;;
'') eval $on_enter $key;;
esac
if [ $_break_keypress = true ]; then
break
fi
done
IFS=$OLD_IFS
}

gen_index() {
local k=$1
local l=0
if [ $k -gt 0 ]; then
for l in $(seq $k)
do
echo "$l-1" | bc
done
fi
}

cleanup() {
# Reset character attributes, make cursor visible, and restore
# previous screen contents (if possible).
tput sgr0
tput cnorm
stty echo

# Restore `set e` option to its orignal value
if [[ $OLD_SET =~ e ]]
then set -e
else set +e
fi
}

control_c() {
cleanup
exit $?
}

select_indices() {
local _select_list
local _select_indices
local _select_selected=()
eval _select_list=( '"${'${1}'[@]}"' )
eval _select_indices=( '"${'${2}'[@]}"' )
local _select_var_name=$3
eval $_select_var_name\=\(\)
for i in $(gen_index ${#_select_indices[@]}); do
eval $_select_var_name\+\=\(\""${_select_list[${_select_indices[$i]}]}"\"\)
done
}



# Support VIM hjkl move
on_list_input_ascii() {
key=$1
if [[ $key == 'k' || $key == 'K' || $key == 'h' || $key == 'H' ]]; then
on_list_input_up
elif [[ $key == 'j' || $key == 'J' || $key == 'l' || $key == 'L' ]]; then
on_list_input_down
fi
}

on_list_input_up() {
remove_list_instructions
tput cub "$(tput cols)"

printf " ${_list_options[$_list_selected_index]}"
tput el

if [ $_list_selected_index = 0 ]; then
_list_selected_index=$((${#_list_options[@]}-1))
tput cud $((${#_list_options[@]}-1))
tput cub "$(tput cols)"
else
_list_selected_index=$((_list_selected_index-1))

tput cuu1
tput cub "$(tput cols)"
tput el
fi

printf "${cyan}${arrow} %s ${normal}" "${_list_options[$_list_selected_index]}"
}

on_list_input_down() {
remove_list_instructions
tput cub "$(tput cols)"

printf " ${_list_options[$_list_selected_index]}"
tput el

if [ $_list_selected_index = $((${#_list_options[@]}-1)) ]; then
_list_selected_index=0
tput cuu $((${#_list_options[@]}-1))
tput cub "$(tput cols)"
else
_list_selected_index=$((_list_selected_index+1))
tput cud1
tput cub "$(tput cols)"
tput el
fi
printf "${cyan}${arrow} %s ${normal}" "${_list_options[$_list_selected_index]}"
}

on_list_input_enter_space() {
local OLD_IFS
OLD_IFS=$IFS
IFS=$'\n'

tput cud $((${#_list_options[@]}-${_list_selected_index}))
tput cub "$(tput cols)"

for i in $(seq $((${#_list_options[@]}+1))); do
tput el1
tput el
tput cuu1
done
tput cub "$(tput cols)"

tput cuf $((${#prompt}+3))
printf "${cyan}${_list_options[$_list_selected_index]}${normal}"
tput el

tput cud1
tput cub "$(tput cols)"
tput el

_break_keypress=true
IFS=$OLD_IFS
}

remove_list_instructions() {
if [ $_first_keystroke = true ]; then
tput cuu $((${_list_selected_index}+1))
tput cub "$(tput cols)"
tput cuf $((${#prompt}+3))
tput el
tput cud $((${_list_selected_index}+1))
_first_keystroke=false
fi
}

_list_input() {
local i
local j
prompt=$1
eval _list_options=( '"${'${2}'[@]}"' )

_list_selected_index=0
_first_keystroke=true

trap control_c SIGINT EXIT

stty -echo
tput civis

print "${normal}${green}?${normal} ${bold}${prompt}${normal} ${dim}(Use arrow keys)${normal}"

for i in $(gen_index ${#_list_options[@]}); do
tput cub "$(tput cols)"
if [ $i = 0 ]; then
print "${cyan}${arrow} ${_list_options[$i]} ${normal}"
else
print " ${_list_options[$i]}"
fi
tput el
done

for j in $(gen_index ${#_list_options[@]}); do
tput cuu1
done

on_keypress on_list_input_up on_list_input_down on_list_input_enter_space on_list_input_enter_space on_list_input_up on_list_input_down on_list_input_ascii on_list_input_up

}


list_input() {
_list_input "$1" "$2"
local var_name=$3
eval $var_name=\'"${_list_options[$_list_selected_index]}"\'
unset _list_selected_index
unset _list_options
unset _break_keypress
unset _first_keystroke

cleanup
}

list_input_index() {
_list_input "$1" "$2"
local var_name=$3
eval $var_name=\'"$_list_selected_index"\'
unset _list_selected_index
unset _list_options
unset _break_keypress
unset _first_keystroke

cleanup
}

0 comments on commit 42f744c

Please sign in to comment.