diff --git a/README.md b/README.md index 81a6d31..fb6a46c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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]) @@ -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] diff --git a/src/Millboard.jl b/src/Millboard.jl index 385ecfd..4fbb905 100644 --- a/src/Millboard.jl +++ b/src/Millboard.jl @@ -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") diff --git a/src/table.jl b/src/table.jl index c7c6c99..562980f 100644 --- a/src/table.jl +++ b/src/table.jl @@ -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 @@ -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 @@ -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) @@ -116,12 +127,12 @@ 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 @@ -129,13 +140,13 @@ end 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) @@ -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 @@ -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 diff --git a/src/table_modes.jl b/src/table_modes.jl index 05e8bb0..a27f8a8 100644 --- a/src/table_modes.jl +++ b/src/table_modes.jl @@ -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) diff --git a/src/types.jl b/src/types.jl index 84702c5..cdfd048 100644 --- a/src/types.jl +++ b/src/types.jl @@ -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 diff --git a/test/dicts.jl b/test/dicts.jl index a0e7926..7512c08 100644 --- a/test/dicts.jl +++ b/test/dicts.jl @@ -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 | +=====+=======+ @@ -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)) diff --git a/test/enums.jl b/test/enums.jl index 2745586..c28bc44 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) @enum RPS ✊ ✋ ✌️ @@ -26,3 +27,6 @@ set_table_mode(:grid_tables) +-----+-------+ | ✌️ | 2 | +-----+-------+""" == table(✊ ) |> string + + +Base.eval(:(have_color = true)) diff --git a/test/functions.jl b/test/functions.jl index 3f04987..ccb6a24 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) board = [(+) (*); (-) (/)] @test """ @@ -12,3 +13,6 @@ board = [(+) (*); (-) (/)] +---+---+---+ | 2 | - | / | +---+---+---+""" == table(board) |> string + + +Base.eval(:(have_color = true)) diff --git a/test/markdown.jl b/test/markdown.jl index c406d80..abc98aa 100644 --- a/test/markdown.jl +++ b/test/markdown.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:markdown) +Base.eval(:(have_color = false)) board = [] @test """ @@ -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)) diff --git a/test/numbers.jl b/test/numbers.jl index c3d8500..0d052f2 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) board = [] @test """ @@ -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)) diff --git a/test/regex.jl b/test/regex.jl index baf9a30..e9fc2c8 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -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,}"] @@ -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)) diff --git a/test/strings.jl b/test/strings.jl index 39de2dc..dfe3cbd 100644 --- a/test/strings.jl +++ b/test/strings.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) board = ["Lorem ipsum dolor sit amet"] @test """ @@ -48,3 +49,6 @@ board = ["Lorem ipsum\ndolor sit amet" 42] | | Lorem ipsum | 42 | | first row | dolor sit amet | | +-----------+----------------+----+""" == table(board, colnames=["first\ncolumn"], rownames=["first row"]) |> string + + +Base.eval(:(have_color = true)) diff --git a/test/tuples.jl b/test/tuples.jl index d204fa1..c8a8ebe 100644 --- a/test/tuples.jl +++ b/test/tuples.jl @@ -4,62 +4,64 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) -@testset "display tuples" begin - board = () - @test """ - """ == table(board) |> string - - board = ([],) - @test """ - +---+---+ - | | 1 | - +===+===+ - | 1 | | - +---+---+""" == table(board) |> string - - board = ([1,2,3],) - @test """ - +---+---+ - | | 1 | - +===+===+ - | | 1 | - | | 2 | - | 1 | 3 | - +---+---+""" == table(board) |> string - - board = [()] - @test """ - +---+----+ - | | 1 | - +===+====+ - | 1 | () | - +---+----+""" == table(board) |> string +board = () +@test """ +""" == table(board) |> string - board = ((1,2,3),) - @test """ - +---+-----------+ - | | 1 | - +===+===========+ - | 1 | (1, 2, 3) | - +---+-----------+""" == table(board) |> string +board = ([],) +@test """ ++---+---+ +| | 1 | ++===+===+ +| 1 | | ++---+---+""" == table(board) |> string - board = [(1,2,3)] - @test """ - +---+-----------+ - | | 1 | - +===+===========+ - | 1 | (1, 2, 3) | - +---+-----------+""" == table(board) |> string +board = ([1,2,3],) +@test """ ++---+---+ +| | 1 | ++===+===+ +| | 1 | +| | 2 | +| 1 | 3 | ++---+---+""" == table(board) |> string - board = [(1,2,3) (4,5,6)] - @test """ - +---+-----------+-----------+ - | | 1 | 2 | - +===+===========+===========+ - | 1 | (1, 2, 3) | (4, 5, 6) | - +---+-----------+-----------+""" == table(board) |> string -end +board = [()] +@test """ ++---+----+ +| | 1 | ++===+====+ +| 1 | () | ++---+----+""" == table(board) |> string + +board = ((1,2,3),) +@test """ ++---+-----------+ +| | 1 | ++===+===========+ +| 1 | (1, 2, 3) | ++---+-----------+""" == table(board) |> string + +board = [(1,2,3)] +@test """ ++---+-----------+ +| | 1 | ++===+===========+ +| 1 | (1, 2, 3) | ++---+-----------+""" == table(board) |> string + +board = [(1,2,3) (4,5,6)] +@test """ ++---+-----------+-----------+ +| | 1 | 2 | ++===+===========+===========+ +| 1 | (1, 2, 3) | (4, 5, 6) | ++---+-----------+-----------+""" == table(board) |> string + + +Base.eval(:(have_color = true)) end # module test_tuples diff --git a/test/types.jl b/test/types.jl index a8a91c9..1710112 100644 --- a/test/types.jl +++ b/test/types.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) types = [AbstractString, String] board = [T.abstract for T=types] @@ -14,3 +15,6 @@ board = [T.abstract for T=types] +----------------+----------+ | String | false | +----------------+----------+""" == table(board, colnames=["abstract"], rownames=types) |> string + + +Base.eval(:(have_color = true)) diff --git a/test/unicode.jl b/test/unicode.jl index 544388e..a9ccea4 100644 --- a/test/unicode.jl +++ b/test/unicode.jl @@ -2,6 +2,7 @@ using Millboard using Compat.Test set_table_mode(:grid_tables) +Base.eval(:(have_color = false)) board = ["한글"] @test """ @@ -22,3 +23,6 @@ board = ["한\n글"; "abc"] +---+-----+ | 2 | abc | +---+-----+""" == board |> table |> string + + +Base.eval(:(have_color = true))