• 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/ , linux tui , tui linux bash , tui with bash , build whiptail interface , writing a tui in bash , beautiful tui bash , bash tui how to write , bash TUI framework script

This Article Has Been Shared 789 Times!

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

  • 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

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

  • Is it Good to Run Apache Web server and MySQL Database on Separate Cloud Servers? March 27, 2023
  • Advantages of Cloud Server Over Dedicated Server for Hosting WordPress March 26, 2023
  • Get Audiophile-Grade Music on Your Smartphone March 25, 2023
  • Simple Windows Security and Privacy Checklist for 2023 March 24, 2023
  • 7 Best Artificial Intelligence (AI) Software March 24, 2023

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 28, 2023, https://thecustomizewindows.com/2016/12/build-colorful-command-line-tui-shell-scripts/.

Source:The Customize Windows, JiMA.in

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT