Skip to content

Commit

Permalink
Implement scrollbar
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Sep 3, 2021
1 parent c77c3c6 commit 4c7fc42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def match?(key)
end
end
CursorPos = Struct.new(:x, :y)
DialogRenderInfo = Struct.new(:pos, :contents, :pointer, :bg_color, :width, :height, keyword_init: true)
DialogRenderInfo = Struct.new(:pos, :contents, :pointer, :bg_color, :width, :height, :scrollbar, keyword_init: true)

class Core
ATTR_READER_NAMES = %i(
Expand Down Expand Up @@ -228,7 +228,7 @@ def get_screen_size
context.clear
context.push(cursor_pos_to_render, result, pointer, dialog)
end
DialogRenderInfo.new(pos: cursor_pos_to_render, contents: result, pointer: pointer, height: 15)
DialogRenderInfo.new(pos: cursor_pos_to_render, contents: result, pointer: pointer, scrollbar: true, height: 15)
}
Reline::DEFAULT_DIALOG_CONTEXT = Array.new

Expand Down
19 changes: 19 additions & 0 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,13 @@ def add_dialog_proc(name, p, context = nil)
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'
Expand All @@ -672,10 +679,22 @@ def add_dialog_proc(name, p, context = nil)
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
Expand Down

0 comments on commit 4c7fc42

Please sign in to comment.