enchantments:

now double digit counter, but its code is janky needs fix
melody has now became an option, by default it doesn not play it
use -m /path/to/melody.mp3
flake changes
This commit is contained in:
2005 2024-05-22 00:24:51 +02:00
parent dce4e6284d
commit 5dc057f624
6 changed files with 50 additions and 14 deletions

View file

@ -26,7 +26,7 @@ Options:
- [ ] Styling
- [ ] Custom sounds (via .config)
- [x] Nix flake
- [ ] double digit counter for number below 10
- [x] double digit counter for number below 10
# Develop

View file

@ -50,11 +50,6 @@
pkgs.cmake
];
cargoLock.lockFile = ./Cargo.lock;
installPhase = ''
# copy the sound file
mkdir -p $out
cp $src/src/assets/melody.mp3 $out
'';
};
});

View file

@ -28,10 +28,11 @@ pub struct App {
pub current_session: Session,
pub sessions: Vec<Session>,
pub melody_path: String
}
impl App {
pub fn new(sl: i8, pl: i8, lpl: i8) -> Self {
pub fn new(sl: i8, pl: i8, lpl: i8, mldpth: String) -> Self {
Self {
running: true,
start: Local::now(),
@ -39,16 +40,22 @@ impl App {
pause_length: Duration::minutes(pl as i64),
long_pause_length: Duration::minutes(lpl as i64),
current_session: Session::Work,
melody_path: mldpth,
sessions: vec![],
}
}
pub fn tick(&mut self) {
// Notification logic...
let melody_path = self.melody_path.clone();
if self.current_time_left() <= 0 {
thread::spawn( || {
// play the melody
if !melody_path.is_empty(){
thread::spawn( move || {
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("./melody.mp3") {
match File::open(melody_path) {
Ok(file) => {
let file = BufReader::new(file);
let source = Decoder::new(file).unwrap();
@ -61,6 +68,7 @@ impl App {
// 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 => {

View file

@ -22,13 +22,18 @@ struct Args {
/// The length of a long pause (after 4 cycles)
#[arg(short, long, default_value_t = 20)]
long_pause: i8,
#[arg(short, long, default_value="")]
melody_path: String
}
mod tests;
#[tokio::main]
async fn main() -> AppResult<()> {
let args = Args::parse();
let mut app = App::new(args.work, args.pause, args.long_pause);
let mut args = Args::parse();
let mut app = App::new(args.work, args.pause, args.long_pause, args.melody_path);
// Initialize the terminal user interface.
let backend = CrosstermBackend::new(io::stderr());

View file

@ -23,6 +23,7 @@ pub fn test_longpause() {
pause_length: Duration::minutes(1),
long_pause_length: Duration::minutes(25),
current_session: Session::Work,
melody_path: "".into(),
sessions: vec![
Session::Work,
Session::Pause,
@ -50,6 +51,7 @@ pub fn calculate_total_minutes_paused() {
session_length: Duration::minutes(1),
pause_length: Duration::minutes(1),
long_pause_length: Duration::minutes(25),
melody_path: "".into(),
current_session: Session::Work,
sessions: vec![Session::Work, Session::Pause, Session::Work, Session::Pause],
};
@ -63,6 +65,7 @@ pub fn calculate_total_minutes_worked() {
start: Local::now(),
session_length: Duration::minutes(1),
pause_length: Duration::minutes(1),
melody_path: "".into(),
long_pause_length: Duration::minutes(25),
current_session: Session::Work,
sessions: vec![Session::Work, Session::Pause, Session::Work, Session::Pause],
@ -78,6 +81,7 @@ pub fn calculate_total_minutes_paused_with_longpause() {
session_length: Duration::minutes(1),
pause_length: Duration::minutes(5),
long_pause_length: Duration::minutes(25),
melody_path: "".into(),
current_session: Session::Work,
sessions: vec![
Session::Work,
@ -103,6 +107,7 @@ fn test_time_left() {
running: true,
start: Local::now(),
session_length: Duration::minutes(1),
melody_path: "".into(),
pause_length: Duration::minutes(1),
long_pause_length: Duration::minutes(25),
current_session: Session::Work,
@ -118,6 +123,7 @@ fn test_time_left_with_minus() {
start: Local::now() - Duration::seconds(60),
session_length: Duration::minutes(1),
pause_length: Duration::minutes(1),
melody_path: "".into(),
long_pause_length: Duration::minutes(25),
current_session: Session::Work,
sessions: vec![],
@ -133,6 +139,7 @@ fn test_time_spent() {
session_length: Duration::minutes(1),
pause_length: Duration::minutes(1),
long_pause_length: Duration::minutes(25),
melody_path: "".into(),
current_session: Session::Work,
sessions: vec![],
};
@ -148,6 +155,7 @@ pub fn pause() {
pause_length: Duration::minutes(1),
long_pause_length: Duration::minutes(25),
current_session: Session::Pause,
melody_path: "".into(),
sessions: vec![
Session::Work,
Session::Pause,

View file

@ -16,9 +16,26 @@ pub fn render(app: &mut App, frame: &mut Frame) {
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;
// TODO a better implementation of this..... sorry i wanted it quick and dirty
let minutes_str: String;
if minutes <= 9 {
minutes_str = format!("0{}", minutes.to_string())
}else {
minutes_str = minutes.to_string()
};
let seconds_str: String;
if seconds <= 9 {
seconds_str = format!("0{}", seconds.to_string())
}else {
seconds_str = seconds.to_string()
};
let block = Block::bordered()
.title(
@ -36,9 +53,12 @@ pub fn render(app: &mut App, frame: &mut Frame) {
)
.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 counter_text = format!("{}:{}", minutes_str, seconds_str);
let counter_text_size = format!("{}{}", minutes_str, seconds_str).len() * 10;
let big_text = BigText::builder()
.pixel_size(PixelSize::Full)
.style(Style::new().blue())