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,373 @@
1
+ # H1 heading should be red on yellow and bold
2
+
3
+
4
+ ## H2 heading should be black on pink and bold
5
+
6
+
7
+ ### H3 heading should be magenta on lightyellow and underlined
8
+
9
+ This paragraph has **strong text**, *emphasized text*, `inline code`, `=highlighted text=`, and [fatty on github](<https://github.com/ddoherty03/fatty>).
10
+
11
+ Autolink: <https://github.com/ddoherty03/fat_config>
12
+
13
+ > This block quote should have a red quote gutter. The quoted text itself can stay normal output for now.
14
+
15
+ ```ruby
16
+ # This ruby block. Has comments.
17
+ # Two lines, in fact.
18
+ def hello(name)
19
+ puts "hello #{name}"
20
+ end
21
+ ```
22
+
23
+ | Feature | Expected style |
24
+ |------------ |------------------- |
25
+ | Header cells | black on lightgreen |
26
+ | Body cells | normal output |
27
+
28
+
29
+ # Carnival of Constructs
30
+
31
+ These are samples of the kinds of constructs the ANSI renderer for `fatty` are capable of.
32
+
33
+
34
+ ## Unordered Lists
35
+
36
+ - builtin commands are handled by the `fatty` demo, Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
37
+ - other commands are handled by `/bin/sh`
38
+ - keybindings are listed below
39
+
40
+
41
+ ## Ordered Lists
42
+
43
+ 1. builtin commands are handled by the `fatty` demo, Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
44
+ 2. other commands are handled by `/bin/sh`
45
+ 3. keybindings are listed below
46
+
47
+ Some regular text to break things up.
48
+
49
+ 1. Parent ordered item
50
+ 1. Child ordered item
51
+ 2. Another child ordered item
52
+ 2. Back to parent ordered item
53
+
54
+
55
+ ## Description Lists
56
+
57
+ - **bold:** **this text is bold**
58
+ - **italic:** *this text is italic*
59
+ - **code:** `this text is code`
60
+ - **verbatim:** `this text is verbatim`
61
+ - **strike:** ~~this text is strike-through~~
62
+ - **underline:** <span class="underline">this text is underlined</span>
63
+
64
+
65
+ ## A Code Block
66
+
67
+ ```ruby
68
+ def wrap(text, first_prefix: "", rest_prefix: first_prefix)
69
+ width = [@width.to_i, 20].max
70
+
71
+ words = text.to_s.split(/\s+/)
72
+ lines = []
73
+ line = +""
74
+
75
+ words.each do |word|
76
+ prefix = lines.empty? ? first_prefix : rest_prefix
77
+ available = width - Fatty::Ansi.visible_length(prefix)
78
+ available = 20 if available < 20
79
+
80
+ candidate = line.empty? ? word : "#{line} #{word}"
81
+
82
+ if Fatty::Ansi.visible_length(candidate) > available && !line.empty?
83
+ lines << "#{prefix}#{line}"
84
+ line = word.dup
85
+ else
86
+ line = candidate
87
+ end
88
+ end
89
+
90
+ unless line.empty?
91
+ prefix = lines.empty? ? first_prefix : rest_prefix
92
+ lines << "#{prefix}#{line}"
93
+ end
94
+
95
+ lines.join("\n")
96
+ end
97
+ ```
98
+
99
+
100
+ ## Nested lists
101
+
102
+ - Parent item
103
+ - Child item
104
+ - Another child
105
+ - Back to parent
106
+
107
+
108
+ ## Links
109
+
110
+ [Fatty on GitHub](<https://github.com/ddoherty03/fatty>)
111
+
112
+
113
+ ## Block quotes
114
+
115
+ > Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
116
+ >
117
+ > Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
118
+ >
119
+ > But, in a larger sense, we can not dedicate&#x2014;we can not consecrate&#x2014;we can not hallow&#x2014;this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us&#x2014;that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion&#x2014;that we here highly resolve that these dead shall not have died in vain&#x2014;that this nation, under God, shall have a new birth of freedom&#x2014;and that government of the people, by the people, for the people, shall not perish from the earth.
120
+
121
+
122
+ ## Tables with styled cells
123
+
124
+ | Feature | Example |
125
+ |------- |--------------- |
126
+ | bold | **bold cell** |
127
+ | code | `code cell` |
128
+ | strike | ~~strike cell~~ |
129
+
130
+
131
+ ## Horizontal Rule
132
+
133
+ This should be a horizontal rule, but YMMV.
134
+
135
+ ---
136
+
137
+
138
+ ## Forced Line Break
139
+
140
+ This line has a forced line break<br> and this should appear on the next line.
141
+
142
+
143
+ ## Fenced code with various languages
144
+
145
+ Here is the Fibonacci number computed recursively in various languages.
146
+
147
+
148
+ ### Ruby
149
+
150
+ ```ruby
151
+ def fib(n)
152
+ return n if n < 2
153
+
154
+ fib(n - 1) + fib(n - 2)
155
+ end
156
+ puts fib(10)
157
+ ```
158
+
159
+
160
+ ### Python
161
+
162
+ ```python
163
+ def fib(n):
164
+ if n < 2:
165
+ return n
166
+
167
+ return fib(n - 1) + fib(n - 2)
168
+
169
+ print(fib(10))
170
+
171
+ ```
172
+
173
+
174
+ ### C
175
+
176
+ ```c
177
+ #include <stdio.h>
178
+
179
+ int fib(int n)
180
+ {
181
+ if (n < 2) {
182
+ return n;
183
+ }
184
+
185
+ return fib(n - 1) + fib(n - 2);
186
+ }
187
+
188
+ int main(void)
189
+ {
190
+ printf("%d\n", fib(10));
191
+ return 0;
192
+ }
193
+ ```
194
+
195
+
196
+ ### C++
197
+
198
+ ```cpp
199
+ #include <iostream>
200
+
201
+ int fib(int n)
202
+ {
203
+ if (n < 2) {
204
+ return n;
205
+ }
206
+
207
+ return fib(n - 1) + fib(n - 2);
208
+ }
209
+
210
+ int main()
211
+ {
212
+ std::cout << fib(10) << std::endl;
213
+ }
214
+ ```
215
+
216
+
217
+ ### Go
218
+
219
+ ```go
220
+ package main
221
+
222
+ import "fmt"
223
+
224
+ func fib(n int) int {
225
+ if n < 2 {
226
+ return n
227
+ }
228
+
229
+ return fib(n-1) + fib(n-2)
230
+ }
231
+
232
+ func main() {
233
+ fmt.Println(fib(10))
234
+ }
235
+ ```
236
+
237
+
238
+ ### Rust
239
+
240
+ ```rust
241
+ fn fib(n: u32) -> u32 {
242
+ if n < 2 {
243
+ n
244
+ } else {
245
+ fib(n - 1) + fib(n - 2)
246
+ }
247
+ }
248
+
249
+ fn main() {
250
+ println!("{}", fib(10));
251
+ }
252
+ ```
253
+
254
+
255
+ ### PHP
256
+
257
+ ```php
258
+ <?php
259
+
260
+ function fib($n)
261
+ {
262
+ if ($n < 2) {
263
+ return $n;
264
+ }
265
+
266
+ return fib($n - 1) + fib($n - 2);
267
+ }
268
+
269
+ echo fib(10) . PHP_EOL;
270
+ ```
271
+
272
+
273
+ ### JavaScript
274
+
275
+ ```javascript
276
+ function fib(n) {
277
+ if (n < 2) {
278
+ return n;
279
+ }
280
+
281
+ return fib(n - 1) + fib(n - 2);
282
+ }
283
+
284
+ console.log(fib(10));
285
+ ```
286
+
287
+
288
+ ### TypeScript
289
+
290
+ ```typescript
291
+ function fib(n: number): number {
292
+ if (n < 2) {
293
+ return n;
294
+ }
295
+
296
+ return fib(n - 1) + fib(n - 2);
297
+ }
298
+
299
+ console.log(fib(10));
300
+ ```
301
+
302
+
303
+ ### Java
304
+
305
+ ```java
306
+ public class Fib {
307
+ static int fib(int n) {
308
+ if (n < 2) {
309
+ return n;
310
+ }
311
+
312
+ return fib(n - 1) + fib(n - 2);
313
+ }
314
+
315
+ public static void main(String[] args) {
316
+ System.out.println(fib(10));
317
+ }
318
+ }
319
+ ```
320
+
321
+
322
+ ### Shell
323
+
324
+ ```bash
325
+ fib() {
326
+ if [ "$1" -lt 2 ]; then
327
+ echo "$1"
328
+ else
329
+ a=$(fib $(($1 - 1)))
330
+ b=$(fib $(($1 - 2)))
331
+ echo $((a + b))
332
+ fi
333
+ }
334
+
335
+ fib 10
336
+ ```
337
+
338
+
339
+ ### Haskell
340
+
341
+ ```haskell
342
+ fib :: Int -> Int
343
+ fib n
344
+ | n < 2 = n
345
+ | otherwise = fib (n - 1) + fib (n - 2)
346
+
347
+ main :: IO ()
348
+ main = print (fib 10)
349
+ ```
350
+
351
+
352
+ ### SQL (Recursive CTE)
353
+
354
+ ```sql
355
+ WITH RECURSIVE fib(n, a, b) AS (
356
+ SELECT 0, 0, 1
357
+ UNION ALL
358
+ SELECT n + 1, b, a + b
359
+ FROM fib
360
+ WHERE n < 10
361
+ )
362
+ SELECT a
363
+ FROM fib
364
+ ORDER BY n DESC
365
+ LIMIT 1;
366
+ ```
367
+
368
+
369
+ ### Nonsense Language
370
+
371
+ ```madeuplang
372
+ this should still render as plain text
373
+ ```
data/exe/fatty CHANGED
@@ -22,7 +22,7 @@ append_text = lambda do |args|
22
22
  end
23
23
 
24
24
  on_accept_proc = lambda do |line, env|
25
- cmd, *args = Shellwords.split(line)
25
+ cmd, *args = Fatty::Shell.split_arguments(line)
26
26
  case cmd
27
27
  when "cd"
28
28
  dir = args.first || ENV["HOME"]
@@ -116,17 +116,17 @@ on_accept_proc = lambda do |line, env|
116
116
  "Demo menu",
117
117
  choices: {
118
118
  "Write to output pane" => proc { |env|
119
- env.append("menu: wrote this from MenuEnv#output\n")
120
- :output_written
121
- },
119
+ env.append("menu: wrote this from MenuEnv#output\n")
120
+ :output_written
121
+ },
122
122
  "Show status message" => proc { |env|
123
- env.status("menu: status set from MenuEnv#status")
124
- :status_written
125
- },
123
+ env.status("menu: status set from MenuEnv#status")
124
+ :status_written
125
+ },
126
126
  "Show markdown" => proc { |env|
127
- env.markdown("\# Your Choices\n\n- one\n- two\n- three\n")
128
- :markdown_written
129
- },
127
+ env.markdown("# Your Choices\n\n- one\n- two\n- three\n")
128
+ :markdown_written
129
+ },
130
130
  },
