Kreslení rámečků v Bash

PhDr. Mgr. Jeroným Klimeš, Ph.D. 2020-01-06

Pokud pracujete v Linuxu (Ubuntu, Mint, Debian ap.) Vyexportujte si následující text do BASH skriptu. Jedná se o program, který v terminálu kreslí rámečky. Občas se to hodí, viz stereogramy.

Až si nakreslíte hrad, tak si zahrajte tuto písničku O princezně a o hradu na piano( MP3). Jednodušší už to snad být nemohlo. Popř. jinou z http://noty.klimes.us


jkramecky.sh
#!/bin/bash
clear 
echo ""
echo ""
echo ""
echo ""
echo "                            ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒"
echo "                              ▒     Kreslení rámečků v Bash      ▒"
echo "                            ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒"
echo "                                         naprogramoval"
echo "                          PhDr. Mgr. Jeroným Klimeš, Ph.D. 2019-01-06"
echo "             páč občas se to hodí, když si chcete nakreslit hrad nebo stereogram    "
echo ""
echo ""
echo "                 ┌───────────────────────────────────────────────────────┐"
echo "                 │  Nápověda:                                            │"
echo "                 │  Maximalizujete si okno terminalu.                    │"
echo "                 │  esdx = nahoru, dolu, doprava, doleva (misto sipek)   │"
echo "                 │  p = presun/psani       m=mazani         n=sedy blok  │"
echo "                 │  cv = meni znak na pozici kurzoru                     │"
echo "                 └───────────────────────────────────────────────────────┘"
echo ""
echo ""
read -p "Stiskněte enter a uvidíte hrad"                                        
clear
echo ""
echo ""
echo ""
echo "              ┌─┐ ┌─┐ ┌─┐                                    "
echo "              │▒└─┘▒└─┘▒│                                    "
echo "              │         │                                    "
echo "              │   ┌─┐   │                                    "
echo "              │   │┼│   │                                    "
echo "              │   └─┘   │                                    "
echo "              │         ├─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐              "
echo "              │         │▒└─┘▒└─┘▒└─┘▒└─┘▒└─┘▒│              "
echo "              │   ┌─┐   │   ┌─────────────┐   │              "
echo "              │   │┼│   │   └▒│┬───────┬│▒┘   │              "
echo "              │   └─┘   │     ││       ││     │              "
echo "              │         │     ││       ││     │              "
echo "              │         │     ││       ││     │              "
echo "└└└└└└└└└└└└└└└─────────┴─────│└───────┘│─────┘└└└└└└└└└└└└└└" 
echo "                              └─────────┘                    "
echo ""
echo ""
read -p "Stiskněte Enter a uvidíte stereogram s vystouplými a propadlými rámečky"
clear 
echo ""
echo ""
echo ""
echo ""
echo "                         x           x                 "
echo "        ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐"
echo "        │▒┌─────┐▒│ │▒┌─────┐▒│ │▒┌─────┐▒│ │▒┌─────┐▒│"
echo "        │▒│┌───┐│▒│ │▒│┌───┐│▒│ │▒│┌───┐│▒│ │▒│┌───┐│▒│"
echo "        │▒││ ▒▒││▒│ │▒││▒▒ ││▒│ │▒││ ▒▒││▒│ │▒││▒▒ ││▒│"
echo "        │▒│└───┘│▒│ │▒│└───┘│▒│ │▒│└───┘│▒│ │▒│└───┘│▒│"
echo "        │▒└─────┘▒│ │▒└─────┘▒│ │▒└─────┘▒│ │▒└─────┘▒│"
echo "        └─────────┘ └─────────┘ └─────────┘ └─────────┘"
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo "teorie ke stereogramum, viz http://stereogramy.klimes.us "
echo "                                       "
read -p "Stiskněte enter a muzete si kreslit sami"                                        
clear
# Sem si napiste to, co chcete oramovat:
echo ""
echo "                  Předpokládám, že toto je text, který si chcete dát za rámeček:"
echo ""
echo ""
echo ""
echo ""
echo ""
echo "                        Běžela Magda kaňonem, srážela banány ramenem."
echo 
echo ""
echo ""
echo ""
echo ""
echo ""
echo "                      Nápověda:"
echo "                      Maximalizujete si okno terminalu."
echo "                      esdx = nahoru, dolu, doprava, doleva (misto sipek)"
echo "                      p = presun/psani        m=mazani       n=sedy blok"
echo "                      cv = meni znak na pozici kurzoru"
echo "                      Po kreslení si zkopírujte obrazovku - myš a CTRL-SHIFT-C"
echo ""
echo ""
echo ""

