diff options
author | Rory Dudley | 2024-04-04 00:47:24 -0600 |
---|---|---|
committer | Rory Dudley | 2024-04-04 00:47:24 -0600 |
commit | 9b3e4dd71ec1491e3580e079e9be1b42117a74c9 (patch) | |
tree | 6bf49aee7b725e87f34b124eb20def50e475c26c /src/poem/recite.rs | |
parent | beb11773f6ac17d0b97f908311ac5989e1a0a5ae (diff) | |
download | dwarvish-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/recite.rs')
-rw-r--r-- | src/poem/recite.rs | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/src/poem/recite.rs b/src/poem/recite.rs index 45d4d2e..1f24d14 100644 --- a/src/poem/recite.rs +++ b/src/poem/recite.rs @@ -4,6 +4,7 @@ use crate::compose::Environment; use crate::path; use crate::poem::anthology; use crate::poem::elements::rune::Rune; +use crate::poem::read::Readable; use std::env; use std::{ io, @@ -25,8 +26,30 @@ impl Reciteable for Poem { // Keep track of pids for background processes let mut pids: Arc<Mutex<Vec<i32>>> = Arc::new(Mutex::new(Vec::new())); - // Loop through each verse in the poem + // Check for aliases + let mut vv = Poem::new(); for verse in self.iter() { + let alias = match env.aliases.get(&verse.verb()) { + Some(alias) => alias, + None => { + vv.push(verse.clone()); + continue; + } + } + .to_string(); + + let mut poem = Poem::read(alias).unwrap(); + let len = poem.len(); + for (i, new_verse) in poem.iter_mut().enumerate() { + if verse.clause().is_some() && i + 1 == len { + new_verse.stanza.append(&mut verse.clause().unwrap()); + } + vv.push(new_verse.clone()); + } + } + + // Loop through each verse in the poem + for verse in vv.iter() { // Verse may need to be mutable let mut verse = verse.clone(); @@ -59,18 +82,6 @@ impl Reciteable for Poem { *word = word.replace('\x0e', "$"); } - // Check for aliases - match env.aliases.get(&verse.verb()) { - Some(verb) => { - let mut split: Vec<String> = verb.split(" ").map(|s| s.to_string()).collect(); - let mut old_stanza = verse.stanza.clone(); - old_stanza.remove(0); - split.append(&mut old_stanza); - verse.stanza = split; - } - None => {} - }; - // Check if verse is a builtin let index = anthology::lookup(&verse.verb()); @@ -142,7 +153,7 @@ impl Reciteable for Poem { // Incant the verse if it's a built-in let status = if index.is_some() { - anthology::incant(&verse, index.unwrap(), env) + anthology::incant(&verse, &mut out, index.unwrap(), env) } else { // Checking for environment variables and running internal // poems may mean that the verb is empty now, so check it once |