131
131
  )
132
132
  if result.nil?
@@ -266,20 +266,20 @@ on_accept_proc = lambda do |line, env|
266
266
  last_remaining = nil
267
267
  begin
268
268
  loop do
269
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
270
- remaining = (deadline - now).ceil
271
- break if remaining <= 0
269
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
270
+ remaining = (deadline - now).ceil
271
+ break if remaining <= 0
272
272
 
273
- if remaining != last_remaining
274
- env.status(
275
- "Interrupt demo: #{remaining} seconds remaining — press C-c or C-g",
276
- replace: true,
277
- )
278
- last_remaining = remaining
273
+ if remaining != last_remaining
274
+ env.status(
275
+ "Interrupt demo: #{remaining} seconds remaining — press C-c or C-g",
276
+ replace: true,
277
+ )
278
+ last_remaining = remaining
279
+ end
280
+ env.check_interrupt!
281
+ sleep 0.1
279
282
  end
280
- env.check_interrupt!
281
- sleep 0.1
282
- end
283
283
  rescue Fatty::Interrupt
284
284
  env.warn("Interrupt demo interrupted.", replace: true)
285
285
  raise
@@ -298,7 +298,6 @@ on_accept_proc = lambda do |line, env|
298
298
  env.append("rendering mode -> #{truecolor_enabled ? '24-bit RGB' : "#{available}-color indexed"}\n")
299
299
  env.append("\n")
300
300
 
301
-
302
301
  env.append("ANSI colors\n")
303
302
  Fatty::Color.ansi_colors.each do |name, index|
304
303
  rgb = Fatty::Color.xterm_rgb_for_index(index)
@@ -308,7 +307,7 @@ on_accept_proc = lambda do |line, env|
308
307
  requested_rgb: rgb,
309
308
  fallback_index: Fatty::Color.resolve(index, available_colors: available),
310
309
  truecolor: truecolor_enabled,
311
- )
310
+ ),
312
311
  )
