Skip to content

Commit 016edc7

Browse files
committed
rework import / using when using weak exts
1 parent adc63db commit 016edc7

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

ext/FreeTypeExt.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
module FreeTypeExt
55

66
import UnicodePlots
7-
@static if isdefined(Base, :get_extension)
8-
using FreeType, FileIO
9-
else
10-
using ..FreeType, ..FileIO
11-
end
7+
UnicodePlots.@ext_imp_use :using FreeType
8+
UnicodePlots.@ext_imp_use :import FileIO
129
using StaticArrays
1310
using ColorTypes
1411

ext/ImageInTerminalExt.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
module ImageInTerminalExt
22

33
import UnicodePlots
4-
@static if isdefined(Base, :get_extension)
5-
import ImageInTerminal
6-
else
7-
import ..ImageInTerminal
8-
end
4+
UnicodePlots.@ext_imp_use :import ImageInTerminal
95
using ColorTypes
106

117
UnicodePlots.sixel_encode(args...; kw...) = ImageInTerminal.sixel_encode(args...; kw...) # COV_EXCL_LINE

ext/UnitfulExt.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ module UnitfulExt
22

33
import UnicodePlots
44
import UnicodePlots: KEYWORDS, Plot, Canvas
5-
@static if isdefined(Base, :get_extension)
6-
import Unitful: Quantity, RealOrRealQuantity, ustrip, unit
7-
else
8-
import ..Unitful: Quantity, RealOrRealQuantity, ustrip, unit
9-
end
5+
UnicodePlots.@ext_imp_use :import Unitful Quantity RealOrRealQuantity ustrip unit
106

117
function unit_str(x, fancy)
128
io = IOContext(PipeBuffer(), :fancy_exponent => fancy)

src/common.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ const BORDER_COLOR = Ref(:dark_gray)
198198

199199
############################################################################################
200200
# misc
201+
202+
"""
203+
@ext_imp_use :import Unitful Quantity RealOrRealQuantity
204+
205+
Equivalent to the following, for `Requires` or weak deps:
206+
```
207+
@static if isdefined(Base, :get_extension)
208+
import Unitful: Quantity, RealOrRealQuantity
209+
else
210+
import ..Unitful: Quantity, RealOrRealQuantity
211+
end
212+
```
213+
"""
214+
macro ext_imp_use(imp_use::QuoteNode, mod::Symbol, args...)
215+
dots = ntuple(_ -> :., isdefined(Base, :get_extension) ? 1 : 3)
216+
ex = if length(args) > 0
217+
Expr(:(:), Expr(dots..., mod), Expr.(:., args)...)
218+
else
219+
Expr(dots..., mod)
220+
end
221+
Expr(imp_use.value, ex) |> esc
222+
end
223+
201224
const FSCALES = (identity = identity, ln = log, log2 = log2, log10 = log10) # forward
202225
const ISCALES = (identity = identity, ln = exp, log2 = exp2, log10 = exp10) # inverse
203226
const BASES = (identity = nothing, ln = "", log2 = "2", log10 = "10")

0 commit comments

Comments
 (0)