Skip to content

Instantly share code, notes, and snippets.

@Piqlet
Created October 21, 2022 11:33
Show Gist options
  • Save Piqlet/027a5e8a380050ed6d989574858ccbb2 to your computer and use it in GitHub Desktop.
Save Piqlet/027a5e8a380050ed6d989574858ccbb2 to your computer and use it in GitHub Desktop.
Command menu (terminal,bash,fzf_reload)
# Oct 21, 2022, 13:12:29 Command menu (terminal,bash,fzf_reload)
.bashrc :
# folder,export PATH : Private path where the "type -p" command will find your own files.
# source : Short .bashrc entry to make the original .bashrc more transparent. Custom settings and files in a separate directory. Easily portable, i.e. modular architecture.
# $folder content :
# $ ls $folder
# backup.txt bash_settings cmd.txt hdd.txt install.txt menupagedec menupageinc pacman.txt swap.txt
folder=~/Downloads/bash && export PATH="$folder:$PATH" && source bash_settings
# fzf reload subroutines bash script :
$folder/menupageinc :
#!/bin/bash
pages=($(< ~/pages));i=$(< ~/pagen);((i++));[ $i -gt $(< ~/pagem) ] && i=0 || :
echo $i > ~/pagen;cat $(type -p ${pages[i]})
$folder/menupagedec :
#!/bin/bash
pages=($(< ~/pages));i=$(< ~/pagen);((i--));[ $i -lt 0 ] && i=$(< ~/pagem) || :
echo $i > ~/pagen;cat $(type -p ${pages[i]})
~/Downloads/bash/bash_settings (or "$folder"/bash_settings) :
# Command menu with multiple pages (bash, fzf).
#
# fake readline (up and delete line) source :
# https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
# https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
#
# fzf insert = edit source :
# https://github.com/junegunn/fzf/wiki/examples#opening-files
#
# neccesary packages :
# coreutils : head, tail
# ncurses : tput
#
# variables :
# pn : page number
# pmax : page maximum
# pages,line,cmd,key
#
# variables with files (for fzf reload) :
# ~/pagen : page number
# ~/pages : pages
# ~/pagem : page maximum
echo 0 > ~/pagen # neccesary
function menu {
if [ $# -eq 0 ] # No page, then exit.
then
return
fi
pages=($@) # Loading pages (.txt) into a variable (array).
let pmax=${#pages[@]}-1 # Number of text files (or pages).
if [ $(cat ~/pagen) -gt $pmax ]
then
echo $pmax > ~/pagen
fi
echo "$@" > ~/pages
echo $pmax > ~/pagem
tty=""
if [ "$TERM" = linux ] # tty : --height=50% --border=vertical
then
tty="--height=50% --border=vertical"
fi
line=$(< $(type -p ${pages[$(< ~/pagen)]}) fzf --reverse --exact --no-color --no-info --expect=insert --cycle --bind "right:reload(menupageinc)" --bind "left:reload(menupagedec)" $tty)
if [ "$line" = "" ] # fzf escape, ctrl-c, empty string
then
tput cuu1 && tput el # fake readline (up and delete line)
return
fi
key=$(head -1 <<< "$line") # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
cmd=$(head -2 <<< "$line" | tail -1) # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
if [ "$key" = insert ] # only insertion
then
tput cuu1 && tput el # fake readline (up and delete line)
read -p "${PS1@P}" -eri "$cmd" cmd
fi
history -s $FUNCNAME $@
history -s $cmd
tput cuu1 && tput el # fake readline (up and delete line)
echo "${PS1@P}"$cmd
eval $cmd
}
# Personal examples.
alias m='menu cmd.txt pacman.txt backup.txt hdd.txt swap.txt install.txt'
# Older one page settings (out of habit).
alias b='menu backup.txt' s='menu cmd.txt' pk='menu pacman.txt' hdd='menu hdd.txt' swap='menu swap.txt' i='menu install.txt'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment