+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Making a GUI in a Shell Script

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Making a GUI in a Shell Script

    Here I'm going to teach you a bit about ANSI escape sequences and how to use them to give color and pizazz to your shell scripts.

    What Are Escape Sequences?
    An ANSI escape sequence consists of the character 27 followed by an open square bracket. This indicates to the system that you want to do something fun with the terminal. The next few characters are a special code that the ANSI driver understands, and will generally result in it doing something. For example, if I were to put in a shell script the following:

    Code:
    echo -en "\033[2J\033[5;9H"
    This would clear the screen and move the cursor to row 5, column 9. Immediately we can see some implications of this: we can draw anywhere on the screen, not just sequentially. Note that I had to pass -e to echo to force it to interpret \033 as character 27 instead of the literal string "\033". -n just prevents it from automatically appending a newline to the output.

    Making Things Stand Out
    There are two main ways of making things stand out on a Linux terminal without changing the color. The first is to make text bold. The escape sequence for that is \033[1m. The escape sequence for underlining is \033[4m. Because I'm a lazy typer, from now on the \033[ is assumed, and I'll just refer to the escape sequences as 1m, 4m, etc. Whenever you specify an attribute like this, it remains in effect until you get rid of it with the 0m attribute. To pass more than one attribute at the same time, separate the numbers with a semicolon. So if you really wanted to scream at the user, you could use 1;4m.

    Code:
    echo -en "\033[1mI'm bold, \033[0;4mI'm underlined\033[0m, normal...\033[1;4mSCREAMING!"
    Output:
    I'm bold, I'm underlined, normal...SCREAMING!


    Changing the Foreground Color
    Let's say I decide that my script has to put out an error message, and I want it to be in red. No problem! The ANSI driver supports sixteen colors; in the table below, the color on the left is the default, and the color on the right is displayed when you have high intensity set. Depending on the terminal, this is either 1m (yes, the same as bold) or there are a special range of high-intensity colors from 90-99, though those are less common. Play around and see what works with your terminal.

    30m black | dark gray
    31m dark red | red
    32m dark green | green
    33m pissy brown | yellow
    34m dark blue | blue
    35m dark magenta | magenta
    36m dark cyan | cyan
    37m grey | white
    38m (unused)
    39m (reset code)

    Changing the Background Color
    Changes the color displayed behind the character. These codes are just ten higher than the foreground color counterparts. (Same goes for the 90-99 as well; background is 100-110 on the terminals it works for.)

    40m black | dark gray
    41m dark red | red
    42m dark green | green
    43m pissy brown | yellow
    44m dark blue | blue
    45m dark magenta | magenta
    46m dark cyan | cyan
    47m grey | white
    48m (unused)
    49m (reset code)

    So now we can do something really crazy, like bold underlined green text on a bright white background:

    Code:
    echo -e "Clashing colors\033[1;4;32;47m HERE \033[0m."
    Moving the Cursor
    (Note that the origin is 1,1 and not 0,0 as programmers are accustomed to.)

    xA Move cursor up x rows
    xB Move cursor downx rows
    xC Move cursor right x columns (wraps to next line if need be)
    xD Move cursor left x columns (wraps to prev line if need be)
    xE Move cursor down x rows and set at beginning
    xF Move cursor upx rows and set at beginning
    x;yf Move cursor to row y, column x. If you leave out one or the other it will assume 1 for the omitted value. Leaving out both (\033[H) will move the cursor to the origin.
    xG Move cursor to column x
    x;yH Same as f (see above).
    J or 0J (Yes, that's a zero.) Clears the screen from the cursor onwards.
    1J Clears the screen from the origin up to the cursor.
    2J Clears the entire screen. MSDOS will also move the cursor to the origin.
    J or 0K (Zero again.) Clears the current line from the cursor onwards.
    1K Clears the current line from the origin up to the cursor.
    2K Clears the entire line.
    xS Scrolls the entire screen up by x lines. Assumes 1 if x is omitted.
    xT Scrolls the entire screen down by x lines. Also assumes 1.

    Yep, we can do a lot with this. Here's how you can simulate a text box at row 5, column 1:

    Code:
    setxy() {
    	echo -en "\033[$1;$2H" ;
    }
    
    #set position
    setxy(5,1)
    #white background, black text
    echo "\033[30;47m			\033[0m"
    #move back to beginning of textbox
    setxy(5,1)
    #read a string in
    read VALUEOFTEXTBOX
    Other Cool Stuff I Don't Have Experience Doing
    - Set the console video mode to give yourself more colors, control the resolution, etc.
    - You can remap keys, i.e. assign F1 to be the same as F9, or even set the L key to type "CodeCall" every time you hit it. (Great way to piss of friends.)
    - According to Wikipedia, there's a way to read the cursor position by using either 6n or x;yR - it'd be as if the row and column were typed at the console. I've never done this, nor do I know if it's standard. But it's a thing to try, if you have the time.

    References
    Wikipedia - ANSI Escape Codes
    ASCII Table and ANSI Escape Sequence Reference
    sudo rm -rf /

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Making a GUI in a Shell Script

    That reminds me of the good old days on MS-DOS 3.3. You had to load the ANSI driver, and then you could play a lot of cool text games. Nethack forever! +rep.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Making a GUI in a Shell Script

    I remember when I first discovered this, and I tried to use it in my console apps on Windows 95 (yes, I've been programming that long)...completely failed, even with the DEVICE=%SYSYEM%\ANSI.SYS or whatever in the boot config file. Still don't know why.
    sudo rm -rf /

  5. #4
    Jordan Guest

    Re: Making a GUI in a Shell Script

    A lot of good information here. +rep

  6. #5
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Making a GUI in a Shell Script

    Wow, awesome, I learned a bit.

    + rep!
    Wow I changed my sig!

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Making a GUI in a Shell Script

    Good, I'm glad to hear that. Thanks for the rep.
    sudo rm -rf /

  8. #7
    nikhilkhullar is offline Newbie
    Join Date
    Nov 2009
    Location
    Jannat
    Posts
    20
    Rep Power
    0

    Re: Making a GUI in a Shell Script

    Superb...Learnt some new things. Got some old ones refreshed. All in all, a wonderful tutorial...

    Keep it Upp !


    Regards,
    Nikhil Khullar

  9. #8
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Making a GUI in a Shell Script

    I'm bookmarking this page. Very nice tutorial. I gave you rep+.
    Life's too short to be cool. Be a nerd.

  10. #9
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Making a GUI in a Shell Script

    Thanks! Glad I could be of use to someone.
    sudo rm -rf /

  11. #10
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Making a GUI in a Shell Script

    +rep
    Also, 1111 posts!
    Last edited by Guest; 07-13-2010 at 07:35 AM.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [HELP] convert Perl script to Bash shell script
    By Egypte in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 04-24-2011, 05:37 PM
  2. Shell Script Help!
    By SeanStar in forum Linux Programming and Scripting
    Replies: 6
    Last Post: 08-12-2010, 05:20 PM
  3. Shell Script and batch
    By SexYLinuX in forum Bash / Shell Scripting
    Replies: 4
    Last Post: 11-29-2008, 06:09 AM
  4. Learning Shell Script
    By Cosmet in forum General Programming
    Replies: 0
    Last Post: 11-14-2006, 06:16 AM
  5. Replacing with Shell Script and VI
    By Nightracer in forum General Programming
    Replies: 2
    Last Post: 11-05-2006, 07:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts