Skip to content

Commit

Permalink
[ruby/reline] Check the result of GetConsoleScreenBufferInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and matzbot committed Oct 12, 2021
1 parent 1009fd7 commit a48dc89
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/reline/windows.rb
Expand Up @@ -283,15 +283,23 @@ def self.empty_buffer?
end
end

def self.get_screen_size
def self.get_console_screen_buffer_info
csbi = 0.chr * 22
@@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi)
return if @@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi) == 0
csbi
end

def self.get_screen_size
unless csbi = get_console_screen_buffer_info
return [1, 1]
end
csbi[0, 4].unpack('SS').reverse
end

def self.cursor_pos
csbi = 0.chr * 22
@@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi)
unless csbi = get_console_screen_buffer_info
return Reline::CursorPos.new(0, 0)
end
x = csbi[4, 2].unpack1('s*')
y = csbi[6, 2].unpack1('s*')
Reline::CursorPos.new(x, y)
Expand All @@ -313,6 +321,7 @@ def self.move_cursor_up(val)

def self.move_cursor_down(val)
if val > 0
return unless csbi = get_console_screen_buffer_info
screen_height = get_screen_size.first
y = cursor_pos.y + val
y = screen_height - 1 if y > (screen_height - 1)
Expand All @@ -323,8 +332,7 @@ def self.move_cursor_down(val)
end

def self.erase_after_cursor
csbi = 0.chr * 22
@@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi)
return unless csbi = get_console_screen_buffer_info
attributes = csbi[8, 2].unpack1('S')
cursor = csbi[4, 4].unpack1('L')
written = 0.chr * 4
Expand All @@ -343,8 +351,7 @@ def self.scroll_down(val)
end

def self.clear_screen
csbi = 0.chr * 22
return if @@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi) == 0
return unless csbi = get_console_screen_buffer_info
buffer_width = csbi[0, 2].unpack1('S')
attributes = csbi[8, 2].unpack1('S')
_window_left, window_top, _window_right, window_bottom = *csbi[10,8].unpack('S*')
Expand Down

0 comments on commit a48dc89

Please sign in to comment.