Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! A customizable cross-platform graphical process/system monitor for the
2 //! terminal. Supports Linux, macOS, and Windows. Inspired by gtop, gotop, and
3 //! htop.
4 //!
5 //! **Note:** The following documentation is primarily intended for people to
6 //! refer to for development purposes rather than the actual usage of the
7 //! application. If you are instead looking for documentation regarding the
8 //! *usage* of bottom, refer to [here](https://bottom.pages.dev/stable/).
9
10 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod apprust-analyzer cargo bottom 0.14.9 app/;
11 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod componentsrust-analyzer cargo bottom 0.14.9 components/;
12 mod utilsrust-analyzer cargo bottom 0.14.9 utils/ {
13 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod cancellation_tokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/;
14 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod conversionrust-analyzer cargo bottom 0.14.9 utils/conversion/;
15 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod data_unitsrust-analyzer cargo bottom 0.14.9 utils/data_units/;
16 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod generalrust-analyzer cargo bottom 0.14.9 utils/general/;
17 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod inputrust-analyzer cargo bottom 0.14.9 utils/input/;
18 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod int_hashrust-analyzer cargo bottom 0.14.9 utils/int_hash/;
19 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod loggingrust-analyzer cargo bottom 0.14.9 utils/logging/;
20 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod process_killerrust-analyzer cargo bottom 0.14.9 utils/process_killer/;
21 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod stringsrust-analyzer cargo bottom 0.14.9 utils/strings/;
22 }
23 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod canvasrust-analyzer cargo bottom 0.14.9 canvas/;
24 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod collectionrust-analyzer cargo bottom 0.14.9 collection/;
25 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod constantsrust-analyzer cargo bottom 0.14.9 constants/;
26 pub(craterust-analyzer cargo bottom 0.14.9 crate/) mod eventrust-analyzer cargo bottom 0.14.9 event/;
27 pub mod optionsrust-analyzer cargo bottom 0.14.9 options/;
28 pub mod widgetsrust-analyzer cargo bottom 0.14.9 widgets/;
29
30 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
31 boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#,
32 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/Stdout#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stderr()., stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().},
33 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/, PanicHookInforust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/PanicHookInfo#},
34 syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::{
35 Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#,
36 mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/, Receiverrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Receiver#, Senderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Sender#},
37 },
38 threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/, JoinHandlerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/JoinHandle#},
39 timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::{Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#},
40 };
41
42 use apprust-analyzer cargo bottom 0.14.9 app/::{Apprust-analyzer cargo bottom 0.14.9 app/App#, AppConfigFieldsrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#, DataFiltersrust-analyzer cargo bottom 0.14.9 app/DataFilters#, layout_managerrust-analyzer cargo bottom 0.14.9 app/layout_manager/::UsedWidgetsrust-analyzer cargo bottom 0.14.9 app/layout_manager/UsedWidgets#};
43 use crosstermrust-analyzer cargo crossterm 0.29.0 crate/::{
44 cursorrust-analyzer cargo crossterm 0.29.0 cursor/::Showrust-analyzer cargo crossterm 0.29.0 cursor/Show#,
45 eventrust-analyzer cargo crossterm 0.29.0 event/::{
46 DisableBracketedPasterust-analyzer cargo crossterm 0.29.0 event/DisableBracketedPaste#, DisableMouseCapturerust-analyzer cargo crossterm 0.29.0 event/DisableMouseCapture#, EnableBracketedPasterust-analyzer cargo crossterm 0.29.0 event/EnableBracketedPaste#, EnableMouseCapturerust-analyzer cargo crossterm 0.29.0 event/EnableMouseCapture#,
47 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#, KeyEventKindrust-analyzer cargo crossterm 0.29.0 event/KeyEventKind#, MouseEventKindrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#, pollrust-analyzer cargo crossterm 0.29.0 event/poll()., readrust-analyzer cargo crossterm 0.29.0 event/read/,
48 },
49 executerust-analyzer cargo crossterm 0.29.0 macros/execute!,
50 terminalrust-analyzer cargo crossterm 0.29.0 terminal/::{EnterAlternateScreenrust-analyzer cargo crossterm 0.29.0 terminal/EnterAlternateScreen#, LeaveAlternateScreenrust-analyzer cargo crossterm 0.29.0 terminal/LeaveAlternateScreen#, disable_raw_moderust-analyzer cargo crossterm 0.29.0 terminal/disable_raw_mode()., enable_raw_moderust-analyzer cargo crossterm 0.29.0 terminal/enable_raw_mode().},
51 };
52 use eventrust-analyzer cargo bottom 0.14.9 event/::{BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#, CollectionThreadEventrust-analyzer cargo bottom 0.14.9 event/CollectionThreadEvent#, handle_key_event_or_breakrust-analyzer cargo bottom 0.14.9 event/handle_key_event_or_break()., handle_mouse_eventrust-analyzer cargo bottom 0.14.9 event/handle_mouse_event().};
53 use optionsrust-analyzer cargo bottom 0.14.9 options/::{argsrust-analyzer cargo bottom 0.14.9 options/args/, get_or_create_configrust-analyzer cargo bottom 0.14.9 options/get_or_create_config()., init_apprust-analyzer cargo bottom 0.14.9 options/init_app().};
54 use ratatuirust-analyzer cargo ratatui 0.30.2 crate/::{Terminalrust-analyzer cargo ratatui-core 0.1.2 terminal/Terminal#, backendrust-analyzer cargo ratatui 0.30.2 backend/::CrosstermBackendrust-analyzer cargo ratatui-crossterm 0.1.2 CrosstermBackend#, preluderust-analyzer cargo ratatui 0.30.2 prelude/::Backendrust-analyzer cargo ratatui-core 0.1.2 backend/Backend#};
55 #[allow(unused_imports, reason = "this is needed if logging is enabled")]
56 use utilsrust-analyzer cargo bottom 0.14.9 utils/::loggingrust-analyzer cargo bottom 0.14.9 utils/logging/::*;
57 use utilsrust-analyzer cargo bottom 0.14.9 utils/::{cancellation_tokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/::CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#, conversionrust-analyzer cargo bottom 0.14.9 utils/conversion/::*};
58
59 use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::Datarust-analyzer cargo bottom 0.14.9 collection/Data#;
60
61 // Used for heap allocation debugging purposes.
62 // #[global_allocator]
63 // static ALLOC: dhat::Alloc = dhat::Alloc;
64
65 /// Try drawing. If not, clean up the terminal and return an error.
66 fn try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(
67 terminal: &mut Terminalrust-analyzer cargo ratatui-core 0.1.2 terminal/Terminal#<CrosstermBackendrust-analyzer cargo ratatui-crossterm 0.1.2 CrosstermBackend#<Stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/Stdout#>>, app: &mut Apprust-analyzer cargo bottom 0.14.9 app/App#, painter: &mut canvasrust-analyzer cargo bottom 0.14.9 canvas/::Painterrust-analyzer cargo bottom 0.14.9 canvas/Painter#,
68 ) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> {
69 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = painter.draw_datarust-analyzer cargo bottom 0.14.9 canvas/impl#[Painter]draw_data().(terminal, app) {
70 cleanup_terminalrust-analyzer cargo bottom 0.14.9 cleanup_terminal().(terminal)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
71 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
72 } else {
73 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
74 }
75 }
76
77 /// Clean up the terminal before returning it to the user.
78 fn cleanup_terminalrust-analyzer cargo bottom 0.14.9 cleanup_terminal().(terminal: &mut Terminalrust-analyzer cargo ratatui-core 0.1.2 terminal/Terminal#<CrosstermBackendrust-analyzer cargo ratatui-crossterm 0.1.2 CrosstermBackend#<Stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/Stdout#>>) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> {
79 disable_raw_moderust-analyzer cargo crossterm 0.29.0 terminal/disable_raw_mode().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
80
81 executerust-analyzer cargo crossterm 0.29.0 macros/execute!!(
82 terminal.backend_mutrust-analyzer cargo ratatui-core 0.1.2 terminal/backend/impl#[`Terminal<B>`]backend_mut().(),
83 DisableMouseCapturerust-analyzer cargo crossterm 0.29.0 event/DisableMouseCapture#,
84 DisableBracketedPasterust-analyzer cargo crossterm 0.29.0 event/DisableBracketedPaste#,
85 LeaveAlternateScreenrust-analyzer cargo crossterm 0.29.0 terminal/LeaveAlternateScreen#,
86 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
87 terminal.show_cursorrust-analyzer cargo ratatui-core 0.1.2 terminal/cursor/impl#[`Terminal<B>`]show_cursor().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
88
89 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
90 }
91
92 /// Check and report to the user if the current environment is not a terminal.
93 fn check_if_terminalrust-analyzer cargo bottom 0.14.9 check_if_terminal().() {
94 use crosstermrust-analyzer cargo crossterm 0.29.0 crate/::ttyrust-analyzer cargo crossterm 0.29.0 tty/::IsTtyrust-analyzer cargo crossterm 0.29.0 tty/IsTty#;
95
96 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().().is_ttyrust-analyzer cargo crossterm 0.29.0 tty/impl#[S][IsTty]is_tty().() {
97 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!(
98 "Warning: bottom is not being output to a terminal. Things might not work properly."
99 );
100 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("If you're stuck, press 'q', 'Q', or 'Ctrl-c' to quit the program.");
101 stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stderr().().flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stderr][Write]flush().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("should succeed in flushing stderr");
102 threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1));
103 }
104 }
105
106 /// This manually resets stdout back to normal state.
107 pub fn reset_stdoutrust-analyzer cargo bottom 0.14.9 reset_stdout().() {
108 let mut stdout = stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().();
109 let _ = disable_raw_moderust-analyzer cargo crossterm 0.29.0 terminal/disable_raw_mode().();
110 let _ = executerust-analyzer cargo crossterm 0.29.0 macros/execute!!(
111 stdout,
112 DisableMouseCapturerust-analyzer cargo crossterm 0.29.0 event/DisableMouseCapture#,
113 DisableBracketedPasterust-analyzer cargo crossterm 0.29.0 event/DisableBracketedPaste#,
114 LeaveAlternateScreenrust-analyzer cargo crossterm 0.29.0 terminal/LeaveAlternateScreen#,
115 Showrust-analyzer cargo crossterm 0.29.0 cursor/Show#,
116 );
117 }
118
119 /// A panic hook to properly restore the terminal in the case of a panic.
120 /// Originally based on [spotify-tui's implementation](https://github.com/Rigellute/spotify-tui/blob/master/src/main.rs).
121 fn panic_hookrust-analyzer cargo bottom 0.14.9 panic_hook().(panic_info: &PanicHookInforust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/PanicHookInfo#<'_>) {
122 let msg = match panic_info.payloadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/impl#[`PanicHookInfo<'a>`]payload().().downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<&'static str>() {
123 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) => *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().s,
124 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => match panic_info.payloadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/impl#[`PanicHookInfo<'a>`]payload().().downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>() {
125 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) => &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
126 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => "Box<Any>",
127 },
128 };
129
130 let backtrace = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::backtracerust-analyzer cargo std https://github.com/rust-lang/rust/library/std backtrace/::Backtracerust-analyzer cargo std https://github.com/rust-lang/rust/library/std backtrace/Backtrace#::capturerust-analyzer cargo std https://github.com/rust-lang/rust/library/std backtrace/impl#[Backtrace]capture().());
131
132 reset_stdoutrust-analyzer cargo bottom 0.14.9 reset_stdout().();
133
134 // Print stack trace. Must be done after!
135 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(panic_info) = panic_info.locationrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/impl#[`PanicHookInfo<'a>`]location().() {
136 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("thread '<unnamed>' panicked at '{msg}', {panic_info}\n\r{backtrace}")
137 }
138
139 // TODO: Might be cleaner in the future to use a cancellation token, but that
140 // causes some fun issues with lifetimes; for now if it panics then shut
141 // down the main program entirely ASAP.
142 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1);
143 }
144
145 /// Create a thread to poll for user inputs and forward them to the main thread.
146 fn create_input_threadrust-analyzer cargo bottom 0.14.9 create_input_thread().(
147 sender: Senderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Sender#<BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#>, cancellation_token: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#>,
148 app_config_fields: &AppConfigFieldsrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#,
149 ) -> JoinHandlerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/JoinHandle#<()> {
150 let keys_disabled = app_config_fields.disable_keysrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#disable_keys.;
151
152 threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || {
153 let mut mouse_timer = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
154
155 loop {
156 // We don't block.
157 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(is_terminated) = cancellation_token.try_checkrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]try_check().()
158 && is_terminated
159 {
160 break;
161 }
162
163 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(poll) = pollrust-analyzer cargo crossterm 0.29.0 event/poll().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(20))
164 && poll
165 && let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(event) = readrust-analyzer cargo crossterm 0.29.0 event/read().()
166 {
167 match event {
168 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::Resizerust-analyzer cargo crossterm 0.29.0 event/Event#Resize#(_, _) => {
169 // TODO: Might want to debounce this in the future, or take into
170 // account the actual resize values.
171 // Maybe we want to keep the current implementation in case the
172 // resize event might not fire...
173 // not sure.
174
175 if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Resizerust-analyzer cargo bottom 0.14.9 event/BottomEvent#Resize#).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
176 break;
177 }
178 }
179 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::Pasterust-analyzer cargo crossterm 0.29.0 event/Event#Paste#(paste) => {
180 if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::PasteEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#PasteEvent#(paste)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
181 break;
182 }
183 }
184 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::Keyrust-analyzer cargo crossterm 0.29.0 event/Event#Key#(key) if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().keys_disabled && key.kindrust-analyzer cargo crossterm 0.29.0 event/KeyEvent#kind. rust-analyzer cargo crossterm 0.29.0 event/impl#[KeyEventKind][`PartialEq<Self>`]eq().==rust-analyzer cargo crossterm 0.29.0 event/impl#[KeyEventKind][`PartialEq<Self>`]eq(). rust-analyzer cargo crossterm 0.29.0 event/impl#[KeyEventKind][`PartialEq<Self>`]eq().KeyEventKindrust-analyzer cargo crossterm 0.29.0 event/KeyEventKind#::Pressrust-analyzer cargo crossterm 0.29.0 event/KeyEventKind#Press# => {
185 // For now, we only care about key down events. This may change in
186 // the future.
187 if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::KeyInputrust-analyzer cargo bottom 0.14.9 event/BottomEvent#KeyInput#(key)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
188 break;
189 }
190 }
191 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::Mouserust-analyzer cargo crossterm 0.29.0 event/Event#Mouse#(mouse) => match mouse.kindrust-analyzer cargo crossterm 0.29.0 event/MouseEvent#kind. {
192 MouseEventKindrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#::Movedrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#Moved# | MouseEventKindrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#::Dragrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#Drag#(..) => {}
193 MouseEventKindrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#::ScrollDownrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#ScrollDown# | MouseEventKindrust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#::ScrollUprust-analyzer cargo crossterm 0.29.0 event/MouseEventKind#ScrollUp# => {
194 if Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().().duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]duration_since().(mouse_timer).as_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_millis().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]ge().20 {
195 if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::MouseInputrust-analyzer cargo bottom 0.14.9 event/BottomEvent#MouseInput#(mouse)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
196 break;
197 }
198 mouse_timer = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
199 }
200 }
201 _ => {
202 if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::MouseInputrust-analyzer cargo bottom 0.14.9 event/BottomEvent#MouseInput#(mouse)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
203 break;
204 }
205 }
206 },
207 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::Keyrust-analyzer cargo crossterm 0.29.0 event/Event#Key#(_) => {}
208 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::FocusGainedrust-analyzer cargo crossterm 0.29.0 event/Event#FocusGained# => {}
209 Eventrust-analyzer cargo crossterm 0.29.0 event/Event#::FocusLostrust-analyzer cargo crossterm 0.29.0 event/Event#FocusLost# => {}
210 }
211 }
212 }
213 })
214 }
215
216 /// Create a thread to handle data collection.
217 fn create_collection_threadrust-analyzer cargo bottom 0.14.9 create_collection_thread().(
218 sender: Senderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Sender#<BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#>, control_receiver: Receiverrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Receiver#<CollectionThreadEventrust-analyzer cargo bottom 0.14.9 event/CollectionThreadEvent#>,
219 cancellation_token: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#>, app_config_fields: &AppConfigFieldsrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#,
220 filters: DataFiltersrust-analyzer cargo bottom 0.14.9 app/DataFilters#, used_widget_set: UsedWidgetsrust-analyzer cargo bottom 0.14.9 app/layout_manager/UsedWidgets#,
221 ) -> JoinHandlerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/JoinHandle#<()> {
222 let use_current_cpu_total = app_config_fields.use_current_cpu_totalrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#use_current_cpu_total.;
223 let unnormalized_cpu = app_config_fields.unnormalized_cpurust-analyzer cargo bottom 0.14.9 app/AppConfigFields#unnormalized_cpu.;
224 let show_average_cpu = app_config_fields.show_average_cpurust-analyzer cargo bottom 0.14.9 app/AppConfigFields#show_average_cpu.;
225 let update_sleep = app_config_fields.update_raterust-analyzer cargo bottom 0.14.9 app/AppConfigFields#update_rate.;
226 let get_process_threads = app_config_fields.get_process_threadsrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#get_process_threads.;
227 #[cfg(feature = "zfs")]
228 let get_arc_free = app_config_fields.free_arcrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#free_arc.;
229 let include_unmounted_disks =
230 app_config_fields.disk_show_unmountedrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#disk_show_unmounted. || app_config_fields.disk_io_graph_show_unmountedrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#disk_io_graph_show_unmounted.;
231
232 threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || {
233 let mut data_collector = collectionrust-analyzer cargo bottom 0.14.9 collection/::DataCollectorrust-analyzer cargo bottom 0.14.9 collection/DataCollector#::newrust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]new().(filters);
234
235 data_collector.set_collectionrust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_collection().(used_widget_set);
236 data_collector.set_use_current_cpu_totalrust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_use_current_cpu_total().(use_current_cpu_total);
237 data_collector.set_unnormalized_cpurust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_unnormalized_cpu().(unnormalized_cpu);
238 data_collector.set_show_average_cpurust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_show_average_cpu().(show_average_cpu);
239 data_collector.set_get_process_threadsrust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_get_process_threads().(get_process_threads);
240 #[cfg(feature = "zfs")]
241 data_collector.set_free_arc_memrust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_free_arc_mem().(get_arc_free);
242 data_collector.set_include_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]set_include_unmounted_disks().(include_unmounted_disks);
243
244 data_collector.update_datarust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]update_data().();
245 data_collector.datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#data. = Datarust-analyzer cargo bottom 0.14.9 collection/Data#::defaultrust-analyzer cargo bottom 0.14.9 collection/impl#[Data][Default]default().();
246
247 // Tiny sleep I guess? To go between the first update above and the first update
248 // in the loop.
249 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(5));
250
251 loop {
252 // Check once at the very top... don't block though.
253 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(is_terminated) = cancellation_token.try_checkrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]try_check().()
254 && is_terminated
255 {
256 break;
257 }
258
259 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(message) = control_receiver.try_recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]try_recv().() {
260 // trace!("Received message in collection thread: {message:?}");
261 match message {
262 CollectionThreadEventrust-analyzer cargo bottom 0.14.9 event/CollectionThreadEvent#::Resetrust-analyzer cargo bottom 0.14.9 event/CollectionThreadEvent#Reset# => {
263 data_collector.datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#data..cleanuprust-analyzer cargo bottom 0.14.9 collection/impl#[Data]cleanup().();
264 }
265 }
266 }
267
268 data_collector.update_datarust-analyzer cargo bottom 0.14.9 collection/impl#[DataCollector]update_data().();
269
270 // Yet another check to bail if needed... do not block!
271 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(is_terminated) = cancellation_token.try_checkrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]try_check().()
272 && is_terminated
273 {
274 break;
275 }
276
277 let event = BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Updaterust-analyzer cargo bottom 0.14.9 event/BottomEvent#Update#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/convert/impl#[`Box<T>`][`From<T>`]from().(data_collector.datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#data.));
278 data_collector.datarust-analyzer cargo bottom 0.14.9 collection/DataCollector#data. = Datarust-analyzer cargo bottom 0.14.9 collection/Data#::defaultrust-analyzer cargo bottom 0.14.9 collection/impl#[Data][Default]default().();
279
280 if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(event).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
281 break;
282 }
283
284 // Sleep while allowing for interruptions...
285 if cancellation_token.sleep_with_cancellationrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]sleep_with_cancellation().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(update_sleep)) {
286 break;
287 }
288 }
289 })
290 }
291
292 /// Main code to call to start bottom.
293 #[inline]
294 pub fn start_bottomrust-analyzer cargo bottom 0.14.9 start_bottom().(enable_error_hook: &mut bool) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> {
295 // let _profiler = dhat::Profiler::new_heap();
296 let args = argsrust-analyzer cargo bottom 0.14.9 options/args/::get_argsrust-analyzer cargo bottom 0.14.9 options/args/get_args().();
297
298 #[cfg(feature = "logging")]
299 {
300 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = init_logger(
301 log::LevelFilter::Debug,
302 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::new("debug.log")),
303 ) {
304 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Issue initializing logger: {err}");
305 }
306 }
307
308 // Read from config file.
309 let config = get_or_create_configrust-analyzer cargo bottom 0.14.9 options/get_or_create_config().(args.generalrust-analyzer cargo bottom 0.14.9 options/args/BottomArgs#general..config_locationrust-analyzer cargo bottom 0.14.9 options/args/GeneralArgs#config_location..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
310
311 // Create the "app" and initialize a bunch of stuff.
312 let (mut app, widget_layout, styling) = init_apprust-analyzer cargo bottom 0.14.9 options/init_app().(args, config)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
313
314 // Create painter and set colours.
315 let mut painter = canvasrust-analyzer cargo bottom 0.14.9 canvas/::Painterrust-analyzer cargo bottom 0.14.9 canvas/Painter#::initrust-analyzer cargo bottom 0.14.9 canvas/impl#[Painter]init().(widget_layout, styling)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
316
317 // Check if the current environment is in a terminal.
318 check_if_terminalrust-analyzer cargo bottom 0.14.9 check_if_terminal().();
319
320 let cancellation_token = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#::defaultrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken][Default]default().());
321 let (sender, receiver) = mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/channel().();
322
323 // Set up the event loop thread; we set this up early to speed up
324 // first-time-to-data.
325 let (collection_thread_ctrl_sender, collection_thread_ctrl_receiver) = mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/channel().();
326 let _collection_thread = create_collection_threadrust-analyzer cargo bottom 0.14.9 create_collection_thread().(
327 sender.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`][Clone]clone().(),
328 collection_thread_ctrl_receiver,
329 cancellation_token.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(),
330 &app.app_config_fieldsrust-analyzer cargo bottom 0.14.9 app/App#app_config_fields.,
331 app.filtersrust-analyzer cargo bottom 0.14.9 app/App#filters..clonerust-analyzer cargo bottom 0.14.9 app/impl#[DataFilters][Clone]clone().(),
332 app.used_widgetsrust-analyzer cargo bottom 0.14.9 app/App#used_widgets.,
333 );
334
335 // Set up the input handling loop thread.
336 let _input_thread = create_input_threadrust-analyzer cargo bottom 0.14.9 create_input_thread().(
337 sender.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`][Clone]clone().(),
338 cancellation_token.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(),
339 &app.app_config_fieldsrust-analyzer cargo bottom 0.14.9 app/App#app_config_fields.,
340 );
341
342 // Set up the cleaning loop thread.
343 let _cleaning_thread = {
344 let cancellation_token = cancellation_token.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().();
345 let cleaning_sender = sender.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`][Clone]clone().();
346 let offset_wait = Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(app.app_config_fieldsrust-analyzer cargo bottom 0.14.9 app/App#app_config_fields..retention_msrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#retention_ms. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().60000);
347 threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || {
348 loop {
349 if cancellation_token.sleep_with_cancellationrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]sleep_with_cancellation().(offset_wait) {
350 break;
351 }
352
353 if cleaning_sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Cleanrust-analyzer cargo bottom 0.14.9 event/BottomEvent#Clean#).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
354 break;
355 }
356 }
357 })
358 };
359
360 // Set up tui and crossterm
361 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().enable_error_hook = true;
362
363 let mut stdout_val = stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().();
364 executerust-analyzer cargo crossterm 0.29.0 macros/execute!!(stdout_val, EnterAlternateScreenrust-analyzer cargo crossterm 0.29.0 terminal/EnterAlternateScreen#, EnableBracketedPasterust-analyzer cargo crossterm 0.29.0 event/EnableBracketedPaste#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
365 if app.app_config_fieldsrust-analyzer cargo bottom 0.14.9 app/App#app_config_fields..disable_clickrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#disable_click. {
366 executerust-analyzer cargo crossterm 0.29.0 macros/execute!!(stdout_val, DisableMouseCapturerust-analyzer cargo crossterm 0.29.0 event/DisableMouseCapture#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
367 } else {
368 executerust-analyzer cargo crossterm 0.29.0 macros/execute!!(stdout_val, EnableMouseCapturerust-analyzer cargo crossterm 0.29.0 event/EnableMouseCapture#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
369 }
370 enable_raw_moderust-analyzer cargo crossterm 0.29.0 terminal/enable_raw_mode().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
371
372 let mut terminal = Terminalrust-analyzer cargo ratatui-core 0.1.2 terminal/Terminal#::newrust-analyzer cargo ratatui-core 0.1.2 terminal/init/impl#[`Terminal<B>`]new().(CrosstermBackendrust-analyzer cargo ratatui-crossterm 0.1.2 CrosstermBackend#::newrust-analyzer cargo ratatui-crossterm 0.1.2 impl#[`CrosstermBackend<W>`]new().(stdout_val))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
373
374 // This may fail in some environments, like tests, since it may fail to get the cursor position.
375 // In that case, fall back to just manually clearing it with backend.
376 if terminal.clearrust-analyzer cargo ratatui-core 0.1.2 terminal/buffers/impl#[`Terminal<B>`]clear().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
377 terminal.backend_mutrust-analyzer cargo ratatui-core 0.1.2 terminal/backend/impl#[`Terminal<B>`]backend_mut().().clearrust-analyzer cargo ratatui-crossterm 0.1.2 impl#[`CrosstermBackend<W>`][Backend]clear().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
378 }
379
380 terminal.hide_cursorrust-analyzer cargo ratatui-core 0.1.2 terminal/cursor/impl#[`Terminal<B>`]hide_cursor().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
381
382 // Set panic hook
383 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::set_hookrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panicking/set_hook().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(panic_hookrust-analyzer cargo bottom 0.14.9 panic_hook().));
384
385 // Set termination hook
386 ctrlcrust-analyzer cargo ctrlc 3.5.2 crate/::set_handlerrust-analyzer cargo ctrlc 3.5.2 set_handler().(move || {
387 // TODO: Consider using signal-hook (https://github.com/vorner/signal-hook) to handle
388 // more types of signals?
389 let _ = sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Terminaterust-analyzer cargo bottom 0.14.9 event/BottomEvent#Terminate#);
390 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
391
392 let mut first_run = true;
393
394 // Draw once first to initialize the canvas, so it doesn't feel like it's
395 // frozen.
396 try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(&mut terminal, &mut app, &mut painter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
397
398 loop {
399 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(recv) = receiver.recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]recv().() {
400 match recv {
401 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Terminaterust-analyzer cargo bottom 0.14.9 event/BottomEvent#Terminate# => break,
402 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Resizerust-analyzer cargo bottom 0.14.9 event/BottomEvent#Resize# => {
403 try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(&mut terminal, &mut app, &mut painter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
404 }
405 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::KeyInputrust-analyzer cargo bottom 0.14.9 event/BottomEvent#KeyInput#(event) => {
406 if handle_key_event_or_breakrust-analyzer cargo bottom 0.14.9 event/handle_key_event_or_break().(event, &mut app, &collection_thread_ctrl_sender) {
407 break;
408 }
409 app.update_datarust-analyzer cargo bottom 0.14.9 app/impl#[App]update_data().();
410 try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(&mut terminal, &mut app, &mut painter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
411 }
412 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::MouseInputrust-analyzer cargo bottom 0.14.9 event/BottomEvent#MouseInput#(event) => {
413 handle_mouse_eventrust-analyzer cargo bottom 0.14.9 event/handle_mouse_event().(event, &mut app);
414 app.update_datarust-analyzer cargo bottom 0.14.9 app/impl#[App]update_data().();
415 try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(&mut terminal, &mut app, &mut painter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
416 }
417 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::PasteEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#PasteEvent#(paste) => {
418 app.handle_pasterust-analyzer cargo bottom 0.14.9 app/impl#[App]handle_paste().(paste);
419 app.update_datarust-analyzer cargo bottom 0.14.9 app/impl#[App]update_data().();
420 try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(&mut terminal, &mut app, &mut painter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
421 }
422 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Updaterust-analyzer cargo bottom 0.14.9 event/BottomEvent#Update#(data) => {
423 app.data_storerust-analyzer cargo bottom 0.14.9 app/App#data_store..eat_datarust-analyzer cargo bottom 0.14.9 app/data/store/impl#[DataStore]eat_data().(data, &app.app_config_fieldsrust-analyzer cargo bottom 0.14.9 app/App#app_config_fields.);
424
425 // This thing is required as otherwise, some widgets can't draw correctly w/o
426 // some data (or they need to be re-drawn).
427 if first_run {
428 first_run = false;
429 app.is_force_redrawrust-analyzer cargo bottom 0.14.9 app/App#is_force_redraw. = true;
430 }
431
432 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().app.data_storerust-analyzer cargo bottom 0.14.9 app/App#data_store..is_frozenrust-analyzer cargo bottom 0.14.9 app/data/store/impl#[DataStore]is_frozen().() {
433 // Convert all data into data for the displayed widgets.
434
435 if app.used_widgetsrust-analyzer cargo bottom 0.14.9 app/App#used_widgets..use_diskrust-analyzer cargo bottom 0.14.9 app/layout_manager/UsedWidgets#use_disk. {
436 for disk in app.statesrust-analyzer cargo bottom 0.14.9 app/App#states..disk_staterust-analyzer cargo bottom 0.14.9 app/states/AppWidgetStates#disk_state..widget_statesrust-analyzer cargo bottom 0.14.9 app/states/DiskState#widget_states..values_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values_mut().() {
437 disk.force_data_updaterust-analyzer cargo bottom 0.14.9 widgets/disk_table/impl#[DiskTableWidget]force_data_update().();
438 }
439 }
440
441 if app.used_widgetsrust-analyzer cargo bottom 0.14.9 app/App#used_widgets..use_temprust-analyzer cargo bottom 0.14.9 app/layout_manager/UsedWidgets#use_temp. {
442 for temp in app.statesrust-analyzer cargo bottom 0.14.9 app/App#states..temp_staterust-analyzer cargo bottom 0.14.9 app/states/AppWidgetStates#temp_state..widget_statesrust-analyzer cargo bottom 0.14.9 app/states/TempState#widget_states..values_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values_mut().() {
443 temp.force_data_updaterust-analyzer cargo bottom 0.14.9 widgets/temperature_table/impl#[TempWidgetState]force_data_update().();
444 }
445 }
446
447 if app.used_widgetsrust-analyzer cargo bottom 0.14.9 app/App#used_widgets..use_procrust-analyzer cargo bottom 0.14.9 app/layout_manager/UsedWidgets#use_proc. {
448 for proc in app.statesrust-analyzer cargo bottom 0.14.9 app/App#states..proc_staterust-analyzer cargo bottom 0.14.9 app/states/AppWidgetStates#proc_state..widget_statesrust-analyzer cargo bottom 0.14.9 app/states/ProcState#widget_states..values_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values_mut().() {
449 proc.force_data_updaterust-analyzer cargo bottom 0.14.9 widgets/process_table/impl#[ProcWidgetState]force_data_update().();
450 }
451 }
452
453 if app.used_widgetsrust-analyzer cargo bottom 0.14.9 app/App#used_widgets..use_cpurust-analyzer cargo bottom 0.14.9 app/layout_manager/UsedWidgets#use_cpu. {
454 for cpu in app.statesrust-analyzer cargo bottom 0.14.9 app/App#states..cpu_staterust-analyzer cargo bottom 0.14.9 app/states/AppWidgetStates#cpu_state..widget_statesrust-analyzer cargo bottom 0.14.9 app/states/CpuState#widget_states..values_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values_mut().() {
455 cpu.force_data_updaterust-analyzer cargo bottom 0.14.9 widgets/cpu_graph/impl#[CpuWidgetState]force_data_update().();
456 }
457 }
458
459 app.update_datarust-analyzer cargo bottom 0.14.9 app/impl#[App]update_data().();
460 try_drawingrust-analyzer cargo bottom 0.14.9 try_drawing().(&mut terminal, &mut app, &mut painter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
461 }
462 }
463 BottomEventrust-analyzer cargo bottom 0.14.9 event/BottomEvent#::Cleanrust-analyzer cargo bottom 0.14.9 event/BottomEvent#Clean# => {
464 app.data_storerust-analyzer cargo bottom 0.14.9 app/App#data_store.
465 .clean_datarust-analyzer cargo bottom 0.14.9 app/data/store/impl#[DataStore]clean_data().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(app.app_config_fieldsrust-analyzer cargo bottom 0.14.9 app/App#app_config_fields..retention_msrust-analyzer cargo bottom 0.14.9 app/AppConfigFields#retention_ms.));
466 }
467 }
468 }
469 }
470
471 // I think doing it in this order is safe...
472 // TODO: maybe move the cancellation token to the ctrl-c handler?
473 cancellation_token.cancelrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]cancel().();
474 cleanup_terminalrust-analyzer cargo bottom 0.14.9 cleanup_terminal().(&mut terminal)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
475
476 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
477 }