Class: RubyTUI::Widgets::Tabs

Inherits:
Object
  • Object
show all
Includes:
RubyTUI::Widget
Defined in:
lib/rubytui/widgets/tabs.rb

Overview

Tabs renders a horizontal tab bar with selectable tabs.

Examples:

tabs = RubyTUI::Widgets::Tabs.new(titles: ["Home", "Logs", "Help"], selected: 1)
frame.render_widget(tabs, area)

Instance Method Summary collapse

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.

Parameters:

  • titles (Array<#to_s>) (defaults to: []) —

    tab titles (default: [])

  • selected (Integer) (defaults to: 0) —

    index of the active tab (default: 0)

  • style (Style) (defaults to: Style::DEFAULT) —

    base style, patched with active_style or inactive_style (default: Style::DEFAULT)

  • active_style (Style, nil) (defaults to: nil) —

    style of the active tab (default: bold yellow foreground)

  • inactive_style (Style, nil) (defaults to: nil) —

    style of inactive tabs and dividers (default: bright black foreground)

  • divider (String) (defaults to: " \u2502 ") —

    separator drawn between tabs (default: " │ ")

  • highlight_style (Style, nil) (defaults to: nil) —

    currently unused by rendering (default: nil)

  • block (Widgets::Block, nil) (defaults to: nil) —

    optional wrapping block



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.

Parameters:

  • area (Rect) —

    the rectangular area to render into

  • buf (Buffer) —

    the buffer to write cells into



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