Sha256: 233f2f2088eda5c321d17340239716ecb91bc770a3128943383b51f032b24924

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# typed: true
# frozen_string_literal: true

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

module CLI
  module UI
    module Terminal
      extend T::Sig

      DEFAULT_WIDTH = 80
      DEFAULT_HEIGHT = 24

      class << self
        extend T::Sig

        # Returns the width of the terminal, if possible
        # Otherwise will return DEFAULT_WIDTH
        #
        sig { returns(Integer) }
        def width
          winsize[1]
        end

        # Returns the width of the terminal, if possible
        # Otherwise, will return DEFAULT_HEIGHT
        #
        sig { returns(Integer) }
        def height
          winsize[0]
        end

        sig { returns([Integer, Integer]) }
        def 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

        sig { void }
        def setup_winsize_trap
          @winsize_trap ||= Signal.trap('WINCH') do
            @winsize = nil
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cli-ui-2.3.0 lib/cli/ui/terminal.rb