Skip to content

Commit

Permalink
rework import / using when using weak exts
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Feb 5, 2023
1 parent adc63db commit e55237a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
7 changes: 2 additions & 5 deletions ext/FreeTypeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
module FreeTypeExt

import UnicodePlots
@static if isdefined(Base, :get_extension)
using FreeType, FileIO
else
using ..FreeType, ..FileIO
end
UnicodePlots.@ext_imp_use :using FreeType
UnicodePlots.@ext_imp_use :import FileIO
using StaticArrays
using ColorTypes

Expand Down
6 changes: 1 addition & 5 deletions ext/ImageInTerminalExt.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module ImageInTerminalExt

import UnicodePlots
@static if isdefined(Base, :get_extension)
import ImageInTerminal
else
import ..ImageInTerminal
end
UnicodePlots.@ext_imp_use :import ImageInTerminal
using ColorTypes

UnicodePlots.sixel_encode(args...; kw...) = ImageInTerminal.sixel_encode(args...; kw...) # COV_EXCL_LINE
Expand Down
6 changes: 1 addition & 5 deletions ext/UnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ module UnitfulExt

import UnicodePlots
import UnicodePlots: KEYWORDS, Plot, Canvas
@static if isdefined(Base, :get_extension)
import Unitful: Quantity, RealOrRealQuantity, ustrip, unit
else
import ..Unitful: Quantity, RealOrRealQuantity, ustrip, unit
end
UnicodePlots.@ext_imp_use :import Unitful Quantity RealOrRealQuantity ustrip unit

function unit_str(x, fancy)
io = IOContext(PipeBuffer(), :fancy_exponent => fancy)
Expand Down
23 changes: 23 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,29 @@ const BORDER_COLOR = Ref(:dark_gray)

############################################################################################
# misc

"""
@ext_imp_use :import Unitful Quantity RealOrRealQuantity
Equivalent to the following, for `Requires` or weak deps:
```
@static if isdefined(Base, :get_extension)
import Unitful: Quantity, RealOrRealQuantity
else
import ..Unitful: Quantity, RealOrRealQuantity
end
```
"""
macro ext_imp_use(imp_use::QuoteNode, mod::Symbol, args...)
dots = ntuple(_ -> :., isdefined(Base, :get_extension) ? 1 : 3)
ex = if length(args) > 0
Expr(:(:), Expr(dots..., mod), Expr.(:., args)...)
else
Expr(dots..., mod)
end
Expr(imp_use.value, ex) |> esc
end

const FSCALES = (identity = identity, ln = log, log2 = log2, log10 = log10) # forward
const ISCALES = (identity = identity, ln = exp, log2 = exp2, log10 = exp10) # inverse
const BASES = (identity = nothing, ln = "", log2 = "2", log10 = "10")
Expand Down

0 comments on commit e55237a

Please sign in to comment.