From 5c4a75b0612643fedefd2908fca179c8498bf689 Mon Sep 17 00:00:00 2001 From: crunk Date: Sun, 12 Apr 2020 23:57:57 +0200 Subject: [PATCH] added cursor and added pen up pen down with keys t and y --- readme.md | 4 ++-- snakelogan | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/readme.md b/readme.md index 6a6ec4b..f07f8f0 100644 --- a/readme.md +++ b/readme.md @@ -8,5 +8,5 @@ the cursive plaintext snake writer 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 +* ~~Put a blinking cursor where the snake currently is~~ implemented +* ~~implement pen-up and pen-down~~ unit ready diff --git a/snakelogan b/snakelogan index d3fd69b..a7ec5d3 100755 --- a/snakelogan +++ b/snakelogan @@ -4,7 +4,7 @@ _STTY=$(stty -g) # Save current terminal setup printf "\e[2J" # clear screen, set cursos at beginning stty -echo -icanon # Turn off line buffering -printf '\e[?25h' +printf '\e[?25h' # Turn on the cursor # VARIABLES typeset -A IMAGE @@ -17,6 +17,7 @@ snake='═' vertical=false forward=true upward=true +pen_down=true function get_size() { # Get terminal size ('stty' is POSIX and always available). @@ -34,6 +35,7 @@ function start_the_snake() { IMAGE["${i};${j}"]=' ' ; printf ${IMAGE["${i};${j}"]} done done + printf '\e[2J\e[H' ; printf '\e[0;24H' } function save_the_snake() { @@ -56,6 +58,7 @@ function show_the_snake() { printf ${IMAGE["${i};${j}"]} done done + printf '\e[%s;%sH' "$((Y+1))" "$((X+1))" } function at_exit() { printf "\e[?9l" # Turn off mouse reading @@ -75,10 +78,14 @@ function move_snake_up() { else snake='╚' fi - IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) ; vertical=true ; upward=true else - snake='║' ; IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) + snake='║' fi + #draw nothing it the pen is not down + if [ "$pen_down" = false ] ; then + snake=' ' + fi + IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) ; vertical=true ; upward=true } function move_snake_down() { @@ -88,10 +95,14 @@ function move_snake_down() { else snake='╔' fi - IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) ; vertical=true ; upward=false else - snake='║' ; IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) + snake='║' + fi + #draw nothing it the pen is not down + if [ "$pen_down" = false ] ; then + snake=' ' fi + IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) ; vertical=true ; upward=false } function move_snake_forward() { @@ -101,10 +112,14 @@ function move_snake_forward() { else snake='╚' fi - IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) ; vertical=false ; forward=true else - snake='═' ; IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) + snake='═' fi + #draw nothing it the pen is not down + if [ "$pen_down" = false ] ; then + snake=' ' + fi + IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) ; vertical=false ; forward=true } function move_snake_backward() { @@ -114,15 +129,20 @@ function move_snake_backward() { else snake='╝' fi - IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) ; vertical=false ; forward=false else - snake='═' ; IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) + snake='═' + fi + #draw nothing it the pen is not down + if [ "$pen_down" = false ] ; then + snake=' ' fi + IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) ; vertical=false ; forward=false } # Main # get_size start_the_snake +echo while read -s -k 1 cr; do case "$cr" in @@ -130,6 +150,8 @@ do s) move_snake_down ; show_the_snake ;; a) move_snake_backward ; show_the_snake ;; d) move_snake_forward ; show_the_snake ;; + t) pen_down=true ;; + y) pen_down=false ;; q) save_the_snake && at_exit ;; esac done