Skip to content

Commit 2c5ee54

Browse files
committed
clear_screen use Windows API
1 parent 4d334d9 commit 2c5ee54

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/reline/windows.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def call(*args)
9292
@@ReadConsoleInput = Win32API.new('kernel32', 'ReadConsoleInput', ['L', 'P', 'L', 'P'], 'L')
9393
@@GetFileType = Win32API.new('kernel32', 'GetFileType', ['L'], 'L')
9494
@@GetFileInformationByHandleEx = Win32API.new('kernel32', 'GetFileInformationByHandleEx', ['L', 'I', 'P', 'L'], 'I')
95+
@@FillConsoleOutputAttribute = Win32API.new('kernel32', 'FillConsoleOutputAttribute', ['L', 'L', 'L', 'L', 'P'], 'L')
9596

9697
@@input_buf = []
9798
@@output_buf = []
@@ -249,9 +250,15 @@ def self.scroll_down(val)
249250
end
250251

251252
def self.clear_screen
252-
# TODO: Use FillConsoleOutputCharacter and FillConsoleOutputAttribute
253-
write "\e[2J"
254-
write "\e[1;1H"
253+
coord_screen_top_left = 0
254+
written = 0.chr * 4
255+
csbi = 0.chr * 22
256+
return if @@GetConsoleScreenBufferInfo.call(@@hConsoleHandle, csbi) == 0
257+
con_size = csbi[0, 4].unpack('SS').inject(:*)
258+
attributes = csbi[8, 2].unpack('S').first
259+
@@FillConsoleOutputCharacter.call(@@hConsoleHandle, 0x20, con_size, coord_screen_top_left, written)
260+
@@FillConsoleOutputAttribute.call(@@hConsoleHandle, attributes, con_size, coord_screen_top_left, written)
261+
@@SetConsoleCursorPosition.call(@@hConsoleHandle, coord_screen_top_left)
255262
end
256263

257264
def self.set_screen_size(rows, columns)

0 commit comments

Comments
 (0)