Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 separate fields next to each other 1

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi everyone,

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

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
 
Hi

Personally I would cut that in three pieces :
Code:
[blue]master #[/blue] cat TSch.head 
Some Text being displayed
together with several options
that can be chosen from ...

[blue]master #[/blue] cat TSch.menu 
(1) Something
(2) Something else
(Q) Quit

[blue]master #[/blue] cat TSch.logo 



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

[blue]master #[/blue] 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

[blue]master #[/blue] 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:
[blue]master #[/blue] 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

[blue]master #[/blue] 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 [tt]bash[/tt], [tt]dash[/tt] and [tt]mksh[/tt].

Feherke.
[link feherke.github.com/][/url]
 
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
 
Hi

Thomas said:
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 [COLOR=silver gainsboro][box][black]Ctrl[/black][/box][/color]-[COLOR=silver gainsboro][box][black]V[/black][/box][/color], [COLOR=silver gainsboro][box][black]Esc[/black][/box][/color]. 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 :
Bash:
[blue]master #[/blue] cat TSch.menu
[b](1)[/b] Something
[b](2)[/b] Something else
[b](3)[/b] Quit

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

[blue]master #[/blue] 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

[blue]master #[/blue] bash TSch3.sh
+-Version:3.23-------------------+
| Some Text being displayed      |
| together with several options  |
| that can be chosen from ...    |      TTTTTTTTT
|                                |          T
| [b](1)[/b] Something                  |          T
| [b](2)[/b] Something else             |          T
| [b](3)[/b] 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.
[link feherke.github.com/][/url]
 
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
 
Hi

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

[navy]$([/navy][teal]<[/teal] TSch[teal].[/teal]menu [teal])[/teal]
[navy]$([/navy] [b]printf[/b] [green][i]'[/i][/green][lime][i]\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n[/i][/lime][green][i] '[/i][/green] [teal])[/teal]
EOF
Note that MKsh is compatible with Ksh93, if you have Ksh88 will probably not work.


Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top