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
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fatty
4
+ module Curses
5
+ # Native modified-key codes from PDCursesMod curses.h and wincon/pdckbd.c.
6
+ # Ruby curses omits most of these constants. Offsets work for both narrow
7
+ # and Unicode builds; the decoder installs them only on PDCurses, before
8
+ # user overrides. Do not infer extra modifiers the backend did not report.
9
+ module PDCursesKeys
10
+ OFFSETS = {
11
+ 0xca => { key: :keypad_divide, text: "/" }, # PADSLASH
12
+ 0xcb => { key: :keypad_enter }, # PADENTER
13
+ 0xce => { key: :keypad_decimal, text: "." }, # PADSTOP
14
+ 0xcf => { key: :keypad_multiply, text: "*" }, # PADSTAR
15
+ 0xd0 => { key: :keypad_minus, text: "-" }, # PADMINUS
16
+ 0xd1 => { key: :keypad_plus, text: "+" }, # PADPLUS
17
+ 0xfa => { key: :keypad_0, text: "0" }, # PAD0
18
+ 0x97 => { key: :'0', meta: true }, # ALT_0
19
+ 0x98 => { key: :'1', meta: true }, # ALT_1
20
+ 0x99 => { key: :'2', meta: true }, # ALT_2
21
+ 0x9a => { key: :'3', meta: true }, # ALT_3
22
+ 0x9b => { key: :'4', meta: true }, # ALT_4
23
+ 0x9c => { key: :'5', meta: true }, # ALT_5
24
+ 0x9d => { key: :'6', meta: true }, # ALT_6
25
+ 0x9e => { key: :'7', meta: true }, # ALT_7
26
+ 0x9f => { key: :'8', meta: true }, # ALT_8
27
+ 0xa0 => { key: :'9', meta: true }, # ALT_9
28
+ 0xa1 => { key: :a, meta: true }, # ALT_A
29
+ 0xa2 => { key: :b, meta: true }, # ALT_B
30
+ 0xa3 => { key: :c, meta: true }, # ALT_C
31
+ 0xa4 => { key: :d, meta: true }, # ALT_D
32
+ 0xa5 => { key: :e, meta: true }, # ALT_E
33
+ 0xa6 => { key: :f, meta: true }, # ALT_F
34
+ 0xa7 => { key: :g, meta: true }, # ALT_G
35
+ 0xa8 => { key: :h, meta: true }, # ALT_H
36
+ 0xa9 => { key: :i, meta: true }, # ALT_I
37
+ 0xaa => { key: :j, meta: true }, # ALT_J
38
+ 0xab => { key: :k, meta: true }, # ALT_K
39
+ 0xac => { key: :l, meta: true }, # ALT_L
40
+ 0xad => { key: :m, meta: true }, # ALT_M
41
+ 0xae => { key: :n, meta: true }, # ALT_N
42
+ 0xaf => { key: :o, meta: true }, # ALT_O
43
+ 0xb0 => { key: :p, meta: true }, # ALT_P
44
+ 0xb1 => { key: :q, meta: true }, # ALT_Q
45
+ 0xb2 => { key: :r, meta: true }, # ALT_R
46
+ 0xb3 => { key: :s, meta: true }, # ALT_S
47
+ 0xb4 => { key: :t, meta: true }, # ALT_T
48
+ 0xb5 => { key: :u, meta: true }, # ALT_U
49
+ 0xb6 => { key: :v, meta: true }, # ALT_V
50
+ 0xb7 => { key: :w, meta: true }, # ALT_W
51
+ 0xb8 => { key: :x, meta: true }, # ALT_X
52
+ 0xb9 => { key: :y, meta: true }, # ALT_Y
53
+ 0xba => { key: :z, meta: true }, # ALT_Z
54
+ 0xcd => { key: :keypad_enter, meta: true }, # ALT_PADENTER
55
+ 0xd8 => { key: :keypad_plus, meta: true }, # ALT_PADPLUS
56
+ 0xd9 => { key: :keypad_minus, meta: true }, # ALT_PADMINUS
57
+ 0xda => { key: :keypad_divide, meta: true }, # ALT_PADSLASH
58
+ 0xdb => { key: :keypad_multiply, meta: true }, # ALT_PADSTAR
59
+ 0xdc => { key: :keypad_decimal, meta: true }, # ALT_PADSTOP
60
+ 0xde => { key: :delete, meta: true }, # ALT_DEL
61
+ 0xdf => { key: :insert, meta: true }, # ALT_INS
62
+ 0xe3 => { key: :tab, meta: true }, # ALT_TAB
63
+ 0xe4 => { key: "-".to_sym, meta: true }, # ALT_MINUS
64
+ 0xe5 => { key: "=".to_sym, meta: true }, # ALT_EQUAL
65
+ 0xe6 => { key: :home, meta: true }, # ALT_HOME
66
+ 0xe7 => { key: :page_up, meta: true }, # ALT_PGUP
67
+ 0xe8 => { key: :page_down, meta: true }, # ALT_PGDN
68
+ 0xe9 => { key: :end, meta: true }, # ALT_END
69
+ 0xea => { key: :up, meta: true }, # ALT_UP
70
+ 0xeb => { key: :down, meta: true }, # ALT_DOWN
71
+ 0xec => { key: :right, meta: true }, # ALT_RIGHT
72
+ 0xed => { key: :left, meta: true }, # ALT_LEFT
73
+ 0xee => { key: :enter, meta: true }, # ALT_ENTER
74
+ 0xef => { key: :escape, meta: true }, # ALT_ESC
75
+ 0xf0 => { key: "`".to_sym, meta: true }, # ALT_BQUOTE
76
+ 0xf1 => { key: "[".to_sym, meta: true }, # ALT_LBRACKET
77
+ 0xf2 => { key: "]".to_sym, meta: true }, # ALT_RBRACKET
78
+ 0xf3 => { key: ";".to_sym, meta: true }, # ALT_SEMICOLON
79
+ 0xf4 => { key: "'".to_sym, meta: true }, # ALT_FQUOTE
80
+ 0xf5 => { key: ",".to_sym, meta: true }, # ALT_COMMA
81
+ 0xf6 => { key: ".".to_sym, meta: true }, # ALT_STOP
82
+ 0xf7 => { key: "/".to_sym, meta: true }, # ALT_FSLASH
83
+ 0xf8 => { key: :backspace, meta: true }, # ALT_BKSP
84
+ 0x105 => { key: :keypad_0, meta: true }, # ALT_PAD0
85
+ 0x106 => { key: :keypad_1, meta: true }, # ALT_PAD1
86
+ 0x107 => { key: :keypad_2, meta: true }, # ALT_PAD2
87
+ 0x108 => { key: :keypad_3, meta: true }, # ALT_PAD3
88
+ 0x109 => { key: :keypad_4, meta: true }, # ALT_PAD4
89
+ 0x10a => { key: :keypad_5, meta: true }, # ALT_PAD5
90
+ 0x10b => { key: :keypad_6, meta: true }, # ALT_PAD6
91
+ 0x10c => { key: :keypad_7, meta: true }, # ALT_PAD7
92
+ 0x10d => { key: :keypad_8, meta: true }, # ALT_PAD8
93
+ 0x10e => { key: :keypad_9, meta: true }, # ALT_PAD9
94
+ 0x110 => { key: "\\".to_sym, meta: true }, # ALT_BSLASH
95
+ 0xbb => { key: :left, ctrl: true }, # CTL_LEFT
96
+ 0xbc => { key: :right, ctrl: true }, # CTL_RIGHT
97
+ 0xbd => { key: :page_up, ctrl: true }, # CTL_PGUP
98
+ 0xbe => { key: :page_down, ctrl: true }, # CTL_PGDN
99
+ 0xbf => { key: :home, ctrl: true }, # CTL_HOME
100
+ 0xc0 => { key: :end, ctrl: true }, # CTL_END
101
+ 0xcc => { key: :keypad_enter, ctrl: true }, # CTL_PADENTER
102
+ 0xd2 => { key: :keypad_decimal, ctrl: true }, # CTL_PADSTOP
103
+ 0xd3 => { key: :keypad_5, ctrl: true }, # CTL_PADCENTER
104
+ 0xd4 => { key: :keypad_plus, ctrl: true }, # CTL_PADPLUS
105
+ 0xd5 => { key: :keypad_minus, ctrl: true }, # CTL_PADMINUS
106
+ 0xd6 => { key: :keypad_divide, ctrl: true }, # CTL_PADSLASH
107
+ 0xd7 => { key: :keypad_multiply, ctrl: true }, # CTL_PADSTAR
108
+ 0xdd => { key: :insert, ctrl: true }, # CTL_INS
109
+ 0xe0 => { key: :up, ctrl: true }, # CTL_UP
110
+ 0xe1 => { key: :down, ctrl: true }, # CTL_DOWN
111
+ 0xe2 => { key: :tab, ctrl: true }, # CTL_TAB
112
+ 0xf9 => { key: :backspace, ctrl: true }, # CTL_BKSP
113
+ 0xfb => { key: :keypad_0, ctrl: true }, # CTL_PAD0
114
+ 0xfc => { key: :keypad_1, ctrl: true }, # CTL_PAD1
115
+ 0xfd => { key: :keypad_2, ctrl: true }, # CTL_PAD2
116
+ 0xfe => { key: :keypad_3, ctrl: true }, # CTL_PAD3
117
+ 0xff => { key: :keypad_4, ctrl: true }, # CTL_PAD4
118
+ 0x100 => { key: :keypad_5, ctrl: true }, # CTL_PAD5
119
+ 0x101 => { key: :keypad_6, ctrl: true }, # CTL_PAD6
120
+ 0x102 => { key: :keypad_7, ctrl: true }, # CTL_PAD7
121
+ 0x103 => { key: :keypad_8, ctrl: true }, # CTL_PAD8
122
+ 0x104 => { key: :keypad_9, ctrl: true }, # CTL_PAD9
123
+ 0x10f => { key: :delete, ctrl: true }, # CTL_DEL
124
+ 0x111 => { key: :enter, ctrl: true }, # CTL_ENTER
125
+ 0x112 => { key: :keypad_enter, shift: true }, # SHF_PADENTER
126
+ 0x113 => { key: :keypad_divide, shift: true }, # SHF_PADSLASH
127
+ 0x114 => { key: :keypad_multiply, shift: true }, # SHF_PADSTAR
128
+ 0x115 => { key: :keypad_plus, shift: true }, # SHF_PADPLUS
129
+ 0x116 => { key: :keypad_minus, shift: true }, # SHF_PADMINUS
130
+ 0x117 => { key: :up, shift: true }, # SHF_UP
131
+ 0x118 => { key: :down, shift: true }, # SHF_DOWN
132
+ 0x119 => { key: :insert, shift: true }, # SHF_IC
133
+ 0x11a => { key: :delete, shift: true }, # SHF_DC
134
+ 0x123 => { key: :up, shift: true }, # KEY_SUP
135
+ 0x124 => { key: :down, shift: true }, # KEY_SDOWN
136
+ }.transform_values(&:freeze).freeze
137
+
138
+ def self.events(offset)
139
+ events = OFFSETS.to_h do |relative, spec|
140
+ code = offset + relative
141
+ [code, KeyEvent.new(**spec, raw: code)]
142
+ end
143
+ # The Windows backend encodes Shift/Ctrl/Alt F1-F12 as F13-F48.
144
+ { shift: 12, ctrl: 24, meta: 36 }.each do |modifier, base|
145
+ (1..12).each do |number|
146
+ code = offset + 0x08 + base + number
147
+ events[code] = KeyEvent.new(key: :"f#{number}", **{ modifier => true }, raw: code)
148
+ end
149
+ end
150
+ events
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fiddle"
4
+
5
+ module Fatty
6
+ module Curses
7
+ # PDCurses collapses left/right Alt into one flag. Consult the keyboard
8
+ # layout to preserve characters that actually require Ctrl+Alt (AltGr).
9
+ module WindowsKeyboard
10
+ def self.altgr_character?(text)
11
+ return true unless Gem.win_platform?
12
+ return true unless text.length == 1 && text.ord <= 0xffff
13
+
14
+ mapping = key_scan.call(text.ord)
15
+ # Unknown/composed characters stay text rather than becoming shortcuts.
16
+ mapping == -1 || ((mapping >> 8) & 6) == 6
17
+ rescue Fiddle::DLError
18
+ true
19
+ end
20
+
21
+ def self.key_scan
22
+ @key_scan ||= begin
23
+ library = Fiddle.dlopen("user32.dll")
24
+ abi = if Fiddle::Function.const_defined?(:STDCALL)
25
+ Fiddle::Function::STDCALL
26
+ else
27
+ Fiddle::Function::DEFAULT
28
+ end
29
+ Fiddle::Function.new(library["VkKeyScanW"], [Fiddle::TYPE_SHORT], Fiddle::TYPE_SHORT, abi)
30
+ end
31
+ end
32
+ private_class_method :key_scan
33
+ end
34
+ end
35
+ end
data/lib/fatty/env.rb CHANGED
@@ -20,6 +20,18 @@ module Fatty
20
20
  }
