A set of libraries like ncurses in a shell script

7,861

Solution 1

Okay, I feel like you might be asking one of two questions, so I will try to answer both.

  1. What libraries can one use to create ncurses like interfaces for shell scripts?

    Actually, I would never have recommended ncurses directly for shell scripts anyway since it's really not meant to be used by shell languages. Instead, I would recommend dialog. Dialog is a shim library which sits between ncurses and the shell making its use much simpler.

    This would functionally give you two dependencies (one on ncurses and one on dialog) which you seem to be against for some reason.

  2. Given that I don't want any external dependencies, how can I create my own ncurses-like TUI library?

    This is way outside the scope of *nix.SE. Creating a new TUI library is not going to be trivial (particularly if you're trying to create it in pure shell). There have been plenty of projects to attempt making new libraries to replace some of the use of ncurses (e.g., termbox is one of the more successful ones).

    If you intend to create your own library, you may want to look at the lower-level projects like ncurses and termbox and higher-level projects like dialog. Looking at their work might give you an idea of how to get started.


A final recommendation:

Dependencies on external projects, though they require some extra work (for integration and support), are not a bad thing. It means that you can focus only on the tool you want to make and leave the ground-work to those doing the lower infrastructure. Linux, in particular out of the *nix platforms, has a long history of dependency interaction.

If your goal is to learn how the lower-level stuff is done, great go for it. If instead you're trying to make a tool that would benefit from such low level work, just depend on an external tool. You'll be happier and so will be everyone that looks at your code.

Solution 2

#/bin/bash
      E='echo -e';e='echo -en';trap "R;exit" 2
    ESC=$( $e "\e")
   TPUT(){ $e "\e[${1};${2}H";}
  CLEAR(){ $e "\ec";}
  CIVIS(){ $e "\e[?25l";}
   DRAW(){ $e "\e%@\e(0";}
  WRITE(){ $e "\e(B";}
   MARK(){ $e "\e[7m";}
 UNMARK(){ $e "\e[27m";}
      R(){ CLEAR ;stty sane;$e "\ec\e[37;44m\e[J";};
   HEAD(){ DRAW
           for each in $(seq 1 13);do
           $E "   x                                          x"
           done
           WRITE;MARK;TPUT 1 5
           $E "BASH SELECTION MENU                       ";UNMARK;}
           i=0; CLEAR; CIVIS;NULL=/dev/null
   FOOT(){ MARK;TPUT 13 5
           printf "ENTER - SELECT,NEXT                       ";UNMARK;}
  ARROW(){ read -s -n3 key 2>/dev/null >&2
           if [[ $key = $ESC[A ]];then echo up;fi
           if [[ $key = $ESC[B ]];then echo dn;fi;}
     M0(){ TPUT  4 20; $e "Login info";}
     M1(){ TPUT  5 20; $e "Network";}
     M2(){ TPUT  6 20; $e "Disk";}
     M3(){ TPUT  7 20; $e "Routing";}
     M4(){ TPUT  8 20; $e "Time";}
     M5(){ TPUT  9 20; $e "ABOUT  ";}
     M6(){ TPUT 10 20; $e "EXIT   ";}
      LM=6
   MENU(){ for each in $(seq 0 $LM);do M${each};done;}
    POS(){ if [[ $cur == up ]];then ((i--));fi
           if [[ $cur == dn ]];then ((i++));fi
           if [[ $i -lt 0   ]];then i=$LM;fi
           if [[ $i -gt $LM ]];then i=0;fi;}
REFRESH(){ after=$((i+1)); before=$((i-1))
           if [[ $before -lt 0  ]];then before=$LM;fi
           if [[ $after -gt $LM ]];then after=0;fi
           if [[ $j -lt $i      ]];then UNMARK;M$before;else UNMARK;M$after;fi
           if [[ $after -eq 0 ]] || [ $before -eq $LM ];then
           UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;}
   INIT(){ R;HEAD;FOOT;MENU;}
     SC(){ REFRESH;MARK;$S;$b;cur=`ARROW`;}
     ES(){ MARK;$e "ENTER = main menu ";$b;read;INIT;};INIT
  while [[ "$O" != " " ]]; do case $i in
        0) S=M0;SC;if [[ $cur == "" ]];then R;$e "\n$(w        )\n";ES;fi;;
        1) S=M1;SC;if [[ $cur == "" ]];then R;$e "\n$(ifconfig )\n";ES;fi;;
        2) S=M2;SC;if [[ $cur == "" ]];then R;$e "\n$(df -h    )\n";ES;fi;;
        3) S=M3;SC;if [[ $cur == "" ]];then R;$e "\n$(route -n )\n";ES;fi;;
        4) S=M4;SC;if [[ $cur == "" ]];then R;$e "\n$(date     )\n";ES;fi;;
        5) S=M5;SC;if [[ $cur == "" ]];then R;$e "\n$($e by oTo)\n";ES;fi;;
        6) S=M6;SC;if [[ $cur == "" ]];then R;exit 0;fi;;
 esac;POS;done
Share:
7,861

Related videos on Youtube

PersianGulf
Author by

PersianGulf

My God is Allah and my religious book is Quran.Up to now my life was based on my God's will and whatever I gained or lost is due to my God's want. My aspiration is to put my life in favour of my lord, Allah since I believe this would be a desirable life. I am a fan and supporter of Free Software and i do all my computer works based on Freesoftware. My other interests are Linux/GNU family and BSD. My main speciality is network administration but I also have decent knowledge in C/C++ and Python progmming. In general I love programming languages. My favorite sport is mountain climbing because I love outdoors and generaly I'm a nature lover. I truely love to increase my knowledge because I'm eager to know as much as possible. Not only I have this desire for myself, but also for every human being and I will do my best to help anyone who wishes to be on this path. (You can find everythinf about me at http://pahlevanzadeh.net)

Updated on September 18, 2022

Comments

  • PersianGulf
    PersianGulf almost 2 years

    Supose you present the following TUI in the shell:

    enter image description here

    I need a set of libraries that can be used in the shell to do it. Being sure, ncurses has not been used, because it make a dependecy.

    Question: How to build the widget/window or another TUI in the shell?

  • PersianGulf
    PersianGulf almost 10 years
    Thanks, 1. i'm not going create a new lib. 2. I'm not going make a dependency such as ncurses. 3. i checked, dialog dpeend on libncursesw5 . 4. I guess if i want to use same lib, i want to use Terminfo , May be i extract tasksel , or mysql-server packages, because they have preconfigure script and dont' have any dependency. anyway thank you.
  • Jakob Bennemann
    Jakob Bennemann almost 10 years
    I really don't understand why you're against depending on ncurses…
  • PersianGulf
    PersianGulf almost 10 years
    ncurses one of stable library which exist is. But when terminfo is exist, Why make i a dependecy?
  • Jakob Bennemann
    Jakob Bennemann almost 10 years
    I guess it depends on what you want to do. Doing things by-hand in terminfo can be a bit of a nightmare (though the same might be said of ncurses).
  • Marius
    Marius about 9 years
    Very likely terminfo on your system is a part of ncurses, in any case.
  • user394
    user394 almost 5 years
    "though they require some extra work" they are well worth it, considering the payoff, or else they wouldn't have existed as a thing for so long in programming.