Module: Textbringer::Utils
- Included in:
- Commands
- Defined in:
- lib/textbringer/utils.rb
Constant Summary collapse
- COMPLETION =
{ completions_window: nil }
- EXPRESSION_COMPLETOR =
IRB::InputCompletor
- EXPRESSION_COMPLETOR_OPTIONS =
{ bind: TOPLEVEL_BINDING }
- HOOKS =
Hash.new { |h, k| h[k] = [] }
Class Method Summary collapse
- .add_hook(name, func = nil, local: false, &block) ⇒ Object
- .background ⇒ Object
- .complete_for_minibuffer(s, candidates) ⇒ Object
- .delete_completions_window ⇒ Object
- .foreground(&block) ⇒ Object (also: next_tick)
- .foreground! ⇒ Object (also: next_tick!)
- .get_hooks(local) ⇒ Object
- .message(msg, log: true, sit_for: nil, sleep_for: nil) ⇒ Object
- .read_buffer(prompt, default: Buffer.other.name) ⇒ Object
- .read_char ⇒ Object
- .read_command_name(prompt) ⇒ Object
- .read_encoding(prompt, **opts) ⇒ Object
- .read_event ⇒ Object
- .read_expression(prompt = "Expression: ") ⇒ Object
- .read_file_name(prompt, default: nil) ⇒ Object
- .read_from_minibuffer(prompt, completion_proc: nil, default: nil, initial_value: nil, completion_ignore_case: false, keymap: MINIBUFFER_LOCAL_MAP) ⇒ Object
- .read_key_sequence(prompt) ⇒ Object
- .read_object(prompt = "Object: ") ⇒ Object
- .read_single_char(prompt, chars) ⇒ Object
- .received_keyboard_quit? ⇒ Boolean
- .remove_hook(name, func, local: false) ⇒ Object
- .ruby_install_name ⇒ Object
- .run_hooks(name, *args, remove_on_error: false) ⇒ Object
- .run_hooks_in(hooks, name, *args, remove_on_error: false) ⇒ Object
- .self_insert_and_exit_minibuffer ⇒ Object
- .set_transient_map(map) ⇒ Object
- .show_exception(e) ⇒ Object
- .sit_for(secs, no_redisplay = false) ⇒ Object
- .sleep_for(secs) ⇒ Object
- .y_or_n?(prompt) ⇒ Boolean
- .yes_or_no?(prompt) ⇒ Boolean
Class Method Details
.add_hook(name, func = nil, local: false, &block) ⇒ Object
359 360 361 362 363 |
# File 'lib/textbringer/utils.rb', line 359 def add_hook(name, func = nil, local: false, &block) hooks = get_hooks(local) return if hooks[name].include?(func) hooks[name].unshift(func || block) end |
.background ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/textbringer/utils.rb', line 52 def background Thread.start do begin yield rescue Exception => e foreground do raise e end end end end |
.complete_for_minibuffer(s, candidates) ⇒ Object
220 221 222 |
# File 'lib/textbringer/utils.rb', line 220 def complete_for_minibuffer(s, candidates) candidates.select { |i| i.start_with?(s) } end |
.delete_completions_window ⇒ Object
194 195 196 197 198 199 |
# File 'lib/textbringer/utils.rb', line 194 def delete_completions_window if COMPLETION[:completions_window] Window.delete_window(COMPLETION[:completions_window]) COMPLETION[:completions_window] = nil end end |
.foreground(&block) ⇒ Object Also known as: next_tick
64 65 66 |
# File 'lib/textbringer/utils.rb', line 64 def foreground(&block) Controller.current.next_tick(&block) end |
.foreground! ⇒ Object Also known as: next_tick!
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/textbringer/utils.rb', line 70 def foreground! if Thread.current == Thread.main return yield end q = Queue.new foreground do begin result = yield q.push([:ok, result]) rescue Exception => e q.push([:error, e]) end end status, value = q.pop if status == :error raise value else value end end |
.get_hooks(local) ⇒ Object
370 371 372 373 374 375 376 |
# File 'lib/textbringer/utils.rb', line 370 def get_hooks(local) if local Buffer.current[:hooks] ||= Hash.new { |h, k| h[k] = [] } else HOOKS end end |
.message(msg, log: true, sit_for: nil, sleep_for: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/textbringer/utils.rb', line 9 def (msg, log: true, sit_for: nil, sleep_for: nil) str = msg.to_s if log && Buffer.current&.name != "*Messages*" buffer = Buffer["*Messages*"] || Buffer.new_buffer("*Messages*", undo_limit: 0).tap { |b| b[:top_of_window] = b.new_mark } buffer.read_only = false begin buffer.end_of_buffer buffer.insert(str + "\n") if buffer.current_line > 1000 buffer.beginning_of_buffer 10.times do buffer.forward_line end buffer.delete_region(buffer.point_min, buffer.point) buffer.end_of_buffer end ensure buffer.read_only = true end end Window.echo_area.show(str) if sit_for sit_for(sit_for) Window.echo_area. end if sleep_for sleep_for(sleep_for) Window.echo_area. end end |
.read_buffer(prompt, default: Buffer.other.name) ⇒ Object
224 225 226 227 |
# File 'lib/textbringer/utils.rb', line 224 def read_buffer(prompt, default: Buffer.other.name) f = ->(s) { complete_for_minibuffer(s, Buffer.names) } read_from_minibuffer(prompt, completion_proc: f, default: default) end |
.read_char ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/textbringer/utils.rb', line 97 def read_char event = Controller.current.read_event if !event.is_a?(String) raise EditorError, "Non character event: #{event.inspect}" end event end |
.read_command_name(prompt) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/textbringer/utils.rb', line 229 def read_command_name(prompt) f = ->(s) { complete_for_minibuffer(s.tr("-", "_"), Commands.list.map(&:to_s)) } read_from_minibuffer(prompt, completion_proc: f) end |
.read_encoding(prompt, **opts) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/textbringer/utils.rb', line 236 def read_encoding(prompt, **opts) encoding_names = (Encoding.list.map(&:name) + Encoding.aliases.keys). map(&:downcase).uniq f = ->(s) { complete_for_minibuffer(s.downcase, encoding_names) } read_from_minibuffer(prompt, completion_proc: f, **opts) end |
.read_event ⇒ Object
93 94 95 |
# File 'lib/textbringer/utils.rb', line 93 def read_event Controller.current.read_event end |
.read_expression(prompt = "Expression: ") ⇒ Object
345 346 347 348 349 350 |
# File 'lib/textbringer/utils.rb', line 345 def read_expression(prompt = "Expression: ") f = ->(s) { EXPRESSION_COMPLETOR.retrieve_completion_data(s, **EXPRESSION_COMPLETOR_OPTIONS).compact } read_from_minibuffer(prompt, completion_proc: f) end |
.read_file_name(prompt, default: nil) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/textbringer/utils.rb', line 201 def read_file_name(prompt, default: nil) f = ->(s) { s = File.(s) if s.start_with?("~") Dir.glob(s + "*").map { |file| if File.directory?(file) && !file.end_with?(?/) file + "/" else file end } } initial_value = default&.sub(%r"\A#{Regexp.quote(Dir.pwd)}/", "") ignore_case = CONFIG[:read_file_name_completion_ignore_case] file = read_from_minibuffer(prompt, completion_proc: f, initial_value: initial_value, completion_ignore_case: ignore_case) File.(file) end |
.read_from_minibuffer(prompt, completion_proc: nil, default: nil, initial_value: nil, completion_ignore_case: false, keymap: MINIBUFFER_LOCAL_MAP) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/textbringer/utils.rb', line 140 def read_from_minibuffer(prompt, completion_proc: nil, default: nil, initial_value: nil, completion_ignore_case: false, keymap: MINIBUFFER_LOCAL_MAP) if Window.echo_area.active? raise EditorError, "Command attempted to use minibuffer while in minibuffer" end old_buffer = Buffer.current old_minibuffer_selected = Window.minibuffer_selected Window.minibuffer_selected = Window.current old_completion_proc = Buffer.minibuffer[:completion_proc] old_completion_ignore_case = Buffer.minibuffer[:completion_ignore_case] old_current_prefix_arg = Controller.current.current_prefix_arg old_minibuffer_map = Buffer.minibuffer.keymap Buffer.minibuffer.keymap = keymap Buffer.minibuffer[:completion_proc] = completion_proc Buffer.minibuffer[:completion_ignore_case] = completion_ignore_case Window.echo_area.active = true begin Window.current = Window.echo_area Buffer.minibuffer.clear Buffer.minibuffer.insert(initial_value) if initial_value if default prompt = prompt.sub(/:/, " (default #{default}):") end Window.echo_area.prompt = prompt Window.echo_area.redisplay Window.update recursive_edit s = Buffer.minibuffer.to_s if default && s.empty? default else s end ensure Window.echo_area.clear Window.echo_area.redisplay Window.update Window.echo_area.active = false Window.current = Window.minibuffer_selected # Just in case Window.minibuffer_selected has been deleted by resize, # in which case Window.current is set to the first window. Window.current.buffer = Buffer.current = old_buffer Window.minibuffer_selected = old_minibuffer_selected Buffer.minibuffer[:completion_ignore_case] = old_completion_ignore_case Buffer.minibuffer[:completion_proc] = old_completion_proc Buffer.minibuffer.keymap = old_minibuffer_map Buffer.minibuffer.disable_input_method Controller.current.current_prefix_arg = old_current_prefix_arg delete_completions_window end end |
.read_key_sequence(prompt) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/textbringer/utils.rb', line 304 def read_key_sequence(prompt) buffer = Buffer.current key_sequence = [] map = Keymap.new map.define_key("\C-g", :abort_recursive_edit) map.handle_undefined_key do |key| -> { key_sequence.push(key) cmd = buffer.keymap&.lookup(key_sequence) || GLOBAL_MAP.lookup(key_sequence) if !cmd.is_a?(Keymap) exit_recursive_edit end Buffer.current.clear keys = Keymap.key_sequence_string(key_sequence) Buffer.current.insert("#{keys}-") } end read_from_minibuffer(prompt, keymap: map) if buffer.keymap&.lookup(key_sequence) || GLOBAL_MAP.lookup(key_sequence) key_sequence else keys = Keymap.key_sequence_string(key_sequence) raise EditorError, "#{keys} is undefined" end end |
.read_object(prompt = "Object: ") ⇒ Object
352 353 354 355 |
# File 'lib/textbringer/utils.rb', line 352 def read_object(prompt = "Object: ") s = read_expression(prompt) eval(s) end |
.read_single_char(prompt, chars) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/textbringer/utils.rb', line 291 def read_single_char(prompt, chars) map = Keymap.new chars.each do |c| map.define_key(c, :self_insert_and_exit_minibuffer) end map.define_key(?\C-g, :abort_recursive_edit) = chars.join(?/) map.handle_undefined_key do |key| -> { ("Invalid key. Type C-g to quit.", sit_for: 2) } end read_from_minibuffer(prompt + " (#{char_options}) ", keymap: map) end |
.received_keyboard_quit? ⇒ Boolean
105 106 107 |
# File 'lib/textbringer/utils.rb', line 105 def received_keyboard_quit? Controller.current.received_keyboard_quit? end |
.remove_hook(name, func, local: false) ⇒ Object
365 366 367 368 |
# File 'lib/textbringer/utils.rb', line 365 def remove_hook(name, func, local: false) hooks = get_hooks(local) hooks[name].delete(func) end |
.ruby_install_name ⇒ Object
417 418 419 |
# File 'lib/textbringer/utils.rb', line 417 def ruby_install_name RbConfig::CONFIG["ruby_install_name"] end |
.run_hooks(name, *args, remove_on_error: false) ⇒ Object
378 379 380 381 382 383 384 |
# File 'lib/textbringer/utils.rb', line 378 def run_hooks(name, *args, remove_on_error: false) if Buffer.current hooks = Buffer.current[:hooks] run_hooks_in(hooks, name, *args, remove_on_error:) if hooks end run_hooks_in(HOOKS, name, *args, remove_on_error:) end |
.run_hooks_in(hooks, name, *args, remove_on_error: false) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/textbringer/utils.rb', line 386 def run_hooks_in(hooks, name, *args, remove_on_error: false) hooks[name].delete_if do |func| begin case func when Symbol send(func, *args) else func.call(*args) end false rescue Exception => e raise if e.is_a?(SystemExit) if remove_on_error true else raise end end end end |
.self_insert_and_exit_minibuffer ⇒ Object
267 268 269 270 |
# File 'lib/textbringer/utils.rb', line 267 def self_insert_and_exit_minibuffer self_insert exit_recursive_edit end |
.set_transient_map(map) ⇒ Object
407 408 409 410 411 412 413 414 415 |
# File 'lib/textbringer/utils.rb', line 407 def set_transient_map(map) old_overriding_map = Controller.current.overriding_map hook = -> { Controller.current.overriding_map = old_overriding_map remove_hook(:pre_command_hook, hook) } add_hook(:pre_command_hook, hook) Controller.current.overriding_map = map end |
.show_exception(e) ⇒ Object
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 |
# File 'lib/textbringer/utils.rb', line 109 def show_exception(e) if e.is_a?(SystemExit) || e.is_a?(SignalException) raise end if !e.is_a?(Quit) && Buffer.current&.name != "*Backtrace*" buffer = Buffer.find_or_new("*Backtrace*", undo_limit: 0) if !buffer.mode.is_a?(BacktraceMode) buffer.apply_mode(BacktraceMode) end buffer.read_only = false begin buffer.delete_region(buffer.point_min, buffer.point_max) buffer.insert("#{e.class}: #{e}\n") if e.backtrace e.backtrace.each do |line| buffer.insert(line + "\n") end end buffer.beginning_of_buffer ensure buffer.read_only = true end end (e.to_s.chomp) Window.beep end |
.sit_for(secs, no_redisplay = false) ⇒ Object
43 44 45 46 |
# File 'lib/textbringer/utils.rb', line 43 def sit_for(secs, no_redisplay = false) Window.redisplay unless no_redisplay Controller.current.wait_input((secs * 1000).to_i) end |
.sleep_for(secs) ⇒ Object
48 49 50 |
# File 'lib/textbringer/utils.rb', line 48 def sleep_for(secs) sleep(secs) end |
.y_or_n?(prompt) ⇒ Boolean
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/textbringer/utils.rb', line 272 def y_or_n?(prompt) new_prompt = prompt + " (y or n) " prompt_modified = false loop do s = read_from_minibuffer(new_prompt, keymap: Y_OR_N_MAP) case s when ?y break true when ?n break false else unless prompt_modified new_prompt.prepend("Answer y or n. ") prompt_modified = true end end end end |
.yes_or_no?(prompt) ⇒ Boolean
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/textbringer/utils.rb', line 245 def yes_or_no?(prompt) loop { s = read_from_minibuffer(prompt + " (yes or no) ") case s when "yes" return true when "no" return false else ("Please answer yes or no.", sit_for: 2) end } end |