21
21
  end
22
22
 
23
+ def truecolor_detected?
24
+ colorterm = ENV["COLORTERM"].to_s
25
+ term = ENV["TERM"].to_s
26
+ term_program = ENV["TERM_PROGRAM"].to_s
27
+
28
+ (!ENV["WT_SESSION"].to_s.empty? && !tmux? && !screen?) ||
29
+ colorterm.match?(/truecolor|24bit/i) ||
30
+ term.match?(/truecolor|24bit|direct/i) ||
31
+ term.match?(/kitty|wezterm|alacritty|ghostty|foot/i) ||
32
+ term_program.match?(/kitty|wezterm|alacritty|ghostty|iTerm/i)
33
+ end
34
+
23
35
  private
24
36
 
25
37
  # -----------------------
@@ -38,7 +50,7 @@ module Fatty
38
50
 
39
51
  def arch
40
52
  case RUBY_PLATFORM
41
- when /x86_64/ then :x86_64
53
+ when /x86_64|x64|amd64/i then :x86_64
42
54
  when /arm64|aarch64/ then :arm64
43
55
  else :unknown
44
56
  end
@@ -56,6 +68,7 @@ module Fatty
56
68
  def detect_terminal_program
57
69
  return "tmux" if ENV.key?("TMUX")
58
70
  return "screen" if ENV.key?("STY")
