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

Introduce preference for automatic ascii visualization of system #122

Open
wants to merge 7 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
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1.9'
- '1.10'
- 'nightly'
Expand Down
11 changes: 10 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ version = "0.4.2"
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PeriodicTable = "7b2266bf-644c-5ea3-82d8-af4bbd25a884"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"

[weakdeps]
AtomsView = "ee286e10-dd2d-4ff2-afcb-0a3cd50c8041"

[extensions]
AtomsBaseAtomsViewExt = "AtomsView"

[compat]
AtomsView = "0.1"
PeriodicTable = "1"
Preferences = "1.4"
Requires = "1"
StaticArrays = "1"
Unitful = "1"
UnitfulAtomic = "1"
julia = "1.6"
julia = "1.9"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
8 changes: 8 additions & 0 deletions ext/AtomsBaseAtomsViewExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module AtomsBaseAtomsViewExt
using AtomsBase
using AtomsView

function Base.show(io::IO, mime::MIME"text/html", system::AbstractSystem)
write(io, AtomsView.visualize_structure(system, mime))
end
end
14 changes: 0 additions & 14 deletions src/AtomsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,10 @@ include("utils/visualize_ascii.jl")
include("utils/show.jl")
include("utils/atomview.jl")


# prototype implementations
include("implementation/atom.jl")
include("implementation/flexible_system.jl")
include("implementation/fast_system.jl")
include("implementation/utils.jl")


# TODO:
# - this should be converted to an extension
# - should work for AbstractSystem
function __init__()
@require AtomsView="ee286e10-dd2d-4ff2-afcb-0a3cd50c8041" begin
function Base.show(io::IO, mime::MIME"text/html", system::FlexibleSystem)
write(io, AtomsView.visualize_structure(system, mime))
end
end
end


end
39 changes: 34 additions & 5 deletions src/utils/show.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
using Printf
using Preferences

"""
Configures the printing behaviour of `show_system`, which is invoked when a rich `text/plain`
display of an `AbstractSystem` is requested. This is for example the case in a Julia REPL.
The following options can be configured:

- `max_particles_list`: Maximal number of particles in a system until `show_system`
includes a listing of every particle. Default: 10
- `max_particles_visualize_ascii`: Maximal number of particles in a system
until `show_system` includes a representation of the system in the form of
an ascii cartoon using `visualize_ascii`. Default 0, i.e. disabled.
"""
function set_show_preferences!(; max_particles_list=nothing, max_particles_visualize_ascii=nothing)
if !isnothing(max_particles_list)
@set_preferences!("max_particles_list" => max_particles_list)

Check warning on line 17 in src/utils/show.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/show.jl#L15-L17

Added lines #L15 - L17 were not covered by tests
end
if !isnothing(max_particles_visualize_ascii)
@set_preferences!("max_particles_visualize_ascii" => max_particles_visualize_ascii)

Check warning on line 20 in src/utils/show.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/show.jl#L19-L20

Added lines #L19 - L20 were not covered by tests
end
show_preferences()

Check warning on line 22 in src/utils/show.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/show.jl#L22

Added line #L22 was not covered by tests
end

"""
Display the current printing behaviour of `show_system`.
See [`set_show_preferences!](@ref) for more details on the keys.
"""
function show_preferences()
(; max_particles_list=@load_preference("max_particles_list", 10),
max_particles_visualize_ascii=@load_preference("max_particles_visualize_ascii", 0))

Check warning on line 31 in src/utils/show.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/show.jl#L31

Added line #L31 was not covered by tests
end

"""
Suggested function to print AbstractSystem objects to screen
Expand Down Expand Up @@ -26,7 +57,7 @@
println(io, "):")

extra_line = false
if any(pbc)
if any(pbc)
extra_line = true
box = bounding_box(system)
bunit = unit(eltype(first(bounding_box(system))))
Expand All @@ -47,17 +78,15 @@
extra_line = true
@printf io " %-17s : %s\n" string(k) string(v)
end
if length(system) < 10
if length(system) ≤ show_preferences().max_particles_list
extra_line && println(io)
for atom in system
println(io, " ", atom)
end
extra_line = true
end

# TODO We will make this configurable in a follow-up PR
show_ascii = false
if show_ascii
if length(system) ≤ show_preferences().max_particles_visualize_ascii
ascii = visualize_ascii(system)
if !isempty(ascii)
extra_line && println(io)
Expand Down
Loading