summaryrefslogtreecommitdiffstats
path: root/src/poem
diff options
context:
space:
mode:
authorRory Dudley2024-03-30 19:05:23 -0600
committerRory Dudley2024-03-30 19:05:23 -0600
commitd408624afeb0035217d3d327e21b24a62b803f28 (patch)
tree891f52eb69f827ca71de157ab67f51606c7b40f0 /src/poem
parent491d3fbff384d4b04483b54e5bb78d23bb1181c5 (diff)
downloaddwarvish-d408624afeb0035217d3d327e21b24a62b803f28.tar.gz
Add wrapper for global shell environment
Instead of having to pass around a bunch of different data structures for various shell functions, create the wrapper compose::Environment, which serves as a global shell state. It is configured via login/profile/rc scripts initially, but can of course be modified throughout the lifetime of the shell.
Diffstat (limited to 'src/poem')
-rw-r--r--src/poem/anthology.rs4
-rw-r--r--src/poem/anthology/source.rs5
-rw-r--r--src/poem/recite.rs15
3 files changed, 13 insertions, 11 deletions
diff --git a/src/poem/anthology.rs b/src/poem/anthology.rs
index b9e747c..f1adae7 100644
--- a/src/poem/anthology.rs
+++ b/src/poem/anthology.rs
@@ -19,13 +19,13 @@ pub fn lookup(verb: &str) -> Option<usize> {
INDEX.iter().position(|v| v.to_string() == verb)
}
-pub fn incant(verse: &Verse, index: usize, bins: &mut Vec<String>) -> i32 {
+pub fn incant(verse: &Verse, index: usize, env: &mut Environment) -> i32 {
let verb = INDEX[index];
match verb {
"cd" => cd::incant(verse),
"exit" => exit::incant(),
"export" => export::incant(verse),
- "source" => source::incant(verse, bins),
+ "source" => source::incant(verse, env),
_ => unreachable!(),
}
}
diff --git a/src/poem/anthology/source.rs b/src/poem/anthology/source.rs
index f7b9a0b..b4148cb 100644
--- a/src/poem/anthology/source.rs
+++ b/src/poem/anthology/source.rs
@@ -1,8 +1,9 @@
+use crate::compose::Environment;
use crate::poem::Verse;
use crate::poem::{read::Readable, recite::Reciteable, Poem};
use std::fs;
-pub fn incant(verse: &Verse, bins: &mut Vec<String>) -> i32 {
+pub fn incant(verse: &Verse, env: &mut Environment) -> i32 {
let files = match verse.clause() {
Some(clause) => clause,
None => {
@@ -32,7 +33,7 @@ pub fn incant(verse: &Verse, bins: &mut Vec<String>) -> i32 {
}
};
- match poem.recite(bins, None) {
+ match poem.recite(env, None) {
Ok(_) => {}
Err(e) => {
eprintln!("dwvsh: {}", e.to_string().to_lowercase());
diff --git a/src/poem/recite.rs b/src/poem/recite.rs
index f2af591..e37b7c6 100644
--- a/src/poem/recite.rs
+++ b/src/poem/recite.rs
@@ -1,5 +1,6 @@
mod ps;
use super::Poem;
+use crate::compose::Environment;
use crate::path;
use crate::poem::anthology;
use crate::poem::elements::rune::Rune;
@@ -10,11 +11,11 @@ use std::{
};
pub trait Reciteable {
- fn recite(&self, bins: &mut Vec<String>, stdout: Option<bool>) -> Result<String, io::Error>;
+ fn recite(&self, env: &mut Environment, stdout: Option<bool>) -> Result<String, io::Error>;
}
impl Reciteable for Poem {
- fn recite(&self, bins: &mut Vec<String>, stdout: Option<bool>) -> Result<String, io::Error> {
+ fn recite(&self, env: &mut Environment, stdout: Option<bool>) -> Result<String, io::Error> {
// Should we print to stdout or always capture it
let stdout = stdout.unwrap_or(true);
@@ -84,7 +85,7 @@ impl Reciteable for Poem {
Some(poem) => poem,
None => break, // TODO: Return an error
};
- let out = poem.recite(bins, Some(false))?;
+ let out = poem.recite(env, Some(false))?;
if out.contains("\n") {
let mut out = out.split("\n");
let next = out.next().unwrap_or("").trim();
@@ -121,16 +122,16 @@ impl Reciteable for Poem {
// Incant the verse if it's a built-in
let index = anthology::lookup(&verse.verb());
let status = if index.is_some() {
- anthology::incant(&verse, index.unwrap(), bins)
+ anthology::incant(&verse, index.unwrap(), env)
} else {
// Incant the verse, based on its meter
// Check if the verb exists
// If it doesn't exist, try refreshing the binary cache, and check
// again
// If it still doesn't exist, print an error
- if !verse.spellcheck(bins) {
- *bins = path::refresh();
- if !verse.spellcheck(bins) {
+ if !verse.spellcheck(&env.bins) {
+ env.bins = path::refresh();
+ if !verse.spellcheck(&env.bins) {
eprintln!("dwvsh: {}: command not found", verse.verb());
if verse.meter != Rune::And {