71
+ return "windows-terminal" unless ENV["WT_SESSION"].to_s.empty?
59
72
  return "konsole" if ENV.key?("KONSOLE_VERSION")
60
73
  return "kitty" if ENV.key?("KITTY_WINDOW_ID")
61
74
  return "wezterm" if ENV["TERM_PROGRAM"]&.downcase == "wezterm"
@@ -75,17 +88,6 @@ module Fatty
75
88
  end
76
89
  end
77
90
 
78
- def self.truecolor_detected?
79
- colorterm = ENV["COLORTERM"].to_s
80
- term = ENV["TERM"].to_s
81
- term_program = ENV["TERM_PROGRAM"].to_s
82
-
83
- colorterm.match?(/truecolor|24bit/i) ||
84
- term.match?(/truecolor|24bit|direct/i) ||
85
- term.match?(/kitty|wezterm|alacritty|ghostty|foot/i) ||
86
- term_program.match?(/kitty|wezterm|alacritty|ghostty|iTerm/i)
87
- end
88
-
89
91
  def tmux?
90
92
  ENV.key?("TMUX")
91
93
  end
data/lib/fatty/help.rb CHANGED
@@ -54,19 +54,28 @@ module Fatty
54
54
  end
55
55
 
56
56
  def self.keybindings(keymap, width: 83)
