Help with formatting a menu using tput


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with formatting a menu using tput
# 1  
Old 01-15-2012
Help with formatting a menu using tput

I am trying to format a menu using tput. A sample of the menu that i want displayed is the following:
Code:
        printf "***MAIN - MENU***\n"
        printf " 1. OPTION 1\n"
        printf " 2. OPTION 2 \n"
        printf " 3. OPTION 3\n"
        printf " 4. OPTION 4\n"
        printf " 5. Exit \n"
        printf "\n CHOOSE [1-5]"

I'd like the following menu to be displayed in the center of the terminal. If i try and use the following code, only the MAIN MENU prints in the centre of the screen and the rest of the options are on the left margin.
Code:
function view {
       lines=`tput lines`
        cols=`tput cols`
        X=`expr $lines / 3`
        Y=`expr $cols / 3`
        tput cup $X $Y
        tput sc
        return
}
 
view
printf "***MAIN - MENU***\n"
printf " 1. OPTION 1\n"
.
.

Thus i am having to call the view function before every printf statement and passing arguments as below:
Code:
function view {
        i=`expr $i + 1`
        lines=`tput lines`
        cols=`tput cols`
        X=`expr $lines / 3 + $i`
        Y=`expr $cols / 3`
        tput cup $X $Y
        #tput sc
        return
}
        clear
        i=1
        view $i
        printf "***MAIN - MENU***\n"
        view $i
        printf " 1. OPTION 1\n"
        view $i
        printf " 2. OPTION 2 \n"
        view $i
        printf " 3. OPTION 3\n"
        view $i
        printf " 4. OPTION 4\n"
        view $i
        printf " 5. Exit \n"
        view $i
        printf "\n CHOOSE [1-5]"}
        read val
        tput rc

Is there a more effective way of doing this using tput? Thanks
# 2  
Old 01-16-2012
This might help .. Taking your menu file as input ..
Code:
i=1
Y=$(echo "$(tput cols) / 3" | bc)
while read line
do
        X=$(echo "$(tput lines) / 3 + $i" | bc)
        echo "tput cup $X $Y ; $line" >> outfile
        i=$((i+1))
done < infile
clear ; sh outfile ; echo ""

Use ksh to run the script ..

Last edited by jayan_jay; 01-16-2012 at 03:11 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Shell Programming and Scripting

Tput cup, print on current line?

Heyas I'm thinking about a new approach for my core display, basicly as it should make aligments easier. Issue i'm currently facing, is tput cup capable of printing on the current line? My best achievements were: :) tui $ tput cup - 60;echo " ------ testing" ------ testing... (5 Replies)
Discussion started by: sea
5 Replies

3. Shell Programming and Scripting

Need help in create menu with 3 sub menu using the case command

hi all i am a newbie to this is there any examples on creating a main menu with 3 sub menu main menu -> option a , b and c a menu -> option 1 ,2 and 3 b menu -> option 1 ,2 c menu -> option 1 ,2 i am getting headache as my code kept getting unexpected EOF ---------- Post... (0 Replies)
Discussion started by: chercm
0 Replies

4. Shell Programming and Scripting

tput // special keys.

$ read -s -N 5 ch; printf "$ch" |od -An -c 033 Obviously during the read I pressed F1. So check something more normal, right-arrow: $ read -s -N 3 ch; printf "$ch" |od -An -c 033 $ tput kcuf1 |od -An -c 033 O C Is my terminal the only broken one? I'm using PuTTY..... (2 Replies)
Discussion started by: neutronscott
2 Replies

5. Shell Programming and Scripting

need help in using tput command

Plz help me in writing a shell script to print * in the form of 8 using tput command ... pattern shud be in d below form... * * * * * * * * * * * * * * * Thanks in advance. (3 Replies)
Discussion started by: Dpu
3 Replies

6. Shell Programming and Scripting

Use of tput ?

what is use of tput smso tput rmso tput sgr0 Thanks (3 Replies)
Discussion started by: dhananjaysk
3 Replies

7. Shell Programming and Scripting

tput smso (bold) ruins formatting

Hi, I have some very nicely formatted output on a monitor script and I'd like to make one of the fields bold when a threshold is reached but when I do it changes the formatting. I've tried using tabs to separate the fields and I've tried using printf to force the size of the fields. Below is... (2 Replies)
Discussion started by: pondlife
2 Replies

8. Shell Programming and Scripting

tput clear

What is the difference between these two commands? tput clear /usr/bin/clear (4 Replies)
Discussion started by: whatisthis
4 Replies

9. UNIX for Dummies Questions & Answers

quick question! TPUT!?

:mad: forgot the tput command to make the cursor blink... can any one give me a pointer.. one which command it is.. thanx moxxx68:D (3 Replies)
Discussion started by: moxxx68
3 Replies

10. Programming

problem with incorporating tput in a c program

as you all know, tput is somewhat of a utility that can be used to put the cursor on specific places on the screen. now, I usually use this utility when I do shell programming but I began wondering that there should be a way to put that utility into my c program so I can be able to clear the... (3 Replies)
Discussion started by: TRUEST
3 Replies
Login or Register to Ask a Question