diff --git a/Manifest.toml b/Manifest.toml new file mode 100644 index 0000000..05672c6 --- /dev/null +++ b/Manifest.toml @@ -0,0 +1,141 @@ +# This file is machine-generated - editing it directly is not advised + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[CSTParser]] +deps = ["Tokenize"] +git-tree-sha1 = "c69698c3d4a7255bc1b4bc2afc09f59db910243b" +uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f" +version = "0.6.2" + +[[Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "ed2c4abadf84c53d9e58510b5fc48912c2336fbb" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "2.2.0" + +[[DataStructures]] +deps = ["InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "f94423c68f2e47db0d6f626a26d4872266e0ec3d" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.17.2" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[LibGit2]] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LinearAlgebra]] +deps = ["Libdl"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[MacroTools]] +deps = ["CSTParser", "Compat", "DataStructures", "Test", "Tokenize"] +git-tree-sha1 = "d6e9dedb8c92c3465575442da456aec15a89ff76" +uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" +version = "0.5.1" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[Media]] +deps = ["MacroTools", "Test"] +git-tree-sha1 = "75a54abd10709c01f1b86b84ec225d26e840ed58" +uuid = "e89f7d12-3494-54d1-8411-f7d8b9ae1f27" +version = "0.5.0" + +[[Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[OrderedCollections]] +deps = ["Random", "Serialization", "Test"] +git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.1.0" + +[[Pkg]] +deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[Profile]] +deps = ["Printf"] +uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" + +[[ProgressLogging]] +deps = ["Logging"] +git-tree-sha1 = "24f32598a44f1a3bfe456c3a0feb3388b9707629" +repo-rev = "master" +repo-url = "https://github.com/JunoLab/ProgressLogging.jl.git" +uuid = "33c8b6b6-d38a-422a-b730-caa89a2f386c" +version = "0.1.0" + +[[REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[[Test]] +deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[Tokenize]] +git-tree-sha1 = "dfcdbbfb2d0370716c815cbd6f8a364efb6f42cf" +uuid = "0796e94c-ce3b-5d07-9a54-7f471281c624" +version = "0.5.6" + +[[UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" diff --git a/Project.toml b/Project.toml index 5e88a3c..f1c5db4 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,7 @@ Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" Media = "e89f7d12-3494-54d1-8411-f7d8b9ae1f27" Profile = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" +ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] diff --git a/src/Juno.jl b/src/Juno.jl index 76c50dd..52433ec 100644 --- a/src/Juno.jl +++ b/src/Juno.jl @@ -6,6 +6,9 @@ import Media: render export Media, media, @render +using ProgressLogging: @progress, progress +export @progress + _active = false isprecompiling() = ccall(:jl_generating_output, Cint, ()) == 1 @@ -24,7 +27,6 @@ isactive() = _active include("types.jl") include("frontend.jl") -include("progress.jl") include("user.jl") include("utils.jl") diff --git a/src/progress.jl b/src/progress.jl deleted file mode 100644 index 13d472a..0000000 --- a/src/progress.jl +++ /dev/null @@ -1,140 +0,0 @@ -export @progress - -using Logging: @logmsg, LogLevel - -const PROGRESSLEVEL = LogLevel(-1) - -""" - progress(f::Function; name = "", msg = "") - -Evaluates `f` with `id` as its argument and makes sure to destroy the progress -bar afterwards. To update the progress bar in `f` you can call a logging statement -like `@info` or even just `@logmsg` with `_id=id` and `progress` as arguments. - -`progress` can take either of the following values: - - `0 <= progress < 1`: create or update progress bar - - `progress == nothing || progress = NaN`: set progress bar to indeterminate progress - - `progress >= 1 || progress == "done"`: destroy progress bar - -The logging message (e.g. `"foo"` in `@info "foo"`) will be used as the progress -bar's name. - -```julia -Juno.progress() do id - for i = 1:10 - sleep(0.5) - @info "iterating" progress=i/10 _id=id - end -end -``` -""" -function progress(f; name = "") - _id = gensym() - @logmsg PROGRESSLEVEL name progress=NaN _id=_id - try - f(_id) - finally - @logmsg PROGRESSLEVEL name progress="done" _id=_id - end -end - -""" - @progress [name="", threshold=0.005] for i = ..., j = ..., ... - @progress [name="", threshold=0.005] x = [... for i = ..., j = ..., ...] - -Show a progress meter named `name` for the given loop or array comprehension -if possible. Update frequency is limited by `threshold` (one update per 0.5% of -progress by default). -""" -macro progress(args...) - _progress(args...) -end - -_progress(ex) = _progress("", 0.005, ex) -_progress(name::Union{AbstractString, Expr}, ex) = _progress(name, 0.005, ex) -_progress(thresh::Real, ex) = _progress("", thresh, ex) - -function _progress(name, thresh, ex) - if ex.head == Symbol("=") && - ex.args[2].head == :comprehension && - ex.args[2].args[1].head == :generator - # comprehension: = [ for in ,...] - loop = _comprehension - target = esc(ex.args[1]) - result = target - gen_ex = ex.args[2].args[1] - body = esc(gen_ex.args[1]) - iter_exprs = gen_ex.args[2:end] - iter_vars = [e.args[1] for e in iter_exprs] - ranges = [e.args[2] for e in iter_exprs] - elseif ex.head == :for && - ex.args[1].head == Symbol("=") && - ex.args[2].head == :block - # single-variable for: for = ; end - loop = _for - target = :_ - result = :nothing - iter_vars = [ex.args[1].args[1]] - ranges = [ex.args[1].args[2]] - body = esc(ex.args[2]) - elseif ex.head == :for && - ex.args[1].head == :block && - ex.args[2].head == :block - # multi-variable for: for = ,...; end - loop = _for - target = :_ - result = :nothing - # iter_vars and ranges are ordered from inner loop to outer loop, for - # consistent computation of progress between for loops and comprehensions - iter_vars = reverse([e.args[1] for e in ex.args[1].args]) - ranges = reverse([e.args[2] for e in ex.args[1].args]) - body = esc(ex.args[2]) - else - error("@progress requires a for loop (for i in irange, j in jrange, ...; end) " * - "or array comprehension with assignment (x = [ for i in irange, j in jrange, ...])") - end - _progress(name, thresh, ex, target, result, loop, iter_vars, ranges, body) -end - -function _progress(name, thresh, ex, target, result, loop, iter_vars, ranges, body) - count_vars = [Symbol("i$k") for k=1:length(iter_vars)] - iter_exprs = [:(($i,$(esc(v))) = enumerate($(esc(r)))) - for (i,v,r) in zip(count_vars,iter_vars,ranges)] - _id = "progress_$(gensym())" - quote - if isactive() - @logmsg($PROGRESSLEVEL, $(esc(name)), progress=0.0, _id=Symbol($_id)) - $target = try - ranges = $(Expr(:vect,esc.(ranges)...)) - nranges = length(ranges) - lens = length.(ranges) - n = prod(lens) - strides = cumprod([1;lens[1:end-1]]) - _frac(i) = (sum((i-1)*s for (i,s) in zip(i,strides)) + 1) / n - lastfrac = 0.0 - - - $(loop(iter_exprs, - quote - val = $body - frac = _frac($(Expr(:vect, count_vars...))) - if frac - lastfrac > $thresh - @logmsg($PROGRESSLEVEL, $(esc(name)), progress=frac, _id=Symbol($_id)) - lastfrac = frac - end - val - end - )) - - finally - @logmsg($PROGRESSLEVEL, $(esc(name)), progress="done", _id=Symbol($_id)) - end - $result - else - $(esc(ex)) - end - end -end - -_comprehension(iter_exprs, body,) = Expr(:comprehension, Expr(:generator, body, iter_exprs...)) -_for(iter_exprs, body) = Expr(:for, Expr(:block, reverse(iter_exprs)...), body)