Grid
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
Constructors
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
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
Instance methods
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"]]
Count the delimiter of specified column count.
Example:
delimiter_count_of(3) # => 2
delimiter_count_of(7) # => 6
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"]
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"]
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"]
Holds the max width of the canvas.
Its defined by the user.
Default value is 24.
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