markdown_exec 2.0.7 → 2.0.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af8a53f03e48c890798b520b84bd0357ccef4f31d93b97cc7cc8b4b72ca1a035
4
- data.tar.gz: cd5983d2ff18ee8e2a29da6bea364c03bdaf6a43871d45b16483a8996d129894
3
+ metadata.gz: e071fd1fad2603543cbad492e8bed6ee2a1e437f914cf834d3bb99da3a87234f
4
+ data.tar.gz: ba95432a7a17319cde5eaa821c554a472f8cdfeca8238c370869a560e22fcae7
5
5
  SHA512:
6
- metadata.gz: 47e4e75e5a4e473ab9e9726cb7774aec95eb4a26d1d48035e3f6a69df0162c6b004fa625f5ae9f3c973bb25f732c9e938c0008dd07abb759f3211258d943fcc8
7
- data.tar.gz: 306fc1218c64d57b1ca82191060298f87f9ae996e43f278cd72144fa3b839e3e4e4b8d3168f34c2fca4bd1ba9fa8ca1ab0a42659980d115b2d3293edc5e75238
6
+ metadata.gz: 0c3586f87853edbb872a7ad8ba766fe1a76d4184ebb6b02d11eaf6227ed152ae75b83c5b291981f0cf9a2208133cf122e8a3fcd4376bd694607f9bfa5d8bc577
7
+ data.tar.gz: 73a013bc437ba2d5aa87bd9c8a0c8039d6ad8cb6b95767a3b16ab340a90aec26ae1f2c0d0fa355280caa89cb45de7a474b51473683573a147d6cebc91b6522a1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.0.8] - 2024-06-05
4
+
5
+ ### Added
6
+
7
+ - Option to automatically resize the menu.
8
+ Detect the maximum supported screen dimensions.
9
+ Resize the terminal to the maximum dimensions.
10
+ Every time the menu is displayed, format it to the terminal dimensions.
11
+
12
+ ### Changed
13
+
14
+ - Remove "$" from saved file names to simplify handling.
15
+
3
16
  ## [2.0.7] - 2024-06-04
4
17
 
5
18
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdown_exec (2.0.7)
4
+ markdown_exec (2.0.8)
5
5
  clipboard (~> 1.3.6)
6
6
  open3 (~> 0.1.1)
7
7
  optparse (~> 0.1.1)
data/Rakefile CHANGED
@@ -89,6 +89,7 @@ task :minitest do
89
89
  './lib/object_present.rb',
90
90
  './lib/option_value.rb',
91
91
  './lib/regexp.rb',
92
+ './lib/resize_terminal.rb',
92
93
  './lib/saved_assets.rb',
93
94
  './lib/saved_files_matcher.rb'
94
95
  ]
@@ -13,7 +13,7 @@ __filedirs_all()
13
13
  }
14
14
 
15
15
  _mde_echo_version() {
16
- echo "2.0.7"
16
+ echo "2.0.8"
17
17
  }
18
18
 
19
19
  _mde() {
@@ -178,4 +178,4 @@ _mde() {
178
178
 
179
179
  complete -o filenames -o nospace -F _mde mde
180
180
  # _mde_echo_version
181
- # echo "Updated: 2024-06-05 03:00:42 UTC"
181
+ # echo "Updated: 2024-06-06 00:36:41 UTC"
@@ -6,6 +6,7 @@
6
6
  require 'clipboard'
7
7
  require 'English'
8
8
  require 'fileutils'
9
+ require 'io/console'
9
10
  require 'open3'
10
11
  require 'optparse'
11
12
  require 'ostruct'
@@ -31,6 +32,7 @@ require_relative 'hash'
31
32
  require_relative 'link_history'
32
33
  require_relative 'mdoc'
33
34
  require_relative 'regexp'
35
+ require_relative 'resize_terminal'
34
36
  require_relative 'std_out_err_logger'
35
37
  require_relative 'string_util'
36
38
 
@@ -2485,14 +2487,37 @@ module MarkdownExec
2485
2487
  ))
2486
2488
  end
2487
2489
 
