summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology
diff options
context:
space:
mode:
authorRory Dudley2024-04-04 00:47:24 -0600
committerRory Dudley2024-04-04 00:47:24 -0600
commit9b3e4dd71ec1491e3580e079e9be1b42117a74c9 (patch)
tree6bf49aee7b725e87f34b124eb20def50e475c26c /src/poem/anthology
parentbeb11773f6ac17d0b97f908311ac5989e1a0a5ae (diff)
downloaddwarvish-9b3e4dd71ec1491e3580e079e9be1b42117a74c9.tar.gz
Add better support for aliases
Make sure to interpret alias values as their own poems, since aliases can be fairly complex.
Notes
Notes: Previously, I was doing a simple find and replace for aliases within each verse. However, aliases can be fairly complex, containing their own range of meters, commands, and io operations. This could cause problems, since a verse should never have, for instance, a pipe (`|`) in the middle of it. This patch fixes it, so that we iterate once through the poem, generating a new poem based on aliases that are found. In order to avoid two loops in the recite() function, it might make sense to offload handling aliases to read().
Diffstat (limited to 'src/poem/anthology')
-rw-r--r--src/poem/anthology/alias.rs24
-rw-r--r--src/poem/anthology/source.rs10
2 files changed, 22 insertions, 12 deletions
diff --git a/src/poem/anthology/alias.rs b/src/poem/anthology/alias.rs
index 96682db..0183ec2 100644
--- a/src/poem/anthology/alias.rs
+++ b/src/poem/anthology/alias.rs
@@ -10,7 +10,7 @@ use std::collections::HashMap;
/// ```sh
/// alias vim=nvim
/// ```
-pub fn incant(verse: &Verse, aliases: &mut HashMap<String, String>) -> i32 {
+pub fn incant(verse: &Verse, out: &mut String, aliases: &mut HashMap<String, String>) -> i32 {
match verse.clause() {
Some(clause) => {
for stanza in clause {
@@ -22,18 +22,26 @@ pub fn incant(verse: &Verse, aliases: &mut HashMap<String, String>) -> i32 {
}
}
None => {
+ let mut lines = Vec::new();
for (key, val) in aliases {
- if key.contains(' ') && val.contains(' ') {
- println!("'{}'='{}'", key, val);
+ let line = if key.contains(' ') && val.contains(' ') {
+ format!("'{}'='{}'", key, val)
} else if key.contains(' ') {
- println!("'{}'={}", key, val);
+ format!("'{}'={}", key, val)
} else if val.contains(' ') {
- println!("{}='{}'", key, val);
+ format!("{}='{}'", key, val)
} else if val.is_empty() {
- println!("{}=''", key);
+ format!("{}=''", key)
} else {
- println!("{}={}", key, val);
- }
+ format!("{}={}", key, val)
+ };
+ lines.push(line);
+ }
+
+ if verse.couplet {
+ *out = lines.join("\n");
+ } else {
+ println!("{}", lines.join("\n"));
}
}
}
diff --git a/src/poem/anthology/source.rs b/src/poem/anthology/source.rs
index ed4ed13..77d1330 100644
--- a/src/poem/anthology/source.rs
+++ b/src/poem/anthology/source.rs
@@ -12,7 +12,7 @@ use std::fs;
/// ```sh
/// source ~/.dwvshrc
/// ```
-pub fn incant(verse: &Verse, env: &mut Environment) -> i32 {
+pub fn incant(verse: &Verse, out: &mut String, env: &mut Environment) -> i32 {
let files = match verse.clause() {
Some(clause) => clause,
None => {
@@ -42,13 +42,15 @@ pub fn incant(verse: &Verse, env: &mut Environment) -> i32 {
}
};
- match poem.recite(env, None) {
- Ok(_) => {}
+ let stdout = if verse.couplet { Some(true) } else { None };
+
+ *out = match poem.recite(env, stdout) {
+ Ok(out) => out,
Err(e) => {
eprintln!("dwvsh: {}", e.to_string().to_lowercase());
continue;
}
- }
+ };
}
0