Skip to content

Commit

Permalink
Update clock.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
mateowoetam authored Aug 21, 2024
1 parent a95a60e commit e96580f
Showing 1 changed file with 99 additions and 35 deletions.
134 changes: 99 additions & 35 deletions clock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,86 @@ hex_to_rgb() {
echo "$r;$g;$b"
}

# Function to print the ASCII clock
print_clock() {
clear
for i in {1..5}; do
line=""
for ((j=0; j<${#current_time}; j++)); do
ch="${current_time:j:1}"
colored_line=$(echo -e "${digits[$ch]}" | sed -n "${i}p")
line+="\033[38;2;${rgb_color}m${colored_line}\033[0m "
done
echo -e "$line"
done
}

# Initial hex color
hex_color="#00ff00" # Starting with red
# Function to display time in the selected time zone
display_time_in_timezone() {
selected_timezone=$1
system_time=$(date +"%I:%M %p")
TZ=$selected_timezone date +"%I:%M %p"
}

# Function to start the clock mode
start_clock() {
echo "If you are unsure about the time zone, please check the list of time zones in another terminal window."
echo "You can do this by running 'timedatectl list-timezones'."
echo "Would you like to continue and input the time zone now? (Yes/No)"
read user_response

if [[ "$user_response" =~ ^[Yy][Ee][Ss]$ ]]; then
echo "Enter a time zone (e.g., America/New_York):"
read selected_timezone

last_time=""
while true; do
current_time=$(display_time_in_timezone "$selected_timezone")
if [ "$current_time" != "$last_time" ]; then
print_clock
last_time="$current_time"
fi
sleep 1
done
else
echo "Exiting the clock mode."
exit 0
fi
}

# Function to start the timer mode
start_timer() {
echo "Enter timer duration (in seconds):"
read timer_duration
remaining_time=$timer_duration

while [ $remaining_time -ge 0 ]; do
# Format the remaining time as HH:MM:SS
current_time=$(printf '%02d:%02d:%02d' $((remaining_time / 3600)) $(( (remaining_time % 3600) / 60)) $((remaining_time % 60)))
print_clock
sleep 1
remaining_time=$((remaining_time - 1))
done

echo -e "\033[38;2;${rgb_color}mTimer finished!\033[0m"
}

# Function to start the stopwatch mode
start_stopwatch() {
start_time=$(date +%s) # Record the start time in seconds since epoch

while true; do
elapsed_time=$(( $(date +%s) - start_time )) # Calculate elapsed time
# Format the elapsed time as HH:MM:SS
current_time=$(printf '%02d:%02d:%02d' $((elapsed_time / 3600)) $(( (elapsed_time % 3600) / 60)) $((elapsed_time % 60)))
print_clock
sleep 1
done
}

# Starting prompt
echo "Enter a HEX color value (e.g., #00ff00):"
read hex_color

# Convert the hex color to RGB
rgb_color=$(hex_to_rgb "$hex_color")
Expand All @@ -34,37 +111,24 @@ digits["A"]=" A \n A A \n AAAAA \n A A \n A A \n"
digits["M"]=" M M \n MM MM \n M M M \n M M \n M M \n"
digits["P"]=" PPP \n P P \n PPPP \n P \n P \n"

print_clock() {
# Clear the screen
clear

# Format the time into the ASCII clock with color
for i in {1..5}; do
line=""
for ((j=0; j<${#current_time}; j++)); do
ch="${current_time:j:1}"
# Apply color to each line of the ASCII art
colored_line=$(echo -e "${digits[$ch]}" | sed -n "${i}p")
line+="\033[38;2;${rgb_color}m${colored_line}\033[0m "
done
echo -e "$line"
done
}

# Initialize last_time with an empty string
last_time=""

while true; do
# Get the current time
current_time=$(date +"%I:%M %p")

# Check if the time has changed
if [ "$current_time" != "$last_time" ]; then
# Update the display if time has changed
print_clock
last_time="$current_time"
fi
# Choose mode
echo "Choose a mode:"
echo "1. Clock"
echo "2. Timer"
echo "3. Stopwatch"
read mode_choice

# Check every 0.1 seconds
sleep 0.1
done
case $mode_choice in
1)
start_clock
;;
2)
start_timer
;;
3)
start_stopwatch
;;
*)
echo "Invalid choice, exiting."
;;
esac

0 comments on commit e96580f

Please sign in to comment.