Sha256: 7b7b82c4e6aaaa8e3afc0c2f7f3decec4572869466b6e5e5723bcecad0c79ae6

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# Monkey-patch Ruby's Fixnum to provide a columns method.
# @todo Don't monkey-patch because it is naughty.
class Fixnum

  # Augment Fixnum to calculate column width in a grid-based layout.
  #
  # The grid system splits the terminal width into 12 equal parts, by dividing
  # the available width by 12. If the terminal width is not a multiple of 12,
  # then Grid chooses the maximum value which will fit.
  #
  # Used primarily at interface creation time:
  #
  #   width: 9.columns  # (Terminal width / 12) * 9 characters wide; e.g.
  #                     # Terminal is 92 characters wide, maximum value is
  #                     # therefore 84, meaning a column is 7 characters wide.
  def columns
    Vedeu::Grid.columns(self)
  end

end # Fixnum

module Vedeu

  # Divides horizontal terminal space into 12 equal columns, discarding
  # the remainder.
  #
  class Grid

    # @see (#initialize)
    def self.columns(value)
      new(value).columns
    end

    # Returns a new instance of Vedeu::Grid.
    #
    # @param value [Fixnum]
    # @return [Grid]
    def initialize(value)
      @value = value
    end

    # @raise [OutOfRange] When the value parameter is not between 1 and 12
    #   inclusive.
    # @return [Fixnum|OutOfRange]
    def columns
      fail OutOfRange,
        'Valid value between 1 and 12 inclusive.' if out_of_range?

      column * value
    end

    private

    # @!attribute [r] value
    # @return [Fixnum]
    attr_reader :value

    # Returns the width of a single column in characters.
    #
    # @return [Fixnum]
    def column
      actual / 12
    end

    # @return [Fixnum]
    def actual
      Vedeu::Terminal.width
    end

    # @return [Boolean]
    def out_of_range?
      value < 1 || value > 12
    end

  end # Grid

end # Vedeu

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vedeu-0.4.13 lib/vedeu/geometry/grid.rb
vedeu-0.4.12 lib/vedeu/geometry/grid.rb
vedeu-0.4.11 lib/vedeu/geometry/grid.rb
vedeu-0.4.10 lib/vedeu/geometry/grid.rb