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.4.1 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
rfix-1.4.0.pre.201 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
rfix-1.4.0 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.5.0 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
rfix-1.3.0.pre.199 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.4.1 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.4.0 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
rfix-1.2.6.pre.198 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.3.1 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.3.0 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.2.0 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.1.2 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
rfix-1.2.6 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.1.1 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.1.0 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.0.5 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.0.4 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
shopify-cli-1.0.3 vendor/deps/cli-ui/lib/cli/ui/terminal.rb
rfix-1.2.2.pre.174 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb
rfix-1.2.5 vendor/shopify/cli-ui/lib/cli/ui/terminal.rb