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

add new example: stopwatch #503

Merged
merged 35 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4121212
new example: autocomplete search box
TianyiShi2001 Sep 20, 2020
6578e31
add new example: stopwatch
TianyiShi2001 Sep 21, 2020
586e614
better match function
TianyiShi2001 Sep 22, 2020
5bf442d
fixed size and scrollable
TianyiShi2001 Sep 22, 2020
1a85b73
submit without moving into the `matches` view
TianyiShi2001 Sep 22, 2020
1bd2a64
remove unsed imports
TianyiShi2001 Sep 22, 2020
f1f6bab
isolate the search function
TianyiShi2001 Sep 22, 2020
71d0669
no need to own
TianyiShi2001 Sep 22, 2020
258daca
remove the fixed width
TianyiShi2001 Sep 22, 2020
96a0fbb
working timer
TianyiShi2001 Sep 22, 2020
2fd22a6
this won't work
TianyiShi2001 Sep 22, 2020
bad48c4
laps
TianyiShi2001 Sep 22, 2020
8fd04b6
improve precision
TianyiShi2001 Sep 22, 2020
8262a7f
on_stop is working
TianyiShi2001 Sep 22, 2020
9cff334
fix https://github.com/gyscos/cursive/pull/503#discussion_r492864443
TianyiShi2001 Sep 22, 2020
1521803
show laps optionally
TianyiShi2001 Sep 22, 2020
00c2166
Merge remote-tracking branch 'upstream/main' into stopwatch
TianyiShi2001 Sep 22, 2020
5888427
refactor
TianyiShi2001 Sep 22, 2020
f8f3a8f
more docs
TianyiShi2001 Sep 22, 2020
cc13465
return full data when stop
TianyiShi2001 Sep 23, 2020
03b4f23
reduce funtionality
TianyiShi2001 Sep 23, 2020
3c54ac5
more comments
TianyiShi2001 Sep 23, 2020
2f90337
remove cloning
TianyiShi2001 Sep 23, 2020
8fc4faf
however, this won't work
TianyiShi2001 Sep 23, 2020
ad14207
fixed
TianyiShi2001 Sep 23, 2020
761d738
.
TianyiShi2001 Sep 23, 2020
f954e87
add info of my crate
TianyiShi2001 Sep 23, 2020
da2731e
minimize
TianyiShi2001 Sep 29, 2020
f7ce122
simplify
TianyiShi2001 Oct 9, 2020
8875418
add new example: stopwatch
TianyiShi2001 Sep 21, 2020
4b86afc
Improved the `select` example. (#501)
LunarEclipse363 Sep 21, 2020
7a3d906
Merge branch 'stopwatch' of https://github.com/TianyiShi2001/cursive …
TianyiShi2001 Oct 9, 2020
4d90e36
remove dependency on chrono
TianyiShi2001 Oct 9, 2020
7712beb
Merge branch 'main' of https://github.com/gyscos/cursive into main
TianyiShi2001 Oct 9, 2020
2ff936a
Merge branch 'main' into stopwatch
TianyiShi2001 Oct 9, 2020
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 Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Here are some cool applications using cursive:
* [fui](https://github.com/xliiv/fui): Add CLI & form interface to your program.
* [grin-tui](https://github.com/mimblewimble/grin): Minimal implementation of the MimbleWimble protocol.
* [ripasso](https://github.com/cortex/ripasso): A simple password manager written in Rust.
* [clock-cli](https://github.com/TianyiShi2001/clock-cli-rs): A clock with stopwatch and countdown timer functionalities
* [sudoku-tui](https://github.com/TianyiShi2001/sudoku-tui): Play sudoku on the command line.

## Goals
Expand Down
91 changes: 91 additions & 0 deletions examples/src/bin/stopwatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! A simple stopwatch implementation.
//! Also check out `clock-cli` (https://github.com/TianyiShi2001/cl!ock-cli-rs),
//! which aims to implement a fully-fledged clock with stopwatch, countdown
//! timer, and possibly more functionalities.

use cursive::traits::{Nameable, Resizable};
use cursive::views::{Button, Canvas, Dialog, LinearLayout};
use std::time::{Duration, Instant};

fn main() {
let mut siv = cursive::default();

siv.add_layer(
Dialog::new()
.title("Stopwatch")
.content(
LinearLayout::horizontal()
.child(
Canvas::new(Watch {
last_started: Instant::now(),
last_elapsed: Duration::default(),
running: true,
})
.with_draw(|s, printer| {
printer.print(
(0, 1),
&format!("{:.2?}", s.elapsed()),
);
})
.with_name("stopwatch")
.fixed_size((8, 3)),
)
.child(
LinearLayout::vertical()
.child(Button::new("Start", run(Watch::start)))
.child(Button::new("Pause", run(Watch::pause)))
.child(Button::new("Stop", run(Watch::stop))),
),
)
.button("Quit", |s| s.quit())
.h_align(cursive::align::HAlign::Center),
);

siv.set_fps(20);

siv.run();
}

struct Watch {
last_started: Instant,
last_elapsed: Duration,
running: bool,
}

impl Watch {
fn start(&mut self) {
self.running = true;
self.last_started = Instant::now();
}

fn elapsed(&self) -> Duration {
self.last_elapsed
+ if self.running {
Instant::now() - self.last_started
} else {
Duration::default()
}
}

fn pause(&mut self) {
self.last_elapsed = self.elapsed();
self.running = false;
}

fn stop(&mut self) {
self.running = false;
self.last_elapsed = Duration::default();
}
}

// Helper function to find the stopwatch view and run a closure on it.
fn run<F>(f: F) -> impl Fn(&mut cursive::Cursive)
where
F: Fn(&mut Watch),
{
move |s| {
s.call_on_name("stopwatch", |c: &mut Canvas<Watch>| {
f(c.state_mut());
});
}
}