1504 lines
62 KiB
Rust
1504 lines
62 KiB
Rust
/*
|
|
* meli
|
|
*
|
|
* 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 Command mode.
|
|
*/
|
|
use crate::melib::parser::BytesExt;
|
|
use melib::nom::{
|
|
self,
|
|
branch::alt,
|
|
bytes::complete::{is_a, is_not, tag, take_until},
|
|
character::complete::{digit1, not_line_ending},
|
|
combinator::{map, map_res},
|
|
error::Error as NomError,
|
|
multi::separated_list1,
|
|
sequence::{pair, preceded, separated_pair},
|
|
IResult,
|
|
};
|
|
pub use melib::thread::{SortField, SortOrder};
|
|
use melib::MeliError;
|
|
pub mod actions;
|
|
use actions::MailboxOperation;
|
|
use std::collections::HashSet;
|
|
pub mod history;
|
|
pub use crate::actions::AccountAction::{self, *};
|
|
pub use crate::actions::Action::{self, *};
|
|
pub use crate::actions::ComposeAction::{self, *};
|
|
pub use crate::actions::ListingAction::{self, *};
|
|
pub use crate::actions::MailingListAction::{self, *};
|
|
pub use crate::actions::TabAction::{self, *};
|
|
pub use crate::actions::TagAction::{self, *};
|
|
pub use crate::actions::ViewAction::{self, *};
|
|
use std::str::FromStr;
|
|
|
|
/// Helper macro to convert an array of tokens into a TokenStream
|
|
macro_rules! to_stream {
|
|
($token: expr) => {
|
|
TokenStream {
|
|
tokens: &[$token],
|
|
}
|
|
};
|
|
($($tokens:expr),*) => {
|
|
TokenStream {
|
|
tokens: &[$($tokens),*],
|
|
}
|
|
};
|
|
}
|
|
pub struct Command {
|
|
tags: &'static str,
|
|
desc: &'static str,
|
|
tokens: TokenStream,
|
|
}
|
|
|
|
/// Macro to 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, tokens: $tokens:expr, parser: ($parser:item)}),*]) => {
|
|
pub const COMMANDS: &[Command] = &[$($( Command { tags: $tags, desc: $desc, tokens: TokenStream { tokens: $tokens } } ),*),* ];
|
|
$( $parser )*
|
|
};
|
|
}
|
|
|
|
pub fn quoted_argument(input: &[u8]) -> IResult<&[u8], &str> {
|
|
if input.is_empty() {
|
|
return Err(nom::Err::Error(NomError {
|
|
input,
|
|
code: nom::error::ErrorKind::Tag,
|
|
}));
|
|
}
|
|
|
|
if input[0] == b'"' {
|
|
let mut i = 1;
|
|
while i < input.len() {
|
|
if input[i] == b'\"' && input[i - 1] != b'\\' {
|
|
return Ok((&input[i + 1..], unsafe {
|
|
std::str::from_utf8_unchecked(&input[1..i])
|
|
}));
|
|
}
|
|
i += 1;
|
|
}
|
|
Err(nom::Err::Error(NomError {
|
|
input,
|
|
code: nom::error::ErrorKind::Tag,
|
|
}))
|
|
} else {
|
|
map_res(is_not(" "), std::str::from_utf8)(input)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct TokenStream {
|
|
tokens: &'static [TokenAdicity],
|
|
}
|
|
|
|
use Token::*;
|
|
use TokenAdicity::*;
|
|
|
|
impl TokenStream {
|
|
fn matches<'s>(&self, s: &mut &'s str, sugg: &mut HashSet<String>) -> Vec<(&'s str, Token)> {
|
|
let mut tokens = vec![];
|
|
for t in self.tokens.iter() {
|
|
let mut ptr = 0;
|
|
while ptr + 1 < s.len() && s.as_bytes()[ptr].is_ascii_whitespace() {
|
|
ptr += 1;
|
|
}
|
|
*s = &s[ptr..];
|
|
//println!("\t before s.is_empty() {:?} {:?}", t, s);
|
|
if s.is_empty() || *s == " " {
|
|
match t.inner() {
|
|
Literal(lit) => {
|
|
sugg.insert(format!("{}{}", if s.is_empty() { " " } else { "" }, lit));
|
|
}
|
|
Alternatives(v) => {
|
|
for t in v.iter() {
|
|
//println!("adding empty suggestions for {:?}", t);
|
|
let mut _s = *s;
|
|
let mut m = t.matches(&mut _s, sugg);
|
|
tokens.append(&mut m);
|
|
}
|
|
}
|
|
Seq(_s) => {}
|
|
RestOfStringValue => {
|
|
sugg.insert(String::new());
|
|
}
|
|
t @ AttachmentIndexValue
|
|
| t @ MailboxIndexValue
|
|
| t @ IndexValue
|
|
| t @ ExistingFilepath
|
|
| t @ NewFilepath
|
|
| t @ AccountName
|
|
| t @ MailboxPath
|
|
| t @ QuotedStringValue
|
|
| t @ AlphanumericStringValue => {
|
|
let _t = t;
|
|
//sugg.insert(format!("{}{:?}", if s.is_empty() { " " } else { "" }, t));
|
|
}
|
|
}
|
|
tokens.push((*s, *t.inner()));
|
|
return tokens;
|
|
}
|
|
match t.inner() {
|
|
Literal(lit) => {
|
|
if lit.starts_with(*s) && lit.len() != s.len() {
|
|
sugg.insert(lit[s.len()..].to_string());
|
|
tokens.push((s, *t.inner()));
|
|
return tokens;
|
|
} else if s.starts_with(lit) {
|
|
tokens.push((&s[..lit.len()], *t.inner()));
|
|
*s = &s[lit.len()..];
|
|
} else {
|
|
return vec![];
|
|
}
|
|
}
|
|
Alternatives(v) => {
|
|
let mut cont = true;
|
|
for t in v.iter() {
|
|
let mut _s = *s;
|
|
let mut m = t.matches(&mut _s, sugg);
|
|
if !m.is_empty() {
|
|
tokens.append(&mut m);
|
|
//println!("_s is empty {}", _s.is_empty());
|
|
cont = !_s.is_empty();
|
|
*s = _s;
|
|
break;
|
|
}
|
|
}
|
|
if tokens.is_empty() {
|
|
return tokens;
|
|
}
|
|
if !cont {
|
|
*s = "";
|
|
}
|
|
}
|
|
Seq(_s) => {
|
|
return vec![];
|
|
}
|
|
RestOfStringValue => {
|
|
tokens.push((*s, *t.inner()));
|
|
return tokens;
|
|
}
|
|
AttachmentIndexValue
|
|
| MailboxIndexValue
|
|
| IndexValue
|
|
| ExistingFilepath
|
|
| NewFilepath
|
|
| AccountName
|
|
| MailboxPath
|
|
| QuotedStringValue
|
|
| AlphanumericStringValue => {
|
|
let mut ptr = 0;
|
|
while ptr + 1 < s.len() && !s.as_bytes()[ptr].is_ascii_whitespace() {
|
|
ptr += 1;
|
|
}
|
|
tokens.push((&s[..ptr + 1], *t.inner()));
|
|
*s = &s[ptr + 1..];
|
|
}
|
|
}
|
|
}
|
|
tokens
|
|
}
|
|
|
|
fn matches_tokens<'s>(
|
|
&self,
|
|
tokens: &'s [Cow<'s, str>],
|
|
sugg: &mut HashSet<String>,
|
|
) -> Result<Vec<(&'s str, Token)>, usize> {
|
|
let mut ret = vec![];
|
|
let mut tok_index = 0;
|
|
for t in self.tokens.iter() {
|
|
if tokens[tok_index..].is_empty() {
|
|
match t.inner() {
|
|
Literal(lit) => {
|
|
sugg.insert(format!(" {}", lit));
|
|
}
|
|
Alternatives(v) => {
|
|
for t in v.iter() {
|
|
//println!("adding empty suggestions for {:?}", t);
|
|
if let Ok(mut m) = t.matches_tokens(&tokens[tok_index..], sugg) {
|
|
ret.append(&mut m);
|
|
}
|
|
}
|
|
}
|
|
Seq(_s) => {}
|
|
RestOfStringValue => {
|
|
sugg.insert(String::new());
|
|
}
|
|
t @ AttachmentIndexValue
|
|
| t @ MailboxIndexValue
|
|
| t @ IndexValue
|
|
| t @ ExistingFilepath
|
|
| t @ NewFilepath
|
|
| t @ AccountName
|
|
| t @ MailboxPath
|
|
| t @ QuotedStringValue
|
|
| t @ AlphanumericStringValue => {
|
|
let _t = t;
|
|
//sugg.insert(format!("{}{:?}", if s.is_empty() { " " } else { "" }, t));
|
|
}
|
|
}
|
|
return Ok(ret);
|
|
}
|
|
let tok = tokens[tok_index].as_ref();
|
|
match t.inner() {
|
|
Literal(lit) => {
|
|
if lit.starts_with(tok) && lit.len() != tok.len() {
|
|
if tokens[tok_index..].len() != 1 {
|
|
return Err(tok_index);
|
|
}
|
|
sugg.insert(lit[tok.len()..].to_string());
|
|
ret.push((tok, *t.inner()));
|
|
return Ok(ret);
|
|
} else if &tok == lit {
|
|
ret.push((tok, *t.inner()));
|
|
} else {
|
|
return Err(tok_index);
|
|
}
|
|
}
|
|
Alternatives(v) => {
|
|
let mut cont = true;
|
|
for t in v.iter() {
|
|
if let Ok(mut m) = t.matches_tokens(&tokens[tok_index..], sugg) {
|
|
if !m.is_empty() {
|
|
ret.append(&mut m);
|
|
//println!("_s is empty {}", _s.is_empty());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ret.is_empty() {
|
|
return Ok(ret);
|
|
}
|
|
}
|
|
Seq(_s) => {
|
|
return Ok(ret);
|
|
}
|
|
RestOfStringValue => {
|
|
if tokens[tok_index..].len() != 1 {
|
|
return Err(tok_index);
|
|
}
|
|
ret.push((tok, *t.inner()));
|
|
return Ok(ret);
|
|
}
|
|
AttachmentIndexValue
|
|
| MailboxIndexValue
|
|
| IndexValue
|
|
| ExistingFilepath
|
|
| NewFilepath
|
|
| AccountName
|
|
| MailboxPath
|
|
| QuotedStringValue
|
|
| AlphanumericStringValue => {
|
|
ret.push((tok, *t.inner()));
|
|
}
|
|
}
|
|
tok_index += 1;
|
|
}
|
|
Ok(ret)
|
|
}
|
|
}
|
|
|
|
/// `Token` wrapper that defines how many times a token is expected to be repeated
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub enum TokenAdicity {
|
|
ZeroOrOne(Token),
|
|
ZeroOrMore(Token),
|
|
One(Token),
|
|
OneOrMore(Token),
|
|
}
|
|
|
|
impl TokenAdicity {
|
|
fn inner(&self) -> &Token {
|
|
match self {
|
|
ZeroOrOne(ref t) => t,
|
|
ZeroOrMore(ref t) => t,
|
|
One(ref t) => t,
|
|
OneOrMore(ref t) => t,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A token encountered in the UI's command execution bar
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub enum Token {
|
|
Literal(&'static str),
|
|
ExistingFilepath,
|
|
NewFilepath,
|
|
Alternatives(&'static [TokenStream]),
|
|
Seq(&'static [TokenAdicity]),
|
|
AccountName,
|
|
MailboxPath,
|
|
QuotedStringValue,
|
|
RestOfStringValue,
|
|
AlphanumericStringValue,
|
|
AttachmentIndexValue,
|
|
MailboxIndexValue,
|
|
IndexValue,
|
|
}
|
|
|
|
fn eof(input: &[u8]) -> IResult<&[u8], ()> {
|
|
if input.is_empty() {
|
|
Ok((input, ()))
|
|
} else {
|
|
Err(nom::Err::Error(NomError {
|
|
input,
|
|
code: nom::error::ErrorKind::Tag,
|
|
}))
|
|
}
|
|
}
|
|
|
|
define_commands!([
|
|
{ tags: ["set"],
|
|
desc: "set [seen/unseen], toggles message's Seen flag.",
|
|
tokens: &[One(Literal("set")), One(Alternatives(&[to_stream!(One(Literal("seen"))), to_stream!(One(Literal("unseen")))]))],
|
|
parser: (
|
|
fn seen_flag(input: &'_ [u8]) -> IResult<&'_ [u8], Action> {
|
|
let (input, _) = tag("set")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, ret) = alt((map(tag("seen"), |_| Listing(SetSeen)), map(tag("unseen"), |_| Listing(SetUnseen))))(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, ret))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["delete"],
|
|
desc: "delete message",
|
|
tokens: &[One(Literal("delete"))],
|
|
parser: (
|
|
fn delete_message(input: &'_ [u8]) -> IResult<&'_ [u8], Action> {
|
|
let (input, ret) = map(preceded(tag("delete"), eof), |_| Listing(Delete))(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, ret))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["copyto", "moveto"],
|
|
desc: "copy/move message",
|
|
tokens: &[One(Alternatives(&[to_stream!(One(Literal("copyto"))), to_stream!(One(Literal("moveto")))])), ZeroOrOne(AccountName), One(MailboxPath)],
|
|
parser: (
|
|
fn copymove<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
|
|
alt((
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("copyto")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, { Listing(CopyTo(path.to_string())) }))
|
|
},
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("copyto")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, { Listing(CopyToOtherAccount(account.to_string(), path.to_string())) }))
|
|
},
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("moveto")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, { Listing(MoveTo(path.to_string())) }))
|
|
},
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("moveto")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, { Listing(MoveToOtherAccount(account.to_string(), path.to_string())) }))
|
|
}
|
|
))(input)
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["import "],
|
|
desc: "import FILESYSTEM_PATH MAILBOX_PATH",
|
|
tokens: &[One(Literal("import")), One(ExistingFilepath), One(MailboxPath)],
|
|
parser:(
|
|
fn import(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("import")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, file) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, mailbox_path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(Import(file.to_string().into(), mailbox_path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["close"],
|
|
desc: "close non-sticky tabs",
|
|
tokens: &[One(Literal("close"))],
|
|
parser: (
|
|
fn close(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("close")(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, { Tab(Close) }))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["go"],
|
|
desc: "go [n], switch to nth mailbox in this account",
|
|
tokens: &[One(Literal("goto")), One(MailboxIndexValue)],
|
|
parser: (
|
|
fn goto(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("go")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, nth) = usize_c(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, { Action::ViewMailbox(nth) }))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["subsort"],
|
|
desc: "subsort [date/subject] [asc/desc], sorts first level replies in threads.",
|
|
tokens: &[One(Literal("subsort")), One(Alternatives(&[to_stream!(One(Literal("date"))), to_stream!(One(Literal("subject")))])), One(Alternatives(&[to_stream!(One(Literal("asc"))), to_stream!(One(Literal("desc")))])) ],
|
|
parser: (
|
|
fn subsort(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("subsort")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, p) = pair(sortfield, sortorder)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, SubSort(p.0, p.1)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["sort"],
|
|
desc: "sort [date/subject] [asc/desc], sorts threads.",
|
|
tokens: &[One(Literal("sort")), One(Alternatives(&[to_stream!(One(Literal("date"))), to_stream!(One(Literal("subject")))])), One(Alternatives(&[to_stream!(One(Literal("asc"))), to_stream!(One(Literal("desc")))])) ],
|
|
parser: (
|
|
fn sort(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("sort")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, p) = separated_pair(sortfield, tag(" "), sortorder)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, (Sort(p.0, p.1))))
|
|
}
|
|
)},
|
|
{ tags: ["set", "set plain", "set threaded", "set compact"],
|
|
desc: "set [plain/threaded/compact/conversations], changes the mail listing view",
|
|
tokens: &[One(Literal("set")), One(Alternatives(&[to_stream!(One(Literal("plain"))), to_stream!(One(Literal("threaded"))), to_stream!(One(Literal("compact"))), to_stream!(One(Literal("conversations")))]))],
|
|
parser: (
|
|
fn toggle(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("set")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, ret) = alt((threaded, plain, compact, conversations))(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, ret))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["toggle thread_snooze"],
|
|
desc: "turn off new notifications for this thread",
|
|
tokens: &[One(Literal("toggle")), One(Literal("thread_snooze"))],
|
|
parser: (
|
|
fn toggle_thread_snooze(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("toggle")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, _) = tag("thread_snooze")(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(ToggleThreadSnooze)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["search"],
|
|
desc: "search <TERM>, searches list with given term",
|
|
tokens: &[One(Literal("search")), One(RestOfStringValue)],
|
|
parser:(
|
|
fn search(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("search")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, string) = map_res(not_line_ending, std::str::from_utf8)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(Search(String::from(string)))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["select"],
|
|
desc: "select <TERM>, selects envelopes matching with given term",
|
|
tokens: &[One(Literal("select")), One(RestOfStringValue)],
|
|
parser:(
|
|
fn select(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("select")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, string) = map_res(not_line_ending, std::str::from_utf8)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(Select(String::from(string)))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["export-mbox "],
|
|
desc: "export-mbox PATH",
|
|
tokens: &[One(Literal("export-mbox")), One(NewFilepath)],
|
|
parser:(
|
|
fn export_mbox(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("export-mbox")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(ExportMbox(Some(melib::backends::mbox::MboxFormat::MboxCl2), path.to_string().into()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["list-archive", "list-post", "list-unsubscribe", "list-"],
|
|
desc: "list-[unsubscribe/post/archive]",
|
|
tokens: &[One(Alternatives(&[to_stream!(One(Literal("list-archive"))), to_stream!(One(Literal("list-post"))), to_stream!(One(Literal("list-unsubscribe")))]))],
|
|
parser: (
|
|
fn mailinglist(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, ret) = alt((
|
|
map(tag("list-post"), |_| MailingListAction(ListPost))
|
|
, map(tag("list-unsubscribe"), |_| MailingListAction(
|
|
ListUnsubscribe
|
|
))
|
|
, map(tag("list-archive"), |_| MailingListAction(
|
|
ListArchive
|
|
))
|
|
))(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, ret))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["setenv "],
|
|
desc: "setenv VAR=VALUE",
|
|
tokens: &[One(Literal("setenv")), OneOrMore(Seq(&[One(AlphanumericStringValue), One(Literal("=")), One(QuotedStringValue)]))],
|
|
parser: (
|
|
fn setenv(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("setenv")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, key) = map_res(take_until("="), std::str::from_utf8)(input)?;
|
|
let (input, _) = tag("=")(input.trim())?;
|
|
let (input, val) = map_res(not_line_ending, std::str::from_utf8)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, SetEnv(key.to_string(), val.to_string())))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["printenv "],
|
|
desc: "printenv VAR",
|
|
tokens: &[],
|
|
parser:(
|
|
fn printenv(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("printenv")(input.ltrim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, key) = map_res(not_line_ending, std::str::from_utf8)(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, PrintEnv(key.to_string())))
|
|
}
|
|
)
|
|
},
|
|
/* Pipe pager contents to binary */
|
|
{ tags: ["pipe "],
|
|
desc: "pipe EXECUTABLE ARGS",
|
|
tokens: &[One(Literal("pipe")), One(ExistingFilepath), ZeroOrMore(QuotedStringValue)],
|
|
parser:(
|
|
fn pipe<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
|
|
alt((
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("pipe")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, bin) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, args) = separated_list1(is_a(" "), quoted_argument)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, {
|
|
View(Pipe(bin.to_string(), args.into_iter().map(String::from).collect::<Vec<String>>()))
|
|
}))
|
|
},
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("pipe")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, bin) = quoted_argument(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, {
|
|
View(Pipe(bin.to_string(), Vec::new()))
|
|
}))
|
|
}
|
|
))(input)
|
|
}
|
|
)
|
|
},
|
|
/* Filter pager contents through binary */
|
|
{ tags: ["filter "],
|
|
desc: "filter EXECUTABLE ARGS",
|
|
tokens: &[One(Literal("filter")), One(ExistingFilepath), ZeroOrMore(QuotedStringValue)],
|
|
parser:(
|
|
fn filter(input: &'_ [u8]) -> IResult<&'_ [u8], Action> {
|
|
let (input, _) = tag("filter")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, cmd) = map_res(not_line_ending, std::str::from_utf8)(input)?;
|
|
Ok((input, {
|
|
View(Filter(cmd.to_string()))
|
|
}))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["add-attachment ", "add-attachment-file-picker "],
|
|
desc: "add-attachment PATH",
|
|
tokens: &[One(
|
|
Alternatives(&[to_stream!(One(Literal("add-attachment")), One(ExistingFilepath)), to_stream!(One(Literal("add-attachment-file-picker")))]))],
|
|
parser:(
|
|
fn add_attachment<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
|
|
alt((
|
|
|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("add-attachment")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, _) = tag("<")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, cmd) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(AddAttachmentPipe(cmd.to_string()))))
|
|
}, |input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("add-attachment")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(AddAttachment(path.to_string()))))
|
|
}, |input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("add-attachment-file-picker")(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(AddAttachmentFilePicker(None))))
|
|
}, |input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("add-attachment-file-picker")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, _) = tag("<")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, shell) = map_res(not_line_ending, std::str::from_utf8)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(AddAttachmentFilePicker(Some(shell.to_string())))))
|
|
}
|
|
))(input)
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["remove-attachment "],
|
|
desc: "remove-attachment INDEX",
|
|
tokens: &[One(Literal("remove-attachment")), One(IndexValue)],
|
|
parser:(
|
|
fn remove_attachment(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("remove-attachment")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, idx) = map_res(quoted_argument, usize::from_str)(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(RemoveAttachment(idx))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["save-draft"],
|
|
desc: "save draft",
|
|
tokens: &[One(Literal("save-draft"))],
|
|
parser:(
|
|
fn save_draft(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("save-draft")(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(SaveDraft)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["toggle sign "],
|
|
desc: "switch between sign/unsign for this draft",
|
|
tokens: &[One(Literal("toggle")), One(Literal("sign"))],
|
|
parser:(
|
|
fn toggle_sign(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("toggle")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, _) = tag("sign")(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(ToggleSign)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["toggle encrypt"],
|
|
desc: "toggle encryption for this draft",
|
|
tokens: &[One(Literal("toggle")), One(Literal("encrypt"))],
|
|
parser:(
|
|
fn toggle_encrypt(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("toggle")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, _) = tag("encrypt")(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Compose(ToggleEncrypt)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["create-mailbox "],
|
|
desc: "create-mailbox ACCOUNT MAILBOX_PATH",
|
|
tokens: &[One(Literal("create-mailbox")), One(AccountName), One(MailboxPath)],
|
|
parser:(
|
|
fn create_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("create-mailbox")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input,Mailbox(account.to_string(), MailboxOperation::Create(path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["subscribe-mailbox "],
|
|
desc: "subscribe-mailbox ACCOUNT MAILBOX_PATH",
|
|
tokens: &[One(Literal("subscribe-mailbox")), One(AccountName), One(MailboxPath)],
|
|
parser:(
|
|
fn sub_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("subscribe-mailbox")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input,Mailbox(account.to_string(), MailboxOperation::Subscribe(path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["unsubscribe-mailbox "],
|
|
desc: "unsubscribe-mailbox ACCOUNT MAILBOX_PATH",
|
|
tokens: &[One(Literal("unsubscribe-mailbox")), One(AccountName), One(MailboxPath)],
|
|
parser:(
|
|
fn unsub_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("unsubscribe-mailbox")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Mailbox(account.to_string(), MailboxOperation::Unsubscribe(path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["rename-mailbox "],
|
|
desc: "rename-mailbox ACCOUNT MAILBOX_PATH_SRC MAILBOX_PATH_DEST",
|
|
tokens: &[One(Literal("rename-mailbox")), One(AccountName), One(MailboxPath), One(MailboxPath)],
|
|
parser:(
|
|
fn rename_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("rename-mailbox")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, src) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, dest) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Mailbox(account.to_string(), MailboxOperation::Rename(src.to_string(), dest.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["delete-mailbox "],
|
|
desc: "delete-mailbox ACCOUNT MAILBOX_PATH",
|
|
tokens: &[One(Literal("delete-mailbox")), One(AccountName), One(MailboxPath)],
|
|
parser:(
|
|
fn delete_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("delete-mailbox")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok ((input, Mailbox(account.to_string(), MailboxOperation::Delete(path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["reindex "],
|
|
desc: "reindex ACCOUNT, rebuild account cache in the background",
|
|
tokens: &[One(Literal("reindex")), One(AccountName)],
|
|
parser:(
|
|
fn reindex(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("reindex")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok( (input, AccountAction(account.to_string(), ReIndex)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["open-in-tab"],
|
|
desc: "opens envelope view in new tab",
|
|
tokens: &[One(Literal("open-in-tab"))],
|
|
parser:(
|
|
fn open_in_new_tab(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("open-in-tab")(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(OpenInNewTab)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["save-attachment "],
|
|
desc: "save-attachment INDEX PATH",
|
|
tokens: &[One(Literal("save-attachment")), One(AttachmentIndexValue), One(NewFilepath)],
|
|
parser:(
|
|
fn save_attachment(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("save-attachment")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, idx) = map_res(quoted_argument, usize::from_str)(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, View(SaveAttachment(idx, path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["export-mail "],
|
|
desc: "export-mail PATH",
|
|
tokens: &[One(Literal("export-mail")), One(NewFilepath)],
|
|
parser:(
|
|
fn export_mail(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("export-mail")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, path) = quoted_argument(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, View(ExportMail(path.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["add-addresses-to-contacts "],
|
|
desc: "add-addresses-to-contacts",
|
|
tokens: &[One(Literal("add-addresses-to-contacts"))],
|
|
parser:(
|
|
fn add_addresses_to_contacts(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("add-addresses-to-contacts")(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, View(AddAddressesToContacts)))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["tag", "tag add", "tag remove"],
|
|
desc: "tag [add/remove], edits message's tags.",
|
|
tokens: &[One(Literal("tag")), One(Alternatives(&[to_stream!(One(Literal("add"))), to_stream!(One(Literal("remove")))]))],
|
|
parser: (
|
|
fn _tag<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
|
|
preceded(
|
|
tag("tag"),
|
|
alt((|input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("add")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, tag) = quoted_argument(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(Tag(Add(tag.to_string())))))
|
|
}, |input: &'a [u8]| -> IResult<&'a [u8], Action> {
|
|
let (input, _) = tag("remove")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, tag) = quoted_argument(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, Listing(Tag(Remove(tag.to_string())))))
|
|
}
|
|
))
|
|
)(input.trim())
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["print "],
|
|
desc: "print ACCOUNT SETTING",
|
|
tokens: &[One(Literal("print")), One(AccountName), One(QuotedStringValue)],
|
|
parser:(
|
|
fn print_account_setting(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("print")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, account) = quoted_argument(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, setting) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, AccountAction(account.to_string(), PrintAccountSetting(setting.to_string()))))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["print "],
|
|
desc: "print SETTING",
|
|
tokens: &[One(Literal("print")), One(QuotedStringValue)],
|
|
parser:(
|
|
fn print_setting(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("print")(input.trim())?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, setting) = quoted_argument(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, PrintSetting(setting.to_string())))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["toggle mouse"],
|
|
desc: "toggle mouse support",
|
|
tokens: &[One(Literal("toggle")), One(Literal("mouse"))],
|
|
parser:(
|
|
fn toggle_mouse(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("toggle")(input)?;
|
|
let (input, _) = is_a(" ")(input)?;
|
|
let (input, _) = tag("mouse")(input)?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, ToggleMouse))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["manage-mailboxes"],
|
|
desc: "view and manage mailbox preferences",
|
|
tokens: &[One(Literal("manage-mailboxes"))],
|
|
parser:(
|
|
fn manage_mailboxes(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("manage-mailboxes")(input.trim())?;
|
|
let (input, _) = eof(input)?;
|
|
Ok((input, ManageMailboxes))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["quit"],
|
|
desc: "quit meli",
|
|
tokens: &[One(Literal("quit"))],
|
|
parser:(
|
|
fn quit(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("quit")(input.trim())?;
|
|
let (input, _) = eof(input.trim())?;
|
|
Ok((input, Quit))
|
|
}
|
|
)
|
|
},
|
|
{ tags: ["reload-config"],
|
|
desc: "reload configuration file",
|
|
tokens: &[One(Literal("reload-config"))],
|
|
parser:(
|
|
fn reload_config(input: &[u8]) -> IResult<&[u8], Action> {
|
|
let (input, _) = tag("reload-config")(input.trim())?;
|
|
let (input, _) = eof(input.trim())?;
|
|
Ok((input, ReloadConfiguration))
|
|
}
|
|
)
|
|
}
|
|
]);
|
|
|
|
fn usize_c(input: &[u8]) -> IResult<&[u8], usize> {
|
|
map_res(
|
|
map_res(digit1, std::str::from_utf8),
|
|
std::str::FromStr::from_str,
|
|
)(input.trim())
|
|
}
|
|
|
|
fn sortfield(input: &[u8]) -> IResult<&[u8], SortField> {
|
|
map_res(
|
|
map_res(take_until(" "), std::str::from_utf8),
|
|
std::str::FromStr::from_str,
|
|
)(input.trim())
|
|
}
|
|
|
|
fn sortorder(input: &[u8]) -> IResult<&[u8], SortOrder> {
|
|
map_res(
|
|
map_res(not_line_ending, std::str::from_utf8),
|
|
std::str::FromStr::from_str,
|
|
)(input)
|
|
}
|
|
|
|
fn threaded(input: &[u8]) -> IResult<&[u8], Action> {
|
|
map(tag("threaded"), |_| Listing(SetThreaded))(input.trim())
|
|
}
|
|
|
|
fn plain(input: &[u8]) -> IResult<&[u8], Action> {
|
|
map(tag("plain"), |_| Listing(SetPlain))(input.trim())
|
|
}
|
|
|
|
fn compact(input: &[u8]) -> IResult<&[u8], Action> {
|
|
map(tag("compact"), |_| Listing(SetCompact))(input.trim())
|
|
}
|
|
|
|
fn conversations(input: &[u8]) -> IResult<&[u8], Action> {
|
|
map(tag("conversations"), |_| Listing(SetConversations))(input.trim())
|
|
}
|
|
|
|
fn listing_action(input: &[u8]) -> IResult<&[u8], Action> {
|
|
alt((
|
|
toggle,
|
|
seen_flag,
|
|
delete_message,
|
|
copymove,
|
|
import,
|
|
search,
|
|
select,
|
|
toggle_thread_snooze,
|
|
open_in_new_tab,
|
|
export_mbox,
|
|
_tag,
|
|
))(input)
|
|
}
|
|
|
|
fn compose_action(input: &[u8]) -> IResult<&[u8], Action> {
|
|
alt((
|
|
add_attachment,
|
|
remove_attachment,
|
|
toggle_sign,
|
|
toggle_encrypt,
|
|
save_draft,
|
|
))(input)
|
|
}
|
|
|
|
fn account_action(input: &[u8]) -> IResult<&[u8], Action> {
|
|
alt((reindex, print_account_setting))(input)
|
|
}
|
|
|
|
fn view(input: &[u8]) -> IResult<&[u8], Action> {
|
|
alt((
|
|
filter,
|
|
pipe,
|
|
save_attachment,
|
|
export_mail,
|
|
add_addresses_to_contacts,
|
|
))(input)
|
|
}
|
|
|
|
pub fn parse_command(input: &[u8]) -> Result<Action, MeliError> {
|
|
alt((
|
|
goto,
|
|
listing_action,
|
|
sort,
|
|
subsort,
|
|
close,
|
|
mailinglist,
|
|
setenv,
|
|
printenv,
|
|
view,
|
|
compose_action,
|
|
create_mailbox,
|
|
sub_mailbox,
|
|
unsub_mailbox,
|
|
delete_mailbox,
|
|
rename_mailbox,
|
|
manage_mailboxes,
|
|
account_action,
|
|
print_setting,
|
|
toggle_mouse,
|
|
reload_config,
|
|
quit,
|
|
))(input)
|
|
.map(|(_, v)| v)
|
|
.map_err(|err| err.into())
|
|
}
|
|
|
|
#[test]
|
|
fn test_parser() {
|
|
let mut input = "sort".to_string();
|
|
macro_rules! match_input {
|
|
($input:expr) => {{
|
|
let mut sugg = Default::default();
|
|
let mut vec = vec![];
|
|
//print!("{}", $input);
|
|
for Command {
|
|
tags: _,
|
|
desc: _,
|
|
tokens,
|
|
} in COMMANDS.iter()
|
|
{
|
|
//println!("{:?}, {:?}, {:?}", _tags, _desc, tokens);
|
|
let m = tokens.matches(&mut $input.as_str(), &mut sugg);
|
|
if !m.is_empty() {
|
|
vec.push(tokens);
|
|
//print!("{:?} ", desc);
|
|
//println!(" result = {:#?}\n\n", m);
|
|
}
|
|
}
|
|
//println!("suggestions = {:#?}", sugg);
|
|
sugg.into_iter()
|
|
.map(|s| format!("{}{}", $input.as_str(), s.as_str()))
|
|
.collect::<HashSet<String>>()
|
|
}};
|
|
}
|
|
assert_eq!(
|
|
&match_input!(input),
|
|
&IntoIterator::into_iter(["sort date".to_string(), "sort subject".to_string()]).collect(),
|
|
);
|
|
input = "so".to_string();
|
|
assert_eq!(
|
|
&match_input!(input),
|
|
&IntoIterator::into_iter(["sort".to_string()]).collect(),
|
|
);
|
|
input = "so ".to_string();
|
|
assert_eq!(&match_input!(input), &HashSet::default(),);
|
|
input = "to".to_string();
|
|
assert_eq!(
|
|
&match_input!(input),
|
|
&IntoIterator::into_iter(["toggle".to_string()]).collect(),
|
|
);
|
|
input = "toggle ".to_string();
|
|
assert_eq!(
|
|
&match_input!(input),
|
|
&IntoIterator::into_iter([
|
|
"toggle mouse".to_string(),
|
|
"toggle sign".to_string(),
|
|
"toggle encrypt".to_string(),
|
|
"toggle thread_snooze".to_string()
|
|
])
|
|
.collect(),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
#[ignore]
|
|
fn test_parser_interactive() {
|
|
use std::io;
|
|
let mut input = String::new();
|
|
loop {
|
|
input.clear();
|
|
print!("> ");
|
|
match io::stdin().read_line(&mut input) {
|
|
Ok(_n) => {
|
|
println!("Input is {:?}", input.as_str().trim());
|
|
let mut sugg = Default::default();
|
|
let mut vec = vec![];
|
|
//print!("{}", input);
|
|
for Command {
|
|
tags: _,
|
|
desc: _,
|
|
tokens,
|
|
} in COMMANDS.iter()
|
|
{
|
|
//println!("{:?}, {:?}, {:?}", _tags, _desc, tokens);
|
|
let m = tokens.matches(&mut input.as_str().trim(), &mut sugg);
|
|
if !m.is_empty() {
|
|
vec.push(tokens);
|
|
//print!("{:?} ", desc);
|
|
//println!(" result = {:#?}\n\n", m);
|
|
}
|
|
}
|
|
println!(
|
|
"suggestions = {:#?}",
|
|
sugg.into_iter()
|
|
.zip(vec.into_iter())
|
|
.map(|(s, v)| format!(
|
|
"{}{} {:?}",
|
|
input.as_str().trim(),
|
|
if input.trim().is_empty() {
|
|
s.trim()
|
|
} else {
|
|
s.as_str()
|
|
},
|
|
v
|
|
))
|
|
.collect::<Vec<String>>()
|
|
);
|
|
if input.trim() == "quit" {
|
|
break;
|
|
}
|
|
}
|
|
Err(error) => println!("error: {}", error),
|
|
}
|
|
}
|
|
println!("alright");
|
|
}
|
|
|
|
/// Get command suggestions for input
|
|
pub fn command_completion_suggestions(input: &str) -> Vec<String> {
|
|
use crate::melib::ShellExpandTrait;
|
|
let mut sugg = Default::default();
|
|
for Command {
|
|
tags: _,
|
|
desc: _,
|
|
tokens,
|
|
} in COMMANDS.iter()
|
|
{
|
|
let _m = tokens.matches(&mut &(*input), &mut sugg);
|
|
if _m.is_empty() {
|
|
continue;
|
|
}
|
|
if let Some((s, ExistingFilepath)) = _m.last() {
|
|
let p = std::path::Path::new(s);
|
|
sugg.extend(p.complete(true).into_iter());
|
|
}
|
|
}
|
|
sugg.into_iter()
|
|
.map(|s| format!("{}{}", input, s.as_str()))
|
|
.collect::<Vec<String>>()
|
|
}
|
|
|
|
use std::borrow::Cow;
|
|
|
|
pub enum LexerToken<'a> {
|
|
Complete {
|
|
input: Cow<'a, str>,
|
|
token: Token,
|
|
},
|
|
Incomplete {
|
|
input: Cow<'a, str>,
|
|
token: Token,
|
|
},
|
|
Invalid {
|
|
input: Cow<'a, str>,
|
|
reason: &'static str,
|
|
},
|
|
}
|
|
|
|
pub fn lexer(input: &'_ str) -> Result<Vec<Cow<'_, str>>, usize> {
|
|
#[inline(always)]
|
|
fn unescape(input: &str) -> Cow<'_, str> {
|
|
if input.is_empty() {
|
|
return input.into();
|
|
}
|
|
if let Some(input) = input
|
|
.strip_prefix('\'')
|
|
.and_then(|input| input.strip_suffix('\''))
|
|
{
|
|
input.into()
|
|
} else if let Some(input) = input
|
|
.strip_prefix('"')
|
|
.and_then(|input| input.strip_suffix('"'))
|
|
{
|
|
if input.contains('\\') {
|
|
input.replace('\\', "").into()
|
|
} else {
|
|
input.into()
|
|
}
|
|
} else if input.contains('\\') {
|
|
input.replace('\\', "").into()
|
|
} else {
|
|
input.into()
|
|
}
|
|
}
|
|
|
|
let mut prev_token_start = 0;
|
|
let mut tokens: Vec<Cow<'_, str>> = vec![];
|
|
#[repr(u8)]
|
|
#[derive(Copy, Clone)]
|
|
enum QuoteState {
|
|
QNone = 0,
|
|
QSingle,
|
|
QDouble,
|
|
QOne,
|
|
QDoubleOne,
|
|
}
|
|
use QuoteState::*;
|
|
|
|
let mut state = QNone;
|
|
|
|
for i in 0..input.as_bytes().len() {
|
|
match (state, input.as_bytes()[i]) {
|
|
/* handle escaped character \ */
|
|
(QOne, _) => {
|
|
state = QNone;
|
|
}
|
|
(QDoubleOne, _) => {
|
|
state = QDouble;
|
|
}
|
|
/* handle single quote ' */
|
|
(QNone, b'\'') => {
|
|
prev_token_start = i;
|
|
state = QSingle;
|
|
}
|
|
(QSingle, b'\'') => {
|
|
tokens.push(unescape(&input[prev_token_start..i + 1]));
|
|
prev_token_start = i + 1;
|
|
state = QNone;
|
|
}
|
|
(QDouble, b'\'') => {}
|
|
/* handle double quote " */
|
|
(QNone, b'"') => {
|
|
prev_token_start = i;
|
|
state = QDouble;
|
|
}
|
|
(QSingle, b'"') => {}
|
|
(QDouble, b'"') => {
|
|
tokens.push(unescape(&input[prev_token_start..i + 1]));
|
|
prev_token_start = i + 1;
|
|
state = QNone;
|
|
}
|
|
/* handle escape character \ */
|
|
(QNone, b'\\') => {
|
|
state = QOne;
|
|
}
|
|
(QSingle, b'\\') => {
|
|
// stay
|
|
}
|
|
(QDouble, b'\\') => {
|
|
// quote next character
|
|
state = QDoubleOne;
|
|
}
|
|
(QNone, b'\n') => break,
|
|
(QSingle | QDouble, b'\n') => {}
|
|
/* handle white spaces \ */
|
|
(QNone, b) if b"\t \n".contains(&b) => {
|
|
tokens.push(unescape(&input[prev_token_start..i]));
|
|
prev_token_start = i + 1;
|
|
}
|
|
(_, _) => {}
|
|
}
|
|
}
|
|
|
|
if prev_token_start < input.as_bytes().len() {
|
|
let last_token = input[prev_token_start..].trim();
|
|
if !last_token.is_empty() {
|
|
tokens.push(unescape(last_token));
|
|
}
|
|
}
|
|
match state {
|
|
QNone => Ok(tokens),
|
|
_ => Err(prev_token_start),
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ParseResult {
|
|
Valid {
|
|
inner: Action,
|
|
},
|
|
Invalid {
|
|
position: usize,
|
|
reason: &'static str,
|
|
},
|
|
Incomplete {
|
|
suggestions: Vec<String>,
|
|
},
|
|
}
|
|
|
|
pub fn parser(input: &'_ str, context: &crate::Context) -> ParseResult {
|
|
use crate::melib::ShellExpandTrait;
|
|
let mut lex_output: Vec<Cow<'_, str>> = match lexer(input) {
|
|
Ok(v) => v,
|
|
Err(err) => {
|
|
return ParseResult::Invalid {
|
|
position: err,
|
|
reason: "",
|
|
}
|
|
}
|
|
};
|
|
let mut sugg = HashSet::default();
|
|
for Command {
|
|
tags: _,
|
|
desc: _,
|
|
tokens,
|
|
} in COMMANDS.iter()
|
|
{
|
|
let m = tokens.matches_tokens(&lex_output, &mut sugg);
|
|
let m = match m {
|
|
Err(i) => {
|
|
//println!("error at `{}`", lex_output[i]);
|
|
continue;
|
|
}
|
|
Ok(m) => {
|
|
if !m.is_empty() {
|
|
//print!("{:?} ", desc);
|
|
//println!(" result = {:#?}\n\n", m);
|
|
}
|
|
m
|
|
}
|
|
};
|
|
if let Some((s, ExistingFilepath)) = m.last() {
|
|
let p = std::path::Path::new(s);
|
|
sugg.extend(p.complete(true).into_iter());
|
|
}
|
|
}
|
|
ParseResult::Incomplete {
|
|
suggestions: sugg
|
|
.into_iter()
|
|
.map(|s| format!("{}{}", input, s.as_str()))
|
|
.collect::<Vec<String>>(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_lexer() {
|
|
assert_eq!(
|
|
&[
|
|
Cow::Owned::<str>("set".to_string()),
|
|
Cow::Owned::<str>("plain".to_string())
|
|
],
|
|
lexer("set plain\n").unwrap().as_slice()
|
|
);
|
|
assert_eq!(
|
|
&[
|
|
Cow::Owned::<str>("export-mail".to_string()),
|
|
Cow::Owned::<str>("/path/to/file".to_string())
|
|
],
|
|
lexer("export-mail /path/to/file\n").unwrap().as_slice()
|
|
);
|
|
assert_eq!(
|
|
&[
|
|
Cow::Owned::<str>("export-mail".to_string()),
|
|
Cow::Owned::<str>("/path/to/ file".to_string())
|
|
],
|
|
lexer("export-mail /path/to/\\ file\n").unwrap().as_slice()
|
|
);
|
|
assert_eq!(
|
|
&[
|
|
Cow::Owned::<str>("export-mail".to_string()),
|
|
Cow::Owned::<str>("/path/to/ file".to_string())
|
|
],
|
|
lexer("export-mail \"/path/to/ file\"\n")
|
|
.unwrap()
|
|
.as_slice()
|
|
);
|
|
assert_eq!(12, lexer("export-mail \"/path/to/ file\n").unwrap_err());
|
|
use std::io;
|
|
let mut input = String::new();
|
|
loop {
|
|
input.clear();
|
|
print!("> ");
|
|
match io::stdin().read_line(&mut input) {
|
|
Ok(_n) => {
|
|
if input.trim() == "quit" {
|
|
break;
|
|
}
|
|
println!("Input is {:?}", input.as_str().trim());
|
|
//print!("{}", input);
|
|
let lex_output = lexer(input.as_str());
|
|
println!("lex output: {:#?}", &lex_output);
|
|
let lex_output = if let Ok(v) = lex_output {
|
|
v
|
|
} else {
|
|
continue;
|
|
};
|
|
let mut sugg = Default::default();
|
|
let mut vec = vec![];
|
|
//print!("{}", input);
|
|
for Command {
|
|
tags: _,
|
|
desc: _,
|
|
tokens,
|
|
} in COMMANDS.iter()
|
|
{
|
|
//println!("{:?}, {:?}, {:?}", _tags, _desc, tokens);
|
|
let m = tokens.matches_tokens(&lex_output, &mut sugg);
|
|
match m {
|
|
Err(i) => {
|
|
//println!("error at `{}`", lex_output[i]);
|
|
}
|
|
Ok(m) => {
|
|
if !m.is_empty() {
|
|
vec.push(tokens);
|
|
//print!("{:?} ", desc);
|
|
//println!(" result = {:#?}\n\n", m);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
println!(
|
|
"suggestions = {:#?}",
|
|
sugg.into_iter()
|
|
.zip(vec.into_iter())
|
|
.map(|(s, v)| format!(
|
|
"{}{} {:?}",
|
|
input.as_str().trim(),
|
|
if input.trim().is_empty() {
|
|
s.trim()
|
|
} else {
|
|
s.as_str()
|
|
},
|
|
v
|
|
))
|
|
.collect::<Vec<String>>()
|
|
);
|
|
}
|
|
Err(error) => println!("error: {}", error),
|
|
}
|
|
}
|
|
}
|