|
| 1 | +## |
| 2 | +# BIW-BIND - Key binder for BIW Menus |
| 3 | +# |
| 4 | +# Copyright 2017 by Chad Juliano |
| 5 | +# |
| 6 | +# Licensed under GNU Lesser General Public License v3.0 only. Some rights |
| 7 | +# reserved. See LICENSE. |
| 8 | +## |
| 9 | + |
| 10 | +function fn_choose_show() |
| 11 | +{ |
| 12 | + if ! ${BIW_HOME}/biw-hist-chooser.sh |
| 13 | + then |
| 14 | + return 1 |
| 15 | + fi |
| 16 | + |
| 17 | + READLINE_LINE=$(cat $BIW_CH_RES_FILE) |
| 18 | + READLINE_POINT=${#READLINE_LINE} |
| 19 | + |
| 20 | + rm $BIW_CH_RES_FILE |
| 21 | +} |
| 22 | + |
| 23 | +function fn_choose_bind() |
| 24 | +{ |
| 25 | + local -r _bind_key=$1 |
| 26 | + |
| 27 | + # We use 2 binds here because of an issue bash has with |
| 28 | + # multi-char escape sequences. |
| 29 | + local -r bind_int_char=$'"\201"' |
| 30 | + local -r bind_esc_char="\"\e${_bind_key}\"" |
| 31 | + |
| 32 | + bind -x ${bind_int_char}:fn_choose_show |
| 33 | + bind ${bind_esc_char}:${bind_int_char} |
| 34 | +} |
| 35 | + |
| 36 | +function fn_biw_set_home() |
| 37 | +{ |
| 38 | + # file where history result will be saved |
| 39 | + export BIW_CH_RES_FILE=$HOME/.chooser_history |
| 40 | + |
| 41 | + # save the home dir |
| 42 | + local _script_name=${BASH_SOURCE[0]} |
| 43 | + local _script_dir=${_script_name%/*} |
| 44 | + |
| 45 | + # convert to absolute path |
| 46 | + _script_dir=$(cd $_script_dir; pwd -P) |
| 47 | + |
| 48 | + export BIW_HOME=$_script_dir |
| 49 | +} |
| 50 | + |
| 51 | +function fn_biw_init() |
| 52 | +{ |
| 53 | + fn_biw_set_home |
| 54 | + |
| 55 | + # some keys you can bind |
| 56 | + local -r key_f1='OP' |
| 57 | + local -r key_f2='OQ' |
| 58 | + local -r key_f10='[21~' |
| 59 | + local -r key_f11='[23~' |
| 60 | + local -r key_f12='[24~' |
| 61 | + local -r key_up='[A' |
| 62 | + local -r key_down='[B' |
| 63 | + local -r key_left='[D' |
| 64 | + local -r key_right='[C' |
| 65 | + |
| 66 | + fn_choose_bind $key_down |
| 67 | +} |
| 68 | + |
| 69 | +fn_biw_init |
0 commit comments