-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
[package] | ||
name = "turnout-for-what" | ||
version = "0.1.0" | ||
authors = ["John Wrenn <[email protected]>"] | ||
version = "0.2.0" | ||
authors = ["Jack Wrenn <[email protected]>"] | ||
|
||
[[bin]] | ||
name = "tfw" | ||
|
||
[dependencies] | ||
clap = "2.14.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,76 @@ | ||
use std::io; | ||
use std::env; | ||
use std::thread; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::sync::Arc; | ||
#[macro_use] | ||
extern crate clap; | ||
|
||
use clap::App; | ||
use clap::Arg; | ||
|
||
use std::io::copy; | ||
use std::io::stdin; | ||
use std::io::BufRead; | ||
use std::io::BufReader; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
k4rtik
|
||
use std::io::Result; | ||
use std::fs::OpenOptions; | ||
use std::sync::atomic::Ordering; | ||
use std::sync::atomic::AtomicUsize; | ||
use std::thread; | ||
use std::process; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
|
||
|
||
fn main() { | ||
|
||
let files = env::args(). | ||
skip(2). | ||
map(|path| | ||
OpenOptions::new(). | ||
read(false). | ||
write(true). | ||
create(true). | ||
open(path)). | ||
map(Result::unwrap). | ||
collect::<Vec<File>>(); | ||
|
||
let target = Arc::new(AtomicUsize::new(0)); | ||
let target_writer = target.clone(); | ||
|
||
thread::spawn(move || { | ||
for line in BufReader::new( | ||
env::args().nth(1). | ||
map(|path| | ||
OpenOptions::new(). | ||
read(true). | ||
write(false). | ||
create(false). | ||
open(path)). | ||
unwrap().unwrap()). | ||
lines() { | ||
target_writer.store( | ||
line.unwrap().parse::<usize>().unwrap_or( | ||
target_writer.load(Ordering::Relaxed)), | ||
Ordering::Relaxed); | ||
} | ||
}); | ||
|
||
let stdin = io::stdin(); | ||
let mut buffer = stdin.lock(); | ||
|
||
loop { | ||
let consumed = buffer.fill_buf(). | ||
map(|bytes| | ||
files.get(target.load(Ordering::Relaxed)). | ||
map(|mut file| file.write(bytes))). | ||
unwrap().unwrap().unwrap(); | ||
buffer.consume(consumed); | ||
let matches = App::new("tfw") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about("Shell utility for conditional redirection") | ||
.arg(Arg::with_name("switch") | ||
.value_name("SWITCH") | ||
.help("File whose last line indicates where to direct the input.") | ||
.takes_value(true) | ||
.required(true) | ||
.multiple(false)) | ||
.get_matches(); | ||
|
||
let switch = unwrap( | ||
This comment has been minimized.
Sorry, something went wrong.
k4rtik
|
||
matches.value_of("switch") | ||
.map(|path| OpenOptions::new().read(true).open(path)) | ||
.unwrap()); | ||
|
||
let target = Arc::new(Mutex::new(None)); | ||
|
||
{ | ||
let target = target.clone(); | ||
thread::spawn(move || { | ||
let files = | ||
BufReader::new(switch) | ||
.lines().map(unwrap) | ||
.map(|path| | ||
OpenOptions::new() | ||
.create(true) | ||
.append(true) | ||
.open(path)).map(unwrap); | ||
for file in files { | ||
*(target.lock().unwrap()) = Some(file); | ||
} | ||
}); | ||
} | ||
|
||
let stdin = stdin(); | ||
let lines = stdin.lock().lines().map(unwrap); | ||
|
||
while target.lock().unwrap().is_none() {}; | ||
|
||
for mut line in lines { | ||
line.push('\n'); | ||
unwrap(copy( | ||
&mut line.as_bytes(), | ||
(*target.lock().unwrap()).as_mut().unwrap())); | ||
} | ||
} | ||
|
||
|
||
fn unwrap<T>(v : Result<T>) -> T { | ||
v.unwrap_or_else(|e| { | ||
println!("{}", e); | ||
process::exit(1) | ||
}) | ||
} |
Idiomatic Rust way is to combine dependencies from the same module, like so:
Also, suggest using
rustfmt
, takes care of most syntactic issues.