• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » How To Build Colorful Command Line TUI For Shell Scripts

By Abhishek Ghosh December 31, 2016 12:29 am Updated on December 31, 2016

How To Build Colorful Command Line TUI For Shell Scripts

Advertisement

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 :

Vim
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 :

Vim
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 :

Vim
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

How To Build Colorful Command Line TUI For Shell Scripts

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 :

Vim
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 :

Vim
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 :

Vim
1
http://urwid.org

and pdcurses :

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

and curses :

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

Ncurses is almost standard, you can read guide here :

Vim
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

Vim
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 :

Vim
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 :

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

the program named example.c :

Vim
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:

Vim
1
make && ./example

Tagged With BASH TUI , linux tui , tui linux bash , tui with bash , create bash tui app , colorful messages linux command line , writing a tui in bash , build whiptail interface , build python with beautiful cli run in command prompt , bash TUI framework script

This Article Has Been Shared 394 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

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

Here’s what we’ve got for you which might like :

Articles Related to How To Build Colorful Command Line TUI For Shell Scripts

  • Command Line Interface or CLI

    Command Line Interface or CLI is a method that enables the user to instruct a computer program using plain text commands. Concept of CLI, Shell and Terminal Emulator are not the same.

  • SMB2 or Server Message Block and OS X

    SMB2 or Server Message Block 2 is an application-layer network protocol, which was originally designed as SMB at IBM before the year 1990. It is in use on OS X.

  • Text-based User Interface (TUI)

    Text-based User Interface (TUI) was coined after the Graphical User Interface came and virtually became the standard over Command Line Interface (CLI).

  • What is Wheel Group in UNIX and Unix-Like OS?

    Wheel Group originated in the TENEX OS, distributed, widely used in 1960s. Wheel Group has wheel account, has additional system privileges.

  • Warning : TCP Stack Vulnerability in the Linux Kernel (CVE-2016-5696)

    This Article Gives Information About TCP Stack Vulnerability in the Linux Kernel For the End Users including Android Mobile Phone Users.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

You can subscribe to our Free Once a Day, Regular Newsletter by clicking the subscribe button below.

Click To Subscribe

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (21K Followers)
  • Twitter (5.3k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Basics on Python Tornado (web server) March 8, 2021
  • What You Need to Know About Hybrid Mobile App Development March 6, 2021
  • Why Not to Use Your Host for Email Marketing March 5, 2021
  • What You Need to Know About the Microservices March 4, 2021
  • Fix Missing/Bad FileProvider for Freshchat (Android error code 354) March 3, 2021

 

About This Article

Cite this article as: Abhishek Ghosh, "How To Build Colorful Command Line TUI For Shell Scripts," in The Customize Windows, December 31, 2016, March 8, 2021, https://thecustomizewindows.com/2016/12/build-colorful-command-line-tui-shell-scripts/.

Source:The Customize Windows, JiMA.in

 

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Cookie Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2021 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy