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 5 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
76 changes: 63 additions & 13 deletions src/PrettyPrinting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1386,24 +1386,74 @@
#
################################################################################

# 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(allowed::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 the scope of a
`with_unicode` do-block.
"""
function allow_unicode(allowed::Bool; temporary::Bool=false)
global ALLOW_UNICODE_OVERRIDE_VALUE
if temporary
old_allowed = is_unicode_allowed()
ALLOW_UNICODE_OVERRIDE_VALUE = allowed
return old_allowed

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_allowed = is_unicode_allowed()
@set_preferences!("unicode" => allowed)
ALLOW_UNICODE_OVERRIDE_VALUE = nothing
return old_allowed

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
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved
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
override = ALLOW_UNICODE_OVERRIDE_VALUE
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved
!isnothing(override) && return override
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, allowed::Bool=true)

Temporarily set whether unicode characters are allowed in pretty printing
during the execution of `f`.
This is useful for e.g. running doctests independently on the user preference.

`with_unicode` is expected to be called in the following way:
```julia
with_unicode([allowed]) do
# code that should be executed with unicode allowed/disallowed
end
```
"""
function with_unicode(f::Function, allowed::Bool=true)
global ALLOW_UNICODE_OVERRIDE_VALUE
previous = ALLOW_UNICODE_OVERRIDE_VALUE
ALLOW_UNICODE_OVERRIDE_VALUE = allowed
benlorenz marked this conversation as resolved.
Show resolved Hide resolved
try
f()

Check warning on line 1452 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1447-L1452

Added lines #L1447 - L1452 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 == allowed
ALLOW_UNICODE_OVERRIDE_VALUE = previous

Check warning on line 1455 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1454-L1455

Added lines #L1454 - L1455 were not covered by tests
benlorenz marked this conversation as resolved.
Show resolved Hide resolved
end
end

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