Sha256: c3b52018e09ba4f3063e7590c1883a5c948cf170393f9ed9ab26ff948b928c6d

Contents?: true

Size: 1.62 KB

Versions: 12

Compression:

Stored size: 1.62 KB

Contents

module ANSI

  module Terminal
    require "termios"             # Unix, first choice.

    module_function

    #CHARACTER_MODE = "termios"    # For Debugging purposes only.

    #
    # Unix savvy getc().  (First choice.)
    #
    # *WARNING*:  This method requires the "termios" library!
    #
    def get_character( input = STDIN )
      old_settings = Termios.getattr(input)

      new_settings                     =  old_settings.dup
      new_settings.c_lflag             &= ~(Termios::ECHO | Termios::ICANON)
      new_settings.c_cc[Termios::VMIN] =  1

      begin
        Termios.setattr(input, Termios::TCSANOW, new_settings)
        input.getc
      ensure
        Termios.setattr(input, Termios::TCSANOW, old_settings)
      end
    end

    # A Unix savvy method to fetch the console columns, and rows.
    def terminal_size
      if /solaris/ =~ RUBY_PLATFORM and
          `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
        [$2, $1].map { |c| x.to_i }
      else
        `stty size`.split.map { |x| x.to_i }.reverse
      end
    end

    # Console screen width (taken from progress bar)
    #
    # NOTE: Don't know how portable #screen_width is.
    # TODO: How to fit in to system?
    #
    def screen_width(out=STDERR)
      default_width = ENV['COLUMNS'] || 76
      begin
        tiocgwinsz = 0x5413
        data = [0, 0, 0, 0].pack("SSSS")
        if out.ioctl(tiocgwinsz, data) >= 0 then
          rows, cols, xpixels, ypixels = data.unpack("SSSS")
          if cols >= 0 then cols else default_width end
        else
          default_width
        end
      rescue Exception
        default_width
      end
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ansi-1.4.1 lib/ansi/terminal/termios.rb
ansi-1.4.0 lib/ansi/terminal/termios.rb
ansi-1.3.0 lib/ansi/terminal/termios.rb
ansi-1.2.5 lib/ansi/terminal/termios.rb
ansi-1.2.4 lib/ansi/terminal/termios.rb
ansi-1.2.3 lib/ansi/terminal/termios.rb
ansi-1.2.2 lib/ansi/terminal/termios.rb
ansi-1.2.1 lib/ansi/terminal/termios.rb
ansi-1.2.0 lib/ansi/terminal/termios.rb
ansi-1.1.0 lib/ansi/terminal/termios.rb
ansi-1.0.1 lib/ansi/terminal/termios.rb
ansi-1.0.0 lib/ansi/terminal/termios.rb