class TerminalNotes::Widget

Constants

TITLE_PADDING

Attributes

height[R]
width[R]
window[R]

Public Class Methods

new(parent:, title:, width: 1, height: 1, x: 0, y: 0, align: :center, valign: :center, border: true) click to toggle source
# File lib/terminal-notes/widget.rb, line 7
def initialize(parent:, title:, width: 1, height: 1,
               x: 0, y: 0, align: :center, valign: :center,
               border: true)
  @parent = parent
  @title  = title || ""

  @width  = width
  @height = height

  parent_size = @parent.size

  # Relative layout calculation
  remaining_width = @width
  if @width <= 1
    remaining_width = parent_size[:columns] - x
    @width = (@width * remaining_width).to_i
  end

  remaining_height = @height
  if @height <= 1
    remaining_height = parent_size[:lines] - y
    @height = (@height * remaining_height).to_i
  end

  if align == :center
    pos_x = x + (remaining_width - @width) / 2
    @title_x = (@width - @title.size) / 2
  elsif align == :right
    pos_x = (remaining_width - @width)
    @title_x = @width - @title.size - TITLE_PADDING
  else
    pos_x = x # do nothing
    @title_x = TITLE_PADDING
  end

  if valign == :center
    pos_y = y + (remaining_height - @height) / 2
  elsif valign == :bottom
    pos_y = y + (remaining_height - @height)
  else
    pos_y = y # do nothing
  end

  @position = Cursor.new(x: pos_x, y: pos_y)

  @window = Rurses::Window.new(
    lines: @height, columns: @width,
    x: @position.x, y: @position.y)

  @has_border = border

  # @window.refresh_in_memory
end

Public Instance Methods

draw() { || ... } click to toggle source
# File lib/terminal-notes/widget.rb, line 66
def draw
  old_cursor = @window.cursor_xy

  # Draw title
  if @has_border || !@title.empty?
    @window.draw_border
    @window.move_cursor(x: @title_x, y: 0)
    @window.draw_string(" #{@title} ")
  end

  yield if block_given?

  @window.move_cursor(old_cursor)
  @window.refresh_in_memory
end
focus() click to toggle source
# File lib/terminal-notes/widget.rb, line 82
def focus
end
redraw() click to toggle source
# File lib/terminal-notes/widget.rb, line 61
def redraw
  @window.clear
  draw
end
resize() click to toggle source
# File lib/terminal-notes/widget.rb, line 85
def resize
  calculate_and_draw
end

Private Instance Methods

calculate_and_draw(new_window: false) click to toggle source
# File lib/terminal-notes/widget.rb, line 91
def calculate_and_draw(new_window: false)
  @window.clear
  @window.refresh_in_memory
end