From 77079f8635ad54ab3c926256643f66d5bc6f14e2 Mon Sep 17 00:00:00 2001 From: Josh Langsfeld Date: Fri, 6 Feb 2015 15:51:08 -0500 Subject: [PATCH] Add filtering behavior to whos() Defaults to not displaying modules --- base/show.jl | 20 ++++++++++++-------- doc/stdlib/base.rst | 10 +++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/base/show.jl b/base/show.jl index a9fddd8847579..b40807549cfa8 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1111,17 +1111,21 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices) cartesianmap(print_slice, tail) end -function whos(m::Module, pattern::Regex) - for v in sort(names(m)) - s = string(v) - if isdefined(m,v) && ismatch(pattern, s) - println(rpad(s, 30), summary(eval(m,v))) +function whos(m::Module, pattern::Regex; filter=[]) + filtertypes = applicable(start, filter) ? + filter : [filter] + filtertest(var) = ! any(T -> typeof(var) <: T, filtertypes) + for n in sort(names(m)) + s = string(n) + v = eval(m,n) + if isdefined(m,n) && ismatch(pattern, s) && filtertest(v) + println(rpad(s, 30), summary(v)) end end end -whos() = whos(r"") -whos(m::Module) = whos(m, r"") -whos(pat::Regex) = whos(current_module(), pat) +whos(;filter=Module) = whos(r""; filter=filter) +whos(m::Module; filter=[]) = whos(m, r""; filter=filter) +whos(pat::Regex; filter=[]) = whos(current_module(), pat; filter=filter) # global flag for limiting output # TODO: this should be replaced with a better mechanism. currently it is only diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index bb71603bd23d8..69f4e69d76496 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -36,10 +36,14 @@ Getting Around Determine whether Julia is running an interactive session. -.. function:: whos([Module,] [pattern::Regex]) +.. function:: whos([Module,] [pattern::Regex]; [filter=nothing]) - Print information about exported global variables in a module, optionally restricted - to those matching ``pattern``. + Print information about exported global variables in a module (defaults to + the current module), optionally restricted to those matching ``pattern``. + + The ``filter`` keyword is either a single type or iterable object of types. + Variables who are of type or subtype of an element in ``filter`` will not be + displayed. If invoked without arguments, ``filter`` defaults to ``Module``. .. function:: edit(file::AbstractString, [line])