Sha256: ea50b533ebb9e94fc7536d1399ab2a791204ad14432278f6e39427479e0168ea

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

require 'io/console'

require 'vedeu/support/esc'

module Vedeu
  module Terminal
    extend self
    # :nocov:
    def open(mode, &block)
      @mode = mode

      if block_given?
        if raw_mode?
          console.raw    { initialize_screen { yield } }

        else
          console.cooked { initialize_screen { yield } }

        end
      end
    ensure
      restore_screen
    end

    def input
      if raw_mode?
        keys = console.getch
        if keys.ord == 27
          keys << console.read_nonblock(3) rescue nil
          keys << console.read_nonblock(2) rescue nil
        end
        keys

      else
        console.gets.chomp

      end
    end
    # :nocov:

    def output(stream = '')
      console.print(stream)

      stream
    end

    # :nocov:
    def initialize_screen(&block)
      output Esc.string 'reset'
      output Esc.string 'clear'
      output Esc.string 'hide_cursor'

      yield
    end

    def restore_screen
      output Esc.string 'show_cursor'
      output Esc.string 'reset'
      output clear_last_line
    end

    def set_cursor_mode
      output Esc.string 'show_cursor' unless raw_mode?
    end

    def raw_mode?
      @mode == :raw
    end
    # :nocov:

    def clear_last_line
      Esc.set_position((height - 1), 1) + "\e[2K"
    end

    def centre
      [(height / 2), (width / 2)]
    end

    def width
      size.last
    end

    def height
      size.first
    end

    def size
      console.winsize
    end

    def console
      IO.console
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vedeu-0.1.7 lib/vedeu/support/terminal.rb
vedeu-0.1.6 lib/vedeu/support/terminal.rb
vedeu-0.1.5 lib/vedeu/support/terminal.rb
vedeu-0.1.4 lib/vedeu/support/terminal.rb
vedeu-0.1.3 lib/vedeu/support/terminal.rb
vedeu-0.1.2 lib/vedeu/support/terminal.rb
vedeu-0.1.1 lib/vedeu/support/terminal.rb
vedeu-0.1.0 lib/vedeu/support/terminal.rb
vedeu-0.0.42 lib/vedeu/support/terminal.rb
vedeu-0.0.41 lib/vedeu/support/terminal.rb
vedeu-0.0.40 lib/vedeu/support/terminal.rb