Skip to content

Commit

Permalink
print arrays of multiline strings in REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianKurz committed Jun 1, 2018
1 parent 9fe1953 commit 4addc82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Terminal.jl/Terminal.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Terminal

using Compat, Compat.REPL, Crayons
export rawmode, clear_screen, readKey, cursor_move_abs, put, cursor_deleteline
export rawmode, clear_screen, readKey, put, terminal_screen

@compat function __init__()
global terminal
Expand Down
27 changes: 26 additions & 1 deletion Terminal.jl/cursor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,30 @@ function put(buf::IO, pos::Vector, color::Crayon, s::String)
print(buf, color)
put(buf, pos, s)
end

put(buf::IO, pos::Vector, c::Symbol, s::String) = put(buf, pos, Crayon(foreground=c), s)
put(pos::Vector, c::Union{Symbol, Crayon}, s::String) = put(terminal.out_stream, pos, c, s)

function terminal_screen(data::Array{String}, origin::Vector{Int}=[1,1];
colors::Array{Symbol}=fill(:white, size(data)))
buf = IOBuffer()
x1, y1 = origin
dy, dx = size(data)
row_heigths, column_widths = _row_column_sizes(data)
for x in 1:dx, y in 1:dy
d = data[y, x]
s = String.(split(d, "\n"))
for i in 1:row_heigths[y]
line = i > length(s) ? "" : s[i]
put(buf, [sum(column_widths[1:x-1])+3x+x1,sum(row_heigths[1:y-1])+y+y1+i], colors[y,x], line)
end
end
print(String(take!(buf)))
end

function _row_column_sizes(data::Array{String})
size_string(s::String) = length(split(s, "\n")), maximum(length.(split(s, "\n")))
size_data = size_string.(data)
row_heigths = maximum(getindex.(size_data, 1), 2)
column_widths = maximum(getindex.(size_data,2), 1)
row_heigths, column_widths
end

0 comments on commit 4addc82

Please sign in to comment.