quartz_cli/action/
last.rs1use std::convert::Infallible;
2
3use crate::{
4 cli::LastCmd as Cmd,
5 cli::LastResCmd as ResCmd,
6 history::{self, History},
7 Ctx, QuartzResult,
8};
9
10pub fn cmd(ctx: &Ctx, maybe_command: Option<Cmd>) -> QuartzResult<(), Infallible> {
11 let entry = History::last(ctx).expect("no history found");
12
13 if maybe_command.is_none() {
14 println!("{entry}");
15 return Ok(());
16 }
17
18 if let Some(command) = maybe_command {
19 match command {
20 Cmd::Handle => println!("{}", entry.handle()),
21 Cmd::Req => req(&entry),
22 Cmd::Res { command } => res(command, &entry),
23 }
24 };
25
26 Ok(())
27}
28
29pub fn req(entry: &history::Entry) {
30 req_head(entry);
31}
32
33pub fn req_head(entry: &history::Entry) {
34 let iter = entry
35 .messages()
36 .iter()
37 .filter_map(|p| match p.starts_with('>') {
38 true => Some(
39 p.split('\n')
40 .map(|s| s.trim_start_matches('>').trim())
41 .collect::<Vec<&str>>()
42 .join("\n"),
43 ),
44 false => None,
45 });
46
47 for m in iter {
48 println!("{m}");
49 }
50}
51
52pub fn res(command: Option<ResCmd>, entry: &history::Entry) {
53 if let Some(command) = command {
54 match command {
55 ResCmd::Head => res_head(entry),
56 ResCmd::Body => res_body(entry),
57 }
58 } else {
59 res_head(entry);
60 res_body(entry);
61 }
62}
63
64pub fn res_head(entry: &history::Entry) {
65 let iter = entry
66 .messages()
67 .iter()
68 .filter_map(|p| match p.starts_with('<') {
69 true => Some(
70 p.split('\n')
71 .map(|s| s.trim_start_matches('<').trim())
72 .collect::<Vec<&str>>()
73 .join("\n"),
74 ),
75 false => None,
76 });
77
78 for m in iter {
79 println!("{m}");
80 }
81}
82
83pub fn res_body(entry: &history::Entry) {
84 if let Some(body) = entry.messages().last() {
85 println!("{}", body);
86 }
87}