struct Grid

Overview

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

Included Modules

Defined in:

grid.cr

Constant Summary

VERSION = "0.1.1"

Constructors

Instance Method Summary

Instance methods inherited from module TopDown

canvas_td : Array(Array(String)) canvas_td, canvas_td=(canvas_td : Array(Array(String))) canvas_td=, col_width_td : Array(Int32) col_width_td, col_width_td=(col_width_td : Array(Int32)) col_width_td=, one_column_td : Array(Array(String)) one_column_td, virtual_column_width_td(col_size : Int32) : Array(Int32) virtual_column_width_td

Instance methods inherited from module LeftRight

canvas_lr : Array(Array(String)) canvas_lr, canvas_lr=(canvas_lr : Array(Array(String))) canvas_lr=, col_width_lr : Array(Int32) col_width_lr, col_width_lr=(col_width_lr : Array(Int32)) col_width_lr=, one_column_lr : Array(Array(String)) one_column_lr, virtual_column_width_lr(row_size : Int32) : Array(Int32) virtual_column_width_lr

Constructor Detail

def self.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

[View source]
def self.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

[View source]

Instance Method Detail

def 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"]]

[View source]
def 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

[View source]
def list : Array(String) #

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"]

[View source]
def 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"]

[View source]
def 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"]

[View source]
def max_width : Int32 #

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


[View source]
def max_width=(max_width : Int32) #

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


[View source]
def separator : String #

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


[View source]
def 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


[View source]