mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-04 05:50:44 +02:00
This commit is contained in:
parent
4e8d58d793
commit
b0fbe3c173
18 changed files with 1724 additions and 1808 deletions
59
src/play.rs
59
src/play.rs
|
|
@ -1,9 +1,11 @@
|
|||
use crate::*;
|
||||
use crate::{*, time::*, lang::*};
|
||||
use ::std::{thread::JoinHandle, time::Duration};
|
||||
#[cfg(feature = "tui")] use ::crossterm::event::poll;
|
||||
|
||||
#[derive(Clone)] pub struct Exit(Arc<AtomicBool>);
|
||||
|
||||
impl Exit {
|
||||
pub fn run <T> (run: impl Fn()->Usually<T>) -> Usually<T> {
|
||||
pub fn run <T> (run: impl Fn(Self)->Usually<T>) -> Usually<T> {
|
||||
run(Self(Arc::new(AtomicBool::new(false))))
|
||||
}
|
||||
}
|
||||
|
|
@ -34,7 +36,7 @@ impl Thread {
|
|||
})
|
||||
}
|
||||
|
||||
/// Spawn a TUI thread that runs `callt least one, then repeats
|
||||
/// Spawn a thread that runs `call` least one, then repeats
|
||||
/// until `exit`, sleeping for `time` msec after every iteration.
|
||||
pub fn new_sleep <F> (
|
||||
exit: Arc<AtomicBool>, time: Duration, call: F
|
||||
|
|
@ -44,9 +46,9 @@ impl Thread {
|
|||
Self::new(exit, move |perf| { let _ = call(perf); std::thread::sleep(time); })
|
||||
}
|
||||
|
||||
/// Spawn a TUI thread that runs `callt least one, then repeats
|
||||
/// until `exit`, using polling to run every `time` msec.
|
||||
pub fn new_poll <F> (
|
||||
/// Spawn a thread that uses [crossterm::event::poll]
|
||||
/// to run `call` every `time` msec.
|
||||
#[cfg(feature = "tui")]pub fn new_poll <F> (
|
||||
exit: Arc<AtomicBool>, time: Duration, call: F
|
||||
) -> Result<Self, std::io::Error>
|
||||
where F: Fn(&PerfModel)->() + Send + Sync + 'static
|
||||
|
|
@ -98,48 +100,3 @@ impl Thread {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl<S, T: Action<S>> Action<S> for Option<T> { fn action (&self, _: &mut S) -> Perhaps<Self> { Ok(None) } }
|
||||
|
||||
impl_default!(PerfModel: Self {
|
||||
enabled: true,
|
||||
clock: quanta::Clock::new(),
|
||||
used: Default::default(),
|
||||
window: Default::default(),
|
||||
});
|
||||
|
||||
impl PerfModel {
|
||||
pub fn get_t0 (&self) -> Option<u64> {
|
||||
if self.enabled {
|
||||
Some(self.clock.raw())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn get_t1 (&self, t0: Option<u64>) -> Option<std::time::Duration> {
|
||||
if let Some(t0) = t0 {
|
||||
if self.enabled {
|
||||
Some(self.clock.delta(t0, self.clock.raw()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn update (&self, t0: Option<u64>, microseconds: f64) {
|
||||
if let Some(t0) = t0 {
|
||||
let t1 = self.clock.raw();
|
||||
self.used.store(self.clock.delta_as_nanos(t0, t1) as f64, Relaxed);
|
||||
self.window.store(microseconds, Relaxed,);
|
||||
}
|
||||
}
|
||||
pub fn percentage (&self) -> Option<f64> {
|
||||
let window = self.window.load(Relaxed) * 1000.0;
|
||||
if window > 0.0 {
|
||||
let used = self.used.load(Relaxed);
|
||||
Some(100.0 * used / window)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue