• 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 , https://thecustomizewindows com/2016/12/build-colorful-command-line-tui-shell-scripts/ , tui linux bash , linux tui , tui with bash , build tui bash , writing a tui in bash , bash tui list , bash tui how to write , bash TUI framework script
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, 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

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Changing Data With cURL for OpenStack Swift (HP Cloud CDN)

    Changing Data With cURL For Object is Quite Easy in OpenStack Swift. Here Are Examples With HP Cloud CDN To Make it Clear. Official Examples Are Bad.

  • OpenShift OctoPress Auto install Script

    OpenShift OctoPress Auto install Script is an Advanced Script to Run OctoPress on Free OpenShift PaaS Practically Without Any Knowing Ruby or Git.

  • OpenStack Swift & HPCloud CDN PHP Bindings : Basics

    Here is the basics of OpenStack Swift & HPCloud CDN PHP Bindings for the WordPress Plugin developers and those who works with PHP based CMS.

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

Get new posts by email:

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 (24.3K Followers)
  • Twitter (5.8k 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

  • Market Segmentation in BriefSeptember 20, 2023
  • What is Booting?September 18, 2023
  • What is ncurses?September 16, 2023
  • What is JTAG in Electronics?September 15, 2023
  • iPhone 15 Pro Max Vs Samsung Galaxy S22/S23 UltraSeptember 14, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

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

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