From 9af0edf8f5bd1477d1ec28cd1e986783ca39a906 Mon Sep 17 00:00:00 2001 From: crunk Date: Sun, 12 Apr 2020 23:03:23 +0200 Subject: [PATCH] added support for different size of terminal --- readme.md | 8 +++++++- snakelogan | 30 +++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index dff95c4..6a6ec4b 100644 --- a/readme.md +++ b/readme.md @@ -2,5 +2,11 @@ the cursive plaintext snake writer * new so it still works pretty bad -* you need your terminal to 80 characters wide and 30 characters high. +* now support for any size of terminal * use w,s,a,d to move the snake use q to exit and save the snake + +toekomstmuziek: +* build a more TUI like interface where we don't clear the screen +* removing the flasing effect while drawing. +* put a blinking cursor where the snake currently is +* implement pen-up and pen-down diff --git a/snakelogan b/snakelogan index 46e8e51..d3fd69b 100755 --- a/snakelogan +++ b/snakelogan @@ -2,24 +2,34 @@ _STTY=$(stty -g) # Save current terminal setup -#printf "\e[2J" # clear screen, set cursos at beginning +printf "\e[2J" # clear screen, set cursos at beginning stty -echo -icanon # Turn off line buffering +printf '\e[?25h' # VARIABLES typeset -A IMAGE X=0 Y=24 +height=0 +width=0 snake='═' vertical=false forward=true upward=true +function get_size() { + # Get terminal size ('stty' is POSIX and always available). + read -r LINES COLUMNS < <(stty size) + height=$((LINES-1)) + width=$((COLUMNS-1)) +} + function start_the_snake() { rm thesnake - for i in {0..29} + for i in {0..$height} do - for j in {0..79} + for j in {0..$width} do IMAGE["${i};${j}"]=' ' ; printf ${IMAGE["${i};${j}"]} done @@ -27,20 +37,21 @@ function start_the_snake() { } function save_the_snake() { - for i in {0..29} + for i in {0..$height} do - for j in {0..79} + for j in {0..$width} do printf ${IMAGE["${i};${j}"]} done done >> thesnake + #sed -e "s/.\{$((width-1))\}/&\n/g" < thesnake > snake80 } function show_the_snake() { - clear - for i in {0..29} + printf '\e[2J\e[H' + for i in {0..$height} do - for j in {0..79} + for j in {0..$width} do printf ${IMAGE["${i};${j}"]} done @@ -110,6 +121,7 @@ function move_snake_backward() { } # Main # +get_size start_the_snake while read -s -k 1 cr; do @@ -117,7 +129,7 @@ do w) move_snake_up ; show_the_snake ;; s) move_snake_down ; show_the_snake ;; a) move_snake_backward ; show_the_snake ;; - d) move_snake_forward ; show_the_snake ;; + d) move_snake_forward ; show_the_snake ;; q) save_the_snake && at_exit ;; esac done