Skip to content

Commit

Permalink
Implement dialog mode
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l committed Dec 6, 2020
1 parent ad088b6 commit abbd722
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/command.rs

This file was deleted.

22 changes: 20 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use structopt::{clap::ArgGroup, StructOpt};

pub use desktop::Entry as DesktopEntry;

mod command;
mod config;
mod desktop;
mod draw;
mod input;
mod mode;
mod state;
mod surface;

Expand Down Expand Up @@ -69,6 +69,20 @@ struct Args {
log_file: Option<PathBuf>,
#[structopt(long)]
config_file: Option<PathBuf>,
#[structopt(subcommand)]
mode: Option<ModeArg>,
}

#[derive(StructOpt)]
enum ModeArg {
Apps,
Dialog,
}

impl Default for ModeArg {
fn default() -> Self {
ModeArg::Apps
}
}

fn main() {
Expand Down Expand Up @@ -98,7 +112,11 @@ fn main() {
.quick_insert(event_loop.handle())
.unwrap();

let cmd = command::apps::AppsCommand::new(desktop::find_entries(), config.terminal_command());
let cmd = match args.mode.take().unwrap_or_default() {
ModeArg::Apps => mode::Mode::apps(desktop::find_entries(), config.terminal_command()),
ModeArg::Dialog => mode::Mode::dialog(),
};

let mut state = state::State::new(cmd);

loop {
Expand Down
49 changes: 49 additions & 0 deletions src/mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::ffi::CString;

use crate::DesktopEntry;

mod apps;
mod dialog;

macro_rules! delegate {
(pub fn $name:ident ( &mut self ) -> $ret:ty $(, wrap_with ($wrap:path))?) => {
delegate!(pub fn $name ( & [mut] self, ) -> $ret $(, wrap_with ($wrap))?);
};
(pub fn $name:ident ( &mut self, $($ident:ident : $tp:ty),* ) -> $ret:ty $(, wrap_with ($wrap:path))?) => {
delegate!(pub fn $name ( & [mut] self, $($ident : $tp),* ) -> $ret $(, wrap_with ($wrap))?);
};
(pub fn $name:ident ( & $([$m:ident])? self ) -> $ret:ty $(, wrap_with ($wrap:path))?) => {
delegate!(pub fn $name ( & $([$m])? self, ) -> $ret $(, wrap_with ($wrap))?);
};
(pub fn $name:ident ( & $([$m:ident])? self, $($ident:ident : $tp:ty),* ) -> $ret:ty $(, wrap_with ($wrap:path))?) => {
pub fn $name ( & $($m)? self, $($ident : $tp),* ) -> $ret {
match self {
Mode::AppsMode(mode) => $($wrap)?(mode.$name($($ident),*)),
Mode::DialogMode(mode) => $($wrap)?(mode.$name($($ident),*)),
}
}
}
}

pub enum Mode {
AppsMode(apps::AppsMode),
DialogMode(dialog::DialogMode),
}

impl Mode {
pub fn apps(entries: Vec<DesktopEntry>, term: Vec<CString>) -> Self {
Self::AppsMode(apps::AppsMode::new(entries, term))
}

pub fn dialog() -> Self {
Self::DialogMode(dialog::DialogMode::new())
}

delegate!(pub fn eval(&mut self, idx: usize) -> std::convert::Infallible);
delegate!(pub fn entries_len(&self) -> usize);
delegate!(pub fn list_item(&self, idx: usize) -> crate::draw::ListItem<'_>);
delegate!(
pub fn text_entries(&self) -> Box<dyn Iterator<Item = &str> + '_>,
wrap_with(Box::new)
);
}
4 changes: 2 additions & 2 deletions src/command/apps.rs → src/mode/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::ffi::CString;
use crate::draw::ListItem;
use crate::DesktopEntry;

pub struct AppsCommand {
pub struct AppsMode {
entries: Vec<DesktopEntry>,
term: Vec<CString>,
}

impl AppsCommand {
impl AppsMode {
pub fn new(entries: Vec<DesktopEntry>, term: Vec<CString>) -> Self {
Self { entries, term }
}
Expand Down
40 changes: 40 additions & 0 deletions src/mode/dialog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::io::{BufRead, BufReader};

use crate::draw::ListItem;

pub struct DialogMode {
lines: Vec<String>,
}

impl DialogMode {
pub fn new() -> Self {
let stdin = std::io::stdin();
let rdr = stdin.lock();

Self {
lines: BufReader::new(rdr)
.lines()
.collect::<Result<_, _>>()
.expect("Failed to read stdin"),
}
}

pub fn eval(&mut self, idx: usize) -> std::convert::Infallible {
println!("{}", &self.lines[idx]);
std::process::exit(0);
}

pub fn entries_len(&self) -> usize {
self.lines.len()
}

pub fn list_item(&self, idx: usize) -> ListItem<'_> {
ListItem {
name: self.lines[idx].as_str(),
}
}

pub fn text_entries(&self) -> impl Iterator<Item = &str> {
self.lines.iter().map(|e| e.as_str())
}
}
6 changes: 3 additions & 3 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use fuse_rust::SearchResult;

use crate::command::apps::AppsCommand;
use crate::draw::ListItem;
use crate::input::KeyPress;
use crate::mode::Mode;

pub struct State {
input_buf: String,
selected_item: usize,
processed_entries: Vec<SearchResult>,
inner: AppsCommand,
inner: Mode,
}

impl State {
pub fn new(inner: AppsCommand) -> Self {
pub fn new(inner: Mode) -> Self {
Self {
input_buf: String::new(),
selected_item: 0,
Expand Down

0 comments on commit abbd722

Please sign in to comment.