Sha256: 5deca74229b3b852f9c46a4fee24bbfe9f449c0b4c1cbb0df012f7b77730a6fd

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module WhirledPeas
  module Utils
    # Helper module for working with ANSI escape codes. The most useful ANSI escape codes
    # relate to text formatting.
    #
    # @see https://en.wikipedia.org/wiki/ANSI_escape_code
    module Ansi
      ESC = "\033"

      END_FORMATTING = 0
      private_constant :END_FORMATTING

      # Text formatting constants
      BOLD = 1
      UNDERLINE = 4

      # Text and background color constants
      BLACK = 30
      RED = 31
      GREEN = 32
      YELLOW = 33
      BLUE = 34
      MAGENTA = 35
      CYAN = 36
      WHITE = 37

      # Bright colors are offset by this much from their standard versions
      BRIGHT_OFFSET = 60

      class << self
        def with_screen(output=STDOUT, width: nil, height: nil, &block)
          require 'highline'
          unless width && height
            width, height = HighLine.new.terminal.terminal_size
          end
          output.print cursor_visible(false)
          output.flush
          yield width, height
        ensure
          output.print clear
          output.print cursor_pos(left: 0, top: height - 1)
          output.print cursor_visible(true)
          output.flush
        end

        def cursor_pos(left:, top:)
          "#{ESC}[#{top + 1};#{left + 1}H"
        end

        def cursor_visible(visible)
          visible ? "#{ESC}[?25h" : "#{ESC}[?25l"
        end

        def clear_down
          "#{ESC}[J"
        end

        def clear
          esc_seq(END_FORMATTING)
        end

        def esc_seq(code)
          "#{ESC}[#{code}m"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
whirled_peas-0.13.0 lib/whirled_peas/utils/ansi.rb
whirled_peas-0.12.0 lib/whirled_peas/utils/ansi.rb