Skip to content

Commit

Permalink
add gridplot in TermExt (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg authored Jun 18, 2023
1 parent 8accdbc commit 3719325
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [3.6] - 2023-06-18
### Added
- Support `Term` extension, export `gridplot`.

## [3.5] - 2023-04-09
### Changed
- Swap `Function` order in `lineplot`.
Expand Down
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ julia = "1.6"
FreeTypeExt = ["FileIO", "FreeType"]
ImageInTerminalExt = "ImageInTerminal"
IntervalSetsExt = "IntervalSets"
TermExt = "Term"
UnitfulExt = "Unitful"

[extras]
Expand All @@ -55,17 +56,19 @@ IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Aqua", "DataFrames", "FileIO", "FreeType", "ImageInTerminal", "ImageMagick", "IntervalSets", "Random", "ReferenceTests", "StableRNGs", "Test", "TestImages", "TimerOutputs", "Unitful"]
test = ["Aqua", "DataFrames", "FileIO", "FreeType", "ImageInTerminal", "ImageMagick", "IntervalSets", "Random", "ReferenceTests", "StableRNGs", "Term", "Test", "TestImages", "TimerOutputs", "Unitful"]

[weakdeps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
FreeType = "b38be410-82b0-50bf-ab77-7b57e271db43"
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,25 +452,21 @@ julia> Pkg.add("UnicodePlots")

`UnicodePlots` is integrated in [`Plots`](https://github.com/JuliaPlots/Plots.jl) as a backend, with support for [basic layout](https://docs.juliaplots.org/stable/gallery/unicodeplots/generated/unicodeplots-ref17).

For a more complex layout, use the [`grid`](https://fedeclaudi.github.io/Term.jl/dev/layout/grid) function from [`Term`](https://github.com/FedeClaudi/Term.jl):
For a more complex layout, use the [`gridplot`] function (requires loading [`Term`](https://github.com/FedeClaudi/Term.jl)).
```julia
using UnicodePlots, Term
panel(p; kw...) = begin
p.margin[] = p.padding[] = 0 # make plots more compact
Panel(string(p; color=true); style="hidden", fit=true, kw...)
end
(
panel(lineplot(1:2)) *
panel(scatterplot(rand(100)))
UnicodePlots.panel(lineplot(1:2)) *
UnicodePlots.panel(scatterplot(rand(100)))
) / (
panel(lineplot(2:-1:1)) *
panel(densityplot(randn(1_000), randn(1_000)))
) |> print
UnicodePlots.panel(lineplot(2:-1:1)) *
UnicodePlots.panel(densityplot(randn(1_000), randn(1_000)))
) |> display
grid(map(i -> panel(lineplot(-i:i)), 1:5); show_placeholder=true) |> print
grid(map(i -> panel(lineplot(-i:i)), 1:3); layout=(2, nothing)) |> print
grid(map(i -> panel(lineplot(-i:i)), 1:3); layout=(nothing, 1)) |> print
gridplot(map(i -> lineplot(-i:i), 1:5); show_placeholder=true) |> display
gridplot(map(i -> lineplot(-i:i), 1:3); layout=(2, nothing)) |> display
gridplot(map(i -> lineplot(-i:i), 1:3); layout=(nothing, 1)) |> display
```
</details>

Expand Down
22 changes: 9 additions & 13 deletions docs/gen_docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -605,25 +605,21 @@ $(indent(examples.isosurface))
`UnicodePlots` is integrated in [`Plots`](https://github.com/JuliaPlots/Plots.jl) as a backend, with support for [basic layout](https://docs.juliaplots.org/stable/gallery/unicodeplots/generated/unicodeplots-ref17).
For a more complex layout, use the [`grid`](https://fedeclaudi.github.io/Term.jl/dev/layout/grid) function from [`Term`](https://github.com/FedeClaudi/Term.jl):
For a more complex layout, use the [`gridplot`] function (requires loading [`Term`](https://github.com/FedeClaudi/Term.jl) as extension).
```julia
using UnicodePlots, Term
panel(p; kw...) = begin
p.margin[] = p.padding[] = 0 # make plots more compact
Panel(string(p; color=true); style="hidden", fit=true, kw...)
end
(
panel(lineplot(1:2)) *
panel(scatterplot(rand(100)))
UnicodePlots.panel(lineplot(1:2)) *
UnicodePlots.panel(scatterplot(rand(100)))
) / (
panel(lineplot(2:-1:1)) *
panel(densityplot(randn(1_000), randn(1_000)))
) |> print
UnicodePlots.panel(lineplot(2:-1:1)) *
UnicodePlots.panel(densityplot(randn(1_000), randn(1_000)))
) |> display
grid(map(i -> panel(lineplot(-i:i)), 1:5); show_placeholder=true) |> print
grid(map(i -> panel(lineplot(-i:i)), 1:3); layout=(2, nothing)) |> print
grid(map(i -> panel(lineplot(-i:i)), 1:3); layout=(nothing, 1)) |> print
gridplot(map(i -> lineplot(-i:i), 1:5); show_placeholder=true) |> display
gridplot(map(i -> lineplot(-i:i), 1:3); layout=(2, nothing)) |> display
gridplot(map(i -> lineplot(-i:i), 1:3); layout=(nothing, 1)) |> display
```
</details>
Expand Down
17 changes: 17 additions & 0 deletions ext/TermExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module TermExt

import UnicodePlots: UnicodePlots, Plot
UnicodePlots.@ext_imp_use :import Term

function UnicodePlots.panel(p; kw...)
p.margin[] = p.padding[] = 0 # make plots more compact
p.compact[] = true
Term.Panel(string(p; color = true); style = "hidden", fit = true, kw...)
end

UnicodePlots.gridplot(plots::Union{AbstractVector,Tuple}; kw...) =
UnicodePlots.gridplot(plots...; kw...)
UnicodePlots.gridplot(plots::Plot...; kw...) =
Term.grid(map(UnicodePlots.panel, plots); kw...)

end
6 changes: 5 additions & 1 deletion src/UnicodePlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export default_size!,
zlabel,
vline!,
hline!,
savefig
savefig,
gridplot

# methods with mutating variants
export scatterplot!,
Expand Down Expand Up @@ -132,6 +133,9 @@ function __init__()
@require IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" include(
joinpath("..", "ext", "IntervalSetsExt.jl"),
)
@require Term = "22787eb5-b846-44ae-b979-8e399b8463ab" include(
joinpath("..", "ext", "TermExt.jl"),
)
@require Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" include(
joinpath("..", "ext", "UnitfulExt.jl"),
)
Expand Down
4 changes: 4 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,7 @@ split_plot_kw(kw) =
end

warn_on_lost_kw(kw) = (isempty(kw) || @warn "keyword(s) `$kw` will be lost"; nothing)

# TermExt
function panel end
function gridplot end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ImageInTerminal, FreeType, FileIO, IntervalSets, Unitful # weak deps, or @require
using ImageInTerminal, FreeType, FileIO, IntervalSets, Unitful, Term # weak deps, or @require
using UnicodePlots, Test

import UnicodePlots: lines!, points!, pixel!, nrows, ncols
Expand Down
6 changes: 6 additions & 0 deletions test/tst_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ end
GC.enable(true)
end
end

@testset "Term extension" begin
gridplot(map(i -> lineplot((-i):i), 1:5); show_placeholder = true) |> display
gridplot(map(i -> lineplot((-i):i), 1:3); layout = (2, nothing)) |> display
gridplot(map(i -> lineplot((-i):i), 1:3); layout = (nothing, 1)) |> display
end

0 comments on commit 3719325

Please sign in to comment.