sdoc/commands/
edit.rs

1use workflow::Work;
2use workflow::Instruction::{Display, SystemCommand, ExitCode};
3use model::Value;
4use dto::Request;
5use dto::Response::{Ok, Err};
6use config::Context;
7
8pub fn execute(request: Request, context: &Context) -> Work {
9    Work::instruction(
10        request.next()
11            .current
12            .and_then(|rc| context.find(&rc, true))
13            .map(|command| {
14                if let Some(ref a) = command.value {
15                    if let Value::Script(ref b) = *a {
16                        return SystemCommand(format!("$EDITOR {}/{}", context.directory.display(), b), true);
17                    }
18                }
19                ExitCode(Err(1))
20            })
21            .unwrap_or_else(|| ExitCode(Err(1))))
22}
23
24pub fn auto_complete(request: Request, context: &Context) -> Work {
25    Work::instruction(
26        request.next()
27            .current
28            .and_then(|rc| context.find(&rc, false))
29            .map(|_| ExitCode(Ok))
30            .unwrap_or_else(|| {
31                let s = format!("{}", context.get_commands().iter()
32                    .filter(|c| {
33                        if let Some(ref a) = c.value {
34                            if let Value::Script(_) = *a {
35                                return true;
36                            }
37                        }
38                        false
39                    })
40                    .fold(s!(), |a, b| format!("{}{}\n", a, &b.name)));
41
42                Display(s, Ok)
43            }))
44}