initial
This commit is contained in:
commit
bdd2e5def9
BIN
.assets/romodoro.gif
Normal file
BIN
.assets/romodoro.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
2029
Cargo.lock
generated
Normal file
2029
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
22
Cargo.toml
Normal file
22
Cargo.toml
Normal file
|
@ -0,0 +1,22 @@
|
|||
[package]
|
||||
name = "romodoro"
|
||||
version = "0.1.1"
|
||||
authors = ["nezsha"]
|
||||
license = "MIT"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.37"
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
crossterm = { version = "0.27.0", features = ["event-stream"] }
|
||||
envy = "0.4.2"
|
||||
futures = "0.3.30"
|
||||
ratatui = "0.26.0"
|
||||
rodio = "0.17.3"
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
soloud = "1.0.5"
|
||||
tokio = { version = "1.35.1", features = ["full"] }
|
||||
tui-big-text = "0.4.2"
|
||||
|
||||
[build-dependencies]
|
||||
dirs = "^5.0"
|
36
README.md
Normal file
36
README.md
Normal file
|
@ -0,0 +1,36 @@
|
|||
# Romodoro
|
||||
|
||||
A faulty pomodoro program
|
||||
|
||||
![Romodoro](.assets/romodoro.gif)
|
||||
|
||||
**Romodoro is a basic TUI application that keeps track of your time with the help of the pomodoro technique.** It follows the original technique by deviding workloads into a set of 4x4 Work and Pause cycles which are then finished off with a long break.
|
||||
|
||||
# Usage
|
||||
|
||||
```
|
||||
Usage: romodoro [OPTIONS]
|
||||
|
||||
Options:
|
||||
-w, --work <WORK> Working length in minutes [default: 25]
|
||||
-p, --pause <PAUSE> The length of a pause [default: 5]
|
||||
-l, --long-pause <LONG_PAUSE> The length of a long pause (after 4 cycles) [default: 20]
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- [x] Adjustable cycle times
|
||||
- [ ] Styling
|
||||
- [ ] Custom sounds (via .config)
|
||||
- [x] Nix flake
|
||||
|
||||
# Develop
|
||||
|
||||
Enter nix shell
|
||||
|
||||
```bash
|
||||
nix develop
|
||||
```
|
26
flake.lock
Normal file
26
flake.lock
Normal file
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1716137900,
|
||||
"narHash": "sha256-sowPU+tLQv8GlqtVtsXioTKeaQvlMz/pefcdwg8MvfM=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "6c0b7a92c30122196a761b440ac0d46d3d9954f1",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-unstable",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
77
flake.nix
Normal file
77
flake.nix
Normal file
|
@ -0,0 +1,77 @@
|
|||
{
|
||||
description = "romodoro builder";
|
||||
|
||||
inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
|
||||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
supportedSystems =
|
||||
[
|
||||
"x86_64-linux"
|
||||
"x86_64-darwin"
|
||||
"aarch64-linux"
|
||||
"aarch64-darwin"
|
||||
];
|
||||
|
||||
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||
|
||||
nixpkgsFor = forAllSystems (system: import nixpkgs {
|
||||
inherit system;
|
||||
});
|
||||
|
||||
in
|
||||
{
|
||||
|
||||
packages = forAllSystems (system:
|
||||
let
|
||||
pkgs = nixpkgsFor.${system};
|
||||
in
|
||||
{
|
||||
default = pkgs.rustPlatform.buildRustPackage {
|
||||
pname = "romodoro";
|
||||
version = "0.1.0";
|
||||
src = ./.;
|
||||
|
||||
nativeBuildInputs = [
|
||||
pkgs.pkg-config
|
||||
pkgs.alsa-oss
|
||||
pkgs.alsa-lib.dev
|
||||
pkgs.alsa-utils
|
||||
pkgs.git
|
||||
pkgs.cmake
|
||||
];
|
||||
buildInputs = [
|
||||
pkgs.alsa-oss
|
||||
pkgs.alsa-lib.dev
|
||||
pkgs.alsa-utils
|
||||
pkgs.git
|
||||
pkgs.pkg-config
|
||||
pkgs.git
|
||||
pkgs.cmake
|
||||
];
|
||||
cargoLock.lockFile = ./Cargo.lock;
|
||||
};
|
||||
});
|
||||
|
||||
devShells.default =
|
||||
with nixpkgs; mkShell
|
||||
{
|
||||
LIBCLANG_PATH = "${pkgs.llvmPackages_17.libclang.lib}/lib";
|
||||
RUST_BACKTRACE = 1;
|
||||
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||
|
||||
packages = with pkgs; [
|
||||
rustc
|
||||
cargo
|
||||
rustfmt
|
||||
rust-analyzer
|
||||
clippy
|
||||
rustup
|
||||
pkg-config
|
||||
openssl
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
175
src/app.rs
Normal file
175
src/app.rs
Normal file
|
@ -0,0 +1,175 @@
|
|||
use std::{
|
||||
error,
|
||||
fs::File,
|
||||
io::BufReader,
|
||||
thread::{self},
|
||||
};
|
||||
|
||||
use chrono::{DateTime, Duration, Local};
|
||||
use rodio::{Decoder, OutputStream, Source};
|
||||
|
||||
pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Session {
|
||||
Work,
|
||||
Pause,
|
||||
LongPause,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct App {
|
||||
pub running: bool,
|
||||
|
||||
pub start: DateTime<Local>,
|
||||
pub session_length: chrono::Duration,
|
||||
pub pause_length: chrono::Duration,
|
||||
pub long_pause_length: chrono::Duration,
|
||||
|
||||
pub current_session: Session,
|
||||
pub sessions: Vec<Session>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(sl: i8, pl: i8, lpl: i8) -> Self {
|
||||
Self {
|
||||
running: true,
|
||||
start: Local::now(),
|
||||
session_length: Duration::minutes(sl as i64),
|
||||
pause_length: Duration::minutes(pl as i64),
|
||||
long_pause_length: Duration::minutes(lpl as i64),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![],
|
||||
}
|
||||
}
|
||||
pub fn tick(&mut self) {
|
||||
// Notification logic...
|
||||
// TODO add ticking sound for the 5 second cooldown
|
||||
if self.current_time_left() <= 0 {
|
||||
thread::spawn(|| {
|
||||
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
|
||||
// TODO add this to the app object instead of opening it every interval (ram)
|
||||
let file = match File::open("./src/assets/melody.mp3") {
|
||||
Ok(file) => file,
|
||||
Err(_) => File::open("~/.config/romodoro/assets/melody.mp3").unwrap(),
|
||||
};
|
||||
|
||||
let file = BufReader::new(file);
|
||||
let source = Decoder::new(file).unwrap();
|
||||
stream_handle.play_raw(source.convert_samples());
|
||||
|
||||
// FIXME increase the sleep based on the length of the sound
|
||||
std::thread::sleep(std::time::Duration::from_secs(5));
|
||||
});
|
||||
|
||||
match self.current_session {
|
||||
Session::Pause => {
|
||||
self.current_session = Session::Work;
|
||||
self.start = Local::now();
|
||||
self.sessions.push(Session::Pause)
|
||||
}
|
||||
Session::Work => {
|
||||
self.start = Local::now();
|
||||
self.sessions.push(Session::Work);
|
||||
|
||||
let works = self
|
||||
.sessions
|
||||
.iter()
|
||||
.filter(|s| s == &&Session::Work)
|
||||
.collect::<Vec<&Session>>()
|
||||
.len();
|
||||
if works % 4 == 0 {
|
||||
self.current_session = Session::LongPause;
|
||||
} else {
|
||||
self.current_session = Session::Pause;
|
||||
}
|
||||
}
|
||||
Session::LongPause => {
|
||||
if self.current_time_left() <= 0 {
|
||||
self.current_session = Session::Work;
|
||||
self.start = Local::now();
|
||||
self.sessions.push(Session::LongPause)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn quit(&mut self) {
|
||||
self.running = false;
|
||||
}
|
||||
|
||||
pub fn current_time_left(&mut self) -> i64 {
|
||||
match self.current_session {
|
||||
Session::Pause => {
|
||||
return (self.pause_length - Duration::seconds(self.current_time_spent()))
|
||||
.num_seconds()
|
||||
}
|
||||
Session::LongPause => {
|
||||
return (self.long_pause_length - Duration::seconds(self.current_time_spent()))
|
||||
.num_seconds()
|
||||
}
|
||||
Session::Work => {
|
||||
return (self.session_length - Duration::seconds(self.current_time_spent()))
|
||||
.num_seconds()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_time_spent(&mut self) -> i64 {
|
||||
(Local::now() - self.start).num_seconds()
|
||||
}
|
||||
pub fn status(&mut self) -> Session {
|
||||
self.current_session.clone()
|
||||
}
|
||||
|
||||
pub fn get_paused_total(&mut self) -> usize {
|
||||
let clone = self.clone();
|
||||
let clone2 = self.clone();
|
||||
|
||||
let short_pauses = clone
|
||||
.sessions
|
||||
.iter()
|
||||
.filter(|t| t == &&Session::Pause)
|
||||
.collect::<Vec<&Session>>();
|
||||
|
||||
let long_pauses = clone2
|
||||
.sessions
|
||||
.iter()
|
||||
.filter(|t| t == &&Session::LongPause)
|
||||
.collect::<Vec<&Session>>();
|
||||
|
||||
let mut saved = short_pauses.len() * self.pause_length.num_seconds() as usize
|
||||
+ long_pauses.len() * self.long_pause_length.num_seconds() as usize;
|
||||
|
||||
if self.current_session == Session::Pause || self.current_session == Session::LongPause {
|
||||
saved += self.current_time_spent() as usize
|
||||
}
|
||||
return saved;
|
||||
}
|
||||
|
||||
pub fn time_worked_in_seconds(&mut self) -> usize {
|
||||
let sessions = self
|
||||
.sessions
|
||||
.iter()
|
||||
.filter(|t| t == &&Session::Work)
|
||||
.collect::<Vec<&Session>>();
|
||||
|
||||
(sessions.len() * self.session_length.num_minutes() as usize * 60) as usize
|
||||
+ (self.current_time_spent()) as usize
|
||||
}
|
||||
pub fn history(&mut self) -> Vec<String> {
|
||||
let stuff = self
|
||||
.clone()
|
||||
.sessions
|
||||
.iter()
|
||||
.map(|a| match a {
|
||||
&Session::Work => return "Work".to_string(),
|
||||
&Session::Pause => return "Pause".to_string(),
|
||||
&Session::LongPause => return "Long pause".to_string(),
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
return stuff;
|
||||
}
|
||||
}
|
BIN
src/assets/melody.mp3
Normal file
BIN
src/assets/melody.mp3
Normal file
Binary file not shown.
88
src/event.rs
Normal file
88
src/event.rs
Normal file
|
@ -0,0 +1,88 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use crossterm::event::{Event as CrosstermEvent, KeyEvent, MouseEvent};
|
||||
use futures::{FutureExt, StreamExt};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::app::AppResult;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Event {
|
||||
Tick,
|
||||
Key(KeyEvent),
|
||||
Mouse(MouseEvent),
|
||||
Resize(u16, u16),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub struct EventHandler {
|
||||
sender: mpsc::UnboundedSender<Event>,
|
||||
receiver: mpsc::UnboundedReceiver<Event>,
|
||||
handler: tokio::task::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
/// Constructs a new instance of [`EventHandler`].
|
||||
pub fn new(tick_rate: u64) -> Self {
|
||||
let tick_rate = Duration::from_millis(tick_rate);
|
||||
let (sender, receiver) = mpsc::unbounded_channel();
|
||||
let _sender = sender.clone();
|
||||
let handler = tokio::spawn(async move {
|
||||
let mut reader = crossterm::event::EventStream::new();
|
||||
let mut tick = tokio::time::interval(tick_rate);
|
||||
loop {
|
||||
let tick_delay = tick.tick();
|
||||
let crossterm_event = reader.next().fuse();
|
||||
tokio::select! {
|
||||
_ = _sender.closed() => {
|
||||
break;
|
||||
}
|
||||
_ = tick_delay => {
|
||||
_sender.send(Event::Tick).unwrap();
|
||||
}
|
||||
Some(Ok(evt)) = crossterm_event => {
|
||||
match evt {
|
||||
CrosstermEvent::Key(key) => {
|
||||
if key.kind == crossterm::event::KeyEventKind::Press {
|
||||
_sender.send(Event::Key(key)).unwrap();
|
||||
}
|
||||
},
|
||||
CrosstermEvent::Mouse(mouse) => {
|
||||
_sender.send(Event::Mouse(mouse)).unwrap();
|
||||
},
|
||||
CrosstermEvent::Resize(x, y) => {
|
||||
_sender.send(Event::Resize(x, y)).unwrap();
|
||||
},
|
||||
CrosstermEvent::FocusLost => {
|
||||
},
|
||||
CrosstermEvent::FocusGained => {
|
||||
},
|
||||
CrosstermEvent::Paste(_) => {
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
Self {
|
||||
sender,
|
||||
receiver,
|
||||
handler,
|
||||
}
|
||||
}
|
||||
|
||||
/// Receive the next event from the handler thread.
|
||||
///
|
||||
/// This function will always block the current thread if
|
||||
/// there is no data available and it's possible for more data to be sent.
|
||||
pub async fn next(&mut self) -> AppResult<Event> {
|
||||
self.receiver
|
||||
.recv()
|
||||
.await
|
||||
.ok_or(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"This is an IO error",
|
||||
)))
|
||||
}
|
||||
}
|
14
src/handler.rs
Normal file
14
src/handler.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use crate::app::{App, AppResult};
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
|
||||
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||
match key_event.code {
|
||||
KeyCode::Char('c') | KeyCode::Char('C') => {
|
||||
if key_event.modifiers == KeyModifiers::CONTROL {
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
14
src/lib.rs
Normal file
14
src/lib.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
/// Application.
|
||||
pub mod app;
|
||||
|
||||
/// Terminal events handler.
|
||||
pub mod event;
|
||||
|
||||
/// Widget renderer.
|
||||
pub mod ui;
|
||||
|
||||
/// Terminal user interface.
|
||||
pub mod tui;
|
||||
|
||||
/// Event handler.
|
||||
pub mod handler;
|
53
src/main.rs
Normal file
53
src/main.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use std::io;
|
||||
|
||||
use clap::Parser;
|
||||
use ratatui::backend::CrosstermBackend;
|
||||
use ratatui::Terminal;
|
||||
use romodoro::app::{App, AppResult};
|
||||
use romodoro::event::{Event, EventHandler};
|
||||
use romodoro::handler::handle_key_events;
|
||||
use romodoro::tui::Tui;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Working length in minutes
|
||||
#[arg(short, long, default_value_t = 25)]
|
||||
work: i8,
|
||||
|
||||
/// The length of a pause
|
||||
#[arg(short, long, default_value_t = 5)]
|
||||
pause: i8,
|
||||
|
||||
/// The length of a long pause (after 4 cycles)
|
||||
#[arg(short, long, default_value_t = 20)]
|
||||
long_pause: i8,
|
||||
}
|
||||
|
||||
mod tests;
|
||||
#[tokio::main]
|
||||
async fn main() -> AppResult<()> {
|
||||
let args = Args::parse();
|
||||
let mut app = App::new(args.work, args.pause, args.long_pause);
|
||||
|
||||
// Initialize the terminal user interface.
|
||||
let backend = CrosstermBackend::new(io::stderr());
|
||||
let terminal = Terminal::new(backend)?;
|
||||
// FIXME return to 250 when audioplay is fixed
|
||||
let events = EventHandler::new(1000);
|
||||
let mut tui = Tui::new(terminal, events);
|
||||
tui.init()?;
|
||||
|
||||
while app.running {
|
||||
tui.draw(&mut app)?;
|
||||
match tui.events.next().await? {
|
||||
Event::Tick => app.tick(),
|
||||
Event::Key(key_event) => handle_key_events(key_event, &mut app)?,
|
||||
Event::Mouse(_) => {}
|
||||
Event::Resize(_, _) => {}
|
||||
}
|
||||
}
|
||||
|
||||
tui.exit()?;
|
||||
Ok(())
|
||||
}
|
162
src/tests/mod.rs
Normal file
162
src/tests/mod.rs
Normal file
|
@ -0,0 +1,162 @@
|
|||
use std::os::unix::thread;
|
||||
|
||||
use chrono::{Duration, Local};
|
||||
use romodoro::app::{App, Session};
|
||||
|
||||
/**
|
||||
* In this test I've set up a basic app that simulates if the user has almost already completed
|
||||
* 4 work cycles. I simulate a tick that should turn the current session into
|
||||
* a long pause since every 4 work sessions there should be a big break.
|
||||
*/
|
||||
#[test]
|
||||
pub fn test_longpause() {
|
||||
// Creating the app object
|
||||
let mut app = App {
|
||||
running: true,
|
||||
|
||||
// Lets say we started this session 61 seconds ago.
|
||||
// So when the app ticks the remeaning time of the current session
|
||||
// should be -1 which means the session is complete
|
||||
// if the logic is right that means the current_session will be LongPause
|
||||
start: Local::now() - Duration::seconds(61),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
],
|
||||
};
|
||||
// Ticking...
|
||||
// Since we started 61 seconds ago, the time left should be 0 meaning its time to complete the
|
||||
// current session
|
||||
// the next shall be a longpause
|
||||
app.tick();
|
||||
|
||||
// Now we check the logic
|
||||
assert_eq!(app.current_session, Session::LongPause);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn calculate_total_minutes_paused() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now(),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![Session::Work, Session::Pause, Session::Work, Session::Pause],
|
||||
};
|
||||
|
||||
assert_eq!(app.get_paused_total(), 120)
|
||||
}
|
||||
#[test]
|
||||
pub fn calculate_total_minutes_worked() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now(),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![Session::Work, Session::Pause, Session::Work, Session::Pause],
|
||||
};
|
||||
|
||||
assert_eq!(app.time_worked_in_seconds(), 120)
|
||||
}
|
||||
#[test]
|
||||
pub fn calculate_total_minutes_paused_with_longpause() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now(),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(5),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
Session::Work,
|
||||
Session::LongPause,
|
||||
Session::Work,
|
||||
// 4 cycles done
|
||||
],
|
||||
};
|
||||
// outcome should be: 2400
|
||||
// (3 * 5 * 60) + (1 * 25 * 60)
|
||||
assert_eq!(app.get_paused_total(), 2400);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_left() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now(),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(app.current_time_left(), 60);
|
||||
}
|
||||
#[test]
|
||||
fn test_time_left_with_minus() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now() - Duration::seconds(60),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(app.current_time_left(), 0);
|
||||
}
|
||||
#[test]
|
||||
fn test_time_spent() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now(),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Work,
|
||||
sessions: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(app.time_worked_in_seconds(), 0);
|
||||
}
|
||||
#[test]
|
||||
pub fn pause() {
|
||||
let mut app = App {
|
||||
running: true,
|
||||
start: Local::now() - Duration::seconds(61),
|
||||
session_length: Duration::minutes(1),
|
||||
pause_length: Duration::minutes(1),
|
||||
long_pause_length: Duration::minutes(25),
|
||||
current_session: Session::Pause,
|
||||
sessions: vec![
|
||||
Session::Work,
|
||||
Session::Pause,
|
||||
Session::Work, //
|
||||
],
|
||||
};
|
||||
// Ticking...
|
||||
app.tick();
|
||||
|
||||
// Now we check the logic
|
||||
assert_eq!(app.current_session, Session::Work);
|
||||
}
|
76
src/tui.rs
Normal file
76
src/tui.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
use crate::app::{App, AppResult};
|
||||
use crate::event::EventHandler;
|
||||
use crate::ui;
|
||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
||||
use ratatui::backend::Backend;
|
||||
use ratatui::Terminal;
|
||||
use std::io;
|
||||
use std::panic;
|
||||
|
||||
/// Representation of a terminal user interface.
|
||||
///
|
||||
/// It is responsible for setting up the terminal,
|
||||
/// initializing the interface and handling the draw events.
|
||||
#[derive(Debug)]
|
||||
pub struct Tui<B: Backend> {
|
||||
/// Interface to the Terminal.
|
||||
terminal: Terminal<B>,
|
||||
/// Terminal event handler.
|
||||
pub events: EventHandler,
|
||||
}
|
||||
|
||||
impl<B: Backend> Tui<B> {
|
||||
/// Constructs a new instance of [`Tui`].
|
||||
pub fn new(terminal: Terminal<B>, events: EventHandler) -> Self {
|
||||
Self { terminal, events }
|
||||
}
|
||||
|
||||
/// Initializes the terminal interface.
|
||||
///
|
||||
/// It enables the raw mode and sets terminal properties.
|
||||
pub fn init(&mut self) -> AppResult<()> {
|
||||
terminal::enable_raw_mode()?;
|
||||
crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableMouseCapture)?;
|
||||
|
||||
// Define a custom panic hook to reset the terminal properties.
|
||||
// This way, you won't have your terminal messed up if an unexpected error happens.
|
||||
let panic_hook = panic::take_hook();
|
||||
panic::set_hook(Box::new(move |panic| {
|
||||
Self::reset().expect("failed to reset the terminal");
|
||||
panic_hook(panic);
|
||||
}));
|
||||
|
||||
self.terminal.hide_cursor()?;
|
||||
self.terminal.clear()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [`Draw`] the terminal interface by [`rendering`] the widgets.
|
||||
///
|
||||
/// [`Draw`]: ratatui::Terminal::draw
|
||||
/// [`rendering`]: crate::ui::render
|
||||
pub fn draw(&mut self, app: &mut App) -> AppResult<()> {
|
||||
self.terminal.draw(|frame| ui::render(app, frame))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resets the terminal interface.
|
||||
///
|
||||
/// This function is also used for the panic hook to revert
|
||||
/// the terminal properties if unexpected errors occur.
|
||||
fn reset() -> AppResult<()> {
|
||||
terminal::disable_raw_mode()?;
|
||||
crossterm::execute!(io::stderr(), LeaveAlternateScreen, DisableMouseCapture)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Exits the terminal interface.
|
||||
///
|
||||
/// It disables the raw mode and reverts back the terminal properties.
|
||||
pub fn exit(&mut self) -> AppResult<()> {
|
||||
Self::reset()?;
|
||||
self.terminal.show_cursor()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
57
src/ui.rs
Normal file
57
src/ui.rs
Normal file
|
@ -0,0 +1,57 @@
|
|||
use crate::app::App;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Offset},
|
||||
style::{Color, Style, Stylize},
|
||||
widgets::{
|
||||
block::{Position, Title},
|
||||
Block, BorderType,
|
||||
},
|
||||
Frame,
|
||||
};
|
||||
use tui_big_text::{BigText, PixelSize};
|
||||
|
||||
pub fn render(app: &mut App, frame: &mut Frame) {
|
||||
let time_worked = app.time_worked_in_seconds() / 60;
|
||||
let time_paused = app.get_paused_total() / 60;
|
||||
|
||||
let current_left = app.current_time_left();
|
||||
|
||||
// TODO add double digit counter to minutes and seconds
|
||||
let minutes = current_left / 60;
|
||||
let seconds = current_left % 60;
|
||||
|
||||
let block = Block::bordered()
|
||||
.title(
|
||||
Title::from(format!(
|
||||
" Worked: {}m Paused: {}m ",
|
||||
time_worked, time_paused
|
||||
))
|
||||
.alignment(Alignment::Center)
|
||||
.position(Position::Top),
|
||||
)
|
||||
.title(
|
||||
Title::from(format!(" {} ", app.history().join(", ")))
|
||||
.alignment(Alignment::Center)
|
||||
.position(Position::Bottom),
|
||||
)
|
||||
.border_type(BorderType::Rounded);
|
||||
|
||||
// FIXME stupid way of calculating the width of the text, but its good enough
|
||||
let counter_text = format!("{}:{}", minutes, seconds);
|
||||
let counter_text_size = format!("{}{}", minutes, seconds).len() * 10;
|
||||
let big_text = BigText::builder()
|
||||
.pixel_size(PixelSize::Full)
|
||||
.style(Style::new().blue())
|
||||
.lines(vec![counter_text.into()])
|
||||
.style(Style::new().fg(Color::White))
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
frame.render_widget(&block, frame.size());
|
||||
let area = block.inner(frame.size());
|
||||
let area = block.inner(frame.size()).offset(Offset {
|
||||
x: ((area.width as usize - counter_text_size) / 2) as i32,
|
||||
y: ((area.height - 5) / 2) as i32,
|
||||
});
|
||||
frame.render_widget(big_text, area);
|
||||
}
|
Loading…
Reference in a new issue