Class: Henge::Runner
- Inherits:
-
Object
- Object
- Henge::Runner
- Defined in:
- lib/henge/runner.rb
Constant Summary collapse
- DIRECT_KEYS =
'0123456789)!@#$%^&*('.split('')
Instance Method Summary collapse
- #draw_center_label(index, marker) ⇒ Object
- #draw_dictionary ⇒ Object
- #draw_moved_cursor ⇒ Object
- #draw_word(index, marker) ⇒ Object
- #erase_all_words ⇒ Object
- #get_key_input ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #intro(say_word) ⇒ Object
- #load_words ⇒ Object
- #move_cursor(index) ⇒ Object
- #open_image ⇒ Object
- #redraw_words ⇒ Object
- #run ⇒ Object
- #say ⇒ Object
- #say_all ⇒ Object
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
6 7 8 9 10 |
# File 'lib/henge/runner.rb', line 6 def initialize @o = [40, 18] @circles_dir = File.join(__dir__, '../../data/circles') Curses.timeout = 200 end |
Instance Method Details
#draw_center_label(index, marker) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/henge/runner.rb', line 249 def draw_center_label(index, marker) if marker Curses.attron(Curses::A_REVERSE) else Curses.attroff(Curses::A_REVERSE) end Curses.setpos(@o[1], @o[0]) key = DIRECT_KEYS[index] # Curses.addstr("#{@pattern}:#{key}") Curses.addstr("#{@pattern}") Curses.attroff(Curses::A_REVERSE) end |
#draw_dictionary ⇒ Object
167 168 169 170 171 172 |
# File 'lib/henge/runner.rb', line 167 def draw_dictionary Curses.clear() Curses.setpos(5, 0) Curses.addstr(%x{osx-dictionary #{Shellwords.shellescape(@words[@cursor])}}) Curses.refresh end |
#draw_moved_cursor ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/henge/runner.rb', line 157 def draw_moved_cursor unless @last_cursor.nil? draw_word(@last_cursor, false) end draw_word(@cursor, true) draw_center_label(@cursor, false) Curses.refresh end |
#draw_word(index, marker) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/henge/runner.rb', line 192 def draw_word(index, marker) word = @words[index] rad = index * @unit - Math::PI / 2 x = 32 * Math.cos(rad) + @o[0] y = 16 * Math.sin(rad) + @o[1] if marker Curses.attron(Curses::A_REVERSE) else Curses.attroff(Curses::A_REVERSE) end Curses.setpos(y.to_i, x.to_i) Curses.addstr(word) Curses.attroff(Curses::A_REVERSE) @visible_words[index] = true end |
#erase_all_words ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/henge/runner.rb', line 183 def erase_all_words @cursor = -1 @last_cursor = nil @visible_words = [] Curses.clear() Curses.refresh draw_center_label(0, false) end |
#get_key_input ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/henge/runner.rb', line 49 def get_key_input Curses.noecho() c = Curses.getch Curses.echo() @key = nil if c == 'q' @key = :quit return end if @cursor.nil? # First key pressed @words.length.times {|i| draw_word(i, false) } @cursor = 0 @last_cursor = nil @key = :cursor return end if c == 'j' @key = :cursor move_cursor(@cursor + 1) elsif c == 'k' @key = :cursor move_cursor(@cursor - 1) elsif DIRECT_KEYS.include?(c) @key = :cursor move_cursor(DIRECT_KEYS.index(c)) elsif c == ' ' @key = :dictionary elsif c == 'c' @key = :erase elsif c == 'p' @key = :play elsif c == 'a' @key = :say_all elsif c == 'r' @key = :reload elsif c == 'i' @key = :image end end |
#init ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/henge/runner.rb', line 93 def init load_words Curses.clear() Curses.curs_set(0) @cursor = nil @key = nil @screen_mode = nil @last_cursor = nil @visible_words = [] intro(false) end |
#intro(say_word) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/henge/runner.rb', line 29 def intro(say_word) @words.each.with_index do |w, i| move_cursor(i) draw_word(@cursor, @cursor % @square == 0) draw_center_label(@cursor, @cursor % @square == 0) Curses.refresh say if say_word end move_cursor(0) end |
#load_words ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/henge/runner.rb', line 12 def load_words if ARGV[0] @pattern = ARGV[0] ARGV[0] = nil else Dir.chdir(@circles_dir) do file = Dir.glob('*.yml').shuffle.first @pattern = file[/(.+)\.yml/, 1] end end file = File.join(@circles_dir, "#{@pattern}.yml") @words = YAML.load(File.read(file)) @unit = Math::PI * 2 / @words.length @square = @words.length / 4 end |
#move_cursor(index) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/henge/runner.rb', line 40 def move_cursor(index) @cursor = index if @cursor < 0 @cursor = @words.length - 1 elsif @cursor >= @words.length @cursor = @cursor - @words.length end end |
#open_image ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/henge/runner.rb', line 238 def open_image word = Shellwords.shellescape(@words[@cursor]) %x{open 'https://www.google.co.jp/search?q=#{word}&tbm=isch'} # @words.each do |word| # word = Shellwords.shellescape(word) # %x{open 'https://www.google.co.jp/search?q=#{word}&tbm=isch'} # sleep 0.5 # end end |
#redraw_words ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/henge/runner.rb', line 174 def redraw_words Curses.clear() @visible_words.each.with_index do |visible, i| draw_word(i, @cursor == i) if visible end draw_center_label(@cursor.to_i, false) Curses.refresh end |
#run ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/henge/runner.rb', line 108 def run begin Curses.init_screen init loop do get_key_input if @key.nil? next elsif @key == :quit break elsif @key == :erase erase_all_words next elsif @key == :play erase_all_words intro(true) elsif @key == :dictionary if @screen_mode == :dictionary @screen_mode = :circle redraw_words say else @screen_mode = :dictionary draw_dictionary say end elsif @key == :cursor if @screen_mode == :dictionary draw_dictionary @visible_words[@cursor] = true else draw_moved_cursor end say @last_cursor = @cursor elsif @key == :say_all say_all elsif @key == :reload init elsif @key == :image open_image end end ensure Curses.close_screen end end |
#say ⇒ Object
212 213 214 |
# File 'lib/henge/runner.rb', line 212 def say %x{say #{Shellwords.shellescape(@words[@cursor])}} end |
#say_all ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/henge/runner.rb', line 216 def say_all text = '' unit = @words.length / 4 unit = 4 if unit == 2 @words.each_with_index do |w, i| text += w if i != @words.length - 1 if i != 0 && (i + 1) % unit == 0 text += ', ' else text += ' ' end end end # puts text # text = @words.join(' ') %x{say #{Shellwords.shellescape(text)}} end |