fatty 0.99.10.0 → 0.99.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.org +30 -0
  3. data/README.md +263 -230
  4. data/README.org +49 -5
  5. data/examples/markdown/native-markdown.md +370 -0
  6. data/examples/markdown/ox-gfm-markdown.md +373 -0
  7. data/exe/fatty +43 -41
  8. data/exe/themer +10 -10
  9. data/help/help.md +1 -0
  10. data/lib/fatty/alert.rb +1 -1
  11. data/lib/fatty/ansi/renderer.rb +8 -4
  12. data/lib/fatty/ansi/windows_console.rb +40 -0
  13. data/lib/fatty/ansi.rb +0 -5
  14. data/lib/fatty/api/choose.rb +2 -2
  15. data/lib/fatty/api/menu.rb +3 -0
  16. data/lib/fatty/api/output.rb +13 -5
  17. data/lib/fatty/api/progress.rb +6 -6
  18. data/lib/fatty/api/prompt.rb +3 -1
  19. data/lib/fatty/api/status.rb +1 -1
  20. data/lib/fatty/api.rb +12 -10
  21. data/lib/fatty/callback_environment.rb +1 -0
  22. data/lib/fatty/colors/color.rb +7 -30
  23. data/lib/fatty/config.rb +11 -11
  24. data/lib/fatty/curses/context.rb +41 -11
  25. data/lib/fatty/curses/event_source.rb +85 -26
  26. data/lib/fatty/curses/key_decoder.rb +54 -22
  27. data/lib/fatty/curses/patch.rb +2 -0
  28. data/lib/fatty/curses/pdcurses_keys.rb +154 -0
  29. data/lib/fatty/curses/windows_keyboard.rb +35 -0
  30. data/lib/fatty/env.rb +14 -12
  31. data/lib/fatty/help.rb +26 -18
  32. data/lib/fatty/input_field.rb +55 -17
  33. data/lib/fatty/key_event.rb +1 -4
  34. data/lib/fatty/keymaps/emacs.rb +12 -2
  35. data/lib/fatty/markdown/ansi_renderer.rb +1 -1
  36. data/lib/fatty/markdown/render.rb +10 -3
  37. data/lib/fatty/mouse_event.rb +3 -3
  38. data/lib/fatty/output_buffer.rb +1 -2
  39. data/lib/fatty/pager.rb +7 -7
  40. data/lib/fatty/progress.rb +2 -2
  41. data/lib/fatty/renderer/curses.rb +28 -27
  42. data/lib/fatty/renderer/truecolor.rb +8 -1
  43. data/lib/fatty/renderer.rb +6 -6
  44. data/lib/fatty/session/keytest_session.rb +2 -1
  45. data/lib/fatty/session/output_session.rb +3 -5
  46. data/lib/fatty/session/popup_session.rb +24 -24
  47. data/lib/fatty/session/prompt_session.rb +1 -1
  48. data/lib/fatty/session/shell_session.rb +11 -5
  49. data/lib/fatty/session/status_session.rb +2 -2
  50. data/lib/fatty/shell.rb +60 -0
  51. data/lib/fatty/terminal.rb +24 -19
  52. data/lib/fatty/version.rb +1 -1
  53. data/lib/fatty.rb +9 -10
  54. metadata +24 -18
@@ -105,8 +105,8 @@ module Fatty
105
105
  selected = payload[:items] || {}
106
106
  result =
107
107
  selected.each_with_object({}) do |(label, _), h|
108
- h[label] = label_to_value.fetch(label, cancel_value)
109
- end
108
+ h[label] = label_to_value.fetch(label, cancel_value)
109
+ end
110
110
  done = true
111
111
  end
112
112
  cancel_proc = -> do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # lib/fatty/api/prompt.rb
2
4
 
3
5
  module Fatty
@@ -93,6 +95,7 @@ module Fatty
93
95
  unless action.respond_to?(:call)
