Skip to content

Commit

Permalink
fix wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Oct 10, 2024
1 parent 783a4b2 commit 7ca1129
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 35 deletions.
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sendmail = ["email-lib/sendmail", "pimalaya-tui/sendmail"]

keyring = ["email-lib/keyring", "pimalaya-tui/keyring", "secret-lib?/keyring-tokio"]
oauth2 = ["dep:oauth-lib", "email-lib/oauth2", "pimalaya-tui/oauth2", "keyring"]
wizard = ["dep:email_address", "dep:secret-lib", "email-lib/autoconfig"]
wizard = ["dep:email_address", "dep:secret-lib", "pimalaya-tui/wizard", "email-lib/autoconfig"]

pgp = []
pgp-commands = ["email-lib/pgp-commands", "mml-lib/pgp-commands", "pgp"]
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use email::sendmail::config::SendmailConfig;
#[cfg(feature = "smtp")]
use email::smtp::config::SmtpConfig;
use email::{account::config::AccountConfig, template::config::TemplateConfig};
use pimalaya_tui::config::toml::himalaya::{
use pimalaya_tui::config::toml::himalaya::config::{
BackendKind, EnvelopeConfig, FolderConfig, MessageConfig,
};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use email::{
},
AnyResult,
};
use pimalaya_tui::config::toml::himalaya::BackendKind;
use pimalaya_tui::config::toml::himalaya::config::BackendKind;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BackendConfig {
Expand Down
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ pub struct Cli {
#[arg(value_name = "PATH", value_parser = path_parser)]
pub config_paths: Vec<PathBuf>,

/// Override the default account.
///
/// An account name corresponds to an entry in the table at the
/// root level of your TOML configuration file.
#[arg(long, short, name = "account", value_name = "NAME")]
pub account: Option<String>,

/// Enable logs with spantrace.
///
/// This is the same as running the command with `RUST_LOG=debug`
Expand Down
80 changes: 65 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::{collections::HashMap, path::PathBuf};

use color_eyre::{eyre::eyre, Result};
use async_trait::async_trait;
use email::{account::config::AccountConfig, config::Config};
use pimalaya_tui::config::toml::himalaya::AccountsConfig;
use pimalaya_tui::{
config::toml::himalaya::config::{AccountsConfig, HimalayaTomlConfig},
Result,
};
use serde::{Deserialize, Serialize};

use crate::account::config::TomlAccountConfig;
Expand Down Expand Up @@ -69,29 +72,76 @@ impl From<TomlConfig> for Config {
}
}

#[async_trait]
impl pimalaya_tui::config::toml::TomlConfig for TomlConfig {
type AccountConfig = TomlAccountConfig;

fn project_name() -> &'static str {
"himalaya"
HimalayaTomlConfig::project_name()
}

fn get_default_account_config(&self) -> Result<(String, Self::AccountConfig)> {
self.accounts
.iter()
.find_map(|(name, account)| {
account
.default
.filter(|default| *default)
.map(|_| (name.to_owned(), account.clone()))
})
.ok_or_else(|| eyre!("cannot find default account"))
fn get_default_account_config(&self) -> Option<(String, Self::AccountConfig)> {
self.accounts.iter().find_map(|(name, account)| {
account
.default
.filter(|default| *default)
.map(|_| (name.to_owned(), account.clone()))
})
}

fn get_account_config(&self, name: &str) -> Result<(String, Self::AccountConfig)> {
fn get_account_config(&self, name: &str) -> Option<(String, Self::AccountConfig)> {
self.accounts
.get(name)
.map(|account| (name.to_owned(), account.clone()))
.ok_or_else(|| eyre!("cannot find account {name}"))
}

#[cfg(feature = "wizard")]
async fn from_wizard(path: &std::path::Path) -> Result<Self> {
let config = HimalayaTomlConfig::from_wizard(path).await?;
Ok(Self {
display_name: config.display_name,
signature: config.signature,
signature_delim: config.signature_delim,
downloads_dir: config.downloads_dir,
accounts: config
.accounts
.into_iter()
.map(|(name, config)| {
(
name,
TomlAccountConfig {
default: config.default,
email: config.email,
display_name: config.display_name,
signature: config.signature,
signature_delim: config.signature_delim,
downloads_dir: config.downloads_dir,
backend: config.backend,

#[cfg(feature = "pgp")]
pgp: config.pgp,

folder: config.folder,
envelope: config.envelope,
message: config.message,
template: config.template,

#[cfg(feature = "imap")]
imap: config.imap,
#[cfg(feature = "maildir")]
maildir: config.maildir,
#[cfg(feature = "notmuch")]
notmuch: config.notmuch,
#[cfg(feature = "smtp")]
smtp: config.smtp,
#[cfg(feature = "sendmail")]
sendmail: config.sendmail,
},
)
})
.collect(),
account: config.account,
repl: None,
})
}
}
29 changes: 20 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use email::{
};
use pimalaya_tui::{
cli::tracing,
config::toml::{himalaya::BackendKind, TomlConfig as _},
config::toml::{himalaya::config::BackendKind, TomlConfig as _},
prompt,
};
use reedline::{
Expand All @@ -54,21 +54,26 @@ use crate::{
id_mapper::IdMapper,
};

static COMMANDS: [&str; 11] = [
"help", "select", "unselect", "list", "read", "write", "reply", "forward", "copy", "move",
"delete",
];

#[tokio::main]
async fn main() -> Result<()> {
tracing::install()?;

let cli = Cli::parse();

println!("Welcome to Himalaya REPL!");
println!("Setting up backends…");

let toml_cfg = TomlConfig::from_paths_or_default(&cli.config_paths).await?;
let keybinds = toml_cfg.repl_keybinds().cloned().unwrap_or_default();
let (toml_account_cfg, account_cfg) = toml_cfg.into_account_configs(None)?;
let (toml_account_cfg, account_cfg) = toml_cfg.into_account_configs(cli.account.as_deref())?;

let account_cfg = Arc::new(account_cfg);

println!("Welcome to Himalaya REPL!");
println!("Starting up backends…");

let backend =
BackendBuilder::new(
account_cfg.clone(),
Expand Down Expand Up @@ -129,6 +134,9 @@ async fn main() -> Result<()> {

match mode.read_line(&prompt)? {
Signal::Success(cmd) => match cmd.trim() {
"help" | "h" => {
println!("Available commands: {}", COMMANDS.join(", "));
}
"select" => {
let folders = backend.list_folders().await?.into_iter().map(|f| f.name);
let f = prompt::item("Select a folder:", folders, None)?;
Expand Down Expand Up @@ -314,13 +322,16 @@ struct UnselectedMode(Reedline);

impl UnselectedMode {
pub fn new(keybinds: KeybindsStyle) -> impl DerefMut<Target = Reedline> {
let commands = vec!["create".into(), "list".into(), "select".into()];
let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
let completion_menu = Box::new(ColumnarMenu::default().with_name("completion"));
let completer = Box::new(DefaultCompleter::new_with_wordlen(
COMMANDS.iter().map(ToString::to_string).collect(),
0,
));

let completion = Box::new(ColumnarMenu::default().with_name("completion"));

let reedline = Reedline::create()
.with_completer(completer)
.with_menu(ReedlineMenu::EngineCompleter(completion_menu));
.with_menu(ReedlineMenu::EngineCompleter(completion));

let reedline = match keybinds {
KeybindsStyle::Emacs => {
Expand Down

0 comments on commit 7ca1129

Please sign in to comment.