Skip to content

Commit

Permalink
replace "fancy io" by PrettyTables!
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed Nov 29, 2023
1 parent 2847813 commit d9ba8d8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 101 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GroupsCore = "d5909c97-4eac-4ecc-a3dc-fdd0858a4120"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PermutationGroups = "8bc5a954-2dfc-11e9-10e6-cd969bffa420"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StarAlgebras = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1"
Expand All @@ -18,6 +19,7 @@ Cyclotomics = "0.3"
GroupsCore = "0.4"
PermutationGroups = "0.4.2"
PrecompileTools = "1"
PrettyTables = "2"
Primes = "0.4, 0.5"
StarAlgebras = "0.2"
julia = "1.6"
Expand Down
2 changes: 2 additions & 0 deletions src/Characters/Characters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ include("cmmatrix.jl")
include("powermap.jl")
include("character_tables.jl")
include("class_functions.jl")
include("io.jl")

include("dixon.jl")
end
83 changes: 0 additions & 83 deletions src/Characters/character_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,86 +162,3 @@ function normalize!(chtbl::CharacterTable{<:Group,<:FiniteFields.GF})
end
return chtbl
end

## "fancy" io

function Base.show(io::IO, ::MIME"text/plain", chtbl::CharacterTable)
println(io, "Character table of ", parent(chtbl), " over $(eltype(chtbl))")

if !(get(io, :limit, false)::Bool)
screenheight = screenwidth = typemax(Int)
else
sz = displaysize(io)::Tuple{Int,Int}
screenheight, screenwidth = sz[1] - 4, sz[2]
end

nirr = nirreps(chtbl)
nccl = nconjugacy_classes(chtbl)
pre_width = 1 + length(string(nirr))
sep = ""

if nirr > screenheight - 1
rows_to_print = [1:screenheight-2; nirr]
else
rows_to_print = [1:nirr;]
end

maxpossiblecols = div(screenwidth - pre_width - 3, 3)
if nccl > maxpossiblecols
cols_to_print = [1:maxpossiblecols-1; nccl]
else
cols_to_print = [1:nccl;]
end

A = Base.alignment(
io,
chtbl.values,
rows_to_print,
cols_to_print,
screenwidth,
screenwidth,
2,
)

hellipsis = nccl > length(A) ? "" : ""

print(io, " "^(pre_width), " ")
Base.print_matrix_row(
io,
reshape(cols_to_print, (1, length(cols_to_print))),
A,
1,
cols_to_print,
" ",
)

println(io, hellipsis)
println(
io,
""^pre_width,
"─┬─",
""^(sum(sum, A) + length(A) - 1),
hellipsis,
)

if nirr > screenheight - 1
for i in 1:screenheight-2
print(io, rpad("χ$(FiniteFields.subscriptify(i))", pre_width), sep)
Base.print_matrix_row(io, chtbl.values, A, i, cols_to_print, " ")
println(io, hellipsis)
end
print(io, "", " "^(pre_width - 2), sep)
Base.print_matrix_vdots(io, "", A, " ", 2, 1, false)
println(io)

print(io, rpad("χ$(FiniteFields.subscriptify(nirr))", pre_width), sep)
Base.print_matrix_row(io, chtbl.values, A, nirr, cols_to_print, " ")
print(io, hellipsis)
else
for i in 1:nirr
print(io, rpad("χ$(FiniteFields.subscriptify(i))", pre_width), sep)
Base.print_matrix_row(io, chtbl.values, A, i, cols_to_print, " ")
i != nirr && println(io, hellipsis)
end
end
end
18 changes: 0 additions & 18 deletions src/Characters/class_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,3 @@ end

Base.isreal::Character) = frobenius_schur(χ) > 0

function Base.show(io::IO, ::MIME"text/plain", χ::Character)
println(io, "Character over ", eltype(χ))
return _print_char(io, χ)
end

Base.show(io::IO, χ::Character) = _print_char(io, χ)

function _print_char(io::IO, χ::Character)
first = true
for (i, c) in enumerate(multiplicities(χ))
iszero(c) && continue
first || print(io, " ")
print(io, ((c < 0 || first) ? "" : '+'))
!isone(c) && print(io, c, '·')
print(io, 'χ', FiniteFields.subscriptify(i))
first = false
end
end
75 changes: 75 additions & 0 deletions src/Characters/io.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import PrettyTables
import PrettyTables.Tables

Tables.istable(::Type{<:CharacterTable}) = true
Tables.rowaccess(::Type{<:CharacterTable}) = true
Tables.rows(chtbl::CharacterTable) = irreducible_characters(chtbl)
Tables.getcolumn(row::Character, i::Int) = row[i]

# ugly hack to get this going
function Tables.columnnames(char::Character)
return [Symbol(i) for i in axes(conjugacy_classes(char), 1)]
end
function Tables.getcolumn(row::Character, nm::Symbol)
i = parse(Int, string(nm))
return Tables.getcolumn(row, i)
end

__coefs_desc(T::Type) = T
__coefs_desc(::Type{<:Rational{T}}) where {T} = "rationals ($T)"
__coefs_desc(::Type{<:Cyclotomics.Cyclotomic{T}}) where {T} = "cyclotomics ($T)"

function Base.show(io::IO, ::MIME"text/plain", chtbl::CharacterTable)
hl_odd = PrettyTables.Highlighter(;
f = (rule, i, j) -> i % 2 == 0,
crayon = PrettyTables.Crayon(;
foreground = :dark_gray,
negative = true,
),
)

fmt = (v, args...) -> sprint(show, MIME"text/plain"(), v)

return PrettyTables.pretty_table(
io,
chtbl;
title = "Character table of $(parent(chtbl)) over $(__coefs_desc(eltype(chtbl)))",
header = ["$(first(cc))^G" for cc in conjugacy_classes(chtbl)],
row_labels = [
Symbol('χ', FiniteFields.subscriptify(i)) for i in axes(chtbl, 1)
],
row_label_column_title = "",
# hlines = [:header, :end],
# vlines = [1],
formatters = fmt,
autowrap = true,
linebreaks = true,
columns_width = displaysize(io)[2] ÷ size(chtbl, 2) - 8,
reserved_display_lines = 3[],
# vcrop_mode = :middle,
# equal_columns_width = true,
# crop = :vertical,
ellipsis_line_skip = 1,
# alignment = [:r, :l],
highlighters = hl_odd,
)
end

function Base.show(io::IO, ::MIME"text/plain", χ::Character)
println(io, "Character over ", __coefs_desc(eltype(χ)))
return _print_char(io, χ)
end

Base.show(io::IO, χ::Character) = _print_char(io, χ)

function _print_char(io::IO, χ::Character)
first = true
for (i, c) in enumerate(multiplicities(χ))
iszero(c) && continue
first || print(io, " ")
print(io, ((c < 0 || first) ? "" : '+'))
!isone(c) && print(io, c, '·')
print(io, 'χ', FiniteFields.subscriptify(i))
first = false
end
end

0 comments on commit d9ba8d8

Please sign in to comment.