94
96
  raise ArgumentError, "menu choice #{label.inspect} is not callable"
95
97
  end
98
+
96
99
  normalized[label.to_s] = action
97
100
  end
98
101
  end
@@ -33,18 +33,26 @@ module Fatty
33
33
  end
34
34
 
35
35
  def markdown(text)
36
- md = Fatty::Markdown.render(
36
+ append(render_markdown(text))
37
+ nil
38
+ end
39
+
40
+ def markdown_now(text)
41
+ append_now(render_markdown(text))
42
+ nil
43
+ end
44
+
45
+ private
46
+
47
+ def render_markdown(text)
48
+ Fatty::Markdown.render(
37
49
  text,
38
50
  palette: markdown_palette,
39
51
  theme: markdown_theme,
40
52
  truecolor: markdown_truecolor?,
41
53
  )
42
- append(md)
43
- nil
44
54
  end
45
55
 
46
- private
47
-
48
56
  def markdown_palette
49
57
  terminal.renderer.palette if terminal.respond_to?(:renderer) && terminal.renderer
50
58
  end
@@ -5,12 +5,12 @@ module Fatty
5
5
  # Create a transient status-line progress indicator.
6
6
  # For style :spinner, total may be omitted for indeterminate progress.
7
7
  def add_progress(
8
- label: Progress::DEFAULT_LABEL,
9
- total: nil,
10
- style: Progress::DEFAULT_STYLE,
11
- role: Progress::DEFAULT_ROLE,
12
- width: Progress::DEFAULT_WIDTH
13
- )
8
+ label: Progress::DEFAULT_LABEL,
9
+ total: nil,
10
+ style: Progress::DEFAULT_STYLE,
11
+ role: Progress::DEFAULT_ROLE,
12
+ width: Progress::DEFAULT_WIDTH
13
+ )
14
14
  @progress = Progress.new(
15
15
  terminal: terminal,
16
16
  label: label,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # lib/fatty/api/prompt.rb
2
4
 
3
5
  module Fatty
@@ -47,7 +49,7 @@ module Fatty
47
49
 
48
50
  begin
49
51
  terminal.apply_command(
50
- Fatty::Command.terminal(:push_modal, session: popup, owner: owner)
52
+ Fatty::Command.terminal(:push_modal, session: popup, owner: owner),
51
53
  )
52
54
  terminal.render_frame
53
55
 
@@ -15,7 +15,7 @@ module Fatty
15
15
  text: text,
16
16
  role: role,
17
17
  append: !replace,
18
- )
18
+ ),
19
19
  )
20
20
  terminal.without_cursor_restore do
21
21
  terminal.render_frame
data/lib/fatty/api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fatty
2
4
  module ChooseApi
3
5
  # simplecov:disable
@@ -27,13 +29,13 @@ end
27
29
 
28
30
  # simplecov:enable
29
31
 
30
- require_relative './api/output.rb'
31
- require_relative './api/status.rb'
32
- require_relative './api/alert.rb'
33
- require_relative './api/choose.rb'
34
- require_relative './api/prompt.rb'
35
- require_relative './api/progress.rb'
36
- require_relative './api/menu.rb'
37
- require_relative './api/keytest.rb'
38
- require_relative './api/environment.rb'
39
- require_relative './api/suspend.rb'
32
+ require_relative 'api/output.rb'
33
+ require_relative 'api/status.rb'
34
+ require_relative 'api/alert.rb'
35
+ require_relative 'api/choose.rb'
36
+ require_relative 'api/prompt.rb'
37
+ require_relative 'api/progress.rb'
38
+ require_relative 'api/menu.rb'
39
+ require_relative 'api/keytest.rb'
40
+ require_relative 'api/environment.rb'
41
+ require_relative 'api/suspend.rb'
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  #
3
4
  # This class provides a wrapper around components that can be handed off to