# tady zacina vlastni program

# defaultni promenne
sire=121
vyse=32
# vyhozi souradnice kurzoru, pozor kurzor začíná ihned psát
k=5
l=5


newk=$k
newl=$l

while true
do
read -s -n1 
case $REPLY in
    E|e)		
# 		echo E n$nahoru d$dolu p$doprava l$doleva
		if [ "$nahoru"  == "1" ]; then 
			newk=$(( $k - 1 ))
			napis="\033(0x\033(B";
			fi #"N" 
		if [ "$dolu"    == "1" ]; then 
			newk=$(( $k - 1 ))
			napis="\033(0x\033(B";
			fi #"N" 
		if [ "$doprava" == "1" ]; then napis="\033(0j\033(B";fi #"LevyDolni" 
		if [ "$doleva"  == "1" ]; then napis="\033(0m\033(B";fi #"PravyDolni" 
		nahoru=1
		dolu=0
		doprava=0
		doleva=0
		if [[ newk -lt 0 ]]; then newk=0;fi
		;;
    S|s)		
		if [ "$nahoru"  == "1" ]; then napis="\033(0k\033(B";fi #"PravyHorni" 
		if [ "$dolu"    == "1" ]; then napis="\033(0j\033(B";fi #"PravyDolni" 
		if [ "$doprava" == "1" ]; then 
			newl=$(( $l - 1 ))
			napis="\033(0q\033(B";
			fi #"P"
		if [ "$doleva"  == "1" ]; then 
		    newl=$(( $l - 1 ))
			napis="\033(0q\033(B";
			fi #"P" 
	    nahoru=0
		dolu=0
		doprava=0
		doleva=1
		if [[ newl -lt 0 ]]; then newl=0;fi
		;;
    D|d) 
		if [ "$nahoru"  == "1" ]; then napis="\033(0l\033(B";fi #"LevyHorni" 
		if [ "$dolu"    == "1" ]; then napis="\033(0m\033(B";fi #"LevyDolni" 
		if [ "$doprava" == "1" ]; then 
			newl=$(( $l + 1 ))
			napis="\033(0q\033(B";
			fi #"P" 
		if [ "$doleva"  == "1" ]; then 
			newl=$(( $l + 1 ))
			napis="\033(0q\033(B";
			fi #"P" 
	    nahoru=0
		dolu=0
		doprava=1
		doleva=0
		if [[ newl -gt $sire ]]; then newl=$sire;fi
		;;
    X|x) 
		if [ "$nahoru"  == "1" ]; then 
			newk=$(( $k + 1 ))
			napis="\033(0x\033(B";			
			fi #"N" 
		if [ "$dolu"    == "1" ]; then 
			newk=$(( $k + 1 ))
			napis="\033(0x\033(B";
			fi #"N" 
		if [ "$doprava" == "1" ]; then napis="\033(0k\033(B";fi #"PravyHorni" 	
		if [ "$doleva"  == "1" ]; then napis="\033(0l\033(B";fi #"LevyHorni" 
	    nahoru=0
		dolu=1
		doprava=0
		doleva=0
		if [[ newk -gt $vyse ]]; then newk=$vyse;fi
		;;
	M|m)
		if [[ $mazani -eq 1 ]]; then 
			mazani=0
		else
			mazani=1
			mazani2=0
			presun=0
		fi	
		;;	
	N|n)
		if [[ $mazani2 -eq 1 ]]; then 
			mazani2=0
		else
			mazani2=1
			mazani=0
			presun=0
		fi	
		;;	
	P|p)
		if [[ $presun -eq 1 ]]; then 
			presun=0
		else
			presun=1
			mazani=0
			mazani2=0
		fi	
		;;	
	C|c)
		if [[ $pismeno -gt 12 ]]; then
			pismeno=0 # index do nasledujici promenne ramecky
		else
			pismeno=$(( $pismeno + 1 ))
		fi
		ramecky=" ajklmnqtuvwx"; # toto jsou ramecky, ktere se meni
		napis="\033(0"${ramecky:pismeno:1}"\033(B"
		zmena=1
		mazani=0
		mazani2=0
		presun=0
	;;	
	V|v)
		if [[ $pismeno -lt 1 ]]; then
			pismeno=13
		else
			pismeno=$(( $pismeno - 1 ))
		fi
			ramecky=" ajklmnqtuvwx"
			napis="\033(0"${ramecky:pismeno:1}"\033(B"
		zmena=1
		mazani=0
		mazani2=0
		presun=O
	;;	
