life.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash
  2. RED="\033[41m \033[0m"
  3. GREEN="\033[42m \033[0m"
  4. GAP=" "
  5. if [ $# -lt 1 ]; then
  6. echo "Usage: $0 birthdate [username]"
  7. echo "Example: $0 1985-06-08 Joe"
  8. exit 1
  9. fi
  10. birthdate=$1
  11. name=${2:-$USER}
  12. columns=${3:-20}
  13. life_expectancy=80
  14. last_year_index=$((life_expectancy - 1))
  15. case "$(uname)" in
  16. "Linux")
  17. birth_year=$(date -d "$birthdate" +"%Y")
  18. birth_timestamp=$(date -d "$birthdate" +%s)
  19. ;;
  20. "Darwin")
  21. birth_year=$(date -j -f "%Y-%m-%d" "$birthdate" +"%Y")
  22. birth_timestamp=$(date -j -f "%Y-%m-%d" "$birthdate" +%s)
  23. ;;
  24. *) echo "Unsupported OS"; exit 1 ;;
  25. esac
  26. current_year=$(date +"%Y")
  27. current_timestamp=$(date +%s)
  28. years_passed=$((current_year - birth_year))
  29. weeks_passed=$(( (current_timestamp - birth_timestamp) / 604800 ))
  30. total_weeks=$((life_expectancy * 52))
  31. weeks_remaining=$((total_weeks - weeks_passed))
  32. echo -e "$name, only $weeks_remaining Sundays remain\n"
  33. rows=$((life_expectancy / columns))
  34. for (( row=0; row<rows; row++ )); do
  35. for (( col=0; col<columns; col++ )); do
  36. year_index=$((row * columns + col))
  37. if (( year_index == 0 )); then
  38. echo $birth_year
  39. fi
  40. if (( year_index < years_passed )); then
  41. echo -ne "${RED}${GAP}"
  42. else
  43. echo -ne "${GREEN}${GAP}"
  44. fi
  45. done
  46. echo
  47. if (( year_index != $last_year_index )); then
  48. echo
  49. fi
  50. done
  51. # print spaces before last year
  52. gaps=$((columns - 1))
  53. squares=$((columns * 2))
  54. indent=$((gaps + squares - 4))
  55. for ((i=0; i<indent; i++)); do
  56. echo -n ' '
  57. done
  58. last_year=$((birth_year + last_year_index))
  59. echo $last_year
  60. echo -e "\nHow are you going to spend these Sundays, $name?"