4
5
  # Terminal's on_accept method (or other callbacks) for access to Fatty's
@@ -109,9 +109,9 @@ module Fatty
109
109
  end
110
110
 
111
111
  def x11_colors_by_name
112
- x11_colors.sort_by do |name, _rgb|
112
+ x11_colors.sort_by { |name, _rgb|
113
113
  color_name_sort_key(name)
114
- end.to_h
114
+ }.to_h
115
115
  end
116
116
 
117
117
  def xterm_color_name(index)
@@ -178,9 +178,9 @@ module Fatty
178
178
  parsed = parse_hex(s)
179
179
  parsed ||= x11_rgb_for_name(s)
180
180
  parsed ||= begin
181
- ansi = ANSI_NAMES[s]
182
- xterm_rgb_for_index(ansi) unless ansi == DEFAULT_INDEX || ansi.nil?
183
- end
181
+ ansi = ANSI_NAMES[s]
182
+ xterm_rgb_for_index(ansi) unless ansi == DEFAULT_INDEX || ansi.nil?
183
+ end
184
184
  parsed
185
185
  end
186
186
  end
@@ -432,7 +432,8 @@ module Fatty
432
432
  x
433
433
  end
434
434
 
435
- def dist2(r1, g1, b1, r2, g2, b2)
435
+ # Two RGB triples keep callers explicit about the color components.
436
+ def dist2(r1, g1, b1, r2, g2, b2) # rubocop:disable Metrics/ParameterLists
436
437
  dr = r1 - r2
437
438
  dg = g1 - g2
438
439
  db = b1 - b2
@@ -454,30 +455,6 @@ module Fatty
454
455
  @x11_table
455
456
  end
456
457
 
457
- def load_x11_rgb_txt(path)
458
- table = {}
459
-
460
- if File.file?(path)
461
- File.foreach(path) do |line|
462
- next if line.strip.empty?
463
- next if line.lstrip.start_with?("!")
464
-
465
- # Format: R G B <name...>
466
- parts = line.strip.split(/\s+/)
467
- next if parts.length < 4
468
-
469
- r = parts[0].to_i
470
- g = parts[1].to_i
471
- b = parts[2].to_i
472
- name = parts[3..].join(" ")
473
- key = normalize_name(name)
474
- table[key] = [r, g, b]
475
- end
476
- end
477
-
478
- table
479
- end
480
-
481
458
  def load_x11_rgb_txt(path)
482
459
  table = {}
483
460
 
data/lib/fatty/config.rb CHANGED
@@ -156,17 +156,17 @@ module Fatty
156
156
  path = app_config_path || File.join(user_config_dir, "config.yml")
157
157
 
158
158
  config = if app_config_path
159
- read_app("config")
160
- elsif File.exist?(path)
161
- YAML.safe_load_file(
162
- path,
163
- aliases: true,
164
- permitted_classes: [Date, DateTime, Time],
165
- symbolize_names: true,
166
- ) || {}
167
- else
168
- {}
169
- end
159
+ read_app("config")
160
+ elsif File.exist?(path)
161
+ YAML.safe_load_file(
162
+ path,
163
+ aliases: true,
164
+ permitted_classes: [Date, DateTime, Time],
165
+ symbolize_names: true,
166
+ ) || {}
167
+ else
168
+ {}
169
+ end
170
170
  config[name.to_sym] = value
171
171
  FileUtils.mkdir_p(File.dirname(path))
172
172
  File.write(path, YAML.dump(yaml_safe_value(config)))
@@ -6,11 +6,24 @@ require "fiddle/import"
6
6
  module Fatty
7
7
  module Curses
8
8
  module Native
9
- extend Fiddle::Importer
9
+ class SuspensionUnavailable < StandardError; end
10
10
 
