Class: Panes

Inherits:
Object show all
Defined in:
lib/muflax/panes.rb

Defined Under Namespace

Classes: Section

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns: 2, separator: "|", divider: "=") ⇒ Panes

Returns a new instance of Panes.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/muflax/panes.rb', line 36

def initialize columns: 2, separator: "|", divider: "="
  # can't be bigger than the terminal
  @width, @height = HighLine::SystemExtensions.terminal_size
  @separator      = separator
  @divider        = divider
  seps            = (columns-1) * separator.size
  @columns        = columns
  @column_width   = @width / columns - seps
  @column_height  = @height
  @sections       = []

  # keep track of drawing positions
  @used_lines   = 0
  @need_divider = false
end

Instance Attribute Details

#column_heightObject (readonly)

Returns the value of attribute column_height.



32
33
34
# File 'lib/muflax/panes.rb', line 32

def column_height
  @column_height
end

#column_widthObject (readonly)

Returns the value of attribute column_width.



32
33
34
# File 'lib/muflax/panes.rb', line 32

def column_width
  @column_width
end

#columnsObject (readonly)

Returns the value of attribute columns.



31
32
33
# File 'lib/muflax/panes.rb', line 31

def columns
  @columns
end

#heightObject (readonly)

Returns the value of attribute height.



31
32
33
# File 'lib/muflax/panes.rb', line 31

def height
  @height
end

#remaining_columnsObject (readonly)

Returns the value of attribute remaining_columns.



33
34
35
# File 'lib/muflax/panes.rb', line 33

def remaining_columns
  @remaining_columns
end

#remaining_linesObject (readonly)

Returns the value of attribute remaining_lines.



33
34
35
# File 'lib/muflax/panes.rb', line 33

def remaining_lines
  @remaining_lines
end

#sectionsObject (readonly)

Returns the value of attribute sections.



34
35
36
# File 'lib/muflax/panes.rb', line 34

def sections
  @sections
end

#widthObject (readonly)

Returns the value of attribute width.



31
32
33
# File 'lib/muflax/panes.rb', line 31

def width
  @width
end

Instance Method Details

#column_full?Boolean

Returns:

  • (Boolean)


67
# File 'lib/muflax/panes.rb', line 67

def column_full?              ; remaining_lines_on_column <= 0                                ; end

#last_sectionObject



52
53
54
55
# File 'lib/muflax/panes.rb', line 52

def last_section
  @sections << Section.new if @sections.empty?
  @sections.last
end

#new_columnObject



69
70
71
72
73
74
75
# File 'lib/muflax/panes.rb', line 69

def new_column
  unless column_full?
    section [""]* remaining_lines_on_column
    last_section.divider = false
    last_section.lines << ""
  end
end

#new_sectionObject



57
58
59
60
# File 'lib/muflax/panes.rb', line 57

def new_section
  @sections << Section.new unless last_section.empty?
  @sections.last
end

#remaining_lines_on_columnObject



64
# File 'lib/muflax/panes.rb', line 64

def remaining_lines_on_column ; remaining_lines <= 0 ? 0 : (remaining_lines % @column_height) ; end

#section(*lines, max: :all, tail: false, header: false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/muflax/panes.rb', line 77

def section *lines, max: :all, tail: false, header: false
  lines   = [*lines].flatten
  section = new_section
  left    = remaining_lines_on_column
  rem     = remaining_lines

  num = case max
        when :all             ; lines.size
        when :column          ; left == 0 ? @column_height : left
          # when :full_column ; @column_height
        else                  ; raise "unknown: #{max}"
        end

  num = [num, remaining_lines].min
  @used_lines += num

  case
  when header
    h               = lines.first
    lines           = lines[1..-1]
    div             = h.sub(/^\s+/){|s| @divider*s.size}.sub(/\s+$/){|s| @divider*s.size}
    section.divider = div + @divider * (@column_width - div.str_length)

  when @need_divider
    @used_lines     += 1
    num             -= 1 if max != :all or num >= rem # compensate for the divider
    section.divider = @divider * @column_width
  end

  num           = [num, rem].min
  to_print      = tail ? lines.last(num) : lines.first(num)
  @need_divider = ! column_full?

  # ap(
  #   height:     @height,
  #   total:      total_lines,
  #   rem:        rem,
  #   left:       left,
  #   rem_lines:  remaining_lines,
  #   rem_column: remaining_lines_on_column,
  #   lines:      lines,
  #   num:        num
  #   )

  to_print.each do |line|
    section << line.truncate(@column_width, omission: "⇏")
  end
end

#show!Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/muflax/panes.rb', line 126

def show!
  output = []
  @sections.each do |s|
    output << s.divider unless s.divider.nil?
    output += s.lines
  end

  # ap w: @width, h: @height, cw: @column_width, ch: @column_height, c: @columns
  # ap ul: @used_lines, os: output.size
  # ap output

  max_row = @column_height - 1
  max_col = @columns - 1

  clear_screen

  (0..max_row).each do |row|
    (0..max_col).each do |col|
      i   = col * @column_height + row
      out = output[i]&.rstrip || ""
      print out
      print " " * (@column_width - out.str_length)
      print HighLine.color("", :reset)

      if col < max_col    ; print @separator
      elsif row < max_row ; print "\n"
      else                ; # skip
      end
    end
  end
end

#total_linesObject



62
# File 'lib/muflax/panes.rb', line 62

def total_lines               ; @height * @columns                                            ; end

#used_columnsObject



65
# File 'lib/muflax/panes.rb', line 65

def used_columns              ; @used_lines / @height                                         ; end