Skip to content

Commit 85f8ac3

Browse files
authored
Merge pull request #18 from rust-embedded-community/feature/borrowed-context
Removing ownership of menu context
2 parents be64eea + 5e33c7f commit 85f8ac3

File tree

4 files changed

+186
-117
lines changed

4 files changed

+186
-117
lines changed

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
## [Unreleased](https://github.com/rust-embedded-community/menu/compare/v0.4.0...master)
9+
10+
### Changed
11+
* [breaking] The `menu` `Context` is now borrowed during I/O input processing to support borrowed data
12+
* [breaking] The `pub context` item on the runner was updated to `pub interface`
13+
14+
## [v0.4.0] - 2023-09-16
15+
16+
Note: Changes before this version were not tracked via the CHANGELOG
17+
18+
### Changed
19+
* Changed the struct `Runner` to own the struct `Menu` instead of borrowing it
20+
21+
### Added
22+
23+
* Made struct `Menu` implement `Clone`
24+
* Add the possibility to disable local echo (via `echo` feature, enabled by default)
25+
26+
[v0.4.0]: https://github.com/rust-embedded-community/menu/releases/tag/v0.4.0

examples/simple.rs

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ use menu::*;
44
use pancurses::{endwin, initscr, noecho, Input};
55
use std::fmt::Write;
66

7-
const ROOT_MENU: Menu<Output> = Menu {
7+
#[derive(Default)]
8+
struct Context {
9+
_inner: u32,
10+
}
11+
12+
const ROOT_MENU: Menu<Output, Context> = Menu {
813
label: "root",
914
items: &[
1015
&Item {
@@ -94,91 +99,111 @@ fn main() {
9499
window.scrollok(true);
95100
noecho();
96101
let mut buffer = [0u8; 64];
97-
let mut r = Runner::new(ROOT_MENU, &mut buffer, Output(window));
102+
let mut context = Context::default();
103+
let mut r = Runner::new(ROOT_MENU, &mut buffer, Output(window), &mut context);
98104
loop {
99-
match r.context.0.getch() {
105+
match r.interface.0.getch() {
100106
Some(Input::Character('\n')) => {
101-
r.input_byte(b'\r');
107+
r.input_byte(b'\r', &mut context);
102108
}
103109
Some(Input::Character(c)) => {
104110
let mut buf = [0; 4];
105111
for b in c.encode_utf8(&mut buf).bytes() {
106-
r.input_byte(b);
112+
r.input_byte(b, &mut context);
107113
}
108114
}
109115
Some(Input::KeyDC) => break,
110116
Some(input) => {
111-
r.context.0.addstr(&format!("{:?}", input));
117+
r.interface.0.addstr(&format!("{:?}", input));
112118
}
113119
None => (),
114120
}
115121
}
116122
endwin();
117123
}
118124

119-
fn enter_root(_menu: &Menu<Output>, context: &mut Output) {
120-
writeln!(context, "In enter_root").unwrap();
125+
fn enter_root(_menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
126+
writeln!(interface, "In enter_root").unwrap();
121127
}
122128

123-
fn exit_root(_menu: &Menu<Output>, context: &mut Output) {
124-
writeln!(context, "In exit_root").unwrap();
129+
fn exit_root(_menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
130+
writeln!(interface, "In exit_root").unwrap();
125131
}
126132

127-
fn select_foo<'a>(_menu: &Menu<Output>, item: &Item<Output>, args: &[&str], context: &mut Output) {
128-
writeln!(context, "In select_foo. Args = {:?}", args).unwrap();
133+
fn select_foo(
134+
_menu: &Menu<Output, Context>,
135+
item: &Item<Output, Context>,
136+
args: &[&str],
137+
interface: &mut Output,
138+
_context: &mut Context,
139+
) {
140+
writeln!(interface, "In select_foo. Args = {:?}", args).unwrap();
129141
writeln!(
130-
context,
142+
interface,
131143
"a = {:?}",
132144
::menu::argument_finder(item, args, "a")
133145
)
134146
.unwrap();
135147
writeln!(
136-
context,
148+
interface,
137149
"b = {:?}",
138150
::menu::argument_finder(item, args, "b")
139151
)
140152
.unwrap();
141153
writeln!(
142-
context,
154+
interface,
143155
"verbose = {:?}",
144156
::menu::argument_finder(item, args, "verbose")
145157
)
146158
.unwrap();
147159
writeln!(
148-
context,
160+
interface,
149161
"level = {:?}",
150162
::menu::argument_finder(item, args, "level")
151163
)
152164
.unwrap();
153165
writeln!(
154-
context,
166+
interface,
155167
"no_such_arg = {:?}",
156168
::menu::argument_finder(item, args, "no_such_arg")
157169
)
158170
.unwrap();
159171
}
160172

161-
fn select_bar<'a>(_menu: &Menu<Output>, _item: &Item<Output>, args: &[&str], context: &mut Output) {
162-
writeln!(context, "In select_bar. Args = {:?}", args).unwrap();
173+
fn select_bar(
174+
_menu: &Menu<Output, Context>,
175+
_item: &Item<Output, Context>,
176+
args: &[&str],
177+
interface: &mut Output,
178+
_context: &mut Context,
179+
) {
180+
writeln!(interface, "In select_bar. Args = {:?}", args).unwrap();
163181
}
164182

165-
fn enter_sub(_menu: &Menu<Output>, context: &mut Output) {
166-
writeln!(context, "In enter_sub").unwrap();
183+
fn enter_sub(_menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
184+
writeln!(interface, "In enter_sub").unwrap();
167185
}
168186

169-
fn exit_sub(_menu: &Menu<Output>, context: &mut Output) {
170-
writeln!(context, "In exit_sub").unwrap();
187+
fn exit_sub(_menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
188+
writeln!(interface, "In exit_sub").unwrap();
171189
}
172190

173-
fn select_baz<'a>(_menu: &Menu<Output>, _item: &Item<Output>, args: &[&str], context: &mut Output) {
174-
writeln!(context, "In select_baz: Args = {:?}", args).unwrap();
191+
fn select_baz(
192+
_menu: &Menu<Output, Context>,
193+
_item: &Item<Output, Context>,
194+
args: &[&str],
195+
interface: &mut Output,
196+
_context: &mut Context,
197+
) {
198+
writeln!(interface, "In select_baz: Args = {:?}", args).unwrap();
175199
}
176200

177-
fn select_quux<'a>(
178-
_menu: &Menu<Output>,
179-
_item: &Item<Output>,
201+
fn select_quux(
202+
_menu: &Menu<Output, Context>,
203+
_item: &Item<Output, Context>,
180204
args: &[&str],
181-
context: &mut Output,
205+
interface: &mut Output,
206+
_context: &mut Context,
182207
) {
183-
writeln!(context, "In select_quux: Args = {:?}", args).unwrap();
208+
writeln!(interface, "In select_quux: Args = {:?}", args).unwrap();
184209
}

0 commit comments

Comments
 (0)