Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printing with styled/non-unit-textwidth-character contents #443

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function _blockarray_print_matrix_row(io::IO,
print(io, l, prettysx, r)

# Jump forward
n_chars += length(l) + length(prettysx) + length(r) + 2
n_chars += length(l) + textwidth(Base.ANSIIterator(prettysx)) + length(r) + 2

cumul += 1
if ndims(X) == 2
Expand Down
29 changes: 29 additions & 0 deletions test/test_blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ using Test, BlockArrays
@test blocks(mortar(matrix_blocks)) === matrix_blocks
end

@testset "blocks::BlockMatrix{::MyString}" begin
# test printing with ANSI escape sequences & textwidth(::Char) ≠ 1
struct MyString
s::String
end
function Base.show(io::IO, x::MyString)
if all(isnumeric, x.s)
printstyled(io, x.s; bold=true, color=:green)
elseif all(isascii, x.s)
printstyled(io, x.s, color=:red)
print(io, " ascii!")
else
print(io, x.s)
end
end

B = BlockArray(undef_blocks, Matrix{MyString}, [1,2], [1,2])
B[Block(1), Block(1)] = [MyString("abc");;]
B[Block(1), Block(2)] = [MyString("123") MyString("γ");]
B[Block(2), Block(1)] = [MyString("γ"); MyString("1");;]
B[Block(2), Block(2)] = [MyString("⛵⛵⛵⛵⛵") MyString("x"); MyString("⛵⛵⛵") MyString("4")]

strip_ansi(s) = reduce(*, filter(c->!(c isa Base.ANSIDelimiter),
map(last, Base.ANSIIterator(s))))
reference_str = "2×2-blocked 3×3 BlockMatrix{$(@__MODULE__).MyString}:\n \e[31mabc\e[39m ascii! │ \e[32m\e[1m123\e[22m\e[39m γ \n ────────────┼──────────────────────\n γ │ ⛵⛵⛵⛵⛵ \e[31mx\e[39m ascii!\n \e[32m\e[1m1\e[22m\e[39m │ ⛵⛵⛵ \e[32m\e[1m4\e[22m\e[39m "
@test strip_ansi(sprint(show, "text/plain", B; context=stdout)) == strip_ansi(reference_str)
@test strip_ansi(sprint(show, "text/plain", B)) == strip_ansi(reference_str)
end

@testset "blocks(::BlockedVector)" begin
v0 = rand(3)
vb = BlockedArray(v0, [1, 2])
Expand Down
Loading