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

Overhaul Unicode preferences #1586

Merged
merged 6 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/docs/build/
Manifest.toml
LocalPreferences.toml
.DS_Store
68 changes: 55 additions & 13 deletions src/PrettyPrinting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1386,24 +1386,66 @@
#
################################################################################

# A non-compiletime preference
function allow_unicode(flag::Bool)
old_flag = is_unicode_allowed()
@set_preferences!("unicode" => flag)
return old_flag
@static if VERSION < v"1.8.0-DEV.1465"
ALLOW_UNICODE_OVERRIDE_VALUE = nothing
else
ALLOW_UNICODE_OVERRIDE_VALUE::Union{Bool,Nothing} = nothing
end
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved

@doc """
allow_unicode(flag::Bool; temporary::Bool=false) -> Bool

Set whether unicode characters are allowed in pretty printing and returns the
previous value.
If `temporary` is `true`, then the change is only active for the current worker and session.
Otherwise, the change is permanently saved in the preferences.
A permanent change will always override a temporary change.

This function may behave arbitrarily, if called from within an argument to
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved
`with_unicode`.
"""
function allow_unicode(flag::Bool; temporary::Bool=false)
global ALLOW_UNICODE_OVERRIDE_VALUE
if temporary
old_flag = is_unicode_allowed()
ALLOW_UNICODE_OVERRIDE_VALUE = flag
return old_flag

Check warning on line 1412 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1407-L1412

Added lines #L1407 - L1412 were not covered by tests
else
old_flag = is_unicode_allowed()
@set_preferences!("unicode" => flag)
ALLOW_UNICODE_OVERRIDE_VALUE = nothing
return old_flag

Check warning on line 1417 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1414-L1417

Added lines #L1414 - L1417 were not covered by tests
end
end

@doc """
is_unicode_allowed() -> Bool

Return whether unicode characters are allowed in pretty printing.
"""
function is_unicode_allowed()
return @load_preference("unicode", default = false)
global ALLOW_UNICODE_OVERRIDE_VALUE
value = ALLOW_UNICODE_OVERRIDE_VALUE
!isnothing(value) && return value
return @load_preference("unicode", default = false)::Bool

Check warning on line 1430 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1427-L1430

Added lines #L1427 - L1430 were not covered by tests
end

function with_unicode(f::Function)
old_allow_unicode = allow_unicode(true)
try
f()
finally
allow_unicode(old_allow_unicode)
end
@doc """
with_unicode(f::Function, flag::Bool=true)
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved

Temporarily set whether unicode characters are allowed in pretty printing
during the execution of `f`.
"""
function with_unicode(f::Function, flag::Bool=true)
global ALLOW_UNICODE_OVERRIDE_VALUE
previous = ALLOW_UNICODE_OVERRIDE_VALUE
ALLOW_UNICODE_OVERRIDE_VALUE = flag
try
f()

Check warning on line 1444 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1439-L1444

Added lines #L1439 - L1444 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we have no tests for with_unicode in AA` :-(. But of course this is out of scope for this PR.

Copy link
Collaborator Author

@lgoettgens lgoettgens Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. But after thinking again about it, this seems to be kind of hard anyway:
Calling allow_unicode() from the test would change the user preferences while running tests (not a great experience for devs), and from a within with_unicode block allow_unicode is unsafe.

But overall, we should postpone it

finally
@assert ALLOW_UNICODE_OVERRIDE_VALUE == flag
ALLOW_UNICODE_OVERRIDE_VALUE = previous

Check warning on line 1447 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1446-L1447

Added lines #L1446 - L1447 were not covered by tests
end
end

################################################################################
Expand Down
Loading