reline 0.1.1 → 0.1.6

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.
@@ -1,3 +1,3 @@
1
1
  module Reline
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.6'
3
3
  end
@@ -1,6 +1,14 @@
1
1
  require 'fiddle/import'
2
2
 
3
3
  class Reline::Windows
4
+ def self.encoding
5
+ Encoding::UTF_8
6
+ end
7
+
8
+ def self.win?
9
+ true
10
+ end
11
+
4
12
  RAW_KEYSTROKE_CONFIG = {
5
13
  [224, 72] => :ed_prev_history, # ↑
6
14
  [224, 80] => :ed_next_history, # ↓
@@ -68,6 +76,8 @@ class Reline::Windows
68
76
  STD_INPUT_HANDLE = -10
69
77
  STD_OUTPUT_HANDLE = -11
70
78
  WINDOW_BUFFER_SIZE_EVENT = 0x04
79
+ FILE_TYPE_PIPE = 0x0003
80
+ FILE_NAME_INFO = 2
71
81
  @@getwch = Win32API.new('msvcrt', '_getwch', [], 'I')
72
82
  @@kbhit = Win32API.new('msvcrt', '_kbhit', [], 'I')
73
83
  @@GetKeyState = Win32API.new('user32', 'GetKeyState', ['L'], 'L')
@@ -80,9 +90,37 @@ class Reline::Windows
80
90
  @@hConsoleInputHandle = @@GetStdHandle.call(STD_INPUT_HANDLE)
81
91
  @@GetNumberOfConsoleInputEvents = Win32API.new('kernel32', 'GetNumberOfConsoleInputEvents', ['L', 'P'], 'L')
82
92
  @@ReadConsoleInput = Win32API.new('kernel32', 'ReadConsoleInput', ['L', 'P', 'L', 'P'], 'L')
93
+ @@GetFileType = Win32API.new('kernel32', 'GetFileType', ['L'], 'L')
94
+ @@GetFileInformationByHandleEx = Win32API.new('kernel32', 'GetFileInformationByHandleEx', ['L', 'I', 'P', 'L'], 'I')
95
+ @@FillConsoleOutputAttribute = Win32API.new('kernel32', 'FillConsoleOutputAttribute', ['L', 'L', 'L', 'L', 'P'], 'L')
96
+
83
97
  @@input_buf = []
84
98
  @@output_buf = []
85
99
 
100
+ def self.msys_tty?(io=@@hConsoleInputHandle)
101
+ # check if fd is a pipe
102
+ if @@GetFileType.call(io) != FILE_TYPE_PIPE
103
+ return false
104
+ end
105
+
106
+ bufsize = 1024
107
+ p_buffer = "\0" * bufsize
108
+ res = @@GetFileInformationByHandleEx.call(io, FILE_NAME_INFO, p_buffer, bufsize - 2)
109
+ return false if res == 0
110
+
111
+ # get pipe name: p_buffer layout is:
112
+ # struct _FILE_NAME_INFO {
113
+ # DWORD FileNameLength;
114
+ # WCHAR FileName[1];
115
+ # } FILE_NAME_INFO
116
+ len = p_buffer[0, 4].unpack("L")[0]
117
+ name = p_buffer[4, len].encode(Encoding::UTF_8, Encoding::UTF_16LE, invalid: :replace)
118
+
119
+ # Check if this could be a MSYS2 pty pipe ('\msys-XXXX-ptyN-XX')
120
+ # or a cygwin pty pipe ('\cygwin-XXXX-ptyN-XX')
121
+ name =~ /(msys-|cygwin-).*-pty/ ? true : false
122
+ end
123
+
86
124
  def self.getwch
87
125
  unless @@input_buf.empty?
88
126
  return @@input_buf.shift
@@ -99,7 +137,7 @@ class Reline::Windows
99
137
  return @@input_buf.shift
100
138
  end
101
139
  begin
102
- bytes = ret.chr(Encoding::UTF_8).encode(Encoding.default_external).bytes
140
+ bytes = ret.chr(Encoding::UTF_8).bytes
103
141
  @@input_buf.push(*bytes)
104
142
  rescue Encoding::UndefinedConversionError
105
143
  @@input_buf << ret
@@ -161,6 +199,20 @@ class Reline::Windows
161
199
  @@output_buf.unshift(c)
162
200
  end
163
201
 
202
+ def self.in_pasting?
203
+ not self.empty_buffer?
204
+ end
205
+
206
+ def self.empty_buffer?
207
+ if not @@input_buf.empty?
208
+ false
209
+ elsif @@kbhit.call == 0
210
+ true
211
+ else
212
+ false
213
+ end
214
+ end
215
+
164
216
  def self.get_screen_size
165
217
  csbi = 0.chr * 22
166
218
  @@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi)
