summaryrefslogtreecommitdiffstats
path: root/src/state
diff options
context:
space:
mode:
Diffstat (limited to 'src/state')
-rw-r--r--src/state/history/mod.rs35
-rw-r--r--src/state/history/pty.rs14
-rw-r--r--src/state/mod.rs70
-rw-r--r--src/state/readline.rs12
4 files changed, 75 insertions, 56 deletions
diff --git a/src/state/history/mod.rs b/src/state/history/mod.rs
index 5ed86b3..3bddbde 100644
--- a/src/state/history/mod.rs
+++ b/src/state/history/mod.rs
@@ -89,6 +89,7 @@ impl History {
pub async fn run(
&mut self,
ast: crate::parse::Commands,
+ env: crate::env::Env,
event_w: async_std::channel::Sender<crate::event::Event>,
) -> anyhow::Result<usize> {
let (input_w, input_r) = async_std::channel::unbounded();
@@ -97,6 +98,7 @@ impl History {
let entry = async_std::sync::Arc::new(async_std::sync::Mutex::new(
Entry::new(
ast.input_string().to_string(),
+ env.clone(),
self.size,
input_w,
resize_w,
@@ -105,7 +107,7 @@ impl History {
run_commands(
ast,
async_std::sync::Arc::clone(&entry),
- crate::env::Env::new(self.entries.len()),
+ env.clone(),
input_r,
resize_r,
event_w,
@@ -118,8 +120,9 @@ impl History {
pub async fn parse_error(
&mut self,
e: crate::parse::Error,
+ env: crate::env::Env,
event_w: async_std::channel::Sender<crate::event::Event>,
- ) {
+ ) -> anyhow::Result<usize> {
// XXX would be great to not have to do this
let (input_w, input_r) = async_std::channel::unbounded();
let (resize_w, resize_r) = async_std::channel::unbounded();
@@ -129,15 +132,24 @@ impl History {
resize_r.close();
let err_str = format!("{}", e);
- let mut entry =
- Entry::new(e.into_input(), self.size, input_w, resize_w);
+ let mut entry = Entry::new(
+ e.into_input(),
+ env.clone(),
+ self.size,
+ input_w,
+ resize_w,
+ );
entry.vt.process(err_str.replace('\n', "\r\n").as_bytes());
let status = async_std::process::ExitStatus::from_raw(1 << 8);
entry.exit_info = Some(ExitInfo::new(status));
self.entries.push(async_std::sync::Arc::new(
async_std::sync::Mutex::new(entry),
));
- event_w.send(crate::event::Event::PtyClose).await.unwrap();
+ event_w
+ .send(crate::event::Event::PtyClose(env.clone()))
+ .await
+ .unwrap();
+ Ok(self.entries.len() - 1)
}
pub async fn entry(
@@ -257,6 +269,7 @@ impl std::iter::DoubleEndedIterator for VisibleEntries {
pub struct Entry {
cmdline: String,
+ env: crate::env::Env,
vt: vt100::Parser,
audible_bell_state: usize,
visual_bell_state: usize,
@@ -271,12 +284,14 @@ pub struct Entry {
impl Entry {
fn new(
cmdline: String,
+ env: crate::env::Env,
size: (u16, u16),
input: async_std::channel::Sender<Vec<u8>>,
resize: async_std::channel::Sender<(u16, u16)>,
) -> Self {
Self {
cmdline,
+ env,
vt: vt100::Parser::new(size.0, size.1, 0),
audible_bell_state: 0,
visual_bell_state: 0,
@@ -544,7 +559,7 @@ fn run_commands(
for pipeline in ast.pipelines() {
env.set_pipeline(pipeline.input_string().to_string());
let (pipeline_status, done) =
- run_pipeline(&pty, &env, event_w.clone()).await;
+ run_pipeline(&pty, &mut env, event_w.clone()).await;
env.set_status(pipeline_status);
if done {
break;
@@ -553,17 +568,18 @@ fn run_commands(
entry.lock_arc().await.exit_info =
Some(ExitInfo::new(*env.latest_status()));
- pty.close().await;
+ pty.close(env).await;
});
}
async fn run_pipeline(
pty: &pty::Pty,
- env: &crate::env::Env,
+ env: &mut crate::env::Env,
event_w: async_std::channel::Sender<crate::event::Event>,
) -> (async_std::process::ExitStatus, bool) {
let mut cmd = pty_process::Command::new(std::env::current_exe().unwrap());
cmd.arg("--internal-cmd-runner");
+ env.apply(&mut cmd);
let (to_r, to_w) =
nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC).unwrap();
let (from_r, from_w) =
@@ -602,6 +618,9 @@ async fn run_pipeline(
};
let exit = async { Res::Exit(child.status_no_drop().await) };
match read.or(exit).await {
+ Res::Read(Ok(crate::event::Event::PipelineExit(new_env))) => {
+ *env = new_env;
+ }
Res::Read(Ok(event)) => event_w.send(event).await.unwrap(),
Res::Read(Err(e)) => {
if let bincode::ErrorKind::Io(e) = &*e {
diff --git a/src/state/history/pty.rs b/src/state/history/pty.rs
index 4df5358..4ac0f7d 100644
--- a/src/state/history/pty.rs
+++ b/src/state/history/pty.rs
@@ -3,7 +3,7 @@ use futures_lite::future::FutureExt as _;
pub struct Pty {
pty: async_std::sync::Arc<pty_process::Pty>,
- close_w: async_std::channel::Sender<()>,
+ close_w: async_std::channel::Sender<crate::env::Env>,
}
impl Pty {
@@ -39,8 +39,8 @@ impl Pty {
Ok(cmd.spawn(&self.pty)?)
}
- pub async fn close(&self) {
- self.close_w.send(()).await.unwrap();
+ pub async fn close(&self, env: crate::env::Env) {
+ self.close_w.send(env).await.unwrap();
}
}
@@ -49,7 +49,7 @@ async fn pty_task(
entry: async_std::sync::Arc<async_std::sync::Mutex<super::Entry>>,
input_r: async_std::channel::Receiver<Vec<u8>>,
resize_r: async_std::channel::Receiver<(u16, u16)>,
- close_r: async_std::channel::Receiver<()>,
+ close_r: async_std::channel::Receiver<crate::env::Env>,
event_w: async_std::channel::Sender<crate::event::Event>,
) {
loop {
@@ -57,7 +57,7 @@ async fn pty_task(
Read(Result<usize, std::io::Error>),
Write(Result<Vec<u8>, async_std::channel::RecvError>),
Resize(Result<(u16, u16), async_std::channel::RecvError>),
- Close(Result<(), async_std::channel::RecvError>),
+ Close(Result<crate::env::Env, async_std::channel::RecvError>),
}
let mut buf = [0_u8; 4096];
let read = async { Res::Read((&*pty).read(&mut buf).await) };
@@ -98,9 +98,9 @@ async fn pty_task(
}
},
Res::Close(res) => match res {
- Ok(()) => {
+ Ok(env) => {
event_w
- .send(crate::event::Event::PtyClose)
+ .send(crate::event::Event::PtyClose(env))
.await
.unwrap();
return;
diff --git a/src/state/mod.rs b/src/state/mod.rs
index 656bd7a..83d75c5 100644
--- a/src/state/mod.rs
+++ b/src/state/mod.rs
@@ -24,6 +24,7 @@ pub enum Action {
pub struct State {
readline: readline::Readline,
history: history::History,
+ env: crate::env::Env,
focus: Focus,
scene: Scene,
escape: bool,
@@ -36,6 +37,7 @@ impl State {
Self {
readline: readline::Readline::new(),
history: history::History::new(),
+ env: crate::env::Env::new(),
focus: Focus::Readline,
scene: Scene::Readline,
escape: false,
@@ -65,12 +67,7 @@ impl State {
)
.await?;
self.readline
- .render(
- out,
- self.history.entry_count(),
- true,
- self.offset,
- )
+ .render(out, &self.env, true, self.offset)
.await?;
}
Focus::History(idx) => {
@@ -90,12 +87,7 @@ impl State {
.await?;
let pos = out.screen().cursor_position();
self.readline
- .render(
- out,
- self.history.entry_count(),
- false,
- self.offset,
- )
+ .render(out, &self.env, false, self.offset)
.await?;
out.move_to(pos.0, pos.1);
}
@@ -111,12 +103,7 @@ impl State {
)
.await?;
self.readline
- .render(
- out,
- self.history.entry_count(),
- idx.is_none(),
- self.offset,
- )
+ .render(out, &self.env, idx.is_none(), self.offset)
.await?;
out.hide_cursor(true);
}
@@ -178,10 +165,15 @@ impl State {
.await;
self.scene = self.default_scene(self.focus, None).await;
}
- crate::event::Event::PtyClose => {
+ crate::event::Event::PtyClose(env) => {
if let Some(idx) = self.focus_idx() {
let entry = self.history.entry(idx).await;
if !entry.running() {
+ if self.hide_readline {
+ let idx = self.env.idx();
+ self.env = env;
+ self.env.set_idx(idx);
+ }
self.set_focus(
if self.hide_readline {
Focus::Readline
@@ -199,6 +191,10 @@ impl State {
self.set_focus(Focus::Readline, None).await;
}
}
+ crate::event::Event::PipelineExit(_) => {
+ // this should be handled by the pipeline runner directly
+ unreachable!();
+ }
crate::event::Event::ClockTimer => {}
};
Some(Action::Refresh)
@@ -225,23 +221,25 @@ impl State {
self.readline.clear_input();
let entry = self.history.entry(idx).await;
let input = entry.cmd();
- match self.parse(input) {
+ let idx = match self.parse(input) {
Ok(ast) => {
let idx = self
.history
- .run(ast, event_w.clone())
+ .run(ast, self.env.clone(), event_w.clone())
.await
.unwrap();
self.set_focus(Focus::History(idx), Some(entry))
.await;
self.hide_readline = true;
+ idx
}
- Err(e) => {
- self.history
- .parse_error(e, event_w.clone())
- .await;
- }
- }
+ Err(e) => self
+ .history
+ .parse_error(e, self.env.clone(), event_w.clone())
+ .await
+ .unwrap(),
+ };
+ self.env.set_idx(idx + 1);
} else {
self.set_focus(Focus::Readline, None).await;
}
@@ -341,22 +339,24 @@ impl State {
textmode::Key::Ctrl(b'm') => {
let input = self.readline.input();
if !input.is_empty() {
- match self.parse(input) {
+ let idx = match self.parse(input) {
Ok(ast) => {
let idx = self
.history
- .run(ast, event_w.clone())
+ .run(ast, self.env.clone(), event_w.clone())
.await
.unwrap();
self.set_focus(Focus::History(idx), None).await;
self.hide_readline = true;
+ idx
}
- Err(e) => {
- self.history
- .parse_error(e, event_w.clone())
- .await;
- }
- }
+ Err(e) => self
+ .history
+ .parse_error(e, self.env.clone(), event_w.clone())
+ .await
+ .unwrap(),
+ };
+ self.env.set_idx(idx + 1);
self.readline.clear_input();
}
}
diff --git a/src/state/readline.rs b/src/state/readline.rs
index db19bd7..33ee1b4 100644
--- a/src/state/readline.rs
+++ b/src/state/readline.rs
@@ -20,11 +20,11 @@ impl Readline {
pub async fn render(
&self,
out: &mut impl textmode::Textmode,
- entry_count: usize,
+ env: &crate::env::Env,
focus: bool,
offset: time::UtcOffset,
) -> anyhow::Result<()> {
- let pwd = crate::info::pwd()?;
+ let pwd = env.current_dir();
let user = crate::info::user()?;
let hostname = crate::info::hostname()?;
let time = crate::info::time(offset)?;
@@ -37,24 +37,24 @@ impl Readline {
out.move_to(self.size.0 - 2, 0);
if focus {
out.set_bgcolor(textmode::Color::Rgb(0x56, 0x1b, 0x8b));
- } else if entry_count % 2 == 0 {
+ } else if env.idx() % 2 == 0 {
out.set_bgcolor(textmode::Color::Rgb(0x24, 0x21, 0x00));
} else {
out.set_bgcolor(textmode::Color::Rgb(0x20, 0x20, 0x20));
}
out.write(b"\x1b[K");
out.set_fgcolor(textmode::color::YELLOW);
- out.write_str(&format!("{}", entry_count + 1));
+ out.write_str(&format!("{}", env.idx() + 1));
out.reset_attributes();
if focus {
out.set_bgcolor(textmode::Color::Rgb(0x56, 0x1b, 0x8b));
- } else if entry_count % 2 == 0 {
+ } else if env.idx() % 2 == 0 {
out.set_bgcolor(textmode::Color::Rgb(0x24, 0x21, 0x00));
} else {
out.set_bgcolor(textmode::Color::Rgb(0x20, 0x20, 0x20));
}
out.write_str(" (");
- out.write_str(&pwd);
+ out.write_str(&crate::format::path(pwd));
out.write_str(")");
out.move_to(self.size.0 - 2, self.size.1 - 4 - idlen - timelen);
out.write_str(&id);