11
- dlload Fiddle.dlopen(nil)
12
- extern "int endwin()"
11
+ def self.prepare_suspend!
12
+ return ::Curses.method(:endwin) if ::Curses.respond_to?(:endwin)
13
+
14
+ @endwin ||= Fiddle::Function.new(Fiddle.dlopen(nil)['endwin'], [], Fiddle::TYPE_INT)
15
+ rescue Fiddle::DLError => e
16
+ raise SuspensionUnavailable, "This curses backend cannot temporarily suspend its screen: #{e.message}"
17
+ end
18
+
19
+ def self.endwin
20
+ result = prepare_suspend!.call
21
+ raise SuspensionUnavailable, "The curses backend failed to suspend" if result == false || result == -1
22
+
23
+ result
24
+ end
13
25
  end
26
+
14
27
  # Context represents the active curses environment.
15
28
  #
16
29
  # It owns:
@@ -41,14 +54,14 @@ module Fatty
41
54
  return self if @started
42
55
 
43
56
  configure_escape_delay!
44
- ::Curses.init_screen
57
+ initialize_screen!
45
58
  MouseConstants.ensure!
46
59
 
47
60
  ::Curses.raw
48
61
  ::Curses.noecho
49
62
  ::Curses.curs_set(1)
50
63
  ::Curses.stdscr.keypad(true)
51
- ::Curses.mousemask(::Curses::ALL_MOUSE_EVENTS)
64
+ ::Curses.mousemask(::Curses::ALL_MOUSE_EVENTS) if ::Curses.respond_to?(:mousemask)
52
65
  enable_bracketed_paste!
53
66
  setup_colors
54
67
  @truecolor = truecolor_enabled?
@@ -143,6 +156,7 @@ module Fatty
143
156
  def suspend
144
157
  return unless @started
145
158
 
159
+ Native.prepare_suspend!
146
160
  ::Curses.def_prog_mode
147
161
  disable_bracketed_paste!
148
162
  ::Curses.curs_set(1)
@@ -241,10 +255,10 @@ module Fatty
241
255
 
242
256
  @ansi_pairs ||= {}
243
257
  @ansi_pair_limit ||= begin
244
- ::Curses.color_pairs.to_i
245
- rescue StandardError
246
- 256
247
- end
258
+ ::Curses.color_pairs.to_i
259
+ rescue StandardError
260
+ 256
261
+ end
248
262
 
249
263
  key = [fg.nil? ? -1 : fg.to_i, bg.nil? ? -1 : bg.to_i]
250
264
  cached = @ansi_pairs[key]
@@ -257,8 +271,8 @@ module Fatty
257
271
  if @ansi_pair_limit.positive? && @ansi_next_pair_id >= @ansi_pair_limit
258
272
  Fatty.debug(
259
273
  "ansi_pair_for exhausted next=#{@ansi_next_pair_id.inspect} " \
260
- "limit=#{@ansi_pair_limit.inspect} fg=#{fg.inspect} bg=#{bg.inspect} " \
261
- "fallback=#{fallback_pair_id.inspect}",
274
+ "limit=#{@ansi_pair_limit.inspect} fg=#{fg.inspect} bg=#{bg.inspect} " \
275
+ "fallback=#{fallback_pair_id.inspect}",
262
276
  tag: :theme,
263
277
  )
264
278
  @ansi_pairs[key] = fallback_pair_id
@@ -286,6 +300,22 @@ module Fatty
286
300
 
287
301
  private
288
302
 
303
+ # PDCurses otherwise draws to a private Windows console buffer while
304
+ # Ruby's ANSI output still targets the original standard-output buffer.
305
+ def initialize_screen!
306
+ unless RUBY_PLATFORM.match?(/mingw|mswin/) && ::Curses::VERSION.start_with?('PDCurses')
307
+ return ::Curses.init_screen
308
+ end
309
+
310
+ previous = ENV['PDC_RESTORE_SCREEN']
311
+ begin
312
+ ENV['PDC_RESTORE_SCREEN'] = '0'
313
+ ::Curses.init_screen
314
+ ensure
315
+ ENV['PDC_RESTORE_SCREEN'] = previous
316
+ end
317
+ end
318
+
289
319
  # simplecov:disable
