From cc7ad1924b01dbd476e32d1a3e0a09b9a5e25c4e Mon Sep 17 00:00:00 2001 From: Speedy Peedy <41125523+peedy2495@users.noreply.github.com> Date: Wed, 1 Aug 2018 10:49:28 +0200 Subject: [PATCH 1/2] Better colouring and added coloured timestamps Colouring fits better for light and dark terminals --- server.sh | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/server.sh b/server.sh index 61e0882..7cf29af 100755 --- a/server.sh +++ b/server.sh @@ -26,44 +26,51 @@ move_cursor_up() { } server() { - echo "Starting on port $port" + echo -e "\e[90m$(date '+%H:%M:%S')\e[32m Starting on port\e[39m $port" tail -f $output | nc -l -p $port > $input - echo server ending + echo -e "\e[90m$(date '+%H:%M:%S')\e[91m server ending\e[39m" } receive() { +# echo -ne "\e[90m$(date '+%H:%M:%S')\e[39m" printf '%s: ' "$client_name" > $output local message while IFS= read -r message; do clear_line - printf '\033[0;36m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$host_name" + echo -ne "\e[90m$(date '+%H:%M:%S') " + printf '\033[0;35m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$host_name" move_cursor_up > $output clear_line > $output - printf '\033[0;37m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$client_name" > $output + echo -ne "\e[90m$(date '+%H:%M:%S') " > $output + printf '\033[0;94m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$client_name" > $output done < $input - echo receive ending + echo -e "\e[90m$(date '+%H:%M:%S')\e[91m receive ending\e[39m" } chat() { + echo -ne "\e[90m$(date '+%H:%M:%S')\e[39m " printf '%s: ' "$host_name" local message while [ 1 ]; do IFS= read -r message clear_line > $output - printf '\033[0;36m%s: \033[0;39m%s\n%s: ' "$host_name" "$message" "$client_name" > $output + echo -ne "\e[90m$(date '+%H:%M:%S') " > $output + printf '\033[0;35m%s: \033[0;39m%s\n%s: ' "$host_name" "$message" "$client_name" > $output move_cursor_up clear_line - printf '\033[0;37m%s: \033[0;39m%s\n%s: ' "$host_name" "$message" "$host_name" + echo -ne "\e[90m$(date '+%H:%M:%S') " + printf '\033[0;94m%s: \033[0;39m%s\n%s: ' "$host_name" "$message" "$host_name" done; - echo chat ending + echo -e "\e[90m$(date '+%H:%M:%S')\e[91m chat ending\e[39m" } -read -r -p 'Enter username: ' host_name +read -p $'\e[94mEnter username:\e[39m ' host_name server & echo 'Waiting for client to join...' -printf 'Enter username: ' > $output +printf '\033[0;94m%s \033[0;39m%s%s' "Enter username:" > $output read -r client_name < $input -echo "$client_name has joined the chat" -echo "Joined $host_name's chat" > $output +echo -e "\e[90m$(date '+%H:%M:%S') \e[32m$client_name has joined the chat\e[39m" +echo -e "\e[90m$(date '+%H:%M:%S') \e[32mJoined $host_name's chat\e[39m" > $output receive & chat + From 6132fed71d7721213c179caa3bc77e6ce458ed9b Mon Sep 17 00:00:00 2001 From: Speedy Peedy <41125523+peedy2495@users.noreply.github.com> Date: Wed, 1 Aug 2018 13:48:29 +0200 Subject: [PATCH 2/2] + interrupt handling including child cleanup --- server.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server.sh b/server.sh index 7cf29af..9ca0b2c 100755 --- a/server.sh +++ b/server.sh @@ -1,5 +1,12 @@ #/bin/sh +inst="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" +trap '{ echo -e "\e[90m$(date '+%H:%M:%S')\e[91m client received interrupt: session closed\e[39m" > $output ; \ + kill $(ps u|grep "nc -l -p $port"|head -n1|awk -v n=2 '"'"'{print $n}'"'"') ; \ + echo "Server-Instance $inst exited by interrupt" ; \ + exit 0 ; \ + }' INT + host_name=host client_name=client @@ -27,8 +34,8 @@ move_cursor_up() { server() { echo -e "\e[90m$(date '+%H:%M:%S')\e[32m Starting on port\e[39m $port" - tail -f $output | nc -l -p $port > $input - echo -e "\e[90m$(date '+%H:%M:%S')\e[91m server ending\e[39m" + tail -f $output | nc -l -p $port >$input + echo -e "\e[90m$(date '+%H:%M:%S')\e[91m server ending\e[39m\n" } receive() { @@ -44,7 +51,7 @@ receive() { echo -ne "\e[90m$(date '+%H:%M:%S') " > $output printf '\033[0;94m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$client_name" > $output done < $input - echo -e "\e[90m$(date '+%H:%M:%S')\e[91m receive ending\e[39m" + echo -e "\e[90m$(date '+%H:%M:%S')\e[91m client received interrupt: session closed\e[39m" } chat() {