bl-fortune 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. msg_icon=bunsen-face
  3. # name of fortune file(s) to use
  4. cookie=cbbl
  5. debug=0
  6. msg_time_default=20000
  7. msg_time=0
  8. # Improved algorithm:
  9. # 1. a fixed delay
  10. # 2. word_delay
  11. # 3. char_delay
  12. # (the final delay is added together from all these)
  13. init_delay=2000
  14. word_delay=250
  15. char_delay=25
  16. HELP=" bl-fortune is a script to display custom fortunes.
  17. Usage: bl-fortune [OPTIONS]
  18. Options:
  19. -i <icon>, --icon=<icon> use different icon
  20. Default is $msg_icon
  21. -w <time>, --word-delay=<time> set delay per word
  22. Default is ${word_delay}ms
  23. Overrides -t
  24. -c <time>, --char-delay=<time> set delay per character
  25. Default is ${char_delay}ms
  26. Overrides -t
  27. -d <time>, --init-delay=<time> set initial delay
  28. Default is ${init_delay}ms
  29. Overrides -t
  30. The options -w, -c and -d are cumulative, they add up to a final
  31. message display time. This is the default mode of operation.
  32. It can be overridden with:
  33. -t <time>, --time=<time> set fixed message expiry time in ms.
  34. If one of the options -w, -c or -d have been
  35. set incorrectly (0 or smaller), this is the
  36. fallback value: ${msg_time_default}ms
  37. Mixing -w, -c, -d options with -t option can result in strange timing.
  38. -C <name>, --cookie='<name>[ name]' set fortune cookie list(s)
  39. Default is $cookie
  40. This can contain multiple fortunes and
  41. options to fortune. Example:
  42. bl-fortune -C \"-e cbbl fortunes\"
  43. (enclose multiple words in quotes)
  44. -D Debug - enable some terminal messages
  45. -h, --help show this message
  46. Description:
  47. This is a wrapper around notify-send. The default behaviour is to choose
  48. a fortune from the 'cbbl' collection, which is gathered from CrunchBang
  49. and BunsenLabs forum posts, and display it for a time appropriate to the
  50. length of the quote, along with the obnoxious Beaker icon.
  51. "
  52. error_exit() {
  53. echo "$0 error: $1" >&2
  54. exit 1
  55. }
  56. # need to escape a few chars that make notify-send puke (because it defaults to pango markup)
  57. escapes() {
  58. retval="${1//-/\\-}"
  59. retval="${retval//&/\&amp;}"
  60. retval="${retval//</\&lt;}"
  61. retval="${retval//>/\&gt;}"
  62. retval="${retval//\"/\&quot;}"
  63. }
  64. debug() {
  65. [[ "$debug" == 1 ]] && echo -e "$@"
  66. }
  67. while [[ -n $1 ]]
  68. do
  69. case $1 in
  70. --help|-h)
  71. echo "$HELP"
  72. exit 0
  73. ;;
  74. -i)
  75. msg_icon="$2"
  76. shift 2
  77. ;;
  78. --icon=*)
  79. msg_icon="${1#--icon=}"
  80. shift
  81. ;;
  82. -t)
  83. msg_time="$2"
  84. init_delay=0
  85. word_delay=0
  86. char_delay=0
  87. shift 2
  88. ;;
  89. --time=*)
  90. msg_time="${1#--time=}"
  91. init_delay=0
  92. word_delay=0
  93. char_delay=0
  94. shift
  95. ;;
  96. -d)
  97. init_delay="$2"
  98. shift 2
  99. ;;
  100. --init-delay=*)
  101. init_delay="${1#--init-delay=}"
  102. shift
  103. ;;
  104. -w)
  105. word_delay="$2"
  106. shift 2
  107. ;;
  108. --word-delay=*)
  109. word_delay="${1#--word-delay=}"
  110. shift
  111. ;;
  112. -c)
  113. char_delay="$2"
  114. shift 2
  115. ;;
  116. --char-delay=*)
  117. char_delay="${1#--char-delay=}"
  118. shift
  119. ;;
  120. -D)
  121. debug=1
  122. shift 1
  123. ;;
  124. -C)
  125. cookie="$2"
  126. shift 2
  127. ;;
  128. --cookie=*)
  129. cookie="${1#--cookie=}"
  130. shift
  131. ;;
  132. *)
  133. error_exit "$1: no such option"
  134. ;;
  135. esac
  136. done
  137. text="$(fortune $cookie)"
  138. # Improved algorithm:
  139. # 1. an initial delay
  140. # 2. word_delay
  141. # 3. char_delay
  142. # (the final delay (msg_time) is added together from all these)
  143. if (( init_delay > 0 )) || (( word_delay > 0 )) || (( char_delay > 0 )); then
  144. debug "Calculate a text-based message time\ninit_delay: $init_delay\nword_delay: $word_delay\nchar_delay: $char_delay"
  145. words="$(wc -w <<<"$text")"
  146. chars="$(wc -m <<<"$text")"
  147. msg_time="$(( init_delay + (words * word_delay) + (chars * char_delay)))"
  148. (( msg_time <= 0 )) && debug "Invalid value for msg_time: $msg_time - using fixed message time" && msg_time=$msg_time_default
  149. else
  150. debug "Fixed message time"
  151. (( msg_time <= 0 )) && msg_time=$msg_time_default
  152. fi
  153. debug "Timeout: ${msg_time}ms"
  154. header=( $text )
  155. if (( "${#header[0]}" < 4 )) && (( "${#header[1]}" < 16 )); then
  156. header="${header[0]} ${header[1]}"
  157. else
  158. header="${header[0]}"
  159. fi
  160. text="${text#$header}"
  161. text="${text#$'\n'}"
  162. text="${text# }"
  163. escapes "$header"
  164. header="$retval"
  165. debug "Header: $header"
  166. escapes "$text"
  167. text="$retval"
  168. debug "Text: $text"
  169. notify-send --icon="$msg_icon" --expire-time="$msg_time" "$header" "$text"
  170. exit