313
312
  end
314
313
 
@@ -321,7 +320,7 @@ on_accept_proc = lambda do |line, env|
321
320
  requested_rgb: rgb,
322
321
  fallback_index: Fatty::Color.resolve(index, available_colors: available),
323
322
  truecolor: truecolor_enabled,
324
- )
323
+ ),
325
324
  )
326
325
  end
327
326
 
@@ -338,7 +337,7 @@ on_accept_proc = lambda do |line, env|
338
337
  requested_rgb: rgb,
339
338
  fallback_index: fallback_index,
340
339
  truecolor: truecolor_enabled,
341
- )
340
+ ),
342
341
  )
343
342
  end
344
343
 
@@ -347,16 +346,18 @@ on_accept_proc = lambda do |line, env|
347
346
  env.markdown(Fatty::Help.text)
348
347
  else
349
348
  unless line.match?(/\A\s*\z/)
350
- out, _ = Open3.capture2e(line)
349
+ out = nil
350
+ begin
351
+ env.terminal.suspend { out, _ = Fatty::Shell.capture(line) }
352
+ rescue Errno::ENOENT
353
+ env.alert("Command not found (#{line})", role: :error)
354
+ end
351
355
  out
352
356
  end
353
357
  end
354
- rescue Errno::ENOENT
355
- env.alert("Command not found (#{line})", role: :error)
356
- nil
357
- rescue SystemCallError => ex
358
- env.alert(ex.message, role: :error)
359
- nil
358
+ rescue SystemCallError => ex
359
+ env.alert(ex.message, role: :error)
360
+ nil
360
361
  end