57
- lines = ["# Current keybindings", "",
58
- "Fatty is the terminal user-interface library used by this application. " \
59
- "It provides command-line editing, output paging and search, and interactive prompts and menus.", "",
60
- "Use the pager to browse or search; quit paging to return.", "",
61
- "The tables below show the loaded keybindings for each context, including user overrides. " \
62
- "Keys use Emacs-like notation: C- means hold Control, M- means hold Alt (also called Meta), " \
63
- "and C-M- means hold both. Shift may be indicated by the final key name, as in M-R, " \
64
- "or by an explicit S- prefix, as in M-S-r. Both examples mean Alt-Shift-R. " \
65
- "Printable characters insert text in text-entry contexts.", "",
66
- "The Action column gives the name of the Fatty action bound to each key. " \
67
- "These names are useful when customizing keybindings or working on Fatty itself."]
57
+ lines = [
58
+ "# Current keybindings",
59
+ "",
60
+ "Fatty is the terminal user-interface library used by this application. " \
61
+ "It provides command-line editing, output paging and search, and interactive prompts and menus.",
62
+ "",
63
+ "Use the pager to browse or search; quit paging to return.",
64
+ "",
65
+ "The tables below show the loaded keybindings for each context, including user overrides. " \
66
+ "Keys use Emacs-like notation: C- means hold Control, M- means hold Alt (also called Meta), " \
67
+ "and C-M- means hold both. Shift may be indicated by the final key name, as in M-R, " \
68
+ "or by an explicit S- prefix, as in M-S-r. Both examples mean Alt-Shift-R. " \
69
+ "Printable characters insert text in text-entry contexts.",
70
+ "",
71
+ "The Action column gives the name of the Fatty action bound to each key. " \
72
+ "These names are useful when customizing keybindings or working on Fatty itself."
73
+ ]
68
74
  key_width, action_width, description_width = keybinding_column_widths(width)
69
- keymap.bindings.sort_by { |context, _| [[:text, :input].index(context) || 2, context.to_s] }.each do |context, bindings|
75
+ first_contexts = [:text, :input]
76
+ keymap.bindings.sort_by { |context, _|
77
+ [first_contexts.index(context) || 2, context.to_s]
78
+ }.each do |context, bindings|
70
79
  description = CONTEXT_DESCRIPTIONS.fetch(context) do
