Skip to content

Commit 88239a7

Browse files
committed
feat: migrate printstyled() to ANSI sequences for Documenter compatibility
- Add ANSI helper functions (_ansi_color, _ansi_reset, _print_ansi_styled) - Replace 7 printstyled() calls with ANSI equivalents in src/Display/print.jl - Enable colors in generated HTML documentation via Documenter's ANSI processing - Update version to 0.9.10-beta This ensures colors appear properly in both terminal and web documentation.
1 parent 110a0ad commit 88239a7

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "CTModels"
22
uuid = "34c4fa32-2049-4079-8329-de33c2a22e2d"
3-
version = "0.9.9-beta"
3+
version = "0.9.10-beta"
44
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
55

66
[deps]

docs/make.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
pushfirst!(LOAD_PATH, joinpath(@__DIR__))
2+
pushfirst!(LOAD_PATH, joinpath(@__DIR__, ".."))
3+
14
using Documenter
25
using CTModels
36
using CTBase

src/Display/print.jl

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
# ------------------------------------------------------------------------------ #
22
# PRINT
33
# ------------------------------------------------------------------------------ #
4+
5+
"""
6+
Generate ANSI escape sequence for the specified color and formatting.
7+
"""
8+
function _ansi_color(color::Symbol, bold::Bool=false)
9+
color_codes = Dict(
10+
:black => 30, :red => 31, :green => 32, :yellow => 33,
11+
:blue => 34, :magenta => 35, :cyan => 36, :white => 37,
12+
:default => 39
13+
)
14+
15+
code = get(color_codes, color, 39)
16+
return bold ? "\033[1;$(code)m" : "\033[$(code)m"
17+
end
18+
19+
"""Generate ANSI reset sequence to clear formatting."""
20+
_ansi_reset() = "\033[0m"
21+
22+
"""
23+
Print text with ANSI color formatting for Documenter compatibility.
24+
"""
25+
function _print_ansi_styled(io, text::Union{String,Symbol,Type}, color::Symbol, bold::Bool=false)
26+
print(io, _ansi_color(color, bold), string(text), _ansi_reset())
27+
end
428
"""
529
$(TYPEDSIGNATURES)
630
@@ -35,7 +59,7 @@ Print the abstract definition of an optimal control problem.
3559
"""
3660
function __print_abstract_definition(io::IO, ocp::Union{Model,PreModel})
3761
@assert hasproperty(definition(ocp), :head)
38-
printstyled(io, "Abstract definition:\n\n"; bold=true)
62+
_print_ansi_styled(io, "Abstract definition:\n\n", :default, true)
3963
tab = 4
4064
code = MacroTools.striplines(definition(ocp))
4165
MLStyle.@match code.head begin
@@ -109,17 +133,17 @@ function __print_mathematical_definition(
109133

110134
#
111135
some_printing && println(io)
112-
printstyled(io, "The "; bold=true)
136+
_print_ansi_styled(io, "The ", :default, true)
113137
if is_time_dependent
114-
printstyled(io, "(non autonomous) "; bold=true)
138+
_print_ansi_styled(io, "(non autonomous) ", :default, true)
115139
else
116-
printstyled(io, "(autonomous) "; bold=true)
140+
_print_ansi_styled(io, "(autonomous) ", :default, true)
117141
end
118-
printstyled(io, "optimal control problem is of the form:\n"; bold=true)
142+
_print_ansi_styled(io, "optimal control problem is of the form:\n", :default, true)
119143
println(io)
120144

121145
# J
122-
printstyled(io, " minimize "; color=:blue)
146+
_print_ansi_styled(io, " minimize ", :blue, false)
123147
# Only include control in objective if u_dim > 0
124148
u_in_obj = u_dim > 0 ? ", " * u_name : ""
125149
print(io, "J(" * x_name * u_in_obj * _v * ") = ")
@@ -149,7 +173,7 @@ function __print_mathematical_definition(
149173

150174
# constraints
151175
println(io, "")
152-
printstyled(io, " subject to\n"; color=:blue)
176+
_print_ansi_styled(io, " subject to\n", :blue, false)
153177
println(io, "")
154178

155179
# dynamics

0 commit comments

Comments
 (0)