====== The Shell and User Interfaces ====== ---- * The shell (and shell scripts) can display simple user interfaces and dialogs for applications that demand them. * ''zenity'' and ''yad'' can be used to generate GUI dialogs. * ''whiptail'' can be used to generate TUI ([[https://en.wikipedia.org/wiki/Text-based_user_interface|text-based user interface]]) dialogs. * ''fzf'' is a newer tool that can be used to generate TUIs. ===== Zenity and YAD ===== * Zenity - http://en.wikipedia.org/wiki/Zenity * Cross platform * Create dialogs with the //zenity// command: zenity --info --text="This is an information box." * Commonly used in shell scripts: #!/bin/sh if zenity --question --text="Please press a button."; then zenity --info --text="You pressed Yes\!" else zenity --error --text="You pressed No\!" fi Also works with conditional chaining: #!/bin/sh zenity --question --text="Please press a button." && zenity --info --text="You pressed Yes\!" || zenity --error --text="You pressed No\!" * Linux GDM login banner example: http://tinyurl.com/nqgmptp * YAD - https://github.com/v1cont/yad * Yet Another Dialog * A more featureful fork of Zenity ===== Whiptail ===== * http://en.wikibooks.org/wiki/Bash_Shell_Scripting/Whiptail * Create dialogs with the //whiptail// command: whiptail --title "Example Dialog" --msgbox "This is an example of a message box. You must hit OK to continue." 8 78 * Can also be used with shell scripts: #!/bin/sh if (whiptail --title "Example Dialog" --yesno "This is an example of a yes/no box." 8 78) then echo "User selected Yes, exit status was $?." else echo "User selected No, exit status was $?." fi * Can be used to gauge progress in a shell script: #!/bin/bash { for ((i = 0 ; i <= 100 ; i+=5)); do sleep 0.1 echo $i done } | whiptail --gauge "Please wait while we are sleeping..." 6 50 0 ---- ===== fzf: fuzzy finder ===== * https://github.com/junegunn/fzf * The ''fzf'' command is a commandline filter that can generate minimalistic TUI dialogs quickly. * Designed to be compatible with the UNIX philosophy, it can be used in command pipelines, functions and shell scripts to display and quickly filter the results sent to its standard input. ---- ===== Zenity example: makescript ===== Modify the [[https://cssegit.monmouth.edu/jchung/csse370repo/-/blob/main/scripts/cat2script|cat2script]] script to use ''zenity'' dialogs. Link to ''makescript'' code: [[https://cssegit.monmouth.edu/jchung/csse370repo/-/blob/main/scripts/makescript|makescript]]. ---- ===== fzf example: fzcd ===== The ''fzcd'' shell function is a version of the [[cs370/cs_370_-_file_network_and_version_utilities#change_to_directory_based_on_find_result|''findcd'' function]]. It displays all possible directory choices to cd to, based on the ''find'' results. The ''fzcd'' code is shown below: # Use fzf to display directories that I can 'cd' to, # based on a find name pattern: function fzcd { # Note the long command substitution that starts on # the next line with $(find -iname ...: findresults=$(find -iname "*$1*" -printf '%P\n' | while read path; do # If $path is a file, can't cd to it, so have to # trim $path to a directory using rev|cut|rev, # else just echo $path: [[ -f "$path" ]] && (echo "$path" | rev | cut -d/ -f2- | rev) || echo "$path" done | sort | uniq) # <= command substitution finally ends with uniq # As a result of the above, $findresults should contain a list # of directories that I can cd to. # If $findresults actually contains something, cd using fzf: [[ -n "$findresults" ]] && cd "$(echo "$findresults" | fzf)" # Must use "" around the echo argument; see http://tiny.cc/2g78vz # Must use "" around the cd argument; see http://tiny.cc/84a8vz } ----