71
80
  "This context is supplied by the application or a custom keymap. Its bindings " \
72
81
  "apply when the application activates it; the application determines how " \
@@ -87,7 +96,8 @@ module Fatty
87
96
  actions = wrap_cell(action, action_width)
88
97
  descriptions = wrap_cell(description, description_width)
89
98
  [keys.length, actions.length, descriptions.length].max.times do |index|
90
- lines << "| #{escape_cell(keys[index])} | #{escape_cell(actions[index])} | #{escape_cell(descriptions[index])} |"
99
+ lines << "| #{escape_cell(keys[index])} | #{escape_cell(actions[index])} | " \
100
+ "#{escape_cell(descriptions[index])} |"
91
101
  end
92
102
  end
93
103
  end
@@ -96,8 +106,8 @@ module Fatty
96
106
 
97
107
  def self.keybinding_column_widths(width)
98
108
  width = [width, 83].min
99
- key_width = [[width / 6, 12].min, 3].max
100
- action_width = [[width - 12 - key_width - 11, 27].min, 6].max
109
+ key_width = (width / 6).clamp(3, 12)
110
+ action_width = (width - 12 - key_width - 11).clamp(6, 27)
101
111
  [key_width, action_width, [width - 12 - key_width - action_width, 11].max]
102
112
  end
103
113
 
@@ -109,9 +119,7 @@ module Fatty
109
119
  lines << line
110
120
  line = +""
111
121
  end
112
- while word.length > width
113
- lines << word.slice!(0, width)
114
- end
122
+ lines << word.slice!(0, width) while word.length > width
115
123
  line << " " unless line.empty?
116
124
  line << word
117
125
  end
@@ -193,7 +193,10 @@ module Fatty
193
193
  end
194
194
 
195
195
  def popup_completion_query
196
- path_completion_prefix || buffer.completion_prefix
196
+ prefix = path_completion_prefix
197
+ return buffer.completion_prefix unless prefix
198
+
199
+ Gem.win_platform? ? unescape_path(prefix).tr("/", "\\") : prefix
197
200
  end
198
201
 
199
202
  def popup_completion_path?
@@ -305,7 +308,7 @@ module Fatty
305
308
  (history_autosuggestions + completion_suggestions)
306
309
  .compact
307
310
  .map(&:to_s)
308
- .select { |candidate| candidate.start_with?(base) }
311
+ .select { |candidate| completion_extends?(candidate, base) }
309
312
  .reject { |candidate| candidate == buffer.text.to_s }
310
313
  .uniq
311
314
  end
@@ -365,6 +368,8 @@ module Fatty
365
368
  s = prefix.to_s
366
369
  return false if s.empty?
367
370
 
371
+ return true if Gem.win_platform? && s.match?(/\A[A-Za-z]:/)
372
+
368
373
  s.start_with?("/", "~/", "./", "../") || s.include?("/")
369
374
  end
370
375
 
@@ -391,7 +396,8 @@ module Fatty
391
396
  until scanner.eos?
392
397
  next if scanner.scan(/\s+/)
393
398
 
