Skip to content

Commit

Permalink
only create file if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
xasopheno committed Feb 16, 2023
1 parent b29c426 commit 821c1cc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn new_socool_file(filename: String, working_path: PathBuf) -> Result<(), Error>
Ok(())
}

const DEFAULT_SOCOOL: &str = indoc! {"
pub const DEFAULT_SOCOOL: &str = indoc! {"
{ f: 311.127, l: 1, g: 1/3, p: 0 }
thing1 = {
Expand Down
28 changes: 28 additions & 0 deletions src/play.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::new::DEFAULT_SOCOOL;
use crate::watch::watch;
use crate::Error;
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
Expand Down Expand Up @@ -57,6 +59,7 @@ pub fn play_once(filename: String, working_path: PathBuf) -> Result<(), Error> {
}

fn play_watch(filename: String, working_path: PathBuf) -> Result<(), Error> {
maybe_create_file_if_needed(filename.clone(), working_path.clone());
let render_manager = Arc::new(Mutex::new(RenderManager::init(None, None, false, None)));
let render_voices = prepare_render_outside(Filename(&filename), Some(working_path.clone()))?;
render_manager
Expand All @@ -69,3 +72,28 @@ fn play_watch(filename: String, working_path: PathBuf) -> Result<(), Error> {
std::thread::park();
Ok(())
}

pub fn maybe_create_file_if_needed(filename: String, working_path: PathBuf) {
let mut input = String::new();
let path = working_path.join(format!("{filename}"));
if !path.exists() {
println!("This file does not exist. Create it? (y/n)");

io::stdin()
.read_line(&mut input)
.expect("Failed to read input");

let input = input.trim();

match input.as_ref() {
"y" => {
std::fs::write(path, DEFAULT_SOCOOL).expect("Unable to write file");
}
"n" => {
println!("Aborting");
std::process::exit(0);
}
_ => println!("Invalid input"),
}
}
}
5 changes: 3 additions & 2 deletions src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ pub fn watch(
render_manager: Arc<Mutex<RenderManager>>,
) -> Result<(), Error> {
were_so_cool_logo(Some("Watching"), Some(filename.clone()));

let path = Path::new(&working_path).join(Path::new(&filename));

std::thread::spawn(move || -> Result<(), Error> {
loop {
let (tx, rx) = channel();

let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap();

let path = Path::new(&working_path).join(Path::new(&filename));

watcher.watch(path.as_ref(), RecursiveMode::NonRecursive)?;

if let Ok(_event) = rx.recv() {
Expand Down
4 changes: 2 additions & 2 deletions todo/release.socool
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

# Bugs
[x] Fix panic that happens when the piece doesn't render the first time on watch.
[] --volume, --samplerate
pass in settings/rate on render_manager creation
[ ] pass in settings/rate on render_manager creation
[ ] --volume, --samplerate

# Simplify
[x] Move core into own package
Expand Down

0 comments on commit 821c1cc

Please sign in to comment.