Skip to content

Commit

Permalink
default set_table_mode to markdown, colored colnames rownames
Browse files Browse the repository at this point in the history
  • Loading branch information
wookay committed Jun 22, 2018
1 parent 07be6b1 commit bb83246
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 111 deletions.
60 changes: 28 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Linux, OSX: [![Build Status](https://api.travis-ci.org/wookay/Millboard.jl.svg?b
Windows: [![Build status](https://ci.appveyor.com/api/projects/status/3hjdk20juucb3kiw?svg=true)](https://ci.appveyor.com/project/wookay/Millboard.jl)
[![Coverage Status](https://coveralls.io/repos/wookay/Millboard.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/wookay/Millboard.jl?branch=master)


# Install
```julia
λ ~$ julia
Expand All @@ -19,14 +20,10 @@ Windows: [![Build status](https://ci.appveyor.com/api/projects/status/3hjdk20juu
julia> Pkg.add("Millboard")
INFO: Installing Millboard v0.1.0
julia> Pkg.checkout("Millboard")
julia> Pkg.update("Millboard")
julia> using Millboard
INFO: Precompiling module Millboard...
julia> Pkg.checkout("Millboard", "master")
```


# Example - numbers
```julia
julia> using Millboard
Expand All @@ -37,40 +34,38 @@ julia> board = [11 12 13; 21 22 23]
21 22 23
julia> table(board)
+---+----+----+----+
| | 1 | 2 | 3 |
+===+====+====+====+
|---|----|----|----|
| 1 | 11 | 12 | 13 |
+---+----+----+----+
| 2 | 21 | 22 | 23 |
+---+----+----+----+
julia> table(board, colnames=["x" "y" "z"])
+---+----+----+----+
| | x | y | z |
+===+====+====+====+
|---|----|----|----|
| 1 | 11 | 12 | 13 |
+---+----+----+----+
| 2 | 21 | 22 | 23 |
+---+----+----+----+
julia> table(board, rownames=["A" "B"])
+---+----+----+----+
| | 1 | 2 | 3 |
+===+====+====+====+
|---|----|----|----|
| A | 11 | 12 | 13 |
+---+----+----+----+
| B | 21 | 22 | 23 |
+---+----+----+----+
julia> table(board, colnames=["x" "y" "z"], rownames=["A" "B"])
+---+----+----+----+
| | x | y | z |
+===+====+====+====+
|---|----|----|----|
| A | 11 | 12 | 13 |
+---+----+----+----+
| B | 21 | 22 | 23 |
+---+----+----+----+
```


* `set_table_mode` : to change display mode
- `:markdown` (default)
- `:grid_tables`

```julia
julia> set_table_mode(:grid_tables)
Millboard.TableMode(:grid_tables, '+', '=')
julia> board = ([1 2], [5 6; 7 8], [9 10; 11 12])
([1 2], [5 6; 7 8], [9 10; 11 12])
Expand All @@ -89,30 +84,31 @@ julia> table(board, colnames=["result"], rownames=["x" "y" "z"])
+---+--------+
```


# Example - strings
```julia
julia> using Millboard
julia> board = ["Lorem ipsum\ndolor sit amet" 42]
1×2 Array{Any,2}:
"Lorem ipsum\ndolor sit amet" 42
julia> table(board, colnames=["first\ncolumn"], rownames=["first row"])
+-----------+----------------+----+
| | first | 2 |
| | column | |
+===========+================+====+
julia> set_table_mode(:markdown)
Millboard.TableMode(:markdown, '|', '-')
julia> table(board, colnames=["first column"], rownames=["first row"])
| | first column | 2 |
|-----------|----------------|----|
| | Lorem ipsum | 42 |
| first row | dolor sit amet | |
+-----------+----------------+----+
```


# Example - Dict
```julia
julia> using Millboard
julia> set_table_mode(:grid_tables)
Millboard.TableMode(:grid_tables, '+', '=')
julia> board = Dict("1x3"=>[1 2 3], "2x3"=>[1 2 3; 4 5 6], "3x1"=> [1; 2; 3])
Dict{String,Any} with 3 entries:
Dict{String,Array{Int64,N} where N} with 3 entries:
"3x1" => [1, 2, 3]
"2x3" => [1 2 3; 4 5 6]
"1x3" => [1 2 3]
Expand Down
2 changes: 1 addition & 1 deletion src/Millboard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ __precompile__(true)
module Millboard

using Compat
import Compat: undef, textwidth, occursin, pairs
import Compat: Nothing, undef, textwidth, occursin, pairs, printstyled

include("types.jl")

Expand Down
49 changes: 30 additions & 19 deletions src/table.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
const colnames_style = Dict(:color=>:yellow,)
const rownames_style = Dict(:color=>:cyan,)

# Base.show
function Base.show(io::IO, colored::ColoredString)
if Base.have_color && colored.style isa Dict
printstyled(io, colored.data, color=colored.style[:color])
else
print(io, colored.data)
end
end

function Base.show(io::IO, v::Vertical)
print(io, v.data)
end
Expand Down Expand Up @@ -40,10 +51,10 @@ function print_plate(io::IO, linear::Linear, isgridtables::Bool, islast::Bool)
end
end
end
if isgridtables
println(io)
else
if !isgridtables && i==height
!islast && println(io)
else
println(io)
end
end
end
Expand Down Expand Up @@ -73,26 +84,26 @@ arraysize(a::AbstractArray) = size(a[:,:])


# precell
precell(::Nothing) = DataCell([], 0, 0)
precell(::Nothing, style=nothing) = DataCell([], 0, 0, style)

function precell(el::String)
function precell(el::String, style=nothing)
if occursin("\n", el)
a = map(split(el, "\n")) do x
width = textwidth(x)
string(x, repeat(" ", width-textwidth(x)))
end
m,n = arraysize(a)
DataCell(a, maximum(map(length, a)), m)
DataCell(a, maximum(map(length, a)), m, style)
else
width = textwidth(el)
DataCell([el], width, 1)
DataCell([el], width, 1, style)
end
end

function precell(el::AbstractArray)
function precell(el::AbstractArray, style=nothing)
rows,cols = arraysize(el)
if 0==rows
DataCell([""], 0, 0)
DataCell([""], 0, 0, style)
else
prelines = similar(el, String)
widths = zeros(Int, rows, cols)
Expand All @@ -116,26 +127,26 @@ function precell(el::AbstractArray)
end
push!(lines, join(line, " "))
end
DataCell(lines, sum(maxwidths) + (cols-1), rows)
DataCell(lines, sum(maxwidths) + (cols-1), rows, style)
end
end

function precell(el::Any)
DataCell([el], length(repr(el)), 1)
function precell(el::Any, style=nothing)
DataCell([el], length(repr(el)), 1, style)
end


# postcell
function postcell(cell::DataCell, width::Int, height::Int, margin::Margin)
data = cell.data
rows,cols = arraysize(data)
A = Array{String}(undef, rows, cols)
A = Array{ColoredString}(undef, rows, cols)
@inbounds for i=1:rows
for j=1:cols
el = data[i,j]
x = isa(el, String) ? el : repr(el)
prep = repeat(" ", margin.leftside + width - textwidth(x))
A[i,j] = string(prep, x, repeat(" ", margin.rightside))
A[i,j] = ColoredString(string(prep, x, repeat(" ", margin.rightside)), cell.style)
end
end
Cell(A, width+margin.leftside+margin.rightside, height)
Expand Down Expand Up @@ -178,18 +189,18 @@ function decking(mill::Mill, tablemode::TableMode)
if haskey(option, :zerocolname)
zerocolname = option[:zerocolname]
end
cel = precell(zerocolname)
cel = precell(zerocolname, colnames_style)
widths[1,1] = cel.width
heights[1,1] = cel.height
push!(headlinear, cel)
ols = Vector{DataCell}(undef, cols)
for j=1:cols
ols[j] = precell(j)
ols[j] = precell(j, colnames_style)
end
if haskey(option, :colnames)
@inbounds for (j,name) in enumerate(option[:colnames])
if cols >= j
ols[j] = precell(name)
ols[j] = precell(name, colnames_style)
end
end
end
Expand All @@ -203,12 +214,12 @@ function decking(mill::Mill, tablemode::TableMode)

prerows = Vector{DataCell}(undef, rows)
for i=1:rows
prerows[i] = precell(i)
prerows[i] = precell(i, rownames_style)
end
if haskey(option, :rownames)
@inbounds for (i,name) in enumerate(option[:rownames])
if rows >= i
prerows[i] = precell(name)
prerows[i] = precell(name, rownames_style)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/table_modes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const table_modes = Dict(
)

display_style = Dict(
:table_mode => table_modes[:grid_tables],
:table_mode => table_modes[:markdown],
)

function set_table_mode(mode::Symbol)
Expand Down
6 changes: 6 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ struct DataCell <: AbstractCell
data::AbstractArray
width::Int
height::Int
style::Union{Nothing,Dict}
end

struct ColoredString
data::String
style::Union{Nothing,Dict}
end

struct Vertical <: AbstractCell
Expand Down
15 changes: 9 additions & 6 deletions test/dicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ using Millboard
using Compat.Test

set_table_mode(:grid_tables)
Base.eval(:(have_color = false))

board = Dict()
@test """
""" == table(board) |> string
@test table(board) |> string == ""


board = Dict(:a=>:b, "a"=>"b")
@test """
@test table(board) |> string == """
+-----+-------+
| KEY | VALUE |
+=====+=======+
| :a | :b |
+-----+-------+
| a | b |
+-----+-------+""" == table(board) |> string
+-----+-------+"""


board = Dict("1x3"=>[1 2 3], "2x3"=>[1 2 3; 4 5 6], "3x1"=> [1; 2; 3])
@test """
@test table(board) |> string == """
+-----+-------+
| KEY | VALUE |
+=====+=======+
Expand All @@ -32,4 +32,7 @@ board = Dict("1x3"=>[1 2 3], "2x3"=>[1 2 3; 4 5 6], "3x1"=> [1; 2; 3])
| | 1 |
| | 2 |
| 3x1 | 3 |
+-----+-------+""" == table(board) |> string
+-----+-------+"""


Base.eval(:(have_color = true))
4 changes: 4 additions & 0 deletions test/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Millboard
using Compat.Test

set_table_mode(:grid_tables)
Base.eval(:(have_color = false))

@enum RPS ✊ ✋ ✌️

Expand All @@ -26,3 +27,6 @@ set_table_mode(:grid_tables)
+-----+-------+
| ✌️ | 2 |
+-----+-------+""" == table(✊ ) |> string


Base.eval(:(have_color = true))
4 changes: 4 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Millboard
using Compat.Test

set_table_mode(:grid_tables)
Base.eval(:(have_color = false))

board = [(+) (*); (-) (/)]
@test """
Expand All @@ -12,3 +13,6 @@ board = [(+) (*); (-) (/)]
+---+---+---+
| 2 | - | / |
+---+---+---+""" == table(board) |> string


Base.eval(:(have_color = true))
4 changes: 4 additions & 0 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Millboard
using Compat.Test

set_table_mode(:markdown)
Base.eval(:(have_color = false))

board = []
@test """
Expand All @@ -14,3 +15,6 @@ board = [11 12 13; 21 22 23; 31 32 33]
| 1 | 11 | 12 | 13 |
| 2 | 21 | 22 | 23 |
| 3 | 31 | 32 | 33 |""" == table(board) |> string


Base.eval(:(have_color = true))
4 changes: 4 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Millboard
using Compat.Test

set_table_mode(:grid_tables)
Base.eval(:(have_color = false))

board = []
@test """
Expand Down Expand Up @@ -92,3 +93,6 @@ board = ([1 2;3 4], [5 6;7 8], [9 10; 11 12])
| | 9 10 |
| 3 | 11 12 |
+---+-------+""" == table(board) |> string


Base.eval(:(have_color = true))
4 changes: 4 additions & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Millboard
using Compat.Test

set_table_mode(:grid_tables)
Base.eval(:(have_color = false))

str = "The quick brown fox jumps over the lazy dog α,β,γ"
regexz = [r"b[\w]*n", r"[\w]{4,}"]
Expand All @@ -15,3 +16,6 @@ board = [func(regex, str) for regex=regexz, func=functions]
+-------------+---------------------+
| r"[\\w]{4,}" | RegexMatch("quick") |
+-------------+---------------------+""" == table(board, colnames=functions, rownames=regexz) |> string


Base.eval(:(have_color = true))
Loading

0 comments on commit bb83246

Please sign in to comment.