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
use std::io;
use futures::StreamExt;
use promkit_widgets::{
core::crossterm::{
event::{
DisableMouseCapture, EnableMouseCapture, Event, EventStream, MouseEvent, MouseEventKind,
},
execute, terminal,
},
spinner::State,
};
use tokio::{sync::mpsc, task::JoinHandle};
use crate::{
completion::CompletionAction,
config::Keybinds,
context::{Index, SharedContext},
guide::{GuideAction, GuideMessage},
json_viewer,
query_editor::QueryEditorAction,
};
/// Actions that can be triggered by terminal events,
/// which are dispatched to the appropriate components.
enum Action {
Resize(u16, u16),
Exit,
CopyQuery,
CopyResult,
/// Switch between query-editor/completion and JSON viewer.
SwitchMode,
}
/// Spawn a background task to listen for terminal events and dispatch corresponding actions
/// to the appropriate components (query editor, completion navigator, JSON viewer, guide).
pub fn spawn_terminal_event_dispatch_task(
ctx: SharedContext,
keybinds: Keybinds,
debounce_resize_tx: mpsc::Sender<(u16, u16)>,
editor_action_tx: mpsc::Sender<QueryEditorAction>,
completion_action_tx: mpsc::Sender<CompletionAction>,
json_viewer_action_tx: mpsc::Sender<json_viewer::ViewerAction>,
guide_action_tx: mpsc::Sender<GuideAction>,
) -> JoinHandle<anyhow::Result<()>> {
let mut stream = EventStream::new();
tokio::spawn(async move {
'main: loop {
tokio::select! {
Some(Ok(event)) = stream.next() => {
// Note: `HashSet<Event>::contains` compares full mouse events (including `column`/`row`),
// so wheel events are normalized to `(0, 0)` to match configured `ScrollUp`/`ScrollDown` bindings.
let event = match event {
Event::Mouse(mouse)
if matches!(
mouse.kind,
MouseEventKind::ScrollUp | MouseEventKind::ScrollDown
) =>
{
Event::Mouse(MouseEvent {
kind: mouse.kind,
column: 0,
row: 0,
modifiers: mouse.modifiers,
})
}
other => other,
};
guide_action_tx.send(GuideAction::Clear).await?;
let action = if let Event::Resize(width, height) = event {
Some(Action::Resize(width, height))
} else if keybinds.exit.contains(&event) {
Some(Action::Exit)
} else if keybinds.copy_query.contains(&event) {
Some(Action::CopyQuery)
} else if keybinds.copy_result.contains(&event) {
Some(Action::CopyResult)
} else if keybinds.switch_mode.contains(&event) {
Some(Action::SwitchMode)
} else {
None
};
if let Some(action) = action {
match action {
Action::Resize(width, height) => {
debounce_resize_tx.send((width, height)).await?;
}
Action::Exit => break 'main,
Action::CopyQuery => {
editor_action_tx.send(QueryEditorAction::CopyQuery).await?;
}
Action::CopyResult => {
if ctx.is_idle().await {
json_viewer_action_tx
.send(json_viewer::ViewerAction::CopyResult)
.await?;
} else {
guide_action_tx
.send(GuideAction::Show(
GuideMessage::FailedToCopyWhileRenderingInProgress,
))
.await?;
}
}
Action::SwitchMode => match ctx.active_index().await {
Index::QueryEditor | Index::Completion => {
if ctx.is_idle().await {
ctx.set_active_index(Index::JsonViewer).await;
completion_action_tx.send(CompletionAction::Leave).await?;
editor_action_tx.send(QueryEditorAction::Leave).await?;
execute!(
io::stdout(),
terminal::EnterAlternateScreen,
EnableMouseCapture,
)?;
} else {
guide_action_tx
.send(GuideAction::Show(
GuideMessage::FailedToSwitchModeWhileRenderingInProgress,
))
.await?;
}
}
Index::JsonViewer => {
ctx.set_active_index(Index::QueryEditor).await;
editor_action_tx.send(QueryEditorAction::Enter).await?;
execute!(
io::stdout(),
terminal::LeaveAlternateScreen,
DisableMouseCapture,
)?;
}
Index::Guide => {}
},
}
continue;
}
match ctx.active_index().await {
Index::QueryEditor => {
editor_action_tx
.send(QueryEditorAction::UserEvent(event))
.await?;
}
Index::Completion => {
completion_action_tx
.send(CompletionAction::UserEvent(event))
.await?;
}
Index::JsonViewer => {
json_viewer_action_tx
.send(json_viewer::ViewerAction::UserEvent(event))
.await?;
}
Index::Guide => {}
}
},
else => {
break 'main;
}
}
}
Ok(())
})
}