290
320
 
291
321
  def ensure_started!
@@ -1,34 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "windows_keyboard"
4
+
3
5
  module Fatty
4
6
  module Curses
5
7
  class EventSource
6
- MOUSE_BSTATE_BUTTON_MAP = {
7
- ::Curses::BUTTON1_PRESSED => :left_pressed,
8
- ::Curses::BUTTON1_RELEASED => :left_released,
9
- ::Curses::BUTTON1_CLICKED => :left_clicked,
10
- ::Curses::BUTTON1_DOUBLE_CLICKED => :left_double_clicked,
11
- ::Curses::BUTTON1_TRIPLE_CLICKED => :left_triple_clicked,
12
-
13
- ::Curses::BUTTON2_PRESSED => :middle_pressed,
14
- ::Curses::BUTTON2_RELEASED => :middle_released,
15
- ::Curses::BUTTON2_CLICKED => :middle_clicked,
16
- ::Curses::BUTTON2_DOUBLE_CLICKED => :middle_double_clicked,
17
- ::Curses::BUTTON2_TRIPLE_CLICKED => :middle_triple_clicked,
18
-
19
- ::Curses::BUTTON3_PRESSED => :right_pressed,
20
- ::Curses::BUTTON3_RELEASED => :right_released,
21
- ::Curses::BUTTON3_CLICKED => :right_clicked,
22
- ::Curses::BUTTON3_DOUBLE_CLICKED => :right_double_clicked,
23
- ::Curses::BUTTON3_TRIPLE_CLICKED => :right_triple_clicked
24
- }.freeze
25
-
26
- SCROLL_UP_BSTATE = ::Curses::BUTTON4_PRESSED
8
+ MOUSE_SUPPORTED = ::Curses.respond_to?(:mousemask) && ::Curses.respond_to?(:getmouse)
9
+
10
+ MOUSE_BSTATE_BUTTON_MAP = (if MOUSE_SUPPORTED
11
+ {
12
+ ::Curses::BUTTON1_PRESSED => :left_pressed,
13
+ ::Curses::BUTTON1_RELEASED => :left_released,
14
+ ::Curses::BUTTON1_CLICKED => :left_clicked,
15
+ ::Curses::BUTTON1_DOUBLE_CLICKED => :left_double_clicked,
16
+ ::Curses::BUTTON1_TRIPLE_CLICKED => :left_triple_clicked,
17
+
18
+ ::Curses::BUTTON2_PRESSED => :middle_pressed,
19
+ ::Curses::BUTTON2_RELEASED => :middle_released,
20
+ ::Curses::BUTTON2_CLICKED => :middle_clicked,
21
+ ::Curses::BUTTON2_DOUBLE_CLICKED => :middle_double_clicked,
22
+ ::Curses::BUTTON2_TRIPLE_CLICKED => :middle_triple_clicked,
23
+
24
+ ::Curses::BUTTON3_PRESSED => :right_pressed,
25
+ ::Curses::BUTTON3_RELEASED => :right_released,
26
+ ::Curses::BUTTON3_CLICKED => :right_clicked,
27
+ ::Curses::BUTTON3_DOUBLE_CLICKED => :right_double_clicked,
28
+ ::Curses::BUTTON3_TRIPLE_CLICKED => :right_triple_clicked
29
+ }
30
+ else
31
+ {}
32
+ end).freeze
33
+
34
+ SCROLL_UP_BSTATE = MOUSE_SUPPORTED ? ::Curses::BUTTON4_PRESSED : 0
27
35
  SCROLL_DOWN_BSTATE = 0x10000000
