|
1 | | -use std::{collections::VecDeque, io}; |
| 1 | +use std::io; |
2 | 2 |
|
3 | 3 | use clap::Parser; |
4 | | -use tokio::{ |
5 | | - sync::mpsc, |
6 | | - time::{timeout, Duration}, |
7 | | -}; |
| 4 | +use tokio::time::Duration; |
8 | 5 |
|
9 | 6 | use promkit_core::crossterm::{ |
10 | 7 | self, cursor, |
@@ -47,10 +44,8 @@ $ stern --context kind-kind etcd |& sig |
47 | 44 | Or the method to retry command by pressing ctrl+r: |
48 | 45 | $ sig --cmd \"stern --context kind-kind etcd\" |
49 | 46 |
|
50 | | -Archived mode: |
51 | | -$ cat README.md |& sig -a |
52 | | -Or |
53 | | -$ sig -a --cmd \"cat README.md\" |
| 47 | +Static input (switches to archived view after EOF): |
| 48 | +$ cat README.md |& sig |
54 | 49 |
|
55 | 50 | Options: |
56 | 51 | {options} |
@@ -86,14 +81,6 @@ pub struct Args { |
86 | 81 | )] |
87 | 82 | pub queue_capacity: usize, |
88 | 83 |
|
89 | | - #[arg( |
90 | | - short = 'a', |
91 | | - long = "archived", |
92 | | - default_value = "false", |
93 | | - help = "Archived mode to grep through static data." |
94 | | - )] |
95 | | - pub archived: bool, |
96 | | - |
97 | 84 | #[arg( |
98 | 85 | short = 'i', |
99 | 86 | long = "ignore-case", |
@@ -139,149 +126,78 @@ async fn main() -> anyhow::Result<()> { |
139 | 126 | ..Default::default() |
140 | 127 | }; |
141 | 128 |
|
142 | | - if args.archived { |
143 | | - let (tx, mut rx) = mpsc::channel(1); |
144 | | - |
145 | | - let input_task = match &args.cmd { |
146 | | - Some(cmd) => spawn::spawn_cmd_result_sender( |
147 | | - cmd, |
148 | | - tx, |
149 | | - Duration::from_millis(args.retrieval_timeout_millis), |
150 | | - ), |
151 | | - None => { |
152 | | - spawn::spawn_stdin_sender(tx, Duration::from_millis(args.retrieval_timeout_millis)) |
153 | | - } |
154 | | - }?; |
155 | | - |
156 | | - let mut queue = VecDeque::with_capacity(args.queue_capacity); |
157 | | - loop { |
158 | | - match timeout( |
159 | | - Duration::from_millis(args.retrieval_timeout_millis), |
160 | | - rx.recv(), |
161 | | - ) |
162 | | - .await |
163 | | - { |
164 | | - Ok(Some(line)) => { |
165 | | - if queue.len() > args.queue_capacity { |
166 | | - queue.pop_front().unwrap(); |
167 | | - } |
168 | | - queue.push_back(line.clone()); |
169 | | - } |
170 | | - Ok(None) => break, |
171 | | - Err(_) => break, |
172 | | - } |
173 | | - } |
174 | | - |
175 | | - // Stop the input task |
176 | | - input_task.handle.abort(); |
177 | | - |
| 129 | + while let Ok((signal, queue)) = sig::run( |
| 130 | + text_editor::State { |
| 131 | + texteditor: TextEditor::new(args.query.clone().unwrap_or_default()), |
| 132 | + prefix: String::from("❯❯ "), |
| 133 | + prefix_style: ContentStyle { |
| 134 | + foreground_color: Some(Color::DarkGreen), |
| 135 | + ..Default::default() |
| 136 | + }, |
| 137 | + active_char_style: ContentStyle { |
| 138 | + background_color: Some(Color::DarkCyan), |
| 139 | + ..Default::default() |
| 140 | + }, |
| 141 | + ..Default::default() |
| 142 | + }, |
| 143 | + highlight_style, |
| 144 | + Duration::from_millis(args.retrieval_timeout_millis), |
| 145 | + args.render_interval_millis.map(Duration::from_millis), |
| 146 | + args.queue_capacity, |
| 147 | + args.case_insensitive, |
| 148 | + args.cmd.clone(), |
| 149 | + ) |
| 150 | + .await |
| 151 | + { |
178 | 152 | crossterm::execute!( |
179 | 153 | io::stdout(), |
180 | 154 | crossterm::terminal::Clear(crossterm::terminal::ClearType::All), |
181 | 155 | cursor::MoveTo(0, 0), |
182 | 156 | )?; |
183 | 157 |
|
184 | | - archived::run( |
185 | | - text_editor::State { |
186 | | - texteditor: TextEditor::new(args.query.clone().unwrap_or_default()), |
187 | | - prefix: String::from("❯❯❯ "), |
188 | | - prefix_style: ContentStyle { |
189 | | - foreground_color: Some(Color::DarkBlue), |
190 | | - ..Default::default() |
191 | | - }, |
192 | | - active_char_style: ContentStyle { |
193 | | - background_color: Some(Color::DarkCyan), |
194 | | - ..Default::default() |
195 | | - }, |
196 | | - ..Default::default() |
197 | | - }, |
198 | | - listbox::State { |
199 | | - listbox: listbox::Listbox::from_displayable(queue), |
200 | | - cursor: String::from("❯ "), |
201 | | - active_item_style: None, |
202 | | - inactive_item_style: None, |
203 | | - lines: Default::default(), |
204 | | - }, |
205 | | - highlight_style, |
206 | | - args.case_insensitive, |
207 | | - // In archived mode, command for retry is meaningless. |
208 | | - None, |
209 | | - ) |
210 | | - .await?; |
211 | | - } else { |
212 | | - while let Ok((signal, queue)) = sig::run( |
213 | | - text_editor::State { |
214 | | - texteditor: TextEditor::new(args.query.clone().unwrap_or_default()), |
215 | | - prefix: String::from("❯❯ "), |
216 | | - prefix_style: ContentStyle { |
217 | | - foreground_color: Some(Color::DarkGreen), |
218 | | - ..Default::default() |
219 | | - }, |
220 | | - active_char_style: ContentStyle { |
221 | | - background_color: Some(Color::DarkCyan), |
222 | | - ..Default::default() |
223 | | - }, |
224 | | - ..Default::default() |
225 | | - }, |
226 | | - highlight_style, |
227 | | - Duration::from_millis(args.retrieval_timeout_millis), |
228 | | - args.render_interval_millis.map(Duration::from_millis), |
229 | | - args.queue_capacity, |
230 | | - args.case_insensitive, |
231 | | - args.cmd.clone(), |
232 | | - ) |
233 | | - .await |
234 | | - { |
235 | | - crossterm::execute!( |
236 | | - io::stdout(), |
237 | | - crossterm::terminal::Clear(crossterm::terminal::ClearType::All), |
238 | | - cursor::MoveTo(0, 0), |
239 | | - )?; |
240 | | - |
241 | | - match signal { |
242 | | - Signal::GotoArchived => { |
243 | | - archived::run( |
244 | | - text_editor::State { |
245 | | - prefix: String::from("❯❯❯ "), |
246 | | - prefix_style: ContentStyle { |
247 | | - foreground_color: Some(Color::DarkBlue), |
248 | | - ..Default::default() |
249 | | - }, |
250 | | - active_char_style: ContentStyle { |
251 | | - background_color: Some(Color::DarkCyan), |
252 | | - ..Default::default() |
253 | | - }, |
| 158 | + match signal { |
| 159 | + Signal::GotoArchived => { |
| 160 | + archived::run( |
| 161 | + text_editor::State { |
| 162 | + prefix: String::from("❯❯❯ "), |
| 163 | + prefix_style: ContentStyle { |
| 164 | + foreground_color: Some(Color::DarkBlue), |
254 | 165 | ..Default::default() |
255 | 166 | }, |
256 | | - listbox::State { |
257 | | - listbox: listbox::Listbox::from_displayable(queue), |
258 | | - cursor: String::from("❯ "), |
259 | | - active_item_style: None, |
260 | | - inactive_item_style: None, |
261 | | - lines: Default::default(), |
| 167 | + active_char_style: ContentStyle { |
| 168 | + background_color: Some(Color::DarkCyan), |
| 169 | + ..Default::default() |
262 | 170 | }, |
263 | | - highlight_style, |
264 | | - args.case_insensitive, |
265 | | - args.cmd.clone(), |
266 | | - ) |
267 | | - .await?; |
268 | | - |
269 | | - // Re-enable raw mode and hide the cursor again here |
270 | | - // because they are disabled and shown, respectively, by promkit. |
271 | | - enable_raw_mode()?; |
272 | | - execute!(io::stdout(), EnableMouseCapture, cursor::Hide)?; |
273 | | - |
274 | | - crossterm::execute!( |
275 | | - io::stdout(), |
276 | | - crossterm::terminal::Clear(crossterm::terminal::ClearType::All), |
277 | | - cursor::MoveTo(0, 0), |
278 | | - )?; |
279 | | - } |
280 | | - Signal::GotoStreaming => { |
281 | | - continue; |
282 | | - } |
283 | | - _ => {} |
| 171 | + ..Default::default() |
| 172 | + }, |
| 173 | + listbox::State { |
| 174 | + listbox: listbox::Listbox::from_displayable(queue), |
| 175 | + cursor: String::from("❯ "), |
| 176 | + active_item_style: None, |
| 177 | + inactive_item_style: None, |
| 178 | + lines: Default::default(), |
| 179 | + }, |
| 180 | + highlight_style, |
| 181 | + args.case_insensitive, |
| 182 | + args.cmd.clone(), |
| 183 | + ) |
| 184 | + .await?; |
| 185 | + |
| 186 | + // Re-enable raw mode and hide the cursor again here |
| 187 | + // because they are disabled and shown, respectively, by promkit. |
| 188 | + enable_raw_mode()?; |
| 189 | + execute!(io::stdout(), EnableMouseCapture, cursor::Hide)?; |
| 190 | + |
| 191 | + crossterm::execute!( |
| 192 | + io::stdout(), |
| 193 | + crossterm::terminal::Clear(crossterm::terminal::ClearType::All), |
| 194 | + cursor::MoveTo(0, 0), |
| 195 | + )?; |
| 196 | + } |
| 197 | + Signal::GotoStreaming => { |
| 198 | + continue; |
284 | 199 | } |
| 200 | + _ => {} |
285 | 201 | } |
286 | 202 | } |
287 | 203 |
|
|
0 commit comments