Sha256: 6fc599224bed06d7b9bb08f5513e4a6dd62822d70fab1c4c48c92568bafa6e79

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'amun/windows/base'
require 'amun/helpers/colors'
require 'amun/mode_line_segments/major_mode'
require 'amun/mode_line_segments/buffer_name'

module Amun
  module Windows
    # a line of small segments that display
    # information about the current window,
    # like mode name, line number, buffer name...etc
    class ModeLine < Base
      attr_reader :left_segments, :right_segments

      def initialize(size)
        super(size)
        @right_segments = []
        @left_segments = [
          ModeLineSegments::BufferName.new,
          ModeLineSegments::MajorMode.new
        ]

        Helpers::Colors.register_default(:mode_line, 0, 255)
      end

      def render(buffer)
        right_output = render_segments(right_segments, buffer)
        left_output = render_segments(left_segments, buffer)
        filler = empty_space(right_output, left_output)

        curses_window.erase
        Helpers::Colors.print(curses_window, *left_output, filler, *right_output)
        curses_window.refresh
      end

      private

      def empty_space(right_output, left_output)
        text_size = (right_output + left_output).map(&:size).inject(0, :+)
        empty_space = [0, width - text_size].max
        (' ' * empty_space).colorize(:mode_line)
      end

      def render_segments(segments, buffer)
        segments.map do |segment|
          segment.render(buffer)
        end.flatten
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amun-0.2.0 lib/amun/windows/mode_line.rb