INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

2 separate fields next to each other

2 separate fields next to each other

(OP)
Hi everyone,

"Now, what's that TSch up to this time ??" you might think reading the subject ... winky smile

I'll tell you:

I wrote a little menu structure that comes together with an ASCII logo.

Might look something like this:

CODE

+- Version:3.23 ---------------------+
| Some Text being displayed          |
| together with several options      |
| that can be chosen from ...        | TTTTTTTTT
|                                    |     T
| (1) Something                      |     T
| (2) Something else                 |     T
| (Q) Quit                           |     T  SSSSSSSSS
|                                    |     T  S
|                                    |     T  S
|                                    |        SSSSSSSSS
|                                    |                S
|                                    |                S
|                                    |        SSSSSSSSS
+------------------------------------+ 

Problem is, that every time I insert or delete a line from the box on the left, it affects the look of the logo on the right and it is quite unnerving having to build the logo again each time ...

Is there any way to separate these from each other but still let them be displayed next to each other ?

Best Regards,
Thomas

RE: 2 separate fields next to each other

Hi

man paste

CODE

paste file-with-menu.txt file-with-logo.txt 

Feherke.
http://feherke.github.com/

RE: 2 separate fields next to each other

Hi

Personally I would cut that in three pieces :

CODE --> sh

master # cat TSch.head 
Some Text being displayed
together with several options
that can be chosen from ...

master # cat TSch.menu 
(1) Something
(2) Something else
(Q) Quit

master # cat TSch.logo 



TTTTTTTTT
    T
    T
    T
    T  SSSSSSSSS
    T  S
    T  S
       SSSSSSSSS
               S
               S
       SSSSSSSSS

master # cat TSch.sh 
(
  cat TSch.head
  echo
  cat TSch.menu
  printf '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'
) | while IFS='' read -r s; do printf '| %-30s |\n' "$s"; done | head -n $( wc -l < TSch.logo ) | paste - TSch.logo

master # sh TSch.sh 
| Some Text being displayed      |
| together with several options  |
| that can be chosen from ...    |
|                                |      TTTTTTTTT
| (1) Something                  |          T
| (2) Something else             |          T
| (Q) Quit                       |          T
|                                |          T  SSSSSSSSS
|                                |          T  S
|                                |          T  S
|                                |             SSSSSSSSS
|                                |                     S
|                                |                     S
|                                |             SSSSSSSSS 

If you want to generate the two horizontal lines also with the script, it gets abit more complicated :

CODE --> sh

master # cat TSch2.sh 
length="$( wc -l < TSch.logo )"
line=0
while IFS='' read -r s; do
  str="$( printf '| %-30s |\n' "$s" )"
  line=$(( line+1 ))
  [ "$line" = 1 -o "$line" = "$length" ] && echo "$str" | tr '| ' '+-' || echo "$str"
done <<EOF | head -n "$length" | paste - TSch.logo
$(
echo 'Version:3.23'
cat TSch.head
echo
cat TSch.menu
printf '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n '
)
EOF

master # sh TSch2.sh 
+-Version:3.23-------------------+
| Some Text being displayed      |
| together with several options  |
| that can be chosen from ...    |      TTTTTTTTT
|                                |          T
| (1) Something                  |          T
| (2) Something else             |          T
| (Q) Quit                       |          T  SSSSSSSSS
|                                |          T  S
|                                |          T  S
|                                |             SSSSSSSSS
|                                |                     S
|                                |                     S
+--------------------------------+             SSSSSSSSS 
Tested with bash, dash and mksh.

Feherke.
http://feherke.github.com/

RE: 2 separate fields next to each other

(OP)
Hi Feherke,

thanks a lot so far !
Everything's working fine except for one thing I forgot to mention ...

To improve the readability of the menu, I'm using colour codes.

e.G.

CODE

\033[1;3(1)\033[m Something
\033[1;3(2)\033[m Something else
\033[1;3(3)\033[m Quit 

Is there any way to preserve those as well ?

Regards,
Thomas

RE: 2 separate fields next to each other

Hi

Quote (Thomas)

To improve the readability of the menu, I'm using colour codes.
Would be much easier if you would use real Esc characters. I mean instead of typing "\033" ( or "\e" in some shells ), press Ctrl-V, Esc. The result will look like ^[, but in fact it will be a single character with code 27 ( or octal 033, or hexadecial 0x1b ), which is the Esc character itself.

Well, I can solve that too, but the portable way would be lengthy and ugly. This is Bash only :

CODE --> Bash

master # cat TSch.menu
(1) Something
(2) Something else
(3) Quit

master # cat -v TSch.menu
^[[1;3m(1)^[[m Something
^[[1;3m(2)^[[m Something else
^[[1;3m(3)^[[m Quit

master # cat TSch3.sh
shopt -s extglob
length="$( wc -l < TSch.logo )"
line=0
while IFS='' read -r str; do
  raw="${str//^[\[*([0-9;])m}"
  printf -v col '%-100s' "$str"
  col="| ${col:0:$(( 30+(${#str}-${#raw}) ))} |"
  (( ++line == 1 || line == length )) && tr '| ' '+-' <<< "$col" || echo "$col"
done < <(
  echo 'Version:3.23'
  cat TSch.head
  echo
  cat TSch.menu
  printf '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n '
) | head -n "$length" | paste - TSch.logo

master # bash TSch3.sh
+-Version:3.23-------------------+
| Some Text being displayed      |
| together with several options  |
| that can be chosen from ...    |      TTTTTTTTT
|                                |          T
| (1) Something                  |          T
| (2) Something else             |          T
| (3) Quit                       |          T  SSSSSSSSS
|                                |          T  S
|                                |          T  S
|                                |             SSSSSSSSS
|                                |                     S
|                                |                     S
+--------------------------------+             SSSSSSSSS 
I used Bash in the above example as it was the easiest for me. If you need the solution for other shell, specify it.

Feherke.
http://feherke.github.com/

RE: 2 separate fields next to each other

(OP)
Hey,

thanks a lot so far, that's perfect !

As for your question: I'm using ksh but maybe I'm able to rewrite the rest of my script in bash ...

Regards,
Thomas

RE: 2 separate fields next to each other

Hi

Then try this one :

CODE --> MKsh

length="$( wc -l < TSch.logo )"
line=0
while IFS='' read -r str; do
  raw="${str//[*([0-9;])m}"
  col="$( printf '%-100s' "$str" )"
  (( nr=30+(${#str}-${#raw}) ))
  col="| ${col:0:nr} |"
  (( ++line == 1 || line == length )) && echo "$col" | tr '| ' '+-' || echo "$col"
done <<EOF | head -n "$length" | paste - TSch.logo
Version:3.23
$(< TSch.head )

$(< TSch.menu )
$( printf '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ' )
EOF 
Note that MKsh is compatible with Ksh93, if you have Ksh88 will probably not work.

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close