esac
REPLY="*"
# 	echo -en "\033[$k;$l""f " # maze stare pismeno
	l=$newl
	k=$newk
	if [ "$mazani" == "1" ]; then napis=" ";fi
	if [ "$mazani2" == "1" ]; then napis="▒";fi
	if [ "$presun" == "1" ]; then napis="";fi
	echo -en "\033[$k;$l""f$napis" # pise nove pismeno
	echo -en "\033[$k;$newl""f" #posune kurzor
	
# 	echo -en "\033[0;0""f $k $l\n"
	
done

exit # toto je konec


# --- poznamky a dalsi navody tput ------------------------------------------------------
# funguje, vyzkouset: 
clear; echo -en "\033[10;10f na koordinaty [10;10] napise tento text a pak se vrati na [0;0]  \033[0;0f jsem tu" #
echo -e "\033(0 abcdefghijklmnopqqqqqrstuvwxxxxxyz \033(B ." # toto jsou ramecky
echo -e "\033(0jklmxq\033(B"
echo -e "\033(0 ajklmnqtuvwx \033(B"


tput  bold; tput dim ;tput setaf 3; echo karel


tput Colour Capabilities
tput setab [1-7]
    Set a background colour using ANSI escape
tput setb [1-7]
    Set a background colour
tput setaf [1-7]
    Set a foreground colour using ANSI escape
tput setf [1-7]
    Set a foreground colour

tput Text Mode Capabilities
tput bold

    Set bold mode
tput dim
    turn on half-bright mode
tput smul
    begin underline mode
tput rmul
    exit underline mode
tput rev

    Turn on reverse mode
tput smso
    Enter standout mode (bold on rxvt)
tput rmso

    Exit standout mode
tput sgr0

    Turn off all attributes (doesn't work quite as expected)

tput Cursor Movement Capabilities

tput cup Y X

    Move cursor to screen location X,Y (top left is 0,0)
tput sc

    Save the cursor position
tput rc

    Restore the cursor position
tput lines

    Output the number of lines of the terminal
tput cols

    Output the number of columns of the terminal
tput cub N

    Move N characters left
tput cuf N

    Move N characters right
tput cub1

    move left one space
tput cuf1

    non-destructive space (move right one space)
tput ll

    last line, first column (if no cup)
tput cuu1

    up one line

tput Clear and Insert Capabilities

tput ech N

    Erase N characters
tput clear

    clear screen and home cursor
tput el1

    Clear to beginning of line
tput el

    clear to end of line
tput ed

    clear to end of screen
tput ich N

    insert N characters (moves rest of line forward!)
tput il N

    insert N lines

# Dalsi rozumy:
# https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

#toto střílí hvězdičku diagonálně
for i in {0..30}
	do 
	echo -en "\033[$i;$(( $i * 2 ))f *  \033[0;0f " # 
	sleep .2
	done