
This is possibly an important guide for many users. MySQL to many applications give beautiful option window. Obviously, there are full applications like we talked about in precious articles like command line file explorer or command line music player or command line browser. Here is How to Give Colourful Prompt, Progress to the User or Build Full TUI Applications. Here is How To Build Colorful Command Line TUI For Shell Scripts.

Plan The Flow Diagram of Before Building Colorful Command Line TUI For Your Shell Scripts

Command line interface and text based user interface (TUI) has difference. We are actually talking about the TUI but referring as Command Line. That is because we want to say that you’ll build the colorful interface for SSH.

The planning part is important. Because, for basic dialog, prompt, we do not need too much complicated libraries but for full applications, invariably we will need complicated bigger libraries. The way of approach for bash scripts and program written in C, C++ or Python will vary.

Advertisement ---

How To Build Colorful Command Line TUI For Shell Scripts

Most simple way for the bash scripts

We can use whiptail , dialog , tput for building quite nice interactive colorful command line TUI prompts, options, for shell scripts. If you perform a web search with each of them, you’ll get lot of tutorials. whiptail is installed by default on most deb-based OS, while dialog is not installed. On rpm-based OS, whiptail is default dialog app. Neither whiptail nor dialog is possibly installed on MacOS X. whiptail is based on newt , while dialog is based on ncurses . Run this command on OS with whiptail installed :

whiptail --title "The Customize Windows" --msgbox "We learned how to create message box with whiptail. Choose Ok to exit." 10 60 1 whiptail -- title "The Customize Windows" -- msgbox "We learned how to create message box with whiptail. Choose Ok to exit." 10 60

Easy, is not it?

This script checks whether whiptail or dialog is installed, if installed then gives an interactive colorful command line TUI, if none installed, gives a command line output :

read dialog <<< "$(which whiptail dialog 2> /dev/null)" # exit if none found [[ "$dialog" ]] || { echo 'neither whiptail nor dialog found' >&2 exit 1 } # just use whichever was found "$dialog" --msgbox "Message displayed with $dialog" 0 0 1 2 3 4 5 6 7 8 9 10 read dialog <<< "$(which whiptail dialog 2> /dev/null)" # exit if none found [ [ "$dialog" ] ] || { echo 'neither whiptail nor dialog found' > &2 exit 1 } # just use whichever was found "$dialog" -- msgbox "Message displayed with $dialog" 0 0

Another example with screenshot :

#!/bin/bash if (whiptail --title "The Customize Windows" --yesno "This guide can make a n00b expert. Choose Yes to read and No to exit." 10 60) then echo "You chose Yes. Exit status was $?." else echo "You chose No. Exit status was $?." fi 1 2 3 4 5 6 # ! / bin / bash if ( whiptail -- title "The Customize Windows" -- yesno "This guide can make a n00b expert. Choose Yes to read and No to exit." 10 60 ) then echo "You chose Yes. Exit status was $?." else echo "You chose No. Exit status was $?." fi

Same goes for tput . Here is a script which checks whether tput is installed, if installed then gives an interactive colorful command line TUI, if not installed, gives a command line output :

if tput os; then echo 'Your OS supports tput\r- -- ---------' else echo 'Your OS does not support tput' fi 1 2 3 4 5 if tput os ; then echo 'Your OS supports tput\r- -- ---------' else echo 'Your OS does not support tput' fi

I kept some example scripts on Github for you. There are also tools like lxdialog , GDB etc.

For more complicated logics many bash scripts, you can use this kind of library to build applications :

https://github.com/AbhishekGhosh/bashsimplecurses 1 https : / / github . com / AbhishekGhosh / bashsimplecurses

Robust way for building full applications

On Linux or MacOS X, the widely recognised standard is ncurses . Python provides a module to wrap this native library. You can see examples of urwid :

http://urwid.org 1 http : / / urwid . org

and pdcurses :

https://github.com/wmcbrine/PDCurses 1 https : / / github . com / wmcbrine / PDCurses

and curses :

https://docs.python.org/2/howto/curses.html 1 https : / / docs . python . org / 2 / howto / curses . html

Ncurses is almost standard, you can read guide here :

https://www.gnu.org/software/guile-ncurses/manual/guile-ncurses.html http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ 1 2 https : / / www . gnu . org / software / guile - ncurses / manual / guile - ncurses . html http : / / tldp . org / HOWTO / NCURSES - Programming - HOWTO /

Ncurses and Program in Python

Create sample program like this named example.py

import curses myscreen = curses.initscr() myscreen.border(0) myscreen.addstr(12, 25, "Python curses in action!") myscreen.refresh() myscreen.getch() curses.endwin() 1 2 3 4 5 6 7 8 9 10 import curses myscreen = curses . initscr ( ) myscreen . border ( 0 ) myscreen . addstr ( 12 , 25 , "Python curses in action!" ) myscreen . refresh ( ) myscreen . getch ( ) curses . endwin ( )

run the script :

chmod +x example.py python example.py 1 2 chmod + x example . py python example . py

Hit enter to exit. Python has pythondialog package.

Ncurses and Program in C

For using ncurses with C language, we need C compiler installed on machine and have the ncurses headers available. For MacOS X, we need XCode installed, for Ubuntu we need libncurses5-dev. We need to create a Makefile :

# Makefile LDFLAGS=-lncurses all: example 1 2 3 4 # Makefile LDFLAGS = - lncurses all : example

the program named example.c :

// example.c #include <ncurses.h> #include <unistd.h> int main(int argc, char *argv[]) { initscr(); noecho(); curs_set(FALSE); sleep(1); endwin(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 / / example . c # include < ncurses . h > # include < unistd . h > int main ( int argc , char * argv [ ] ) { initscr ( ) ; noecho ( ) ; curs_set ( FALSE ) ; sleep ( 1 ) ; endwin ( ) ; }

compile and run the program:

make && ./example 1 make && . / example

Tagged With

This Article Has Been Shared 633 Times! Pinterest

About Abhishek Ghosh Abhishek Ghosh is a Businessman, Orthopaedic Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.