Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async implementation #14

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ echo = []

[dev-dependencies]
pancurses = "0.16"
tokio = { version = "1", features = ["full"] }
187 changes: 123 additions & 64 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(async_fn_in_trait)]

extern crate menu;

use menu::*;
Expand All @@ -9,7 +11,7 @@ const ROOT_MENU: Menu<Output> = Menu {
items: &[
&Item {
item_type: ItemType::Callback {
function: select_foo,
handler: &BoxedHandler(FooItemHandler),
parameters: &[
Parameter::Mandatory {
parameter_name: "a",
Expand Down Expand Up @@ -42,7 +44,7 @@ It contains multiple paragraphs and should be preceeded by the parameter list.
},
&Item {
item_type: ItemType::Callback {
function: select_bar,
handler: &BoxedHandler(BarHandler),
parameters: &[],
},
command: "bar",
Expand All @@ -54,56 +56,66 @@ It contains multiple paragraphs and should be preceeded by the parameter list.
items: &[
&Item {
item_type: ItemType::Callback {
function: select_baz,
handler: &BoxedHandler(BazHandler),
parameters: &[],
},
command: "baz",
help: Some("thingamobob a baz"),
},
&Item {
item_type: ItemType::Callback {
function: select_quux,
handler: &BoxedHandler(QuuxHandler),
parameters: &[],
},
command: "quux",
help: Some("maximum quux"),
},
],
entry: Some(enter_sub),
exit: Some(exit_sub),
handler: Some(&BoxedHandler(SubMenuHandler)),
}),
command: "sub",
help: Some("enter sub-menu"),
},
],
entry: Some(enter_root),
exit: Some(exit_root),
handler: Some(&BoxedHandler(RootMenuHandler)),
};

struct Output(pancurses::Window);

impl Output {
async fn wait_for_q(&self) {
loop {
if let Some(Input::Character('q')) = self.0.getch() {
break;
}
}
}
}

impl std::fmt::Write for Output {
fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {
self.0.printw(s);
Ok(())
}
}

fn main() {
#[tokio::main]
async fn main() {
let window = initscr();
window.timeout(100);
window.scrollok(true);
noecho();
let mut buffer = [0u8; 64];
let mut r = Runner::new(&ROOT_MENU, &mut buffer, Output(window));
loop {
match r.context.0.getch() {
Some(Input::Character('\n')) => {
r.input_byte(b'\r');
r.input_byte(b'\r').await;
}
Some(Input::Character(c)) => {
let mut buf = [0; 4];
for b in c.encode_utf8(&mut buf).bytes() {
r.input_byte(b);
r.input_byte(b).await;
}
}
Some(Input::KeyDC) => break,
Expand All @@ -116,69 +128,116 @@ fn main() {
endwin();
}

fn enter_root(_menu: &Menu<Output>, context: &mut Output) {
writeln!(context, "In enter_root").unwrap();
}
struct RootMenuHandler;

fn exit_root(_menu: &Menu<Output>, context: &mut Output) {
writeln!(context, "In exit_root").unwrap();
}
impl MenuHandler<Output> for RootMenuHandler {
async fn entry(&self, _menu: &Menu<'_, Output>, context: &mut Output) {
writeln!(context, "In enter_root").unwrap();
}

fn select_foo<'a>(_menu: &Menu<Output>, item: &Item<Output>, args: &[&str], context: &mut Output) {
writeln!(context, "In select_foo. Args = {:?}", args).unwrap();
writeln!(
context,
"a = {:?}",
::menu::argument_finder(item, args, "a")
)
.unwrap();
writeln!(
context,
"b = {:?}",
::menu::argument_finder(item, args, "b")
)
.unwrap();
writeln!(
context,
"verbose = {:?}",
::menu::argument_finder(item, args, "verbose")
)
.unwrap();
writeln!(
context,
"level = {:?}",
::menu::argument_finder(item, args, "level")
)
.unwrap();
writeln!(
context,
"no_such_arg = {:?}",
::menu::argument_finder(item, args, "no_such_arg")
)
.unwrap();
async fn exit(&self, _menu: &Menu<'_, Output>, context: &mut Output) {
writeln!(context, "In exit_root").unwrap();
}
}

fn select_bar<'a>(_menu: &Menu<Output>, _item: &Item<Output>, args: &[&str], context: &mut Output) {
writeln!(context, "In select_bar. Args = {:?}", args).unwrap();
struct FooItemHandler;

impl ItemHandler<Output> for FooItemHandler {
async fn handle(
&self,
_menu: &Menu<'_, Output>,
item: &Item<'_, Output>,
args: &[&str],
context: &mut Output,
) {
writeln!(context, "In select_foo. Args = {:?}", args).unwrap();
writeln!(
context,
"a = {:?}",
::menu::argument_finder(item, args, "a")
)
.unwrap();
writeln!(
context,
"b = {:?}",
::menu::argument_finder(item, args, "b")
)
.unwrap();
writeln!(
context,
"verbose = {:?}",
::menu::argument_finder(item, args, "verbose")
)
.unwrap();
writeln!(
context,
"level = {:?}",
::menu::argument_finder(item, args, "level")
)
.unwrap();
writeln!(
context,
"no_such_arg = {:?}",
::menu::argument_finder(item, args, "no_such_arg")
)
.unwrap();

writeln!(context, "Press 'q' to exit this handler").unwrap();

context.wait_for_q().await;
}
}

fn enter_sub(_menu: &Menu<Output>, context: &mut Output) {
writeln!(context, "In enter_sub").unwrap();
struct BarHandler;

impl ItemHandler<Output> for BarHandler {
async fn handle(
&self,
_menu: &Menu<'_, Output>,
_item: &Item<'_, Output>,
args: &[&str],
context: &mut Output,
) {
writeln!(context, "In select_bar. Args = {:?}", args).unwrap();
}
}

fn exit_sub(_menu: &Menu<Output>, context: &mut Output) {
writeln!(context, "In exit_sub").unwrap();
struct SubMenuHandler;

impl MenuHandler<Output> for SubMenuHandler {
async fn entry(&self, _menu: &Menu<'_, Output>, context: &mut Output) {
writeln!(context, "In enter_sub").unwrap();
}

async fn exit(&self, _menu: &Menu<'_, Output>, context: &mut Output) {
writeln!(context, "In exit_sub").unwrap();
}
}

fn select_baz<'a>(_menu: &Menu<Output>, _item: &Item<Output>, args: &[&str], context: &mut Output) {
writeln!(context, "In select_baz: Args = {:?}", args).unwrap();
struct BazHandler;

impl ItemHandler<Output> for BazHandler {
async fn handle(
&self,
_menu: &Menu<'_, Output>,
_item: &Item<'_, Output>,
args: &[&str],
context: &mut Output,
) {
writeln!(context, "In select_baz: Args = {:?}", args).unwrap();
}
}

fn select_quux<'a>(
_menu: &Menu<Output>,
_item: &Item<Output>,
args: &[&str],
context: &mut Output,
) {
writeln!(context, "In select_quux: Args = {:?}", args).unwrap();
struct QuuxHandler;

impl ItemHandler<Output> for QuuxHandler {
async fn handle(
&self,
_menu: &Menu<'_, Output>,
_item: &Item<'_, Output>,
args: &[&str],
context: &mut Output,
) {
writeln!(context, "In select_quux: Args = {:?}", args).unwrap();
}
}
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
Loading