394
- if (token = scanner.scan(/(?:\\.|[^\s\\])+/))
399
+ token_pattern = Gem.win_platform? ? /"[^"]*(?:"|\z)|'[^']*(?:'|\z)|[^\s]+/ : /(?:\\.|[^\s\\])+/
400
+ if (token = scanner.scan(token_pattern))
395
401
  last_token = [scanner.pos - token.length, scanner.pos]
396
402
  else
397
403
  scanner.getch
@@ -410,6 +416,7 @@ module Fatty
410
416
 
411
417
  def rendered_path_candidates(prefix)
412
418
  raw_prefix = unescape_path(prefix)
419
+ raw_prefix += "/" if Gem.win_platform? && raw_prefix.match?(/\A[A-Za-z]:\z/)
413
420
  expanded = expand_path_prefix(raw_prefix)
414
421
  return [] if expanded.nil? || expanded.empty?
415
422
 
@@ -432,18 +439,19 @@ module Fatty
432
439
  # files, but not if the prefix starts with '.' or '#', then treat hidden
433
440
  # files normally.
434
441
  entries =
435
- Dir.children(dir_part)
436
- .select { |name| name.start_with?(base_part) }
442
+ Dir.children(dir_part).select { |name|
443
+ Gem.win_platform? ? name.downcase.start_with?(base_part.downcase) : name.start_with?(base_part)
444
+ }
437
445
  .sort_by do |name|
438
- full = File.join(dir_part, name)
439
- hide_penalty =
440
- if base_part.match?(/\A[.#]/)
441
- 0
442
- else
443
- name.match?(/\A[.#]/) ? 1 : 0
444
- end
445
- file_penalty = File.directory?(full) ? 0 : 1
446
- [hide_penalty, file_penalty, name]
446
+ full = File.join(dir_part, name)
447
+ hide_penalty =
448
+ if base_part.match?(/\A[.#]/)
449
+ 0
450
+ else
451
+ name.match?(/\A[.#]/) ? 1 : 0
452
+ end
453
+ file_penalty = File.directory?(full) ? 0 : 1
454
+ [hide_penalty, file_penalty, name]
447
455
  end
448
456
 
449
457
  return [] if entries.empty?
@@ -471,13 +479,32 @@ module Fatty
471
479
  rendered += "/" if File.directory?(full) && !rendered.end_with?("/")
472
480
  escape_path(rendered)
473
481
  end
482
+ rescue SystemCallError
483
+ # Completion is opportunistic: permissions, removed directories, and
484
+ # unavailable drives must not terminate input or automatic suggestions.
485
+ []
474
486
  end
475
487
 
476
488
  def escape_path(path)
489
+ if Gem.win_platform?
490
+ path = path.tr("/", "\\")
491
+ return path.match?(/[\s&()<>|]/) ? %{"#{path}"} : path
492
+ end
493
+
477
494
  path.gsub(/([ \t\n\\'"`$!#&()*;<>?\[\]\{\}|])/) { "\\#{$1}" }
478
495
  end
479
496
 
480
497
  def unescape_path(path)
498
+ if Gem.win_platform?
499
+ text = path.to_s
500
+ if text.start_with?('"', "'")
501
+ quote = text[0]
502
+ text = text[1..]
503
+ text = text.delete_suffix(quote)
504
+ end
505
+ return text.tr("\\", "/")
506
+ end
507
+
481
508
  path.to_s.gsub(/\\(.)/, '\1')
482
509
  end
483
510
 
@@ -509,11 +536,21 @@ module Fatty
509
536
  "#{before}#{replacement}#{after}"
510
537
  end
511
538
 
539
+ # Windows path candidates use forward slashes and filesystem casing.
540
+ # Comparing literally hides the cycling preview for a typed backslash.
541
+ def completion_extends?(candidate, prefix)
542
+ if Gem.win_platform? && path_completion_range
543
+ candidate.tr("\\", "/").downcase.start_with?(prefix.tr("\\", "/").downcase)
544
+ else
545
+ candidate.start_with?(prefix)
546
+ end
547
+ end
548
+
512
549
  def autosuggestion_visible?
513
550
  suggestion = autosuggestion.to_s
514
551
  text = buffer.text.to_s
515
552
  return false if suggestion.empty?
516
- return false unless suggestion.start_with?(text)
553
+ return false unless completion_extends?(suggestion, text)
517
554
 
518
555
  suggestion != text
519
556
  end
@@ -522,10 +559,10 @@ module Fatty
522
559
  suggestion = autosuggestion.to_s
523
560
  text = buffer.text.to_s
524
561
  return "" if suggestion.empty?
525
- return "" unless suggestion.start_with?(text)
562
+ return "" unless completion_extends?(suggestion, text)
526
563
  return "" if suggestion == text
527
564
 
528
- suggestion.delete_prefix(text)
565
+ suggestion[text.length..]
529
566
  end
530
567
 
531
568
  def accept_autosuggestion!
@@ -540,6 +577,7 @@ module Fatty
540
577
  return if prefix.nil? || prefix.empty?
541
578
 
542
579
  raw_prefix = unescape_path(prefix)
580
+ raw_prefix += "/" if Gem.win_platform? && raw_prefix.match?(/\A[A-Za-z]:\z/)
543
581
  expanded = expand_path_prefix(raw_prefix)
544
582
  return prefix unless File.directory?(expanded)
545
583
 
@@ -8,10 +8,7 @@ module Fatty
8
8
  # known curses key codes to KeyEvents. These can be overriden for different
9
9
  # terminals in Fatty::Config.keydefs.
10
10
  class KeyEvent
11
- CTRL_CODE_TO_LETTER = (1..26).each_with_object({}) { |n, h|
12
- # 1->:a, 2->:b, ... 21->:u, 23->:w
13
- h[n] = (96 + n).chr.to_sym
14
- }.freeze
11
+ CTRL_CODE_TO_LETTER = (1..26).to_h { |n| [n, (96 + n).chr.to_sym] }.freeze
15
12
 
16
13
  CTRL_PUNCT = {
17
14
  27 => :'[', # ESC is also C-[
@@ -18,8 +18,8 @@ module Fatty
18
18
  map.bind(key: :right, ctrl: true, action: :move_word_right)
19
19
  map.bind(key: :left, ctrl: true, action: :move_word_left)
20
20
 
21
- map.bind(key: :t, ctrl: true, context: :input, action: :transpose_chars)
22
- map.bind(key: :t, meta: true, context: :input, action: :transpose_words)
21
+ map.bind(key: :t, ctrl: true, context: :input, action: :transpose_chars)
22
+ map.bind(key: :t, meta: true, context: :input, action: :transpose_words)
23
23
 
24
24
  map.bind(key: :a, ctrl: true, action: :bol)
25
25
  map.bind(key: :e, ctrl: true, action: :eol)
@@ -30,6 +30,9 @@ module Fatty
30
30
  map.bind(key: :delete, action: :delete_char_forward)
31
31
  map.bind(key: :d, ctrl: true, action: :delete_char_forward)
32
32
  map.bind(key: :backspace, action: :delete_char_backward)
33
+ # Some terminals (including Windows) send BS, indistinguishable from C-h.
34
+ map.bind(key: :h, ctrl: true, action: :delete_char_backward)
35
+ map.bind(key: :h, ctrl: true, meta: true, action: :kill_word_backward)
33
36
  map.bind(key: :backspace, meta: true, action: :kill_word_backward)
34
37
  map.bind(key: :w, ctrl: true, action: :kill_word_backward)
35
38
  map.bind(key: :d, meta: true, action: :kill_word_forward)
@@ -74,11 +77,14 @@ module Fatty
74
77
  map.bind(context: :input, key: :tab, shift: true, action: :complete_previous)
75
78
  map.bind(context: :input, key: :tab, meta: true, action: :completion_popup)
76
79
 
80
+ map.bind(context: :input, key: :f2, action: :completion_popup)
81
+
77
82
  # Popup
78
83
  map.bind(context: :popup, key: :c, ctrl: true, action: :popup_cancel)
79
84
  map.bind(context: :popup, key: :g, ctrl: true, action: :popup_cancel)
80
85
  map.bind(context: :popup, key: :escape, action: :popup_cancel)
81
86
  map.bind(context: :popup, key: :enter, action: :popup_accept)
87
+ map.bind(context: :popup, key: :keypad_enter, action: :popup_accept)
82
88
  map.bind(context: :popup, key: :return, action: :popup_accept)
83
89
  map.bind(context: :popup, key: :up, action: :popup_prev)
84
90
  map.bind(context: :popup, key: :down, action: :popup_next)
@@ -109,6 +115,7 @@ module Fatty
109
115
  map.bind(context: :prompt, key: :g, ctrl: true, action: :prompt_cancel)
110
116
  map.bind(context: :prompt, key: :escape, action: :prompt_cancel)
111
117
  map.bind(context: :prompt, key: :enter, action: :prompt_accept)
118
+ map.bind(context: :prompt, key: :keypad_enter, action: :prompt_accept)
112
119
  map.bind(context: :prompt, key: :return, action: :prompt_accept)
113
120
  map.bind(context: :prompt, key: :j, ctrl: true, action: :prompt_accept)
114
121
 
@@ -126,6 +133,7 @@ module Fatty
126
133
  map.bind(context: :terminal, key: :c, ctrl: true, action: :interrupt)
127
134
  map.bind(context: :input, key: :d, ctrl: true, action: :interrupt_if_empty)
128
135
  map.bind(context: :input, key: :enter, action: :submit_line)
136
+ map.bind(context: :input, key: :keypad_enter, action: :submit_line)
129
137
  map.bind(context: :input, key: :return, action: :submit_line)
130
138
  map.bind(context: :input, key: :j, ctrl: true, action: :submit_line)
131
139
  map.bind(context: :input, key: :j, ctrl: true, meta: true, action: :submit_and_scroll)
@@ -184,6 +192,7 @@ module Fatty
184
192
 
185
193
  # I-search controls (within ISearchSession)
186
194
  map.bind(context: :isearch, key: :enter, action: :isearch_accept)
195
+ map.bind(context: :isearch, key: :keypad_enter, action: :isearch_accept)
187
196
  map.bind(context: :isearch, key: :return, action: :isearch_accept)
188
197
  map.bind(context: :isearch, key: :j, ctrl: true, action: :isearch_accept)
189
198
  map.bind(context: :isearch, key: :s, ctrl: true, action: :isearch_next)
@@ -198,6 +207,7 @@ module Fatty
198
207
 
199
208
  # Search controls (within SearchSession)
200
209
  map.bind(context: :search, key: :enter, action: :search_accept)
210
+ map.bind(context: :search, key: :keypad_enter, action: :search_accept)
201
211
  map.bind(context: :search, key: :return, action: :search_accept)
202
212
  map.bind(context: :search, key: :j, ctrl: true, action: :search_accept)
203
213
  map.bind(context: :search, key: :s, ctrl: true, action: :search_step_forward)
@@ -52,7 +52,7 @@ module Fatty
52
52
  text.to_s
53
53
  .gsub(%r{<br\s*/?>}i, HARD_BREAK)
54
54
  .gsub(%r{<span\s+class=["']underline["']>(.*?)</span>}m) do
55
- md(Regexp.last_match(1), :markdown_underline)
55
+ md(Regexp.last_match(1), :markdown_underline)
56
56
  end
57
57
  end
58
58
 
@@ -2,10 +2,17 @@
2
2
 
3
3
  module Fatty
4
4
  module Markdown
5
- def self.render(text, width: 80, palette: nil, theme: nil, truecolor: false, table_widths: nil, table_borders: false)
5
+ def self.render(text, width: 80, palette: nil, theme: nil, truecolor: false, table_widths: nil,
6
+ table_borders: false)
6
7
  markdown = Redcarpet::Markdown.new(
7
- Fatty::AnsiRenderer.new(width: width, palette: palette, theme: theme, truecolor: truecolor,
8
- table_widths: table_widths, table_borders: table_borders),
8
+ Fatty::AnsiRenderer.new(
9
+ width: width,
10
+ palette: palette,
11
+ theme: theme,
12
+ truecolor: truecolor,
13
+ table_widths: table_widths,
14
+ table_borders: table_borders,
15
+ ),
9
16
  tables: true,
10
17
  fenced_code_blocks: true,
11
18
  autolink: true,
@@ -65,7 +65,7 @@ module Fatty
65
65
  out << "Mouse report #{status_string}:\n"
66
66
  out << " raw: #{raw.inspect}\n"
67
67
  out << " button: #{button.inspect}\n"
68
- out << " name: #{to_s}\n"
68
+ out << " name: #{self}\n"
69
69
  out << " x: #{x.inspect}\n"
70
70
  out << " y: #{y.inspect}\n"
71
71
  out << " modifiers: #{modifier_text}\n"
@@ -144,7 +144,7 @@ module Fatty
144
144
  def suggested_mouse_binding
145
145
  out = <<~TEXT
146
146
 
147
- No action is bound to #{to_s}.
147
+ No action is bound to #{self}.
148
148
 
149
149
  Suggested keybindings.yml entry:
150
150
 
@@ -160,7 +160,7 @@ module Fatty
160
160
  end
161
161
 
162
162
  def yaml_desc
163
- lines = ["# #{to_s}", "- button: #{button}"]
163
+ lines = ["# #{self}", "- button: #{button}"]
164
164
  lines << " shift: true" if shift?
165
165
  lines << " ctrl: true" if ctrl?
166
166
  lines << " meta: true" if meta?
@@ -25,13 +25,12 @@ module Fatty
25
25
  nil
26
26
  end
27
27
 
28
-
29
28
  # Append text to the output buffer. Text may contain complete lines,
30
29
  # partial lines, or both. Returns the number of lines trimmed. The
31
30
  # Terminal can use this to adjust the Viewport.
32
31
  def append(text, role: nil)
33
32
  ntrimmed = 0
34
- str = text.to_s
33
+ str = text.to_s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
35
34
  return ntrimmed if str.empty?
36
35
 
37
36
  str.split(/(\n)/).each do |part|
data/lib/fatty/pager.rb CHANGED
@@ -67,11 +67,11 @@ module Fatty
67
67
  end
68
68
 
69
69
  def set_to_paging
70
- @mode = :paging
71
- @paused = true
72
- @autoscroll = false
73
- @anchor = nil
74
- @viewport.clamp!(lines)
70
+ @mode = :paging
71
+ @paused = true
72
+ @autoscroll = false
73
+ @anchor = nil
74
+ @viewport.clamp!(lines)
75
75
  end
76
76
 
77
77
  def active?
@@ -904,8 +904,8 @@ module Fatty
904
904
 
905
905
  def valid_search_match?(match)
906
906
  match.is_a?(Hash) &&
907
- match[:line].is_a?(Integer) &&
908
- match[:from].is_a?(Integer) &&
907
+ match[:line].is_a?(Integer) &&
908
+ match[:from].is_a?(Integer) &&
909
909
  match[:to].is_a?(Integer)
910
910
  end
911
911
  end