struct

Grid

Inherits TopDown < LeftRight < Struct < Value < Object

Grid is a simple string grid formatter library for crystal programming language.

Example:

grid = Grid.new("Rubys Crystals Emeralds Sapphires a b") # Create a new Grid instance

grid.auto(18, true) # generate top-down grid with 18 char as max canvas width
grid.to_s(true) # get the string format (true) in top-down direction
# Rubys    Sapphires
# Crystals a        
# Emeralds b        

grid.auto(18, false) # generate left-right grid with 18 char as max canvas width
grid.to_s(false) # get the string format (false) in left-right direction
# Rubys    Crystals 
# Emeralds Sapphires
# a        b

grid = Grid.new("Rubys Crystals Emeralds Sapphires a b", "|") # Create a new Grid instance with custom separator
grid.auto(18, true) # generate left-right grid with 18 char as max canvas width
grid.to_s(true, false) # get the string format (true) in top-down direction (false) align-right
#    Rubys|Sapphires
# Crystals|        a
# Emeralds|        b

Constants

VERSION = "0.1.1"

Constructors

new(list : Array(String), separator : String = " ")

Initialize grid list with type of Array(String) as a input parameter. Example:

grid = Grid.new(["Ruby", "Crystal", "Emerald", "Sapphire"])
grid = Grid.new() # produce empty string
grid = Grid.new(["Ruby", "Crystal", "Emerald", "Sapphire"], " | ") # with custom separator
Source
new(str : String = "", separator : String = " ")

Initialize grid list with type of String as a input parameter.

Example:

grid = Grid.new("Ruby Crystal Emerald Sapphire")
grid = Grid.new() # produce empty list
grid = Grid.new("Ruby Crystal Emerald Sapphire", " | ") # with custom separator
Source

Instance methods

auto(max_w : Int32 = 24, top_down : Bool = true)

Generate the virtual canvas based on the current @list and specified max width. The max width default value is 24. The second parameter top_down specified the direction of item. True if top-down and false if left-right.

Example:

grid = Grid.new("Rubys Crystals Emeralds Sapphires a b") # Create a new Grid instance

grid.auto(18, true) # generate top-down grid with 18 char as max canvas width    
# => [["Rubys", "Crystals", "Emeralds"], ["Sapphires", "a", "b"]]

grid.auto(18, false) # generate left-right grid with 18 char as max canvas width
# => [["Rubys", "Crystals"], ["Emeralds", "Sapphires"], ["a", "b"]]
Source
delimiter_count_of(col_count : Int32) : Int32

Count the delimiter of specified column count.

Example:

delimiter_count_of(3) # => 2
delimiter_count_of(7) # => 6
Source
list

Holds the list of String from the user.

Example:

grid = grid.new("str_1 str_2 str_3")
grid.list # => ["str_1", "str_2", "str_3"]
Source
list=(str : String)

Set the list from a string.

Example:

grid = grid.new("")
grid.list = "str_1 str_2 str_3"
grid.list # => ["str_1", "str_2", "str_3"]
Source
list=(list : Array(String))

Set the list from an Array().

Example:

grid = grid.new("")
grid.list = ["str_1", "str_2", "str_3"]
grid.list # => ["str_1", "str_2", "str_3"]
Source
max_width

Holds the max width of the canvas. Its defined by the user. Default value is 24.

Source
max_width=(max_width : Int32)

Holds the max width of the canvas. Its defined by the user. Default value is 24.

Source
separator

Holds the separator specified by the user. Default " " (a single space)

Source
to_s(top_down : Bool = true, align_left : Bool = true, sep : String = @separator) : String

Convert all elements in canvas to a single string using String#build. The first parameter top_down default is true. The second parameter align_left default is true. The third parameter separator default is ' ' (single space) || it follows the separator specified from auto.

Example:

grid = Grid.new("Rubys Crystals Emeralds Sapphires a b") # Create a new Grid instance

grid.auto(18, true) # generate top-down grid with 18 char as max canvas width
# => [["Rubys", "Crystals", "Emeralds"], ["Sapphires", "a", "b"]]

grid.to_s(true) # get the string format (true) in top-down direction
# Rubys    Sapphires
# Crystals a        
# Emeralds b        

grid.to_s(true, false)
#    Rubys Sapphires
# Crystals         a
# Emeralds         b

grid.auto(18, false) # generate left-right grid with 18 char as max canvas width
# => [["Rubys", "Crystals"], ["Emeralds", "Sapphires"], ["a", "b"]]

grid.to_s(false) # get the string format (false) in left-right direction
# Rubys    Crystals 
# Emeralds Sapphires
# a        b

grid.to_s(false, false)
#    Rubys  Crystals
# Emeralds Sapphires
#        a         b

Let's see another example for custom separator.

grid = Grid.new("Rubys Crystals Emeralds Sapphires a b", " | ") grid.auto(20, true) grid.to_s(true, false)

Rubys | Sapphires

Crystals | a

Emeralds | b

grid.to_s(true, false, "---")

Rubys---Sapphires

Crystals--- a

Emeralds--- b

grid.to_s(true, false, "........") # It works even the separator exceed virtual separator size

Rubys...Sapphires

Crystals... a

Emeralds... b

Source