361
362
 
362
363
  def color_swatch_line(name:, requested_rgb:, fallback_index:, truecolor:, width: 24)
@@ -396,25 +397,25 @@ def color_band_for_rgb(name:, rgb:, truecolor:, fallback_index:, width: 36)
396
397
  fg = Fatty::Color.contrasting_rgb(rgb)
397
398
 
398
399
  "\e[38;2;#{fg[0]};#{fg[1]};#{fg[2]}m" \
399
- "\e[48;2;#{rgb[0]};#{rgb[1]};#{rgb[2]}m" \
400
- "#{label}" \
401
- "\e[0m"
400
+ "\e[48;2;#{rgb[0]};#{rgb[1]};#{rgb[2]}m" \
401
+ "#{label}" \
402
+ "\e[0m"
402
403
  else
403
404
  fg_index = Fatty::Color.contrasting_ansi_index(fallback_index)
404
405
 
405
406
  "\e[38;5;#{fg_index}m" \
406
- "\e[48;5;#{fallback_index}m" \
407
- "#{label}" \
408
- "\e[0m"
407
+ "\e[48;5;#{fallback_index}m" \
408
+ "#{label}" \
409
+ "\e[0m"
409
410
  end
410
411
  end
411
412
 
412
413
  cutes = []
413
- ENV['PATH'].split(':').each do |dir|
414
+ ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).each do |dir|
414
415
  next unless Dir.exist?(dir)
415
416
 
416
417
  Dir.children(dir).each do |name|
417
- next if name.match?(/\A\./)
418
+ next if name.start_with?('.')
418
419
 
419
420
  path = File.join(dir, name)
420
421
  cutes << name if File.file?(path) && File.executable?(path)
@@ -441,6 +442,7 @@ builtins = %w[
441
442
  choose_multi
442
443
  choosevals_multi
443
444
  colors
445
+ environment
444
446
  help
445
447
  info
446
448
  interrupt
data/exe/themer CHANGED
@@ -61,8 +61,8 @@ def contrast_ratio(first, second)
61
61
  first_luminance = luminance(first)
62
62
  second_luminance = luminance(second)
63
63
 
64
- light, dark = [first_luminance, second_luminance].max,
65
- [first_luminance, second_luminance].min
64
+ light = [first_luminance, second_luminance].max
65
+ dark = [first_luminance, second_luminance].min
66
66
 
67
67
  (light + 0.05) / (dark + 0.05)
68
68
  end
@@ -98,7 +98,7 @@ def suggested_color(background, hue, target: TARGET_CONTRAST)
98
98
  if background_luminance < 0.5
99
99
  (35..90).map { |value| value / 100.0 }
100
100
  else
101
- (65).downto(10).map { |value| value / 100.0 }
101
+ 65.downto(10).map { |value| value / 100.0 }
102
102
  end
103
103
 
104
104
  best_rgb = nil
@@ -192,11 +192,11 @@ def print_roles(title, roles, status: false)
192
192
  puts line
193
193
  else
194
194
  puts format(
195
- "%-6s %-8s contrast unknown%s",
196
- role,
197
- data[:hex],
198
- status ? " #{data[:status]}" : "",
199
- )
195
+ "%-6s %-8s contrast unknown%s",
196
+ role,
197
+ data[:hex],
198
+ status ? " #{data[:status]}" : "",
199
+ )
200
200
  end