28
- MOUSE_MODIFIER_MASK =
29
- ::Curses::BUTTON_CTRL |
30
- ::Curses::BUTTON_ALT |
31
- ::Curses::BUTTON_SHIFT
36
+ MOUSE_MODIFIER_MASK = if MOUSE_SUPPORTED
37
+ (
38
+ ::Curses::BUTTON_CTRL |
39
+ ::Curses::BUTTON_ALT |
40
+ ::Curses::BUTTON_SHIFT)
41
+ else
42
+ 0
43
+ end
32
44
 
33
45
  ESCAPE_LOOKAHEAD_MS = 0
34
46
 
@@ -42,6 +54,7 @@ module Fatty
42
54
  @key_decoder = key_decoder || KeyDecoder.new
43
55
  @poll_ms = poll_ms.to_i
44
56
  configure_input_polling!
57
+ configure_key_modifiers!
45
58
  end
46
59
 
47
60
  def next_event
@@ -72,6 +85,7 @@ module Fatty
72
85
  # operation instead of decoding them as individual key events.
73
86
  Command.session(:active, :terminal_paste, text: raw.last)
74
87
  elsif (ev = @key_decoder.decode(raw))
88
+ ev = apply_key_modifiers(ev, raw)
75
89
  if ev.key == :resize
76
90
  Command.terminal(:resize)
77
91
  else
@@ -109,6 +123,51 @@ module Fatty
109
123
  win.timeout = @poll_ms if win.respond_to?(:timeout=)
110
124
  end
111
125
 
126
+ # PDCurses chooses one modified keycode even when several modifiers are
127
+ # held. Its separate flags retain the full chord for the last getch.
128
+ def configure_key_modifiers!
129
+ @pdc_key_modifiers = ::Curses::VERSION.start_with?("PDCurses") &&
130
+ ::Curses.respond_to?(:save_key_modifiers) &&
131
+ ::Curses.respond_to?(:get_key_modifiers)
132
+ ::Curses.save_key_modifiers(true) if @pdc_key_modifiers
133
+ end
134
+
135
+ def apply_key_modifiers(event, raw)
136
+ return event unless @pdc_key_modifiers
137
+
138
+ if (raw.is_a?(String) || raw.is_a?(Integer)) && event.text&.match?(/\A[[:print:]]\z/)
139
+ return apply_printable_modifiers(event)
140
+ end
141
+ return event unless raw.is_a?(Integer)
142
+
143
+ character_chord = event.key.to_s.length == 1 && (event.ctrl? || event.meta?)
144
+ special_key = event.key.to_s.match?(
145
+ /\A(?:up|down|left|right|home|end|page_up|page_down|insert|delete|backspace|keypad_\w+|f\d+)\z/,
146
+ )
147
+ return event unless character_chord || special_key
148
+
149
+ flags = ::Curses.get_key_modifiers
150
+ KeyEvent.new(
151
+ key: event.key,
152
+ text: event.text,
153
+ raw: event.raw || raw,
154
+ ctrl: event.ctrl? || (flags & ::Curses::PDC_KEY_MODIFIER_CONTROL).positive?,
155
+ meta: event.meta? || (flags & ::Curses::PDC_KEY_MODIFIER_ALT).positive?,
156
+ shift: event.shift? || (flags & ::Curses::PDC_KEY_MODIFIER_SHIFT).positive?,
157
+ )
158
+ end
159
+
160
+ def apply_printable_modifiers(event)
161
+ flags = ::Curses.get_key_modifiers
162
+ ctrl = (flags & ::Curses::PDC_KEY_MODIFIER_CONTROL).positive?
163
+ alt = (flags & ::Curses::PDC_KEY_MODIFIER_ALT).positive?
164
+ # Shift is already represented by the character (~ rather than `).
165
+ return event unless ctrl || alt
166
+ return event if ctrl && alt && WindowsKeyboard.altgr_character?(event.text)
167
+
168
+ KeyEvent.new(key: event.key, raw: event.raw, ctrl: ctrl, meta: alt, shift: event.shift?)
169
+ end
170
+
112
171
  def read_raw(timeout_ms: @poll_ms)
