diff options
author | aycabta <aycabta@gmail.com> | 2021-09-04 07:29:23 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2021-09-04 21:40:12 +0900 |
commit | ec36ceb072be5b08dbdd747443192901a1c771e9 (patch) | |
tree | 86ec57709ebc82de833df50a98a4f5289f71b1eb /lib/reline/line_editor.rb | |
parent | ff372ed74b4cedd8252e589d2b89c59b0bd8d763 (diff) | |
download | ruby-ec36ceb072be5b08dbdd747443192901a1c771e9.tar.gz |
[ruby/reline] Implement scrollbar
https://github.com/ruby/reline/commit/4c7fc42151
Diffstat (limited to 'lib/reline/line_editor.rb')
-rw-r--r-- | lib/reline/line_editor.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index d3020087bf..e52c4e5fd3 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -661,6 +661,13 @@ class Reline::LineEditor reset_dialog(dialog, old_dialog) move_cursor_down(dialog.vertical_offset) Reline::IOGate.move_cursor_column(dialog.column) + if dialog_render_info.scrollbar and dialog_render_info.contents.size > height + bar_max_height = height * 2 + moving_distance = (dialog_render_info.contents.size - height) * 2 + position_ratio = dialog.scroll_top.zero? ? 0.0 : ((dialog.scroll_top * 2).to_f / moving_distance) + bar_height = (bar_max_height * ((dialog.contents.size * 2).to_f / (dialog_render_info.contents.size * 2))).floor.to_i + position = ((bar_max_height - bar_height) * position_ratio).floor.to_i + end dialog.contents.each_with_index do |item, i| if i == pointer bg_color = '45' @@ -672,10 +679,22 @@ class Reline::LineEditor end end str = padding_space_with_escape_sequences(Reline::Unicode.take_range(item, 0, dialog.width), dialog.width) + if dialog_render_info.scrollbar and dialog_render_info.contents.size > height + if position <= (i * 2) and (i * 2) <= (position + bar_height) + str += '█' + elsif position <= (i * 2) and (i * 2 - 1) <= (position + bar_height) + str += '▀' + elsif position <= (i * 2 + 1) and (i * 2) <= (position + bar_height) + str += '▄' + else + str += ' ' + end + end @output.write "\e[#{bg_color}m#{str}\e[49m" Reline::IOGate.move_cursor_column(dialog.column) move_cursor_down(1) if i < (dialog.contents.size - 1) end + dialog.width += 1 if dialog_render_info.scrollbar and dialog_render_info.contents.size > height Reline::IOGate.move_cursor_column(cursor_column) move_cursor_up(dialog.vertical_offset + dialog.contents.size - 1) Reline::IOGate.show_cursor |