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

Taking ownership of menu in runner #15

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 20 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Rust

on: [push, pull_request]

jobs:
formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Check formatting
run: cargo fmt -- --check

build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Build
run: cargo build
- name: Run Tests
run: cargo test
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn main() {
window.scrollok(true);
noecho();
let mut buffer = [0u8; 64];
let mut r = Runner::new(&ROOT_MENU, &mut buffer, Output(window));
let mut r = Runner::new(ROOT_MENU, &mut buffer, Output(window));
loop {
match r.context.0.getch() {
Some(Input::Character('\n')) => {
Expand Down
63 changes: 33 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#![no_std]
#![deny(missing_docs)]

mod menu_manager;

/// The type of function we call when we enter/exit a menu.
pub type MenuCallbackFn<T> = fn(menu: &Menu<T>, context: &mut T);

Expand Down Expand Up @@ -107,9 +109,8 @@ where
{
buffer: &'a mut [u8],
used: usize,
/// Maximum four levels deep
menus: [Option<&'a Menu<'a, T>>; 4],
depth: usize,
menu_mgr: menu_manager::MenuManager<'a, T>,

/// The context object the `Runner` carries around.
pub context: T,
}
Expand Down Expand Up @@ -231,6 +232,17 @@ enum Outcome {
NeedMore,
}

impl<'a, T> core::clone::Clone for Menu<'a, T> {
fn clone(&self) -> Menu<'a, T> {
Menu {
label: self.label,
items: self.items,
entry: self.entry,
exit: self.exit,
}
}
}

impl<'a, T> Runner<'a, T>
where
T: core::fmt::Write,
Expand All @@ -239,13 +251,12 @@ where
/// buffer that the `Runner` can use. Feel free to pass anything as the
/// `context` type - the only requirement is that the `Runner` can
/// `write!` to the context, which it will do for all text output.
pub fn new(menu: &'a Menu<'a, T>, buffer: &'a mut [u8], mut context: T) -> Runner<'a, T> {
pub fn new(menu: Menu<'a, T>, buffer: &'a mut [u8], mut context: T) -> Runner<'a, T> {
if let Some(cb_fn) = menu.entry {
cb_fn(menu, &mut context);
cb_fn(&menu, &mut context);
}
let mut r = Runner {
menus: [Some(menu), None, None, None],
depth: 0,
menu_mgr: menu_manager::MenuManager::new(menu),
buffer,
used: 0,
context,
Expand All @@ -260,15 +271,13 @@ where
if newline {
writeln!(self.context).unwrap();
}
if self.depth != 0 {
let mut depth = 1;
while depth <= self.depth {
if depth > 1 {
write!(self.context, "/").unwrap();
}
write!(self.context, "/{}", self.menus[depth].unwrap().label).unwrap();
depth += 1;
for i in 0..self.menu_mgr.depth() {
if i > 1 {
write!(self.context, "/").unwrap();
}

let menu = self.menu_mgr.get_menu(Some(i));
write!(self.context, "/{}", menu.label).unwrap();
}
write!(self.context, "> ").unwrap();
}
Expand Down Expand Up @@ -344,7 +353,7 @@ where
// We have a valid string
let mut parts = command_line.split_whitespace();
if let Some(cmd) = parts.next() {
let menu = self.menus[self.depth].unwrap();
let menu = self.menu_mgr.get_menu(None);
if cmd == "help" {
match parts.next() {
Some(arg) => match menu.items.iter().find(|i| i.command == arg) {
Expand All @@ -360,7 +369,7 @@ where
for item in menu.items {
self.print_short_help(&item);
}
if self.depth != 0 {
if self.menu_mgr.depth() != 0 {
self.print_short_help(&Item {
command: "exit",
help: Some("Leave this menu."),
Expand All @@ -374,16 +383,14 @@ where
});
}
}
} else if cmd == "exit" && self.depth != 0 {
if let Some(cb_fn) = self.menus[self.depth].as_ref().unwrap().exit {
} else if cmd == "exit" && self.menu_mgr.depth() != 0 {
if let Some(cb_fn) = menu.exit {
cb_fn(menu, &mut self.context);
}

self.menus[self.depth] = None;
self.depth -= 1;
self.menu_mgr.pop_menu();
} else {
let mut found = false;
for item in menu.items {
for (i, item) in menu.items.iter().enumerate() {
if cmd == item.command {
match item.item_type {
ItemType::Callback {
Expand All @@ -397,15 +404,11 @@ where
item,
command_line,
),
ItemType::Menu(m) => {
self.depth += 1;
self.menus[self.depth] = Some(m);

if let Some(cb_fn) =
self.menus[self.depth].as_ref().unwrap().entry
{
ItemType::Menu(_) => {
if let Some(cb_fn) = self.menu_mgr.get_menu(None).entry {
cb_fn(menu, &mut self.context);
}
self.menu_mgr.push_menu(i);
}
ItemType::_Dummy => {
unreachable!();
Expand Down
70 changes: 70 additions & 0 deletions src/menu_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use super::{ItemType, Menu};

pub struct MenuManager<'a, T> {
menu: Menu<'a, T>,
/// Maximum four levels deep
menu_index: [Option<usize>; 4],
}

impl<'a, T> MenuManager<'a, T> {
pub fn new(menu: Menu<'a, T>) -> Self {
Self {
menu,
menu_index: [None, None, None, None],
}
}

pub fn depth(&self) -> usize {
self.menu_index.iter().take_while(|x| x.is_some()).count()
}

pub fn pop_menu(&mut self) {
if let Some(pos) = self
.menu_index
.iter_mut()
.rev()
.skip_while(|x| x.is_none())
.next()
{
pos.take();
}
}

pub fn push_menu(&mut self, index: usize) {
let menu = self.get_menu(None);
let item = menu.items[index];
if !matches!(item.item_type, ItemType::Menu(_)) {
panic!("Specified index is not a menu");
}

let pos = self
.menu_index
.iter_mut()
.skip_while(|x| x.is_some())
.next()
.unwrap();
pos.replace(index);
}

pub fn get_menu(&self, depth: Option<usize>) -> &Menu<'a, T> {
let mut menu = &self.menu;

let depth = depth.unwrap_or_else(|| self.depth());

for position in self
.menu_index
.iter()
.take_while(|x| x.is_some())
.map(|x| x.unwrap())
.take(depth)
{
if let ItemType::Menu(m) = menu.items[position].item_type {
menu = m
} else {
panic!("Selected item is not a menu");
}
}

menu
}
}