summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology
diff options
context:
space:
mode:
authorRory Dudley2024-04-06 23:32:30 -0600
committerRory Dudley2024-04-06 23:32:30 -0600
commitf5db8d64828db756b80b6022322265a2b4f1c11b (patch)
tree68555331ad4dcab6c571c4016c1e8baa3a351ae7 /src/poem/anthology
parent1415c8f9b89699000ef8d864ff8f0e1bebca4a5f (diff)
downloaddwarvish-f5db8d64828db756b80b6022322265a2b4f1c11b.tar.gz
Capture STDOUT as bytes, and convert to string when necessary
Previously, the recite() function created the 'out' variable, which was a String, that got passed to the various incant functions, in order to capture STDOUT in certain situations. In cases where STDOUT was captured, it was first converted to a String, and then appended to the 'out' variable, by means of String::from_utf8_lossy(). This works for basic text, however, does NOT work for binary data. This becomes problematic, when for example, downling a tar file with curl/wget, that is then piped ('|') to the tar program. Using from_utf8_lossy() in this case can corrupt the tar file. This patch makes it so that out is stored as bytes by default, and only converted to a String when necessary.
Diffstat (limited to 'src/poem/anthology')
-rw-r--r--src/poem/anthology/alias.rs4
-rw-r--r--src/poem/anthology/source.rs2
2 files changed, 3 insertions, 3 deletions
diff --git a/src/poem/anthology/alias.rs b/src/poem/anthology/alias.rs
index 4c9b7b6..248342e 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, out: &mut String, aliases: &mut HashMap<String, String>) -> i32 {
+pub fn incant(verse: &Verse, out: &mut Vec<u8>, aliases: &mut HashMap<String, String>) -> i32 {
match verse.clause() {
Some(clause) => {
for stanza in clause {
@@ -39,7 +39,7 @@ pub fn incant(verse: &Verse, out: &mut String, aliases: &mut HashMap<String, Str
}
if verse.couplet {
- *out = format!("{}\n", lines.join("\n"));
+ *out = format!("{}\n", lines.join("\n")).as_bytes().to_vec();
} else {
println!("{}", lines.join("\n"));
}
diff --git a/src/poem/anthology/source.rs b/src/poem/anthology/source.rs
index 182fef4..43d6204 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, out: &mut String, env: &mut Environment) -> i32 {
+pub fn incant(verse: &Verse, out: &mut Vec<u8>, env: &mut Environment) -> i32 {
let files = match verse.clause() {
Some(clause) => clause,
None => {