Sha256: 1834031c243cb214d900a47e6e50deae4c426e99a2b918b163a6e2ee96ad1c77

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Doomfire
  class WindowSize
    def terminal_width
      return 80 unless unix?

      result = dynamic_width
      result < 20 ? 80 : result
    rescue StandardError
      80
    end

    # rubocop:disable Lint/DuplicateMethods
    begin
      require 'io/console'

      def dynamic_width
        if IO.console
          dynamic_width_via_io_object
        else
          dynamic_width_via_system_calls
        end
      end
    rescue LoadError
      def dynamic_width
        dynamic_width_via_system_calls
      end
    end
    # rubocop:enable Lint/DuplicateMethods

    def dynamic_width_via_io_object
      _rows, columns = IO.console.winsize
      columns
    end

    def dynamic_width_via_system_calls
      dynamic_width_stty.nonzero? || dynamic_width_tput
    end

    def dynamic_width_stty
      `stty size 2>/dev/null`.split[1].to_i
    end

    def dynamic_width_tput
      `tput cols 2>/dev/null`.to_i
    end

    def unix?
      RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
doomfire-0.3.2 lib/doomfire/window_size.rb
doomfire-0.3.1 lib/doomfire/window_size.rb
doomfire-0.3.0 lib/doomfire/window_size.rb
doomfire-0.2 lib/doomfire/window_size.rb
doomfire-0.1.0 lib/doomfire/window_size.rb