summaryrefslogtreecommitdiffstats
path: root/ui/src/execute.rs
blob: 7008064ad02d25257837cc99d04c779a1bcbaf99 (plain)
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
/*
 * meli - ui crate.
 *
 * Copyright 2017-2018 Manos Pitsidianakis
 *
 * This file is part of meli.
 *
 * meli is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * meli is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with meli. If not, see <http://www.gnu.org/licenses/>.
 */

/*! A parser module for user commands passed through the Ex mode.
*/
pub use melib::mailbox::{SortField, SortOrder};
use nom::{digit, not_line_ending};
use std;
pub mod actions;
pub use crate::actions::Action::{self, *};
pub use crate::actions::ListingAction::{self, *};
pub use crate::actions::MailingListAction::{self, *};
pub use crate::actions::TabAction::{self, *};

/* Create a const table with every command part that can be auto-completed and its description */
macro_rules! define_commands {
    ( [$({ tags: [$( $tags:literal),*], desc: $desc:literal, parser: ($parser:item)}),*]) => {
        pub const COMMAND_COMPLETION: &[(&str, &str)] = &[$($( ($tags, $desc ) ),*),* ];
        $( $parser )*
    };
}

define_commands!([
                 { tags: ["set"],
                   desc: "set [seen/unseen], toggles message's Seen flag.",
                   parser:
                     ( named!(
                             envelope_action<Action>,
                             alt_complete!(
                                 preceded!(
                                     ws!(tag!("set")),
                                     alt_complete!(
                                         map!(ws!(tag!("read")), |_| Listing(SetRead))
                                         | map!(ws!(tag!("unread")), |_| Listing(SetUnread))
                                     )
                                 ) | map!(ws!(tag!("delete")), |_| Listing(Delete))
                             )
                     ); )
                 },
                 { tags: ["close"],
                   desc: "close non-sticky tabs",
                   parser: (
                       named!(close<Action>, map!(ws!(tag!("close")), |_| Tab(Close)));
                   )
                 },
                 { tags: ["goto"],
                   desc: "goto [n], switch to nth mailbox in this account",
                   parser: (
                       named!(
                           goto<Action>,
                           preceded!(tag!("go "), map!(call!(usize_c), Action::ViewMailbox))
                       );
                   )
                 },
                 { tags: ["subsort"],
                   desc: "subsort [date/subject] [asc/desc], sorts first level replies in threads.",
                   parser: (
                       named!(
                           subsort<Action>,
                           do_parse!(tag!("subsort ") >> p: pair!(sortfield, sortorder) >> (SubSort(p.0, p.1)))
                       );
                   )
                 },
                { tags: ["sort"],
                  desc: "sort [date/subject] [asc/desc], sorts threads.",
                  parser: (
                      named!(
                          sort<Action>,
                          do_parse!(
                              tag!("sort ") >> p: separated_pair!(sortfield, tag!(" "), sortorder) >> (Sort(p.0, p.1))
                          )
                      );
                  )
                },
                { tags: ["set", "set plain", "set threaded", "set compact"],
                  desc: "set [plain/threaded/compact], changes the mail listing view",
                  parser: (
                      named!(
                          toggle<Action>,
                          preceded!(tag!("set "), alt_complete!(threaded | plain | compact))
                      );
                  )
                },
                { tags: ["toggle_thread_snooze"],
                  desc: "turn off new notifications for this thread",
                  parser: (
                      named!(toggle_thread_snooze<Action>,
                             map!(ws!(tag!("toggle_thread_snooze")), |_| ToggleThreadSnooze)
                      );
                  )
                },
                { tags: ["filter"],
                  desc: "filter <TERM>, filters list with given term",
                  parser:(
                      named!(filter<Action>,
                             do_parse!(
                                 ws!(tag!("filter"))
                                 >> string: map_res!(call!(not_line_ending), std::str::from_utf8)
                                 >> (Listing(Filter(String::from(string))))
                             )
                      );
                  )
                },
                { tags: ["list-archive", "list-post", "list-unsubscribe", "list-"],
                  desc: "list-[unsubscribe/post/archive]",
                  parser: (
                      named!(
                          mailinglist<Action>,
                          alt_complete!(
                              map!(ws!(tag!("list-post")), |_| MailingListAction(ListPost))
                              | map!(ws!(tag!("list-unsubscribe")), |_| MailingListAction(
                                      ListUnsubscribe
                              ))
                              | map!(ws!(tag!("list-archive")), |_| MailingListAction(
                                      ListArchive
                              ))
                          )
                      );
                  )
                },
                { tags: ["setenv "],
                  desc:"setenv VAR=VALUE",
                  parser: (
                      named!( setenv<Action>,
                              do_parse!(
                                  ws!(tag!("setenv"))
                                  >> key: map_res!(take_until1!("="), std::str::from_utf8)
                                  >> ws!(tag!("="))
                                  >> val: map_res!(call!(not_line_ending), std::str::from_utf8)
                                  >> (SetEnv(key.to_string(), val.to_string()))
                              )
                      );
                  )
                },
                { tags: ["printenv "],
                  desc: "printenv VAR",
                  parser:(
                      named!( printenv<Action>,
                              do_parse!(
                                  ws!(tag!("env"))
                                  >> key: map_res!(call!(not_line_ending), std::str::from_utf8)
                                  >> (PrintEnv(key.to_string()))
                              )
                      );
                  )
                }
]);

named!(
    usize_c<usize>,
    map_res!(
        map_res!(ws!(digit), std::str::from_utf8),
        std::str::FromStr::from_str
    )
);

named!(
    sortfield<SortField>,
    map_res!(
        map_res!(take_until_s!(" "), std::str::from_utf8),
        std::str::FromStr::from_str
    )
);

named!(
    sortorder<SortOrder>,
    map_res!(
        map_res!(call!(not_line_ending), std::str::from_utf8),
        std::str::FromStr::from_str
    )
);

named!(
    threaded<Action>,
    map!(ws!(tag!("threaded")), |_| Listing(SetThreaded))
);

named!(
    plain<Action>,
    map!(ws!(tag!("plain")), |_| Listing(SetPlain))
);

named!(
    compact<Action>,
    map!(ws!(tag!("compact")), |_| Listing(SetCompact))
);
named!(
    listing_action<Action>,
    alt_complete!(toggle | envelope_action | filter | toggle_thread_snooze)
);
named!(pub parse_command<Action>,
       alt_complete!( goto | listing_action | sort | subsort | close | mailinglist | setenv | printenv)
);