113
172
  return unless window
114
173
 
@@ -123,7 +182,7 @@ module Fatty
123
182
 
124
183
  log_keycode(ch)
125
184
 
126
- if ch.is_a?(Integer) && ch == ::Curses::KEY_MOUSE
185
+ if MOUSE_SUPPORTED && ch.is_a?(Integer) && ch == ::Curses::KEY_MOUSE
127
186
  mouse = ::Curses.getmouse
128
187
  Fatty.debug("EventSource#read_raw: bstate=#{mouse&.bstate}", tag: :mouse)
129
188
  return decode_mouse(mouse)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "pdcurses_keys"
4
+
3
5
  module Fatty
4
6
  module Curses
5
7
  # The KeyDecoder class is responsible for converting raw key events returned
@@ -9,10 +11,24 @@ module Fatty
9
11
  # :ctrl, and :meta, in the KeyEvent so that the KeyMap can assign different
10
12
  # actions to the modified keys.
11
13
  class KeyDecoder
12
- CSI_CURSOR_KEYS = { 'A' => :up, 'B' => :down, 'C' => :right, 'D' => :left,
13
- 'H' => :home, 'F' => :end }.freeze
14
- CSI_TILDE_KEYS = { '1' => :home, '2' => :insert, '3' => :delete, '4' => :end,
15
- '5' => :page_up, '6' => :page_down, '7' => :home, '8' => :end }.freeze
14
+ CSI_CURSOR_KEYS = {
15
+ 'A' => :up,
16
+ 'B' => :down,
17
+ 'C' => :right,
18
+ 'D' => :left,
19
+ 'H' => :home,
20
+ 'F' => :end
21
+ }.freeze
22
+ CSI_TILDE_KEYS = {
23
+ '1' => :home,
24
+ '2' => :insert,
25
+ '3' => :delete,
26
+ '4' => :end,
27
+ '5' => :page_up,
28
+ '6' => :page_down,
29
+ '7' => :home,
30
+ '8' => :end
31
+ }.freeze
16
32
  SS3_TO_EVENT = {
17
33
  "j" => KeyEvent.new(key: :keypad_multiply),
18
34
  "k" => KeyEvent.new(key: :keypad_plus),
@@ -40,6 +56,7 @@ module Fatty
40
56
  @map = {}
41
57
  @sequence_map = {}
42
58
  load_builtin_map
59
+ load_pdcurses_map
43
60
  load_user_config
44
61
  end
45
62
 
@@ -84,9 +101,13 @@ module Fatty
84
101
  modifier = (modifier || '1').to_i
85
102
  if key && modifier.between?(1, 16)
86
103
  bits = modifier - 1
87
- KeyEvent.new(key: key, raw: raw,
88
- shift: (bits & 1).positive?, meta: (bits & 10).positive?,
89
- ctrl: (bits & 4).positive?)
104
+ KeyEvent.new(
105
+ key: key,
106
+ raw: raw,
107
+ shift: (bits & 1).positive?,
108
+ meta: (bits & 10).positive?,
109
+ ctrl: (bits & 4).positive?,
110
+ )
90
111
  else
91
112
  # Preserve one nonprinting unknown event for the normal key alert.
92
113
  KeyEvent.new(key: "\e[#{raw.last}".inspect, raw: raw)
@@ -156,6 +177,8 @@ module Fatty
156
177
 
157
178
  # Decode a raw input returned as a single character rather than an Array.
158
179
  def decode_single(ch)
180
+ ch = 8 if ch == "\b" # Some curses builds return BS as a string.
181
+
159
182
  if ch.is_a?(Integer) && @map.key?(ch)
160
183
  Fatty.debug("#{self.class}#decode_single: #{ch} found in @map -> #{@map[ch]}", tag: :keycode)
161
184
  ev = @map[ch]
@@ -224,7 +247,7 @@ module Fatty
224
247
  # different terminal programs process keys.
225
248
  def load_user_config
226
249
  config = Fatty::Config.keydefs
227
- return nil unless config
250
+ return unless config
228
251
 
229
252
  terminal = @env[:terminal]
230
253
  Fatty.info("KeyDecoder#load_user_config: detected terminal `#{terminal}`")
@@ -248,22 +271,24 @@ module Fatty
248
271
  return
249
272
  end
250
273
 
274
+ paste_delimiters = ["\e[200~", "\e[201~"]
275
+ modifiers = [:ctrl, :meta, :shift]
276
+ allowed_keys = [:key, *modifiers]
251
277
  definitions.each do |label, value|
252
- begin
253
- spec = normalize_spec(value)
254
- sequence = spec.delete(:sequence)
255
- unless sequence.is_a?(String) && sequence.match?(/\A\e\[[0-?]*[ -\/]*[@-~]\z/) &&
256
- sequence.bytesize <= 66 && !["\e[200~", "\e[201~"].include?(sequence)
257
- raise ArgumentError, "sequence must be a complete CSI key sequence (not a paste delimiter)"
258
- end
259
- unless spec[:key].is_a?(Symbol) && (spec.keys - [:key, :ctrl, :meta, :shift]).empty? &&
260
- [:ctrl, :meta, :shift].all? { |flag| !spec.key?(flag) || [true, false].include?(spec[flag]) }
261
- raise ArgumentError, "supply a key name and optional boolean ctrl/meta/shift modifiers"
262
- end
263
- @sequence_map[sequence] = spec
264
- rescue ArgumentError => e
265
- Fatty.warn("Ignoring keydefs sequence #{label.inspect}: #{e.message}", tag: :keycode)
278
+ spec = normalize_spec(value)
279
+ sequence = spec.delete(:sequence)
280
+ unless sequence.is_a?(String) && sequence.match?(%r{\A\e\[[0-?]*[ -/]*[@-~]\z}) &&
281
+ sequence.bytesize <= 66 && !paste_delimiters.include?(sequence)
282
+ raise ArgumentError, "sequence must be a complete CSI key sequence (not a paste delimiter)"
266
283
  end
284
+ unless spec[:key].is_a?(Symbol) && (spec.keys - allowed_keys).empty? &&
285
+ modifiers.all? { |flag| !spec.key?(flag) || spec[flag] == true || spec[flag] == false }
286
+ raise ArgumentError, "supply a key name and optional boolean ctrl/meta/shift modifiers"
287
+ end
288
+
289
+ @sequence_map[sequence] = spec
290
+ rescue ArgumentError => e
291
+ Fatty.warn("Ignoring keydefs sequence #{label.inspect}: #{e.message}", tag: :keycode)
267
292
  end
268
293
  end
269
294
 
@@ -294,6 +319,13 @@ module Fatty
294
319
  end
295
320
  end
296
321
 
322
+ # Derive the wide/narrow key offset from a public curses constant.
323
+ def load_pdcurses_map
324
+ return unless ::Curses::VERSION.start_with?("PDCurses")
325
+
326
+ @map.merge!(PDCursesKeys.events(::Curses::KEY_BACKSPACE - 0x07))
327
+ end
328
+
297
329
  # Make sure the user config Hash uses symbols for its keys. Return the
298
330
  # Hash as so corrected.
299
331
  def normalize_spec(value)
@@ -105,6 +105,8 @@ module Fatty
105
105
  }.freeze
106
106
 
107
107
  def self.ensure!
108
+ return unless ::Curses.respond_to?(:mousemask) && ::Curses.respond_to?(:getmouse)
109
+
108
110
  CONSTANTS.each do |name, value|
109
111
  next if ::Curses.const_defined?(name)
110
112