-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathnavigation.rs
More file actions
630 lines (555 loc) · 25.4 KB
/
navigation.rs
File metadata and controls
630 lines (555 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use crate::utils::fuzzy_actions;
use crate::{
common::parse_datetime_or_date,
interpreter::{ContextArc, WorkerEvent, clickhouse::TraceType, options::ChDigViews},
view,
};
use anyhow::Result;
use chrono::{DateTime, Local};
use cursive::{
Cursive,
event::{Event, EventResult, Key},
theme::{BaseColor, Color, ColorStyle, Effect, PaletteColor, Style, Theme},
utils::{markup::StyledString, span::SpannedString},
view::{IntoBoxedView, Nameable, Resizable, View},
views::{Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView},
};
use cursive_flexi_logger_view::toggle_flexi_logger_debug_console;
fn make_menu_text() -> StyledString {
let mut text = StyledString::new();
// F1
text.append_plain("F1");
text.append_styled("Help", ColorStyle::highlight());
// F2
text.append_plain("F2");
text.append_styled("Views", ColorStyle::highlight());
// F8
text.append_plain("F8");
text.append_styled("Actions", ColorStyle::highlight());
return text;
}
pub trait Navigation {
fn has_view(&mut self, name: &str) -> bool;
fn make_theme_from_therminal(&mut self) -> Theme;
fn pop_ui(&mut self, exit: bool);
fn toggle_pause_updates(&mut self, reason: Option<&str>);
fn refresh_view(&mut self);
fn seek_time_frame(&mut self, is_sub: bool);
fn select_time_frame(&mut self);
fn initialize_global_shortcuts(&mut self, context: ContextArc);
fn initialize_views_menu(&mut self, context: ContextArc);
fn chdig(&mut self, context: ContextArc);
fn show_help_dialog(&mut self);
fn show_views(&mut self);
fn show_actions(&mut self);
fn show_fuzzy_actions(&mut self);
fn show_server_flamegraph(&mut self, tui: bool, trace_type: Option<TraceType>);
fn drop_main_view(&mut self);
fn set_main_view<V: IntoBoxedView + 'static>(&mut self, view: V);
fn set_statusbar_version(&mut self, main_content: impl Into<SpannedString<Style>>);
fn set_statusbar_content(&mut self, content: impl Into<SpannedString<Style>>);
// TODO: move into separate trait
fn call_on_name_or_render_error<V, F>(&mut self, name: &str, callback: F)
where
V: View,
F: FnOnce(&mut V) -> Result<()>;
}
impl Navigation for Cursive {
fn has_view(&mut self, name: &str) -> bool {
return self.focus_name(name).is_ok();
}
fn make_theme_from_therminal(&mut self) -> Theme {
let mut theme = self.current_theme().clone();
theme.palette[PaletteColor::Background] = Color::TerminalDefault;
theme.palette[PaletteColor::View] = Color::TerminalDefault;
theme.palette[PaletteColor::Primary] = Color::TerminalDefault;
theme.palette[PaletteColor::Highlight] = Color::Light(BaseColor::Cyan);
theme.palette[PaletteColor::HighlightText] = Color::Dark(BaseColor::Black);
theme.shadow = false;
return theme;
}
fn pop_ui(&mut self, exit: bool) {
// Close left menu
let mut has_left_menu = false;
self.call_on_name("left_menu", |left_menu_view: &mut LinearLayout| {
if !left_menu_view.is_empty() {
left_menu_view
.remove_child(left_menu_view.len() - 1)
.expect("No child view to remove");
has_left_menu = true;
}
});
// Once at a time
if has_left_menu {
self.focus_name("main").unwrap();
return;
}
if self.screen_mut().len() == 1 {
if exit {
self.quit();
}
} else {
self.pop_layer();
}
}
fn toggle_pause_updates(&mut self, reason: Option<&str>) {
let is_paused;
{
let mut context = self.user_data::<ContextArc>().unwrap().lock().unwrap();
// NOTE: though it will be better to stop sending any message completely, instead of
// simply ignoring them
context.worker.toggle_pause();
is_paused = context.worker.is_paused();
}
self.call_on_name("is_paused", |v: &mut TextView| {
let mut text = StyledString::new();
if is_paused {
text.append_styled(" PAUSED", Effect::Bold);
if let Some(reason) = reason {
text.append_styled(format!(" ({})", reason), Effect::Bold);
}
text.append_styled(" press P to resume", Effect::Italic);
}
v.set_content(text);
});
}
fn refresh_view(&mut self) {
let context = self.user_data::<ContextArc>().unwrap().lock().unwrap();
log::trace!("Toggle refresh");
context.trigger_view_refresh();
}
fn seek_time_frame(&mut self, is_sub: bool) {
let mut context = self.user_data::<ContextArc>().unwrap().lock().unwrap();
context.shift_time_interval(is_sub, 10);
context.trigger_view_refresh();
}
fn select_time_frame(&mut self) {
let on_submit = move |siv: &mut Cursive| {
let start = siv
.call_on_name("start", |view: &mut EditView| view.get_content())
.unwrap();
let end = siv
.call_on_name("end", |view: &mut EditView| view.get_content())
.unwrap();
siv.pop_layer();
let new_begin = match parse_datetime_or_date(&start) {
Ok(new) => new,
Err(err) => {
siv.add_layer(Dialog::info(err));
return;
}
};
let new_end = match parse_datetime_or_date(&end) {
Ok(new) => new,
Err(err) => {
siv.add_layer(Dialog::info(err));
return;
}
};
log::debug!("Set time frame to ({}, {})", new_begin, new_end);
let mut context = siv.user_data::<ContextArc>().unwrap().lock().unwrap();
context.options.view.start = new_begin.into();
context.options.view.end = new_end.into();
context.trigger_view_refresh();
};
let view = OnEventView::new(
Dialog::new()
.title("Set the time interval")
.content(
LinearLayout::vertical()
.child(TextView::new("format: YYYY-MM-DD hh:mm:ss"))
.child(DummyView)
.child(TextView::new("start:"))
.child(EditView::new().with_name("start"))
.child(DummyView)
.child(TextView::new("end:"))
.child(EditView::new().with_name("end")),
)
.button("Submit", on_submit),
);
self.add_layer(view);
}
fn chdig(&mut self, context: ContextArc) {
self.set_user_data(context.clone());
self.initialize_global_shortcuts(context.clone());
self.initialize_views_menu(context.clone());
let theme = self.make_theme_from_therminal();
self.set_theme(theme);
self.add_fullscreen_layer(
LinearLayout::horizontal()
.child(LinearLayout::vertical().with_name("left_menu"))
.child(
LinearLayout::vertical()
.child(
LinearLayout::horizontal()
.child(TextView::new(make_menu_text()))
.child(TextView::new("").with_name("is_paused"))
// Align status to the right
.child(DummyView.full_width())
.child(TextView::new("").with_name("status"))
.child(DummyView.fixed_width(1))
.child(TextView::new("").with_name("version")),
)
.child(view::SummaryView::new(context.clone()).with_name("summary"))
.with_name("main"),
),
);
self.set_statusbar_version(context.lock().unwrap().server_version.clone());
let start_view = context
.lock()
.unwrap()
.options
.start_view
.unwrap_or(ChDigViews::Queries);
let provider = context
.lock()
.unwrap()
.view_registry
.get_by_view_type(start_view);
provider.show(self, context.clone());
}
/// Ignore rustfmt max_width, otherwise callback actions looks ugly
#[rustfmt::skip]
fn initialize_global_shortcuts(&mut self, context: ContextArc) {
let mut context = context.lock().unwrap();
context.add_global_action(self, "Show help", Key::F1, |siv| siv.show_help_dialog());
context.add_global_action(self, "Views", Key::F2, |siv| siv.show_views());
context.add_global_action(self, "Show actions", Key::F8, |siv| siv.show_actions());
context.add_global_action(self, "Fuzzy actions", Event::CtrlChar('p'), |siv| siv.show_fuzzy_actions());
context.add_global_action(self, "CPU Server Flamegraph", 'F', |siv| siv.show_server_flamegraph(true, Some(TraceType::CPU)));
context.add_global_action_without_shortcut(self, "Real Server Flamegraph", |siv| siv.show_server_flamegraph(true, Some(TraceType::Real)));
context.add_global_action_without_shortcut(self, "Memory Server Flamegraph", |siv| siv.show_server_flamegraph(true, Some(TraceType::Memory)));
context.add_global_action_without_shortcut(self, "Memory Sample Server Flamegraph", |siv| siv.show_server_flamegraph(true, Some(TraceType::MemorySample)));
context.add_global_action_without_shortcut(self, "Jemalloc Sample Server Flamegraph", |siv| siv.show_server_flamegraph(true, Some(TraceType::JemallocSample)));
context.add_global_action_without_shortcut(self, "MemoryAllocatedWithoutCheck Server Flamegraph", |siv| siv.show_server_flamegraph(true, Some(TraceType::MemoryAllocatedWithoutCheck)));
context.add_global_action_without_shortcut(self, "Events Server Flamegraph", |siv| siv.show_server_flamegraph(true, Some(TraceType::ProfileEvents)));
context.add_global_action_without_shortcut(self, "Live Server Flamegraph", |siv| siv.show_server_flamegraph(true, None));
context.add_global_action_without_shortcut(self, "CPU Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, Some(TraceType::CPU)));
context.add_global_action_without_shortcut(self, "Real Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, Some(TraceType::Real)));
context.add_global_action_without_shortcut(self, "Memory Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, Some(TraceType::Memory)));
context.add_global_action_without_shortcut(self, "Memory Sample Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, Some(TraceType::MemorySample)));
context.add_global_action_without_shortcut(self, "MemoryAllocatedWithoutCheck Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, Some(TraceType::MemoryAllocatedWithoutCheck)));
context.add_global_action_without_shortcut(self, "Events Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, Some(TraceType::ProfileEvents)));
context.add_global_action_without_shortcut(self, "Live Server Flamegraph in speedscope", |siv| siv.show_server_flamegraph(false, None));
// If logging is done to file, console is always empty
if context.options.service.log.is_none() {
context.add_global_action(
self,
"chdig debug console",
'~',
toggle_flexi_logger_debug_console,
);
}
context.add_global_action(self, "Back/Quit", Key::Esc, |siv| siv.pop_ui(false));
context.add_global_action(self, "Back/Quit", 'q', |siv| siv.pop_ui(true));
context.add_global_action(self, "Quit forcefully", 'Q', |siv| siv.quit());
context.add_global_action(self, "Back", Key::Backspace, |siv| siv.pop_ui(false));
context.add_global_action(self, "Toggle pause", 'p', |siv| siv.toggle_pause_updates(None));
context.add_global_action(self, "Refresh", 'r', |siv| siv.refresh_view());
// Bindings T/t inspiried by atop(1) (so as this functionality)
context.add_global_action(self, "Seek 10 mins backward", 'T', |siv| siv.seek_time_frame(true));
context.add_global_action(self, "Seek 10 mins forward", 't', |siv| siv.seek_time_frame(false));
context.add_global_action(self, "Set time interval", Event::AltChar('t'), |siv| siv.select_time_frame());
}
fn initialize_views_menu(&mut self, context: ContextArc) {
use crate::view::providers::*;
use std::sync::Arc;
let mut c = context.lock().unwrap();
c.register_provider(Arc::new(ProcessesViewProvider));
c.register_provider(Arc::new(SlowQueryLogViewProvider));
c.register_provider(Arc::new(LastQueryLogViewProvider));
c.register_provider(Arc::new(MergesViewProvider));
c.register_provider(Arc::new(S3QueueViewProvider));
c.register_provider(Arc::new(MutationsViewProvider));
c.register_provider(Arc::new(ReplicatedFetchesViewProvider));
c.register_provider(Arc::new(ReplicationQueueViewProvider));
c.register_provider(Arc::new(ReplicasViewProvider));
c.register_provider(Arc::new(TablesViewProvider));
c.register_provider(Arc::new(BackgroundSchedulePoolViewProvider));
c.register_provider(Arc::new(BackupsViewProvider));
c.register_provider(Arc::new(DictionariesViewProvider));
c.register_provider(Arc::new(ServerLogsViewProvider));
c.register_provider(Arc::new(LoggerNamesViewProvider));
c.register_provider(Arc::new(ErrorsViewProvider));
c.register_provider(Arc::new(ClientViewProvider));
}
fn show_help_dialog(&mut self) {
if self.has_view("help") {
self.pop_layer();
return;
}
let mut text = StyledString::default();
text.append_styled(
format!("chdig v{version}\n", version = env!("CARGO_PKG_VERSION")),
Effect::Bold,
);
{
let context = self.user_data::<ContextArc>().unwrap().lock().unwrap();
text.append_styled("\nGlobal shortcuts:\n\n", Effect::Bold);
for shortcut in context.global_actions.iter() {
text.append(shortcut.description.preview_styled());
}
text.append_styled("\nActions:\n\n", Effect::Bold);
for shortcut in context.view_actions.iter() {
text.append(shortcut.description.preview_styled());
}
}
text.append_styled("\nExtended navigation:\n\n", Effect::Bold);
text.append_styled(
format!("{:>10} - reset selection/follow item in table\n", "Home"),
Effect::Bold,
);
text.append_plain(format!(
"\nIssues and suggestions: {homepage}/issues",
homepage = env!("CARGO_PKG_HOMEPAGE")
));
self.add_layer(Dialog::info(text).with_name("help"));
}
fn show_views(&mut self) {
let mut has_views = false;
let context = self.user_data::<ContextArc>().unwrap().clone();
self.call_on_name("left_menu", |left_menu_view: &mut LinearLayout| {
if !left_menu_view.is_empty() {
left_menu_view
.remove_child(left_menu_view.len() - 1)
.expect("No child view to remove");
} else {
let mut select = SelectView::new().autojump();
{
let context = context.clone();
select.set_on_submit(move |siv, selected_action: &str| {
log::trace!("Switching to {:?}", selected_action);
siv.focus_name("main").unwrap();
{
let action_callback = context
.lock()
.unwrap()
.views_menu_actions
.iter()
.find(|x| x.description.text == selected_action)
.unwrap()
.callback
.clone();
action_callback.as_ref()(siv);
};
siv.call_on_name("left_menu", |left_menu_view: &mut LinearLayout| {
left_menu_view
.remove_child(left_menu_view.len() - 1)
.expect("No child view to remove");
});
});
}
{
let context = context.clone();
let context = context.lock().unwrap();
for action in context.views_menu_actions.iter() {
select.add_item_str(action.description.text);
}
}
let select = OnEventView::new(select)
.on_pre_event_inner('k', |s, _| {
let cb = s.select_up(1);
Some(EventResult::Consumed(Some(cb)))
})
.on_pre_event_inner('j', |s, _| {
let cb = s.select_down(1);
Some(EventResult::Consumed(Some(cb)))
})
.with_name("actions_select");
left_menu_view.add_child(select);
has_views = true;
}
});
if has_views {
self.focus_name("left_menu").unwrap();
} else {
self.focus_name("main").unwrap();
}
}
fn show_actions(&mut self) {
let mut has_actions = false;
let context = self.user_data::<ContextArc>().unwrap().clone();
self.call_on_name("left_menu", |left_menu_view: &mut LinearLayout| {
if !left_menu_view.is_empty() {
left_menu_view
.remove_child(left_menu_view.len() - 1)
.expect("No child view to remove");
} else {
let mut select = SelectView::new().autojump();
{
let context = context.clone();
select.set_on_submit(move |siv, selected_action: &str| {
log::trace!("Triggering {:?} (from actions)", selected_action);
siv.focus_name("main").unwrap();
{
let mut context = context.lock().unwrap();
let action_callback = context
.view_actions
.iter()
.find(|x| x.description.text == selected_action)
.unwrap()
.callback
.clone();
context.pending_view_callback = Some(action_callback);
};
siv.on_event(Event::Refresh);
siv.call_on_name("left_menu", |left_menu_view: &mut LinearLayout| {
left_menu_view
.remove_child(left_menu_view.len() - 1)
.expect("No child view to remove");
});
});
}
{
let context = context.clone();
let context = context.lock().unwrap();
for action in context.view_actions.iter() {
select.add_item_str(action.description.text);
}
if context.view_actions.is_empty() {
return;
}
}
let select = OnEventView::new(select)
.on_pre_event_inner('k', |s, _| {
let cb = s.select_up(1);
Some(EventResult::Consumed(Some(cb)))
})
.on_pre_event_inner('j', |s, _| {
let cb = s.select_down(1);
Some(EventResult::Consumed(Some(cb)))
})
.with_name("actions_select");
left_menu_view.add_child(select);
has_actions = true;
}
});
if has_actions {
self.focus_name("left_menu").unwrap();
} else {
self.focus_name("main").unwrap();
}
}
fn show_fuzzy_actions(&mut self) {
let context = self.user_data::<ContextArc>().unwrap().clone();
let all_actions = {
let context = context.lock().unwrap();
context
.global_actions
.iter()
.map(|x| &x.description)
.chain(context.view_actions.iter().map(|x| &x.description))
.chain(context.views_menu_actions.iter().map(|x| &x.description))
.cloned()
.collect()
};
fuzzy_actions(self, all_actions, move |siv, action_text| {
log::trace!("Triggering {:?} (from fuzzy search)", action_text);
// Global callbacks
{
let action_callback = context
.lock()
.unwrap()
.global_actions
.iter()
.find(|x| x.description.text == action_text)
.map(|a| a.callback.clone());
if let Some(action_callback) = action_callback {
action_callback.as_ref()(siv);
}
}
// View callbacks
{
let mut context = context.lock().unwrap();
if let Some(action) = context
.view_actions
.iter()
.find(|x| x.description.text == action_text)
{
context.pending_view_callback = Some(action.callback.clone());
// The pending_view_callback handling is binded to Event::Refresh event, but it
// cannot be called with the context locked, so it will be called
// asynchronously after Event::Refresh below
//
// But, we also need it to cleanup the screen (to avoid any leftovers), so, it
// will be called always.
}
}
// View menus
{
let action_callback = context
.lock()
.unwrap()
.views_menu_actions
.iter()
.find(|x| x.description.text == action_text)
.map(|a| a.callback.clone());
if let Some(action_callback) = action_callback {
action_callback.as_ref()(siv);
}
}
siv.on_event(Event::Refresh);
});
}
fn show_server_flamegraph(&mut self, tui: bool, trace_type: Option<TraceType>) {
let mut context = self.user_data::<ContextArc>().unwrap().lock().unwrap();
let start: DateTime<Local> = context.options.view.start.clone().into();
let end: DateTime<Local> = context.options.view.end.clone().into();
if let Some(trace_type) = trace_type {
context.worker.send(
true,
WorkerEvent::ServerFlameGraph(tui, trace_type, start, end),
);
} else {
context
.worker
.send(true, WorkerEvent::LiveQueryFlameGraph(tui, None));
}
}
fn drop_main_view(&mut self) {
while self.screen_mut().len() > 1 {
self.pop_layer();
}
self.call_on_name("main", |main_view: &mut LinearLayout| {
// Views that should not be touched:
// - top bar (menu text + is_paused + status)
// - summary
if main_view.len() > 2 {
main_view
.remove_child(main_view.len() - 1)
.expect("No child view to remove");
}
});
}
fn set_main_view<V: IntoBoxedView + 'static>(&mut self, view: V) {
self.call_on_name("main", |main_view: &mut LinearLayout| {
main_view.add_child(view);
});
}
fn set_statusbar_version(&mut self, main_content: impl Into<SpannedString<Style>>) {
self.call_on_name("version", |text_view: &mut TextView| {
let content: SpannedString<Style> = main_content.into();
let mut styled = StyledString::new();
// NOTE: may not work in some terminals
styled.append_styled(content.source(), Effect::Dim);
text_view.set_content(styled);
})
.expect("version");
}
fn set_statusbar_content(&mut self, content: impl Into<SpannedString<Style>>) {
self.call_on_name("status", |text_view: &mut TextView| {
text_view.set_content(content);
})
.expect("set_status")
}
fn call_on_name_or_render_error<V, F>(&mut self, name: &str, callback: F)
where
V: View,
F: FnOnce(&mut V) -> Result<()>,
{
let ret = self.call_on_name(name, callback);
if let Some(Err(err)) = ret {
self.add_layer(Dialog::info(err.to_string()));
}
}
}