2490
+ # Registers console attributes by modifying the options hash.
2491
+ # This method handles terminal resizing and adjusts the console dimensions
2492
+ # and pagination settings based on the current terminal size.
2493
+ #
2494
+ # @param opts [Hash] a hash containing various options for the console settings.
2495
+ # - :console_width [Integer, nil] The width of the console. If not provided or if the terminal is resized, it will be set to the current console width.
2496
+ # - :console_height [Integer, nil] The height of the console. If not provided or if the terminal is resized, it will be set to the current console height.
2497
+ # - :console_winsize [Array<Integer>, nil] The dimensions of the console [height, width]. If not provided or if the terminal is resized, it will be set to the current console dimensions.
2498
+ # - :select_page_height [Integer, nil] The height of the page for selection. If not provided or if not positive, it will be set to the maximum of (console height - 3) or 4.
2499
+ # - :per_page [Integer, nil] The number of items per page. If :select_page_height is not provided or if not positive, it will be set to the maximum of (console height - 3) or 4.
2500
+ #
2501
+ # @raise [StandardError] If an error occurs during the process, it will be caught and handled by calling HashDelegator.error_handler with 'register_console_attributes' and { abort: true }.
2502
+ #
2503
+ # @example
2504
+ # opts = { console_width: nil, console_height: nil, select_page_height: nil }
2505
+ # register_console_attributes(opts)
2506
+ # # opts will be updated with the current console dimensions and pagination settings.
2488
2507
  def register_console_attributes(opts)
2489
- unless opts[:console_width]
2490
- require 'io/console'
2491
- opts[:console_height], opts[:console_width] = opts[:console_winsize] = IO.console.winsize
2508
+ begin
2509
+ if (resized = @delegate_object[:menu_resize_terminal])
2510
+ resize_terminal
2511
+ end
2512
+
2513
+ if resized || !opts[:console_width]
2514
+ opts[:console_height], opts[:console_width] = opts[:console_winsize] = IO.console.winsize
2515
+ end
2516
+
2517
+ opts[:per_page] = opts[:select_page_height] = [opts[:console_height] - 3, 4].max unless opts[:select_page_height]&.positive?
2518
+ rescue StandardError
2519
+ HashDelegator.error_handler('register_console_attributes', { abort: true })
2492
2520
  end
2493
- opts[:per_page] = opts[:select_page_height] = [opts[:console_height] - 3, 4].max unless opts[:select_page_height]&.positive?
2494
- rescue StandardError
2495
- HashDelegator.error_handler('register_console_attributes', { abort: true })
2496
2521
  end
2497
2522
 
2498
2523
  # Check if the delegate object responds to a given method.
@@ -7,5 +7,5 @@ module MarkdownExec
7
7
  BIN_NAME = 'mde'
8
8
  GEM_NAME = 'markdown_exec'
9
9
  TAP_DEBUG = 'MDE_DEBUG'
10
- VERSION = '2.0.7'
10
+ VERSION = '2.0.8'
11
11
  end
data/lib/menu.src.yml CHANGED
@@ -727,6 +727,13 @@
727
727
  :opt_name: menu_persist_block_name
728
728
  :procname: val_as_str
729
729
 
730
+ - :arg_name: BOOL
731
+ :default: true
732
+ :description: Resize terminal when displaying menu.
733
+ :env_var: MDE_MENU_RESIZE_TERMINAL
734
+ :opt_name: menu_resize_terminal
735
+ :procname: val_as_bool
736
+
730
737
  - :default: fg_rgbh_ff_ff_ff
731
738
  :description: Color of menu task
732
739
  :env_var: MDE_MENU_TASK_COLOR
data/lib/menu.yml CHANGED
@@ -1,4 +1,4 @@
1
- # MDE - Markdown Executor (2.0.7)
1
+ # MDE - Markdown Executor (2.0.8)
2
2
  ---
3
3
  - :description: Show current configuration values
4
4
  :procname: show_config
@@ -609,6 +609,12 @@
609
609
  :env_var: MDE_MENU_PERSIST_BLOCK_NAME
610
610
  :opt_name: menu_persist_block_name
611
611
  :procname: val_as_str
612
+ - :arg_name: BOOL
613
+ :default: true
614
+ :description: Resize terminal when displaying menu.
615
+ :env_var: MDE_MENU_RESIZE_TERMINAL
616
+ :opt_name: menu_resize_terminal
617
+ :procname: val_as_bool
612
618
  - :default: fg_rgbh_ff_ff_ff
613
619
  :description: Color of menu task
614
620
  :env_var: MDE_MENU_TASK_COLOR
