Skip to content

Commit 8b49833

Browse files
authored
Merge pull request #117 from imtutta/feature/extend_plugin
feat: extend plugin behaviour to customize the view of body's items
2 parents f5b98f1 + b90c7ce commit 8b49833

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

docs/plugin.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,35 @@ Support `{byte, 1024*10}` to ` 10.0000 KB`; `{percent, 0.12}` to `12.00%`.
157157

158158
Support F/B to page up/down.
159159

160-
[A more specific plugin](https://github.com/zhongwencool/os_stats) can collect linux system information such as kernel vsn, loadavg, disk, memory usage, cpu utilization, IO statistics.
160+
[A more specific plugin](https://github.com/zhongwencool/os_stats) can collect linux system information such as kernel vsn, loadavg, disk, memory usage, cpu utilization, IO statistics.
161+
162+
3. Handler: specific per-item behavior
163+
164+
By default, once you select a row, it will show the process information in `observer_cli_process` view. This is done
165+
by looking for a `pid` in the row, so the first one found will be used and passed to the `observer_cli_process` view.
166+
167+
To customize this behavior, you can implement your own handler. The handler is a tuple with a function and a module.
168+
The function is a predicate that will be used to filter all row's items and, if the resulting list in not empty, the
169+
`Handler:start/3` function will be called. The signature is the same of `observer_cli_process:start/3`.
170+
171+
The new configuration will look like this:
172+
173+
```erlang
174+
%% module - Specific module implements plugin behavior. It's mandatory.
175+
%% title - Menu title. It's mandatory.
176+
%% shortcut - Switch plugin by shortcut. It's mandatory.
177+
%% interval - Refresh interval ms. It's optional. default is 1500ms.
178+
%% sort_column - Sort the sheet by this index. It's optional default is 2.
179+
%% handler - Specific handler implements per-item behavior. It's optional, default is `{fun is_pid/1,observer_cli_process}`.`
180+
181+
{plugins,
182+
[
183+
#{module => observer_cli_plug_behaviour_x, title => "XPlug",
184+
interval => 1600, shortcut => "X", sort_column => 3,
185+
handler => {fun is_pid/1, observer_cli_plug_item_behaviour_x}},
186+
#{module => observer_cli_plug_behaviour_y, title => "YPlug",
187+
interval =>2000, shortcut => "Y", sort_column => 3,
188+
handler => {fun is_binary/1, observer_cli_plug_item_behaviour_y}},
189+
]
190+
}
191+
```

src/observer_cli_plugin.erl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ manager(ChildPid, SheetCache, ViewOpts) ->
9595
observer_cli_lib:exit_processes([ChildPid]),
9696
start(ViewOpts#view_opts{plug = PlugOpts#plug{plugs = NewPlugs}});
9797
{jump, CurRow} ->
98+
CurPlugs = maps:get(CurIndex, Plugs),
99+
{Filter, Handler} = maps:get(handler, CurPlugs, {fun is_pid/1, observer_cli_process}),
98100
case ets:lookup(SheetCache, CurRow) of
99101
[{CurRow, Items}] ->
100-
case [I || I <- Items, is_pid(I)] of
101-
[ChoosePid | _] ->
102+
case [I || I <- Items, Filter(I)] of
103+
[ChooseItem | _] ->
102104
observer_cli_lib:exit_processes([ChildPid]),
103105
ets:delete(SheetCache),
104106
NewPlugs = update_plugins(CurIndex, Plugs, #{cur_row => CurRow}),
105107
NewViewOpts = ViewOpts#view_opts{
106108
plug = PlugOpts#plug{plugs = NewPlugs}
107109
},
108-
observer_cli_process:start(plugin, ChoosePid, NewViewOpts);
110+
Handler:start(plugin, ChooseItem, NewViewOpts);
109111
[] ->
110112
manager(ChildPid, SheetCache, ViewOpts)
111113
end;
@@ -114,14 +116,15 @@ manager(ChildPid, SheetCache, ViewOpts) ->
114116
end;
115117
jump ->
116118
CurPlugs = maps:get(CurIndex, Plugs),
117-
CurRow = maps:get(cur_row, CurPlugs),
119+
CurRow = maps:get(cur_row, CurPlugs, observer_cli_process),
120+
{Filter, Handler} = maps:get(handler, CurPlugs, {fun is_pid/1, observer_cli_process}),
118121
case ets:lookup(SheetCache, CurRow) of
119122
[{CurRow, Items}] ->
120-
case [I || I <- Items, is_pid(I)] of
121-
[ChoosePid | _] ->
123+
case [I || I <- Items, Filter(I)] of
124+
[ChooseItem | _] ->
122125
observer_cli_lib:exit_processes([ChildPid]),
123126
ets:delete(SheetCache),
124-
observer_cli_process:start(plugin, ChoosePid, ViewOpts);
127+
Handler:start(plugin, ChooseItem, ViewOpts);
125128
[] ->
126129
manager(ChildPid, SheetCache, ViewOpts)
127130
end;

0 commit comments

Comments
 (0)