From 4b1b8061e79b42128df4f06fd1e439549bf9696b Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sun, 19 May 2024 18:50:06 -0600 Subject: Handle STDERR, in addition to STDOUT This patch overhauls the reading and reciting of verses, such that the redirection of STDERR (in addition to STDOUT, which was already a feature), is now possible. Removed the 'stdout' argument from recite(), since it is no longer needed with how incantations function. A verse's couplet indicator is now a u8, instead of a bool, with certain values corresponding to types of couplets, for instance: ls | grep Ca | lolcat ^ ^ ^ | | 2: right side of a couplet | 3: both sides of a couplet 1: left side of a couplet Incantions are no longer hanlded in rune.rs, and the task macros have been removed. Now, a verse incants itself, matching on its own meter to determine how to handle the next verse. The following runes were added to help with handling STDERR: Write2 -> 2> WriteAll -> &> Addendum2 -> 2>> AddendumAll -> &>> The 'io' field in verse was changed from an Option, to an array of Runes, since a single verse might have multiple IO operations. The following fields were added to Verse, to assist with handling STDERR: ip -> List of filenames to read into STDIN op -> List of filenames to send STDOUT to ep -> List of filenames to send STDERR to Keep track of channels when reading a poem. Channels are relating to IO operations. If channel is None, words get pushed to the verse's primary stanza (i.e. the verb or the clause). If a channel is selected, words are pushed to one of the aforementioned new fields in Verse. Read -> ip Write/Addedum -> op Write2/Addedum2 -> ep WriteAll/AddendumAll -> op and ep --- src/poem/recite.rs | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) (limited to 'src/poem/recite.rs') diff --git a/src/poem/recite.rs b/src/poem/recite.rs index 60b7857..8b2fd1d 100644 --- a/src/poem/recite.rs +++ b/src/poem/recite.rs @@ -1,4 +1,3 @@ -mod ps; use super::Poem; use crate::compose::Environment; use crate::path; @@ -12,14 +11,11 @@ use std::{ }; pub trait Reciteable { - fn recite(&self, env: &mut Environment, stdout: Option) -> Result, io::Error>; + fn recite(&self, env: &mut Environment) -> Result, io::Error>; } impl Reciteable for Poem { - fn recite(&self, env: &mut Environment, stdout: Option) -> Result, io::Error> { - // Should we print to stdout or always capture it - let stdout = stdout.unwrap_or(true); - + fn recite(&self, env: &mut Environment) -> Result, io::Error> { // Variable for storing the output of a piped verse let mut out: Vec = Vec::new(); @@ -94,7 +90,7 @@ impl Reciteable for Poem { Some(poem) => poem, None => break, // TODO: Return an error }; - let mut out = poem.recite(env, Some(false))?; + let mut out = poem.recite(env)?; match out.last() { Some(last) => { if *last == b'\n' { @@ -165,29 +161,7 @@ impl Reciteable for Poem { continue; } - // Incant the verse, based on its meter - if stdout { - match verse.io { - Rune::Read => Rune::incant_read(&mut verse, &mut out, &mut pids)?, - Rune::Write => Rune::incant_write(&mut verse, &mut out, &mut pids)?, - Rune::Addendum => Rune::incant_addendum(&mut verse, &mut out, &mut pids)?, - _ => match verse.meter { - Rune::None => Rune::incant_none(&verse, &mut out)?, - Rune::Couplet => Rune::incant_couplet(&verse, &mut out)?, - Rune::Quiet => Rune::incant_quiet(&verse, &mut out, &mut pids)?, - Rune::And => Rune::incant_and(&verse, &mut out)?, - Rune::Continue => Rune::incant_continue(&verse, &mut out)?, - _ => unreachable!(), - }, - } - } else { - match verse.io { - Rune::Read => Rune::incant_read(&mut verse, &mut out, &mut pids)?, - Rune::Write => Rune::incant_write(&mut verse, &mut out, &mut pids)?, - Rune::Addendum => Rune::incant_addendum(&mut verse, &mut out, &mut pids)?, - _ => Rune::incant_couplet(&verse, &mut out)?, - } - } + verse.incant(&mut out, &mut pids)? }; // Break from the loop if the meter is not [Rune::Continue], and -- cgit v1.2.3