@@ -0,0 +1,215 @@
1
+ #!/usr/bin/env bundle exec ruby
2
+ # frozen_string_literal: true
3
+
4
+ # encoding=utf-8
5
+ require 'io/console'
6
+ require 'timeout'
7
+
8
+ # This function attempts to resize the terminal to its maximum supported size.
9
+ # It checks if the script is running in an interactive terminal with no arguments.
10
+ # If so, it sends escape sequences to query the terminal size and reads the response.
11
+ # It then compares the current terminal size with the calculated size and adjusts if necessary.
12
+ # If the terminal emulator is unsupported, it prints an error message.
13
+ def resize_terminal(show_dims: false, show_rectangle: false)
14
+ # Check if running in an interactive terminal and no arguments are provided
15
+ if $stdin.tty? && ARGV.empty?
16
+ begin
17
+ # Save the current state and send the escape sequence to get the cursor position
18
+ print "\e7\e[r\e[999;999H\e[6n\e8"
19
+ $stdout.flush
20
+
21
+ # Read the response from the terminal
22
+ response = String.new
23
+ Timeout.timeout(5) do
24
+ loop do
25
+ char = $stdin.getch
26
+ response << char
27
+ break if response.include?('R')
28
+ end
29
+ end
30
+
31
+ if response.empty?
32
+ warn "Error: No response received from terminal. Response: #{response.inspect}"
33
+ return 1
34
+ end
35
+
36
+ # Match the response to extract the terminal dimensions
37
+ match_data = response.match(/\[(\d+);(\d+)R/)
38
+ unless match_data
39
+ warn "Error: Failed to match terminal response pattern. Response: #{response.inspect}"
40
+ return 1
41
+ end
42
+
43
+ calculated_rows, calculated_columns = match_data.captures.map(&:to_i)
44
+
45
+ if ENV['COLUMNS'].to_i == calculated_columns && ENV['LINES'].to_i == calculated_rows
46
+ puts "#{ENV.fetch('TERM', nil)} #{calculated_columns}x#{calculated_rows}"
47
+ elsif calculated_columns.positive? && calculated_rows.positive?
48
+ warn "#{ENV.fetch('COLUMNS', nil)}x#{ENV.fetch('LINES', nil)} -> #{calculated_columns}x#{calculated_rows}" if show_dims
49
+ system("stty cols #{calculated_columns} rows #{calculated_rows}")
50
+ else
51
+ warn "Error: Calculated terminal size is invalid. Columns: #{calculated_columns}, Rows: #{calculated_rows}"
52
+ return 1
53
+ end
54
+
55
+ # Display a text rectangle if the option is enabled
56
+ display_terminal_rectangle(calculated_columns, calculated_rows) if show_rectangle
57
+ rescue Timeout::Error
58
+ warn 'Error: Timeout while reading terminal response. Unsupported terminal emulator.'
59
+ 1
60
+ rescue StandardError => err
61
+ warn "Error: #{err.message}. Unsupported terminal emulator."
62
+ 1
63
+ end
64
+ else
65
+ warn 'Usage: resize_terminal'
66
+ end
67
+ end
68
+
69
+ # This function draws a rectangle of the given width and height
70
+ # with stars on the edges and empty space inside.
71
+ def display_terminal_rectangle(width, height)
72
+ puts '*' * width
73
+ (height - 2).times { puts "*#{' ' * (width - 2)}*" }
74
+ puts '*' * width
75
+ end
76
+
77
+ # resize_terminal(show_rectangle: true) if __FILE__ == $PROGRAM_NAME
78
+ return if __FILE__ != $PROGRAM_NAME
79
+
80
+ require 'minitest/autorun'
81
+
82
+ class ResizeTerminalTest < Minitest::Test
83
+ def setup
84
+ # Backup original ARGV and environment variables
85
+ @original_argv = ARGV.dup
86
+ @original_columns = ENV.fetch('COLUMNS', nil)
87
+ @original_lines = ENV.fetch('LINES', nil)
88
+ end
89
+
90
+ def teardown
91
+ # Restore original ARGV and environment variables
92
+ ARGV.replace(@original_argv)
93
+ ENV['COLUMNS'] = @original_columns
94
+ ENV['LINES'] = @original_lines
95
+ end
96
+
97
+ # def test_resize_terminal_successful
98
+ # # Simulate interactive terminal
99
+ # $stdin.stub(:tty?, true) do
100
+ # ARGV.replace([])
101
+ # ENV['COLUMNS'] = '80'
102
+ # ENV['LINES'] = '24'
103
+ # response = "\e[999;999H\e[6n\e[24;80R"
104
+ # $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
105
+ # assert_output(nil, /24x80/) do
106
+ # resize_terminal
107
+ # end
108
+ # end
109
+ # end
110
+ # end
111
+ def test_resize_terminal_successful
112
+ # Simulate interactive terminal
113
+ $stdin.stub(:tty?, true) do
114
+ ARGV.replace([])
115
+ columns = 40 + (2 * rand(10))
116
+ ENV['COLUMNS'] = columns.to_s
117
+ ENV['LINES'] = '24'
118
+ response = "\e[999;999H\e[6n\e[24;#{columns}R".dup
119
+ $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
120
+ assert_output("\e7\e[r\e[999;999H\e[6n\e8xterm-256color #{columns}x24\n") do
121
+ # assert_output('', '') do
122
+ resize_terminal
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ def test_resize_terminal_no_response
129
+ # Simulate interactive terminal with no response
130
+ $stdin.stub(:tty?, true) do
131
+ ARGV.replace([])
132
+ $stdin.stub(:getch, -> { '' }) do
133
+ # assert_output(nil, /Error: No response received from terminal/) do
134
+ assert_output(nil, "Error: Timeout while reading terminal response. Unsupported terminal emulator.\n") do
135
+ assert_equal 1, resize_terminal
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ def test_resize_terminal_invalid_response
142
+ # Simulate interactive terminal with invalid response
143
+ $stdin.stub(:tty?, true) do
144
+ ARGV.replace([])
145
+ response = "\e[999;999H\e[6n\e[InvalidResponse".dup
146
+ $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
147
+ assert_output(nil, /Error: Failed to match terminal response pattern/) do
148
+ assert_equal 1, resize_terminal
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ def test_resize_terminal_timeout
155
+ # Simulate interactive terminal with timeout
156
+ $stdin.stub(:tty?, true) do
157
+ ARGV.replace([])
158
+ Timeout.stub(:timeout, ->(_) { raise Timeout::Error }) do
159
+ assert_output(nil, /Error: Timeout while reading terminal response/) do
160
+ assert_equal 1, resize_terminal
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ def test_resize_terminal_non_interactive
167
+ # Simulate non-interactive terminal
168
+ $stdin.stub(:tty?, false) do
169
+ assert_output(nil, /Usage: resize_terminal/) do
170
+ resize_terminal
171
+ end
172
+ end
173
+ end
174
+
175
+ # def test_resize_terminal_display_rectangle
176
+ # # Simulate interactive terminal with rectangle display
177
+ # $stdin.stub(:tty?, true) do
178
+ # ARGV.replace([])
179
+ # ENV['COLUMNS'] = '80'
180
+ # ENV['LINES'] = '24'
181
+ # response = "\e[999;999H\e[6n\e[24;80R".dup
182
+ # $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
183
+ # expected_output = "\e7\e[r\e[999;999H\e[6n\e8"
184
+ # # expected_output = <<-RECTANGLE
185
+ # # ********************************************************************************
186
+ # # * *
187
+ # # * *
188
+ # # * *
189
+ # # * *
190
+ # # * *
191
+ # # * *
192
+ # # * *
193
+ # # * *
194
+ # # * *
195
+ # # * *
196
+ # # * *
197
+ # # * *
198
+ # # * *
199
+ # # * *
200
+ # # * *
201
+ # # * *
202
+ # # * *
203
+ # # * *
204
+ # # * *
205
+ # # * *
206
+ # # * *
207
+ # # ********************************************************************************
208
+ # # RECTANGLE
209
+ # assert_output(expected_output.strip) do
210
+ # resize_terminal(show_rectangle: true)
211
+ # end
212
+ # end
213
+ # end
214
+ # end
215
+ end
data/lib/saved_assets.rb CHANGED
@@ -12,7 +12,7 @@ module MarkdownExec
12
12
  # method derives a name for stdout redirection.
13
13
  #
14
14
  class SavedAsset
15
- FNR11 = %r{[^!#$%&()\+,\-0-9=A-Z_a-z~]}.freeze
15
+ FNR11 = %r{[^!#%&()\+,\-0-9=A-Z_a-z~]}.freeze
16
16
  # / !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
17
17
  FNR12 = '_'
18
18
  DEFAULT_FTIME = '%F-%H-%M-%S'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_exec
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.7
4
+ version: 2.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fareed Stevenson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-05 00:00:00.000000000 Z
11
+ date: 2024-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clipboard
@@ -173,6 +173,7 @@ files:
173
173
  - lib/object_present.rb
174
174
  - lib/option_value.rb
175
175
  - lib/regexp.rb
176
+ - lib/resize_terminal.rb
176
177
  - lib/rspec_helpers.rb
177
178
  - lib/saved_assets.rb
178
179
  - lib/saved_files_matcher.rb