Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better colouring and added coloured timestamps #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions server.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -26,44 +33,51 @@ move_cursor_up() {
}

server() {
echo "Starting on port $port"
tail -f $output | nc -l -p $port > $input
echo server ending
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\n"
}

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 client received interrupt: session closed\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