Skip to content

Commit 6be37ed

Browse files
committed
Add filtering behavior to whos()
Defaults to not displaying modules
1 parent 8a3d916 commit 6be37ed

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

base/show.jl

+16-8
Original file line numberDiff line numberDiff line change
@@ -1119,17 +1119,25 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
11191119
cartesianmap(print_slice, tail)
11201120
end
11211121

1122-
function whos(m::Module, pattern::Regex)
1123-
for v in sort(names(m))
1124-
s = string(v)
1125-
if isdefined(m,v) && ismatch(pattern, s)
1126-
println(rpad(s, 30), summary(eval(m,v)))
1122+
function whos(m::Module, pattern::Regex; filter=[])
1123+
filtertypes = applicable(start, filter) ?
1124+
filter : [filter]
1125+
for ft in filtertypes
1126+
isa(ft, DataType) ||
1127+
throw(ArgumentError("Non-type object given to filter keyword"))
1128+
end
1129+
filtertest(var) = ! any(T -> typeof(var) <: T, filtertypes)
1130+
for n in sort(names(m))
1131+
s = string(n)
1132+
v = eval(m,n)
1133+
if isdefined(m,n) && ismatch(pattern, s) && filtertest(v)
1134+
println(rpad(s, 30), summary(v))
11271135
end
11281136
end
11291137
end
1130-
whos() = whos(r"")
1131-
whos(m::Module) = whos(m, r"")
1132-
whos(pat::Regex) = whos(current_module(), pat)
1138+
whos(;filter=Module) = whos(r""; filter=filter)
1139+
whos(m::Module; filter=[]) = whos(m, r""; filter=filter)
1140+
whos(pat::Regex; filter=Module) = whos(current_module(), pat; filter=filter)
11331141

11341142
# global flag for limiting output
11351143
# TODO: this should be replaced with a better mechanism. currently it is only

doc/stdlib/base.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ Getting Around
4141

4242
Determine whether Julia is running an interactive session.
4343

44-
.. function:: whos([Module,] [pattern::Regex])
44+
.. function:: whos([Module,] [pattern::Regex]; [filter=Module])
4545

46-
Print information about exported global variables in a module, optionally restricted
47-
to those matching ``pattern``.
46+
Print information about exported global variables in a module (defaults to
47+
the current module), optionally restricted to those matching ``pattern``.
48+
49+
The ``filter`` keyword is either a single type or iterable object of types.
50+
Variables who are of type or subtype of an element in ``filter`` will not be
51+
displayed. E.g., ``filter=Any`` will prevent any output. If the function is
52+
called with an explicit Module argument, nothing will be filtered.
53+
Otherwise, submodules in the current module will not be displayed.
4854

4955
.. function:: edit(file::AbstractString, [line])
5056

0 commit comments

Comments
 (0)