time_binary 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash -e
  2. #---------------------------------------------------------------------
  3. # Script to display a "binary clock" using unicode characters from the
  4. # braille block.
  5. #
  6. # Designed to work with the wonderful byobu(1) but can be run
  7. # stand-alone.
  8. #---------------------------------------------------------------------
  9. #
  10. # Copyright (C) 2011 Canonical Ltd.
  11. #
  12. # Author: James Hunt <james.hunt@canonical.com>
  13. #
  14. # This program is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation, version 3 of the License.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. #
  26. #---------------------------------------------------------------------
  27. script_name=${0##*/}
  28. # Selected Unicode Braille characters:
  29. #
  30. # 0x2800, 0x2880, 0x2820, 0x28a0, 0x2810, 0x2890, 0x2830, 0x28b0, 0x2808, 0x2888,
  31. # 0x2840, 0x28c0, 0x2860, 0x28e0, 0x2850, 0x28d0, 0x2870, 0x28f0, 0x2848, 0x28c8,
  32. # 0x2804, 0x2884, 0x2824, 0x28a4, 0x2814, 0x2894, 0x2834, 0x28b4, 0x280c, 0x288c,
  33. # 0x2844, 0x28c4, 0x2864, 0x28e4, 0x2854, 0x28d4, 0x287b, 0x28f4, 0x284c, 0x28cc,
  34. # 0x2802, 0x2882, 0x2822, 0x28a2, 0x2812, 0x2892, 0x2832, 0x28b2, 0x280a, 0x288a,
  35. # 0x2842, 0x28c2, 0x2862, 0x28e2, 0x2852, 0x28d2, 0x2872, 0x28f2, 0x284a, 0x28ca,
  36. # 0x2806, 0x2886
  37. #
  38. # Index of this array is 0-61 with character returned being
  39. # "two column" binary representation of a time segment.
  40. #
  41. binary=(⠀ ⢀ ⠠ ⢠ ⠐ ⢐ ⠰ ⢰ ⠈ ⢈ \
  42. ⡀ ⣀ ⡠ ⣠ ⡐ ⣐ ⡰ ⣰ ⡈ ⣈ \
  43. ⠄ ⢄ ⠤ ⢤ ⠔ ⢔ ⠴ ⢴ ⠌ ⢌ \
  44. ⡄ ⣄ ⡤ ⣤ ⡔ ⣔ ⡻ ⣴ ⡌ ⣌ \
  45. ⠂ ⢂ ⠢ ⢢ ⠒ ⢒ ⠲ ⢲ ⠊ ⢊ \
  46. ⡂ ⣂ ⡢ ⣢ ⡒ ⣒ ⡲ ⣲ ⡊ ⣊ ⠆ ⢆)
  47. usage()
  48. {
  49. cat <<EOT
  50. Description: A clock that displays the time in binary.
  51. Usage: $script_name [options]
  52. Options:
  53. -b : Display binary clock (default)
  54. -h : Show this help.
  55. -l : Display leading zeros (hex and octal modes only).
  56. -m : Show time in 24-hour military format
  57. (default: 12-hour format).
  58. -n : Suppress newline at end of time.
  59. -o : Display time in octal (base 8).
  60. -s <sep> : Specify separator between hours and minutes
  61. (and seconds if not disabled).
  62. -u : Display alphabetics in upper-case (hex mode only).
  63. -x : Disable time in hexadecimal (base 16).
  64. -z : Disable display of seconds.
  65. EOT
  66. }
  67. suppress_seconds=n
  68. clock_type=binary
  69. format=std
  70. ending=""
  71. uppercase=n
  72. leading_zeros=n
  73. while getopts "bhlmnos:uxz" opt
  74. do
  75. case "$opt" in
  76. b)
  77. clock_type=binary
  78. ;;
  79. h)
  80. usage
  81. exit 0
  82. ;;
  83. l)
  84. leading_zeros=y
  85. ;;
  86. m)
  87. format=mil
  88. ;;
  89. n)
  90. ending=
  91. ;;
  92. o)
  93. clock_type=oct
  94. ;;
  95. s)
  96. sep=$OPTARG
  97. ;;
  98. u)
  99. uppercase=y
  100. ;;
  101. x)
  102. clock_type=hex
  103. ;;
  104. z)
  105. suppress_seconds=y
  106. ;;
  107. esac
  108. done
  109. if [ -z "$clock_type" ]
  110. then
  111. printf "%s\n" "ERROR: must specify clock type"
  112. exit 1
  113. fi
  114. shift $[$OPTIND-1]
  115. # get current time, handling 12-hour or 24-hour
  116. if [ $format = std ]
  117. then
  118. hrs_format=%l
  119. else
  120. hrs_format=%k
  121. fi
  122. time=($(date "+${hrs_format} %M %S"))
  123. h=$(printf ${time[0]})
  124. m=$(printf ${time[1]})
  125. s=$(printf ${time[2]})
  126. if [ $clock_type = binary ]
  127. then
  128. [ $suppress_seconds = n ] && seconds="${sep}${binary[10#$s]}"
  129. display_time="${binary[10#$h]}${sep}${binary[10#$m]}${seconds}${ending}"
  130. else
  131. if [ $clock_type = hex ]
  132. then
  133. conversion=x
  134. [ $uppercase = y ] && conversion=X
  135. else
  136. conversion=o
  137. fi
  138. precision=
  139. [ $leading_zeros = y ] && precision=.2
  140. base_format=%2${precision}${conversion}
  141. hh=$(printf "$base_format" $h)
  142. hm=$(printf "$base_format" $m)
  143. [ "$suppress_seconds" = n ] && hs="${sep}$(printf "$base_format" $s)"
  144. display_time="${hh}${sep}${hm}${hs}${ending}"
  145. fi
  146. color k w
  147. printf "%s" "$display_time"
  148. color --