summaryrefslogtreecommitdiff
path: root/lib/micmot-menu-pane
blob: 0b4900ec8a226f1754dc45d7ae4f209458eba899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/sh -eu

_script_name='micmot'

_micmot_menu_i_cmd='micmot-irssi-wrapper irssi'
_micmot_menu_i_desc='Launch irssi as current user'
_micmot_menu_w_cmd='micmot-weechat-wrapper weechat'
_micmot_menu_w_desc='Launch weechat as current user'
_micmot_menu_d_cmd='sudo -u bar WEECHAT_TMUX_PANE=foo 
    `which micmot-weechat-wrapper` weechat'
_micmot_menu_d_desc='Launch weechat for foo as user bar'

text_width() {
  IFS=''
  echo "$@" | while read -r _line; do
    echo ${#_line}
  done | sort -n | tail -1
  unset IFS
}

micmot_banner() {
  _micmot_banner="$( figlet -f slant "${_script_name}" )" || return 0
  _text_width=$( text_width "$_micmot_banner" ) || return 0
  _terminal_width=$( tput cols ) || return 0

  _padding=$( printf "%$(( (_terminal_width - _text_width) / 2 ))s" ' ' )
  IFS=''
  echo "$_micmot_banner" | while read -r _line; do
    echo "${_padding}${_line}"
  done
  unset IFS

  unset _micmot_banner _terminal_width _text_width
}

micmot_menu() {
  for _menu_option in $( set | sed -n 's/.*_micmot_menu_\(.\)_desc=.*/\1/p' )
  do
    printf "%3s %s\n" "${_menu_option}" \
        "$( eval echo \${_micmot_menu_${_menu_option}_desc} )"
  done
  printf '\n'
  printf "%3s %s\n" 'q' "Quit ${_script_name}. (Will not kill other panes)"
  printf '\n> '
  unset _menu_option
}

clear
_input=''
while ( true ); do
  printf "\ek${_script_name}\e\\"
  printf "\e]2;${_script_name}\e\\"
  micmot_banner
  micmot_menu

  read -r _input
  case "${_input}" in
    'q' | 'Q')
      break
      ;;
    *)
      if [ "$( eval echo \${_micmot_menu_${_input}_cmd} 2>/dev/null )" ]; then
        echo "Selected: $( eval echo \${_micmot_menu_${_input}_desc} )"
        tmux -L "${MICMOT_SOCKET}" \
            new-window "$( eval echo \${_micmot_menu_${_input}_cmd} )"
      else
        echo "Unknown choice: ${_input}" >&2
      fi
      ;;
  esac
done

# vim: sw=2 et