Skip to content

Instantly share code, notes, and snippets.

@Piqlet
Created October 21, 2022 10:45
Show Gist options
  • Save Piqlet/d9c87b3c810ec4e87d61f0f26437835b to your computer and use it in GitHub Desktop.
Save Piqlet/d9c87b3c810ec4e87d61f0f26437835b to your computer and use it in GitHub Desktop.
Command menu (terminal,bash,fzf). (21-10-2022)
Oct 09, 2022, 17:09:47
.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 pacman.txt swap.txt
folder=~/Downloads/bash && export PATH="$folder:$PATH" && source bash_settings
~/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
#
# vaiables :
# pn : page number
# pmax : page maximum
# pages,line,cmd,key
#
# The while, do, continue, done are only used to replace goto. Instead of goto, continue jumps to the beginning.
pn=0 # 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 [ $pn -gt $pmax ]
then
pn=$pmax
fi
while :
do
line=$(< $(type -p ${pages[pn]}) fzf --reverse --exact --no-color --no-info --expect=insert,right,left --no-clear)
if [ "$line" = "" ] # fzf escape, ctrl-c, empty string
then
tput rmcup && tput cuu1 && tput el # restore screen (fzf --no-clear) and fake readline (up and delete line)
cmd=""
break # goto the end (done)
fi
key=$(head -1 <<< "$line") # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
if [ "$key" = right ] # paging right
then
let pn=pn+1
if [ $pn -gt $pmax ]
then
pn=0
fi
continue # goto the top (do)
fi
if [ "$key" = left ] # paging left
then
let pn=pn-1
if [ $pn -lt 0 ]
then
pn=$pmax
fi
continue # goto the top (do)
fi
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 rmcup && tput cuu1 && tput el # restore screen (fzf --no-clear) and fake readline (up and delete line)
read -p "${PS1@P}" -eri "$cmd" cmd
fi
history -s $FUNCNAME $@
history -s $cmd
tput rmcup && tput cuu1 && tput el # restore screen (fzf --no-clear) and fake readline (up and delete line)
echo "${PS1@P}"$cmd
break # goto the end (done)
done
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