class DbSucker::Application::Window::Dialog

Constants

BOX

Attributes

border_color[RW]

Public Class Methods

new(window, &block) click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 21
def initialize window, &block
  @window = window
  @width = false
  @border_color = :yellow
  @border_padding = 1
  @separator_padding = 1
  @lines = []
  block.call(self)
end

Public Instance Methods

_bborder() click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 122
def _bborder
  bl = BOX[:bl]
  br = BOX[:br]
  m = "".ljust(@width - bl.length - br.length, BOX[:b])
  @window.send(@border_color, "#{bl}#{m}#{br}")
end
_hr() click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 90
def _hr
  l = BOX[:hrl]
  r = BOX[:hrr]
  m = "".ljust(@width - l.length - r.length, BOX[:hr])
  @separator_padding.times { _line }
  @window.send(@border_color, "#{l}#{m}#{r}")
  _nl
end
_line(parts = []) click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 99
def _line parts = []
  return _hr if parts == :hr
  l = BOX[:l]
  r = BOX[:r]
  p = "".ljust(@border_padding, " ")

  @window.send(@border_color, "#{l}#{p}")
  parts.each do |str, color|
    @window.send(color, str)
  end
  @window.send(@border_color, "".ljust(@width - l.length - r.length - (p.length * 2) - parts.map{|s, _| s.length }.sum, " "))
  @window.send(@border_color, "#{p}#{r}")
  _nl
end
_nl() click to toggle source

# File lib/db_sucker/application/window/dialog.rb, line 86
def _nl
  @window.next_line
end
_tborder() click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 114
def _tborder
  tl = BOX[:tl]
  tr = BOX[:tr]
  m = "".ljust(@width - tl.length - tr.length, BOX[:t])
  @window.send(@border_color, "#{tl}#{m}#{tr}")
  _nl
end
br() click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 39
def br
  @lines << []
end
build_button(str, color = :yellow) click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 51
def build_button str, color = :yellow
  tl = BOX[:tl]
  t = BOX[:t]
  tr = BOX[:tr]
  l = BOX[:l]
  r = BOX[:r]
  bl = BOX[:bl]
  b = BOX[:b]
  br = BOX[:br]
  width = str.length + 2
  [].tap do |a|
    a << [["#{tl}" << "".ljust(width, t) << "#{tr}", color]]
    a << [["#{l}" << " #{str} ".ljust(width, t) << "#{r}", color]]
    a << [["#{bl}" << "".ljust(width, b) << "#{br}", color]]
  end
end
button(*a) click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 47
def button *a
  @lines.concat(build_button(*a))
end
button_group(buttons = [], spaces = 2, &block) click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 68
def button_group buttons = [], spaces = 2, &block
  spaces = buttons if block
  btns = block ? [].tap{|a| block.call(a) } : buttons.dup
  spaces = "".ljust(spaces, " ")
  first = btns.shift

  btns.each_with_index do |lines, bi|
    lines.each_with_index do |l, i|
      first[i] << [spaces, :yellow]
      first[i].concat(l)
    end
  end

  @lines.concat first
end
hr() click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 43
def hr
  @lines << :hr
end
line(str = nil, color = :yellow, &block) click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 31
def line str = nil, color = :yellow, &block
  if block
    @lines << [].tap{|a| block.call(a) }
  else
    @lines << [[str, color]]
  end
end
render!() click to toggle source
# File lib/db_sucker/application/window/dialog.rb, line 129
def render!
  @width = @lines.map{|c| c.is_a?(Array) ? c.map{|s, _| s.length }.sum : 0 }.compact.max + (@border_padding * 2) + BOX[:l].length + BOX[:r].length

  _tborder
  @border_padding.times { _line }
  @lines.each {|parts| _line(parts) }
  @border_padding.times { _line }
  _bborder
end