Sha256: 856cab6853701ac6c86b2152c68b3b4ad357aec3e033c12761e0693482c9987a

Contents?: true

Size: 1.03 KB

Versions: 55

Compression:

Stored size: 1.03 KB

Contents

require 'cli/ui'
require 'io/console'

module CLI
  module UI
    module Terminal
      DEFAULT_WIDTH = 80
      DEFAULT_HEIGHT = 24

      # Returns the width of the terminal, if possible
      # Otherwise will return DEFAULT_WIDTH
      #
      def self.width
        winsize[1]
      end

      # Returns the width of the terminal, if possible
      # Otherwise, will return DEFAULT_HEIGHT
      #
      def self.height
        winsize[0]
      end

      def self.winsize
        @winsize ||= begin
                       winsize = IO.console.winsize
                       setup_winsize_trap

                       if winsize.any?(&:zero?)
                         [DEFAULT_HEIGHT, DEFAULT_WIDTH]
                       else
                         winsize
                       end
                     rescue
                       [DEFAULT_HEIGHT, DEFAULT_WIDTH]
                     end
      end

      def self.setup_winsize_trap
        @winsize_trap ||= Signal.trap('WINCH') do
          @winsize = nil
        end
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 2 rubygems

Version Path
rfix-1.0.7.pre.67 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.7.pre.66 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.7.pre.65 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.7.pre.61 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.6.pre.60 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.7 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.5.pre.58 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.6 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.5.pre.pre.57 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.5 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.4 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.3 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.2 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.1 vendor/cli-ui/lib/cli/ui/terminal.rb
rfix-1.0.0 vendor/cli-ui/lib/cli/ui/terminal.rb