201
201
  end
202
202
  end
@@ -218,13 +218,13 @@ options = {
218
218
  yaml: false,
219
219
  }
220
220
 
221
- OptionParser.new do |parser|
221
+ OptionParser.new { |parser|
222
222
  parser.banner = "Usage: themer THEME|RGB [options]"
223
223
 
224
224
  parser.on("--yaml", "Output role definitions as YAML") do
225
225
  options[:yaml] = true
226
226
  end
227
- end.parse!
227
+ }.parse!
228
228
 
229
229
  argument = ARGV.shift
230
230
 
data/help/help.md CHANGED
@@ -132,6 +132,7 @@ When editing the input line or text input for widgets like the \`prompt\`, \`fat
132
132
  | :tab | Suggest the next completion |
133
133
  | S-:tab | Suggest the prior completion |
134
134
  | M-:tab | Popup all completions based on input-so-far |
135
+ | :f2 | Popup completions (alternative to Alt-Tab) |
135
136
  | | |
136
137
 
137
138
 
data/lib/fatty/alert.rb CHANGED
@@ -6,7 +6,7 @@ module Fatty
6
6
 
7
7
  # Return a new Alert object
8
8
  #
9
- # @param message [String]
9
+ # @param text [String]
10
10
  # @param role [:good, :info, :warn, :error]
11
11
  # @param details [Hash|String]
12
12
  # @param sticky [Boolean]
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "windows_console"
4
+
3
5
  module Fatty
4
6
  module Ansi
5
7
  class Renderer
@@ -13,13 +15,13 @@ module Fatty
13
15
  spec = palette&.[](role.to_sym)
14
16
  return text.to_s unless spec
15
17
 
16
- text.to_s.split(/(\n)/).map do |part|
18
+ text.to_s.split(/(\n)/).map { |part|
17
19
  if part == "\n"
18
20
  part
19
21
  else
20
22
  "#{sgr_for_spec(spec)}#{part}#{reset}"
21
23
  end
22
- end.join
24
+ }.join
23
25
  end
24
26
 
25
27
  def render_line(row:, col:, width:, text:, role:, palette:)
@@ -86,8 +88,10 @@ module Fatty
86
88
  if defined?(::Curses) && ::Curses.respond_to?(:putp)
87
89
  ::Curses.putp(text)
88
90
  else
89
- io.write(text)
90
- io.flush
91
+ WindowsConsole.with_ansi(io) do
92
+ io.write(text)
93
+ io.flush
94
+ end
91
95
  end
92
96
 
93
97
  nil
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fiddle"
4
+
5
+ module Fatty
6
+ module Ansi
7
+ # PDCurses changes output modes while drawing. Enable VT for each Ruby
8
+ # ANSI write, preserving the console mode owned by the curses backend.
9
+ module WindowsConsole
10
+ def self.with_ansi(io)
11
+ return yield unless Gem.win_platform? && io.respond_to?(:tty?) && io.tty?
12
+
13
+ api = functions
14
+ handle = api[:handle].call(-11)
15
+ buffer = [0].pack("L")
16
+ return yield if api[:get].call(handle, buffer).zero?
17
+
18
+ mode = buffer.unpack1("L")
19
+ changed = api[:set].call(handle, mode | 0x0005) != 0
20
+ begin
21
+ yield
22
+ ensure
23
+ api[:set].call(handle, mode) if changed
24
+ end
25
+ end
26
+
27
+ def self.functions
28
+ @functions ||= begin
29
+ library = Fiddle.dlopen("kernel32.dll")
30
+ {
31
+ handle: Fiddle::Function.new(library["GetStdHandle"], [Fiddle::TYPE_LONG], Fiddle::TYPE_VOIDP),
32
+ get: Fiddle::Function.new(library["GetConsoleMode"], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
33
+ set: Fiddle::Function.new(library["SetConsoleMode"], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG], Fiddle::TYPE_INT),
34
+ }
35
+ end
36
+ end
37
+ private_class_method :functions
38
+ end
39
+ end
40
+ end
data/lib/fatty/ansi.rb CHANGED
@@ -2,11 +2,6 @@
2
2
 
3
3
  require_relative "ansi/renderer"
4
4
 
5
- module Fatty
6
- module Ansi
7
- end
8
- end
9
-
10
5
  module Fatty
11
6
  # Parse ANSI escape sequences (primarily SGR, i.e. "\e[...m") into styled text
12
7
  # segments. This is intentionally curses-agnostic: the renderer/context decides