Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabus1184 committed Nov 15, 2023
1 parent e38efc6 commit ce2c08a
Show file tree
Hide file tree
Showing 17 changed files with 798 additions and 818 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]

# general
itertools = "0.11.0"
itertools = "0.12.0"
log = "0.4.19"
simplelog = "0.12.1"
serde = { version = "1.0.181", features = ["derive", "rc"] }
Expand Down
4 changes: 2 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config::Config, decoder, song::Song};
use crate::{config::Config, song::Song};
use std::{
collections::HashMap,
fs::Metadata,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Cache {
trace!("Found file {}", e.path().display());
})
.filter_map(|(e, m)| {
decoder::song_from_file(e.path())
Song::load(e.path())
.map(|s| (e.path().to_path_buf(), m, s))
.map_err(|e| {
warn!("Failed to read song from {:?}: {}", e, e);
Expand Down
206 changes: 0 additions & 206 deletions src/decoder.rs

This file was deleted.

50 changes: 10 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use std::{
fs::File,
io::{Read, Write},
sync::{atomic::Ordering, Arc},
};
use std::{fs::File, sync::Arc};

use anyhow::Context;
use cache::Cache;
use log::{info, trace, warn, LevelFilter};
use player::Player;
use simplelog::{CombinedLogger, WriteLogger};

use crate::{config::Config, tui::tui};
use crate::{config::Config, player::Player, tui::tui};

mod cache;
mod config;
mod decoder;
mod player;
mod song;
mod tui;

fn main() {
fn main() -> anyhow::Result<()> {
let config_dir = dirs::config_dir()
.expect("Unable to find config directory")
.join("ramp");
Expand All @@ -42,7 +37,7 @@ fn main() {
}),
);

let _logger = CombinedLogger::init(vec![WriteLogger::new(
CombinedLogger::init(vec![WriteLogger::new(
#[cfg(debug_assertions)]
LevelFilter::Trace,
#[cfg(not(debug_assertions))]
Expand All @@ -55,19 +50,8 @@ fn main() {
.build(),
File::create(&config.log_path).expect("Failed to create log file"),
)])
.expect("Failed to initialize logger");

let quit = Arc::new(std::sync::atomic::AtomicBool::new(false));
let mut f = File::open(&config.log_path).expect("Failed to open log file");
let _quit = quit.clone();
let handle = std::thread::spawn(move || {
while !_quit.load(Ordering::SeqCst) {
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
std::io::stdout().write_all(&buf).unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
}
});
.context("Failed to initialize logger")?;
info!("Logger initialized");

trace!("loading cache");
let (cache, old_config) = Cache::load(&config).unwrap_or_else(|e| {
Expand Down Expand Up @@ -99,25 +83,11 @@ fn main() {
let cache = Arc::new(cache);

trace!("initializing player");
let player = Player::new(cache.clone()).expect("Failed to initialize player");

{
trace!("attaching media controls");
player
.lock()
.unwrap()
.attach_media_controls()
.unwrap_or_else(|e| {
warn!("Failed to attach media controls: {e:?}");
});
}

quit.store(true, Ordering::SeqCst);
handle.join().expect("Failed to join log thread");
let (cmd, player) = Player::run(cache.clone()).context("Failed to initialize player")?;

trace!("entering tui");
tui(config.clone(), cache.clone(), player.clone()).expect("Failed to run tui");
tui(config.clone(), cache.clone(), cmd, player).context("Error in tui")?;
trace!("tui exited");

trace!("quitting");
Ok(())
}
Loading

0 comments on commit ce2c08a

Please sign in to comment.