@@ -205,16 +257,24 @@ class Reline::Windows
205
257
 
206
258
  def self.scroll_down(val)
207
259
  return if val.zero?
208
- scroll_rectangle = [0, val, get_screen_size.first, get_screen_size.last].pack('s4')
260
+ scroll_rectangle = [0, val, get_screen_size.last, get_screen_size.first].pack('s4')
209
261
  destination_origin = 0 # y * 65536 + x
210
262
  fill = [' '.ord, 0].pack('SS')
211
263
  @@ScrollConsoleScreenBuffer.call(@@hConsoleHandle, scroll_rectangle, nil, destination_origin, fill)
212
264
  end
213
265
 
214
266
  def self.clear_screen
215
- # TODO: Use FillConsoleOutputCharacter and FillConsoleOutputAttribute
216
- print "\e[2J"
217
- print "\e[1;1H"
267
+ csbi = 0.chr * 22
268
+ return if @@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi) == 0
269
+ buffer_width = csbi[0, 2].unpack('S').first
270
+ attributes = csbi[8, 2].unpack('S').first
271
+ _window_left, window_top, _window_right, window_bottom = *csbi[10,8].unpack('S*')
272
+ fill_length = buffer_width * (window_bottom - window_top + 1)
273
+ screen_topleft = window_top * 65536
274
+ written = 0.chr * 4
275
+ @@FillConsoleOutputCharacter.call(@@hConsoleHandle, 0x20, fill_length, screen_topleft, written)
276
+ @@FillConsoleOutputAttribute.call(@@hConsoleHandle, attributes, fill_length, screen_topleft, written)
277
+ @@SetConsoleCursorPosition.call(@@hConsoleHandle, screen_topleft)
218
278
  end
219
279
 
220
280
  def self.set_screen_size(rows, columns)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - aycabta
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-25 00:00:00.000000000 Z
11
+ date: 2020-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: io-console
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yamatanooroti
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.0.6
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.0.6
69
83
  description: Alternative GNU Readline or Editline implementation by pure Ruby.
70
84
  email:
71
85
  - aycabta@gmail.com
@@ -89,15 +103,16 @@ files:
89
103
  - lib/reline/key_stroke.rb
90
104
  - lib/reline/kill_ring.rb
91
105
  - lib/reline/line_editor.rb
106
+ - lib/reline/sibori.rb
92
107
  - lib/reline/unicode.rb
93
108
  - lib/reline/unicode/east_asian_width.rb
94
109
  - lib/reline/version.rb
95
110
  - lib/reline/windows.rb
96
111
  homepage: https://github.com/ruby/reline
97
112
  licenses:
98
- - Ruby License
113
+ - Ruby
99
114
  metadata: {}
100
- post_install_message:
115
+ post_install_message:
101
116
  rdoc_options: []
102
117
  require_paths:
103
118
  - lib
@@ -112,8 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
127
  - !ruby/object:Gem::Version
113
128
  version: '0'
114
129
  requirements: []
115
- rubygems_version: 3.1.0.pre3
116
- signing_key:
130
+ rubygems_version: 3.1.4
131
+ signing_key:
117
132
  specification_version: 4
118
133
  summary: Alternative GNU Readline or Editline implementation by pure Ruby.
119
134
  test_files: []