Class: RubyTUI::Widgets::Tabs
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::Tabs
- Includes:
- RubyTUI::Widget
- Defined in:
- lib/rubytui/widgets/tabs.rb
Overview
Tabs renders a horizontal tab bar with selectable tabs.
Instance Method Summary collapse
-
#initialize(titles: [], selected: 0, style: Style::DEFAULT, active_style: nil, inactive_style: nil, divider: " \u2502 ", highlight_style: nil, block: nil) ⇒ Tabs
constructor
A new instance of Tabs.
-
#render(area, buf) ⇒ void
Draw the titles left to right on the first row of the area, separated by
dividerand truncated at the right edge.
Constructor Details
#initialize(titles: [], selected: 0, style: Style::DEFAULT, active_style: nil, inactive_style: nil, divider: " \u2502 ", highlight_style: nil, block: nil) ⇒ Tabs
Returns a new instance of Tabs.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rubytui/widgets/tabs.rb', line 23 def initialize( titles: [], selected: 0, style: Style::DEFAULT, active_style: nil, inactive_style: nil, divider: " \u2502 ", highlight_style: nil, block: nil ) @titles = titles @selected = selected @style = style @active_style = active_style || Style.new.fg(Color::YELLOW).bold @inactive_style = inactive_style || Style.new.fg(Color::BRIGHT_BLACK) @divider = divider @highlight_style = highlight_style @block = block end |
Instance Method Details
#render(area, buf) ⇒ void
This method returns an undefined value.
Draw the titles left to right on the first row of the area, separated by
divider and truncated at the right edge.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rubytui/widgets/tabs.rb', line 49 def render(area, buf) return if area.empty? render_area = area if @block @block.render(area, buf) render_area = @block.inner(area) end return if render_area.empty? || @titles.empty? x = render_area.x y = render_area.y @titles.each_with_index do |title, i| break if x >= render_area.right is_active = i == @selected tab_style = is_active ? @style.patch(@active_style) : @style.patch(@inactive_style) # Render tab title display = title.to_s available = render_area.right - x display = Buffer.truncate_to_width(display, available) display_width = Buffer.string_width(display) buf.set_string(x, y, display, tab_style) x += display_width # Render divider (except after last tab) if i < @titles.length - 1 available = render_area.right - x divider_text = Buffer.truncate_to_width(@divider, available) divider_width = Buffer.string_width(divider_text) buf.set_string(x, y, divider_text, @style.patch(@inactive_style)) x += divider_width end end end |