From f9c5cb86304748baf0da99126bf75c5a3cd3e773 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 5 Jan 2022 07:30:34 -0500 Subject: and more simplification --- src/env.rs | 3 ++- src/format.rs | 2 +- src/main.rs | 2 +- src/pipeline/builtins/command.rs | 5 ++--- src/pipeline/builtins/mod.rs | 22 ++++++++++------------ src/pipeline/command.rs | 4 ++-- src/pipeline/mod.rs | 21 ++++++++------------- src/pipeline/prelude.rs | 1 + src/prelude.rs | 11 +++++++++++ src/shell/history/entry.rs | 10 ++++------ src/shell/history/mod.rs | 13 ++++--------- src/shell/history/pty.rs | 3 --- src/shell/mod.rs | 5 ++--- src/shell/prelude.rs | 1 + src/shell/readline.rs | 4 +++- 15 files changed, 52 insertions(+), 55 deletions(-) create mode 100644 src/pipeline/prelude.rs create mode 100644 src/prelude.rs (limited to 'src') diff --git a/src/env.rs b/src/env.rs index 6e5f3e5..07fef56 100644 --- a/src/env.rs +++ b/src/env.rs @@ -1,5 +1,6 @@ +use crate::prelude::*; + use serde::Deserialize as _; -use std::os::unix::process::ExitStatusExt as _; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub enum Env { diff --git a/src/format.rs b/src/format.rs index f2414cc..55757c4 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,4 +1,4 @@ -use std::os::unix::process::ExitStatusExt as _; +use crate::prelude::*; pub fn path(path: &std::path::Path) -> String { let mut path = path.display().to_string(); diff --git a/src/main.rs b/src/main.rs index d69ca20..3e5d431 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,11 +13,11 @@ #![allow(clippy::type_complexity)] mod env; -pub use env::Env; mod format; mod info; mod parse; mod pipeline; +mod prelude; mod shell; async fn async_main() -> anyhow::Result { diff --git a/src/pipeline/builtins/command.rs b/src/pipeline/builtins/command.rs index b69463b..fd5e322 100644 --- a/src/pipeline/builtins/command.rs +++ b/src/pipeline/builtins/command.rs @@ -1,5 +1,4 @@ -use async_std::io::{ReadExt as _, WriteExt as _}; -use std::os::unix::io::{AsRawFd as _, FromRawFd as _, IntoRawFd as _}; +use crate::pipeline::prelude::*; pub struct Command { exe: crate::parse::Exe, @@ -41,7 +40,7 @@ impl Command { self.io.pre_exec(f); } - pub fn spawn(self, env: &crate::Env) -> anyhow::Result { + pub fn spawn(self, env: &Env) -> anyhow::Result { let Self { f, exe, io } = self; (f)(exe, env, io) } diff --git a/src/pipeline/builtins/mod.rs b/src/pipeline/builtins/mod.rs index 36e308a..003892d 100644 --- a/src/pipeline/builtins/mod.rs +++ b/src/pipeline/builtins/mod.rs @@ -1,13 +1,11 @@ -use std::os::unix::ffi::OsStrExt as _; -use std::os::unix::process::ExitStatusExt as _; -use users::os::unix::UserExt as _; +use crate::pipeline::prelude::*; pub mod command; pub use command::{Child, Command}; type Builtin = &'static (dyn for<'a> Fn( crate::parse::Exe, - &'a crate::Env, + &'a Env, command::Io, ) -> anyhow::Result> + Sync @@ -31,12 +29,12 @@ static BUILTINS: once_cell::sync::Lazy< #[allow(clippy::unnecessary_wraps)] fn cd( exe: crate::parse::Exe, - env: &crate::Env, + env: &Env, io: command::Io, ) -> anyhow::Result { async fn async_cd( exe: crate::parse::Exe, - _env: &crate::Env, + _env: &Env, io: command::Io, ) -> std::process::ExitStatus { macro_rules! bail { @@ -121,12 +119,12 @@ fn cd( // this later, since the binary seems totally fine fn echo( exe: crate::parse::Exe, - env: &crate::Env, + env: &Env, io: command::Io, ) -> anyhow::Result { async fn async_echo( exe: crate::parse::Exe, - _env: &crate::Env, + _env: &Env, io: command::Io, ) -> std::process::ExitStatus { macro_rules! write_stdout { @@ -159,7 +157,7 @@ fn echo( fn and( mut exe: crate::parse::Exe, - env: &crate::Env, + env: &Env, io: command::Io, ) -> anyhow::Result { exe.shift(); @@ -175,7 +173,7 @@ fn and( fn or( mut exe: crate::parse::Exe, - env: &crate::Env, + env: &Env, io: command::Io, ) -> anyhow::Result { exe.shift(); @@ -191,7 +189,7 @@ fn or( fn command( mut exe: crate::parse::Exe, - env: &crate::Env, + env: &Env, io: command::Io, ) -> anyhow::Result { exe.shift(); @@ -202,7 +200,7 @@ fn command( fn builtin( mut exe: crate::parse::Exe, - env: &crate::Env, + env: &Env, io: command::Io, ) -> anyhow::Result { exe.shift(); diff --git a/src/pipeline/command.rs b/src/pipeline/command.rs index a5b5f79..a6b363a 100644 --- a/src/pipeline/command.rs +++ b/src/pipeline/command.rs @@ -1,4 +1,4 @@ -use async_std::os::unix::process::CommandExt as _; +use crate::pipeline::prelude::*; pub struct Command { inner: Inner, @@ -90,7 +90,7 @@ impl Command { } } - pub fn spawn(self, env: &crate::Env) -> anyhow::Result { + pub fn spawn(self, env: &Env) -> anyhow::Result { match self.inner { Inner::Binary(mut cmd) => { Ok(Child::Binary(cmd.spawn().map_err(|e| { diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index d4c1cab..015eeef 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -1,20 +1,17 @@ -use async_std::io::{ReadExt as _, WriteExt as _}; -use async_std::stream::StreamExt as _; -use futures_lite::future::FutureExt as _; -use std::os::unix::io::FromRawFd as _; -use std::os::unix::process::ExitStatusExt as _; +use crate::pipeline::prelude::*; const PID0: nix::unistd::Pid = nix::unistd::Pid::from_raw(0); #[derive(Debug, serde::Serialize, serde::Deserialize)] pub enum Event { Suspend(usize), - Exit(crate::Env), + Exit(Env), } mod builtins; mod command; pub use command::{Child, Command}; +mod prelude; pub async fn run() -> anyhow::Result { cloexec(3)?; @@ -37,7 +34,7 @@ pub async fn run() -> anyhow::Result { } async fn run_with_env( - env: &mut crate::Env, + env: &mut Env, shell_write: &async_std::fs::File, ) -> anyhow::Result<()> { let pipeline = crate::parse::Pipeline::parse(env.pipeline().unwrap())?; @@ -47,12 +44,10 @@ async fn run_with_env( Ok(()) } -async fn read_data( - mut fh: async_std::fs::File, -) -> anyhow::Result { +async fn read_data(mut fh: async_std::fs::File) -> anyhow::Result { let mut data = vec![]; fh.read_to_end(&mut data).await?; - let env = crate::Env::from_bytes(&data); + let env = Env::from_bytes(&data); Ok(env) } @@ -67,7 +62,7 @@ async fn write_event( fn spawn_children( pipeline: crate::parse::Pipeline, - env: &crate::Env, + env: &Env, ) -> anyhow::Result<(Vec, Option)> { let mut cmds: Vec<_> = pipeline.into_exes().map(Command::new).collect(); for i in 0..(cmds.len() - 1) { @@ -103,7 +98,7 @@ fn spawn_children( async fn wait_children( children: Vec>, pg: Option, - env: &crate::Env, + env: &Env, shell_write: &async_std::fs::File, ) -> std::process::ExitStatus { enum Res { diff --git a/src/pipeline/prelude.rs b/src/pipeline/prelude.rs new file mode 100644 index 0000000..53b67fc --- /dev/null +++ b/src/pipeline/prelude.rs @@ -0,0 +1 @@ +pub use crate::prelude::*; diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..8c888c8 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,11 @@ +pub use crate::env::Env; + +pub use async_std::io::{ReadExt as _, WriteExt as _}; +pub use async_std::stream::StreamExt as _; +pub use futures_lite::future::FutureExt as _; + +pub use async_std::os::unix::process::CommandExt as _; +pub use std::os::unix::ffi::OsStrExt as _; +pub use std::os::unix::io::{AsRawFd as _, FromRawFd as _, IntoRawFd as _}; +pub use std::os::unix::process::ExitStatusExt as _; +pub use users::os::unix::UserExt as _; diff --git a/src/shell/history/entry.rs b/src/shell/history/entry.rs index ab08a72..d804c92 100644 --- a/src/shell/history/entry.rs +++ b/src/shell/history/entry.rs @@ -1,10 +1,8 @@ use crate::shell::prelude::*; -use std::os::unix::process::ExitStatusExt as _; - pub struct Entry { cmdline: String, - env: crate::Env, + env: Env, vt: vt100::Parser, audible_bell_state: usize, visual_bell_state: usize, @@ -19,7 +17,7 @@ pub struct Entry { impl Entry { pub fn new( cmdline: String, - env: crate::Env, + env: Env, size: (u16, u16), input: async_std::channel::Sender>, resize: async_std::channel::Sender<(u16, u16)>, @@ -216,7 +214,7 @@ impl Entry { &self.cmdline } - pub fn env(&self) -> &crate::Env { + pub fn env(&self) -> &Env { &self.env } @@ -273,7 +271,7 @@ impl Entry { pub async fn finish( &mut self, - env: crate::Env, + env: Env, event_w: async_std::channel::Sender, ) { self.exit_info = Some(ExitInfo::new(*env.latest_status())); diff --git a/src/shell/history/mod.rs b/src/shell/history/mod.rs index 76e7d3b..d8adb49 100644 --- a/src/shell/history/mod.rs +++ b/src/shell/history/mod.rs @@ -1,10 +1,5 @@ use crate::shell::prelude::*; -use async_std::io::WriteExt as _; -use futures_lite::future::FutureExt as _; -use std::os::unix::io::{FromRawFd as _, IntoRawFd as _}; -use std::os::unix::process::ExitStatusExt as _; - mod entry; pub use entry::Entry; mod pty; @@ -93,7 +88,7 @@ impl History { pub async fn run( &mut self, ast: crate::parse::Commands, - env: &crate::Env, + env: &Env, event_w: async_std::channel::Sender, ) -> anyhow::Result { let (input_w, input_r) = async_std::channel::unbounded(); @@ -124,7 +119,7 @@ impl History { pub async fn parse_error( &mut self, e: crate::parse::Error, - env: &crate::Env, + env: &Env, event_w: async_std::channel::Sender, ) -> anyhow::Result { // XXX would be great to not have to do this @@ -274,7 +269,7 @@ impl std::iter::DoubleEndedIterator for VisibleEntries { fn run_commands( ast: crate::parse::Commands, entry: async_std::sync::Arc>, - mut env: crate::Env, + mut env: Env, input_r: async_std::channel::Receiver>, resize_r: async_std::channel::Receiver<(u16, u16)>, event_w: async_std::channel::Sender, @@ -330,7 +325,7 @@ fn run_commands( async fn run_pipeline( pty: &pty::Pty, - env: &mut crate::Env, + env: &mut Env, event_w: async_std::channel::Sender, ) -> anyhow::Result<(async_std::process::ExitStatus, bool)> { let mut cmd = pty_process::Command::new(std::env::current_exe().unwrap()); diff --git a/src/shell/history/pty.rs b/src/shell/history/pty.rs index 602a568..edf06c2 100644 --- a/src/shell/history/pty.rs +++ b/src/shell/history/pty.rs @@ -1,8 +1,5 @@ use crate::shell::prelude::*; -use async_std::io::{ReadExt as _, WriteExt as _}; -use futures_lite::future::FutureExt as _; - pub struct Pty { pty: async_std::sync::Arc, close_w: async_std::channel::Sender<()>, diff --git a/src/shell/mod.rs b/src/shell/mod.rs index a19ae09..2b8c849 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -1,6 +1,5 @@ use crate::shell::prelude::*; -use async_std::stream::StreamExt as _; use textmode::Textmode as _; mod event; @@ -128,7 +127,7 @@ pub enum Action { pub struct Shell { readline: readline::Readline, history: history::History, - env: crate::Env, + env: Env, focus: Focus, scene: Scene, escape: bool, @@ -141,7 +140,7 @@ impl Shell { Self { readline: readline::Readline::new(), history: history::History::new(), - env: crate::Env::new(), + env: Env::new(), focus: Focus::Readline, scene: Scene::Readline, escape: false, diff --git a/src/shell/prelude.rs b/src/shell/prelude.rs index 19f58f2..73897bc 100644 --- a/src/shell/prelude.rs +++ b/src/shell/prelude.rs @@ -1 +1,2 @@ pub use super::event::Event; +pub use crate::prelude::*; diff --git a/src/shell/readline.rs b/src/shell/readline.rs index e423e62..d4f09e7 100644 --- a/src/shell/readline.rs +++ b/src/shell/readline.rs @@ -1,3 +1,5 @@ +use crate::shell::prelude::*; + use unicode_width::{UnicodeWidthChar as _, UnicodeWidthStr as _}; pub struct Readline { @@ -22,7 +24,7 @@ impl Readline { pub async fn render( &self, out: &mut impl textmode::Textmode, - env: &crate::Env, + env: &Env, focus: bool, offset: time::UtcOffset, ) -> anyhow::Result<()> { -- cgit v1.2.3-54-g00ecf