lib/muflax/panes.rb in muflax-0.2.5 vs lib/muflax/panes.rb in muflax-0.2.6

- old
+ new

@@ -1,8 +1,8 @@ #!/usr/bin/env ruby # -*- encoding: utf-8 -*- -# Copyright Stefan Dorn <mail@muflax.com>, 2017 +# Copyright Steffi Dorn <mail@muflax.com>, 2017 # License: GNU GPLv3 (or later) <http://www.gnu.org/copyleft/gpl.html> module Kernel def clear_screen print "\e[H\e[2J" @@ -53,16 +53,16 @@ def new_section @sections << Section.new unless last_section.empty? @sections.last end - def total_lines ; @height * @columns ; end - def remaining_lines ; total_lines - @used_lines ; end - def remaining_lines_on_column ; remaining_lines % @height ; end - def used_columns ; @used_lines / @height ; end - def remaining_columns ; @columns - used_columns ; end - def column_full? ; remaining_lines_on_column <= 1 ; end + def total_lines ; @height * @columns ; end + def remaining_lines ; [total_lines - @used_lines, 0].max ; end + def remaining_lines_on_column ; remaining_lines <= 0 ? 0 : (remaining_lines % @column_height) ; end + def used_columns ; @used_lines / @height ; end + def remaining_columns ; @columns - used_columns ; end + def column_full? ; remaining_lines_on_column <= 0 ; end def new_column unless column_full? section [""]* remaining_lines_on_column last_section.divider = false @@ -72,37 +72,48 @@ def section *lines, max: :all, tail: false, header: false lines = [*lines].flatten section = new_section left = remaining_lines_on_column + rem = remaining_lines - # ap h: @height, t: total_lines, r: remaining_lines, rc: remaining_lines_on_column, l: 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 - if @need_divider - if 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) - else - @used_lines += 1 - num -= 1 unless max == :all # compensate for the divider - section.divider = @divider * @column_width - end + 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