diff --git a/Terminal.jl/Terminal.jl b/Terminal.jl/Terminal.jl index 6e2e6da..a1cd9b6 100644 --- a/Terminal.jl/Terminal.jl +++ b/Terminal.jl/Terminal.jl @@ -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 diff --git a/Terminal.jl/cursor.jl b/Terminal.jl/cursor.jl index 816d3bd..eb5df09 100644 --- a/Terminal.jl/cursor.jl +++ b/Terminal.jl/cursor.jl @@ -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