From 7b6d9e6a14fbff2da040085fc5d52cb3a24ef2f3 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 | 26 ++++++++++++++++++-------- doc/stdlib/base.rst | 10 +++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/base/show.jl b/base/show.jl index ada4cc922ede4..deb542b227e07 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1113,17 +1113,27 @@ 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] + istype(t) = any(T -> isa(t,T), [DataType,UnionType,TypeConstructor]) + if any(T -> ! istype(T), filtertypes) + throw(ArgumentError( + "Keyword argument \"filter\" must be a type or iterable of types" + )) + end + 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 e49336c181648..9ca7a3a1087ce 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 whose type or any supertype is given will not be displayed. If + invoked without arguments, ``filter`` defaults to ``Module``. .. function:: edit(file::AbstractString, [line])