Skip to content

Instantly share code, notes, and snippets.

@yvan-sraka
Created November 10, 2018 10:32
Show Gist options
  • Save yvan-sraka/671d6ab478be0dabe6de7b22231e6ff9 to your computer and use it in GitHub Desktop.
Save yvan-sraka/671d6ab478be0dabe6de7b22231e6ff9 to your computer and use it in GitHub Desktop.
commit 8fd71c8755ca9eebe7a7ae0a3c3595cede043f1b
Author: Yvan Sraka <[email protected]>
Date: Sun Oct 28 20:44:57 2018 +0100
Add option to let user set redefine block symbol, e.g.: `-s="///!//"`
diff --git a/README.md b/README.md
index 9c539de..815701e 100644
--- a/README.md
+++ b/README.md
@@ -169,7 +169,9 @@ IENY
- [x] Add a simple script in the path to update aliases by running a `git pull`.
- [x] Provide plugins for syntaxic coloration in VSCode.
- [ ] Add option to let user set top level interpreter, e.g.: `-i="/usr/bin/env python"`
-- [ ] Add option to let user set redefine block symbol, e.g.: `-s="///!//"`
+
+- [x] Add option to let user set redefine block symbol, e.g.: `-s="///!//"`
+// OR syntax decorators?
## Contributing
diff --git a/src/main.rs b/src/main.rs
index 03c925a..6c504da 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -65,23 +65,24 @@ fn split2(s: &str, b: &str) -> (String, String) {
}
}
-fn yeast(reader: &mut BufRead, args: &Vec<String>) -> String {
+fn yeast(reader: &mut BufRead, args: &Vec<String>, s_: String) -> String {
let mut buffer = String::new();
let mut vec = vec![];
+ let _s = s_.chars().rev().collect::<String>();
loop { // While there is something in the read buffer
let len = reader.read_line(&mut buffer)
.expect(&err("couldn't read the input file buffer")); // PANIC
- if buffer.contains("#!") { // Start of a bloc
- let begin = split2(&buffer, "#!");
+ if buffer.contains(s_) { // Start of a bloc
+ let begin = split2(&buffer, s_);
let cmd = readline(reader, &begin.1, &args);
- let end = split2(&yeast(reader, &args), "!#");
+ let end = split2(&yeast(reader, &args, s_), _s);
buffer = end.clone().1;
vec.push(std::thread::Builder::new().name(cmd.clone()).spawn(
move || -> String {
begin.0 + &exec(cmd, end.0) // Launch command process
}
));
- } else if buffer.contains("!#") || len == 0 { // End of a bloc / file
+ } else if buffer.contains(_s) || len == 0 { // End of a bloc / file
let mut outputs = String::new();
for child in vec { // Join all child threads
outputs += &child.expect(&err("can't unwrap named thread"))
@@ -98,14 +99,27 @@ fn main() {
.author("Yvan SRAKA <[email protected]>")
.about("Yet Another Shell Trick")
.setting(AppSettings::TrailingVarArg)
- .arg(Arg::with_name("INPUT")
+ .arg(Arg::with_name("FILE")
.help("Sets the input file to use")
.required(true)
.multiple(true))
+ .arg(Arg::with_name("symbol")
+ .short("s")
+ .long("symbol")
+ .value_name("STRING")
+ .help("Sets a custom breaking symbol")
+ .takes_value(true))
.get_matches();
let args: Vec<String> = std::env::args().collect();
+
let file = std::fs::File::open(std::path::Path::new(&args[1]))
.expect(&err(&format!("couldn't open: {}", &args[1])));
+
let mut reader = std::io::BufReader::new(file);
- print!("{}", split2(&yeast(&mut reader, &args), "!#").0);
+ let s_ = matches.value_of("symbol").unwrap_or("#!");
+
+ print!("{}", split2(&yeast(&mut reader, &args, s_), "!#").0);
+ // TODO _s ?
+ // cmd param ?
+ // multiple args ?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment