aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorBrooks J Rady <b.j.rady@gmail.com>2021-02-09 17:27:36 +0000
committerBrooks J Rady <b.j.rady@gmail.com>2021-02-09 17:27:36 +0000
commitfd001860f4b08920bb6201af723216031d89020f (patch)
tree12d5a666b6030ba18d966be6660523440710b7d8 /src/common
parentfeat(ui): new keybindings and status bar (#160) (diff)
downloadsl0ppij-fd001860f4b08920bb6201af723216031d89020f.tar.gz
sl0ppij-fd001860f4b08920bb6201af723216031d89020f.tar.xz
sl0ppij-fd001860f4b08920bb6201af723216031d89020f.zip
Push the mosaic folder down a directory
Diffstat (limited to 'src/common')
-rw-r--r--src/common/command_is_executing.rs53
-rw-r--r--src/common/errors.rs288
-rw-r--r--src/common/input/actions.rs47
-rw-r--r--src/common/input/handler.rs349
-rw-r--r--src/common/input/keybinds.rs143
-rw-r--r--src/common/input/mod.rs3
-rw-r--r--src/common/ipc.rs46
-rw-r--r--src/common/mod.rs659
-rw-r--r--src/common/os_input_output.rs217
-rw-r--r--src/common/pty_bus.rs326
-rw-r--r--src/common/screen.rs201
-rw-r--r--src/common/utils/consts.rs6
-rw-r--r--src/common/utils/logging.rs76
-rw-r--r--src/common/utils/mod.rs3
-rw-r--r--src/common/utils/shared.rs19
-rw-r--r--src/common/wasm_vm.rs121
16 files changed, 0 insertions, 2557 deletions
diff --git a/src/common/command_is_executing.rs b/src/common/command_is_executing.rs
deleted file mode 100644
index 93c44eb6..00000000
--- a/src/common/command_is_executing.rs
+++ /dev/null
@@ -1,53 +0,0 @@
-#![allow(clippy::mutex_atomic)]
-use std::sync::{Arc, Condvar, Mutex};
-
-#[derive(Clone)]
-pub struct CommandIsExecuting {
- opening_new_pane: Arc<(Mutex<bool>, Condvar)>,
- closing_pane: Arc<(Mutex<bool>, Condvar)>,
-}
-
-impl CommandIsExecuting {
- pub fn new() -> Self {
- CommandIsExecuting {
- opening_new_pane: Arc::new((Mutex::new(false), Condvar::new())),
- closing_pane: Arc::new((Mutex::new(false), Condvar::new())),
- }
- }
- pub fn closing_pane(&mut self) {
- let (lock, _cvar) = &*self.closing_pane;
- let mut closing_pane = lock.lock().unwrap();
- *closing_pane = true;
- }
- pub fn done_closing_pane(&mut self) {
- let (lock, cvar) = &*self.closing_pane;
- let mut closing_pane = lock.lock().unwrap();
- *closing_pane = false;
- cvar.notify_one();
- }
- pub fn opening_new_pane(&mut self) {
- let (lock, _cvar) = &*self.opening_new_pane;
- let mut opening_new_pane = lock.lock().unwrap();
- *opening_new_pane = true;
- }
- pub fn done_opening_new_pane(&mut self) {
- let (lock, cvar) = &*self.opening_new_pane;
- let mut opening_new_pane = lock.lock().unwrap();
- *opening_new_pane = false;
- cvar.notify_one();
- }
- pub fn wait_until_pane_is_closed(&self) {
- let (lock, cvar) = &*self.closing_pane;
- let mut closing_pane = lock.lock().unwrap();
- while *closing_pane {
- closing_pane = cvar.wait(closing_pane).unwrap();
- }
- }
- pub fn wait_until_new_pane_is_opened(&self) {
- let (lock, cvar) = &*self.opening_new_pane;
- let mut opening_new_pane = lock.lock().unwrap();
- while *opening_new_pane {
- opening_new_pane = cvar.wait(opening_new_pane).unwrap();
- }
- }
-}
diff --git a/src/common/errors.rs b/src/common/errors.rs
deleted file mode 100644
index 27fe3103..00000000
--- a/src/common/errors.rs
+++ /dev/null
@@ -1,288 +0,0 @@
-use super::{AppInstruction, OPENCALLS};
-use crate::pty_bus::PtyInstruction;
-use crate::screen::ScreenInstruction;
-
-use std::fmt::{Display, Error, Formatter};
-
-const MAX_THREAD_CALL_STACK: usize = 6;
-
-#[cfg(not(test))]
-use super::SenderWithContext;
-#[cfg(not(test))]
-use std::panic::PanicInfo;
-#[cfg(not(test))]
-pub fn handle_panic(
- info: &PanicInfo<'_>,
- send_app_instructions: &SenderWithContext<AppInstruction>,
-) {
- use backtrace::Backtrace;
- use std::{process, thread};
- let backtrace = Backtrace::new();
- let thread = thread::current();
- let thread = thread.name().unwrap_or("unnamed");
-
- let msg = match info.payload().downcast_ref::<&'static str>() {
- Some(s) => Some(*s),
- None => info.payload().downcast_ref::<String>().map(|s| &**s),
- };
-
- let err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
-
- let backtrace = match (info.location(), msg) {
- (Some(location), Some(msg)) => format!(
- "{}\n\u{1b}[0;0mError: \u{1b}[0;31mthread '{}' panicked at '{}': {}:{}\n\u{1b}[0;0m{:?}",
- err_ctx,
- thread,
- msg,
- location.file(),
- location.line(),
- backtrace
- ),
- (Some(location), None) => format!(
- "{}\n\u{1b}[0;0mError: \u{1b}[0;31mthread '{}' panicked: {}:{}\n\u{1b}[0;0m{:?}",
- err_ctx,
- thread,
- location.file(),
- location.line(),
- backtrace
- ),
- (None, Some(msg)) => format!(
- "{}\n\u{1b}[0;0mError: \u{1b}[0;31mthread '{}' panicked at '{}'\n\u{1b}[0;0m{:?}",
- err_ctx, thread, msg, backtrace
- ),
- (None, None) => format!(
- "{}\n\u{1b}[0;0mError: \u{1b}[0;31mthread '{}' panicked\n\u{1b}[0;0m{:?}",
- err_ctx, thread, backtrace
- ),
- };
-
- if thread == "main" {
- println!("{}", backtrace);
- process::exit(1);
- } else {
- send_app_instructions
- .send(AppInstruction::Error(backtrace))
- .unwrap();
- }
-}
-
-#[derive(Clone, Copy)]
-pub struct ErrorContext {
- calls: [ContextType; MAX_THREAD_CALL_STACK],
-}
-
-impl ErrorContext {
- pub fn new() -> Self {
- Self {
- calls: [ContextType::Empty; MAX_THREAD_CALL_STACK],
- }
- }
-
- pub fn add_call(&mut self, call: ContextType) {
- for ctx in self.calls.iter_mut() {
- if *ctx == ContextType::Empty {
- *ctx = call;
- break;
- }
- }
- OPENCALLS.with(|ctx| *ctx.borrow_mut() = *self);
- }
-}
-
-impl Default for ErrorContext {
- fn default() -> Self {
- Self::new()
- }
-}
-
-impl Display for ErrorContext {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
- writeln!(f, "Originating Thread(s):")?;
- for (index, ctx) in self.calls.iter().enumerate() {
- if *ctx == ContextType::Empty {
- break;
- }
- writeln!(f, "\u{1b}[0;0m{}. {}", index + 1, ctx)?;
- }
- Ok(())
- }
-}
-
-#[derive(Copy, Clone, PartialEq)]
-pub enum ContextType {
- Screen(ScreenContext),
- Pty(PtyContext),
-
- Plugin(PluginContext),
- App(AppContext),
- IPCServer,
- StdinHandler,
- AsyncTask,
- Empty,
-}
-
-impl Display for ContextType {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
- let purple = "\u{1b}[1;35m";
- let green = "\u{1b}[0;32m";
- match *self {
- ContextType::Screen(c) => write!(f, "{}screen_thread: {}{:?}", purple, green, c),
- ContextType::Pty(c) => write!(f, "{}pty_thread: {}{:?}", purple, green, c),
-
- ContextType::Plugin(c) => write!(f, "{}plugin_thread: {}{:?}", purple, green, c),
- ContextType::App(c) => write!(f, "{}main_thread: {}{:?}", purple, green, c),
- ContextType::IPCServer => write!(f, "{}ipc_server: {}AcceptInput", purple, green),
- ContextType::StdinHandler => {
- write!(f, "{}stdin_handler_thread: {}AcceptInput", purple, green)
- }
- ContextType::AsyncTask => {
- write!(f, "{}stream_terminal_bytes: {}AsyncTask", purple, green)
- }
- ContextType::Empty => write!(f, ""),
- }
- }
-}
-
-#[derive(Debug, Clone, Copy, PartialEq)]
-pub enum ScreenContext {
- HandlePtyEvent,
- Render,
- NewPane,
- HorizontalSplit,
- VerticalSplit,
- WriteCharacter,
- ResizeLeft,
- ResizeRight,
- ResizeDown,
- ResizeUp,
- MoveFocus,
- MoveFocusLeft,
- MoveFocusDown,
- MoveFocusUp,
- MoveFocusRight,
- Quit,
- ScrollUp,
- ScrollDown,
- ClearScroll,
- CloseFocusedPane,
- ToggleActiveTerminalFullscreen,
- SetSelectable,
- SetInvisibleBorders,
- SetMaxHeight,
- ClosePane,
- ApplyLayout,
- NewTab,
- SwitchTabNext,
- SwitchTabPrev,
- CloseTab,
-}
-
-impl From<&ScreenInstruction> for ScreenContext {
- fn from(screen_instruction: &ScreenInstruction) -> Self {
- match *screen_instruction {
- ScreenInstruction::Pty(..) => ScreenContext::HandlePtyEvent,
- ScreenInstruction::Render => ScreenContext::Render,
- ScreenInstruction::NewPane(_) => ScreenContext::NewPane,
- ScreenInstruction::HorizontalSplit(_) => ScreenContext::HorizontalSplit,
- ScreenInstruction::VerticalSplit(_) => ScreenContext::VerticalSplit,
- ScreenInstruction::WriteCharacter(_) => ScreenContext::WriteCharacter,
- ScreenInstruction::ResizeLeft => ScreenContext::ResizeLeft,
- ScreenInstruction::ResizeRight => ScreenContext::ResizeRight,
- ScreenInstruction::ResizeDown => ScreenContext::ResizeDown,
- ScreenInstruction::ResizeUp => ScreenContext::ResizeUp,
- ScreenInstruction::MoveFocus => ScreenContext::MoveFocus,
- ScreenInstruction::MoveFocusLeft => ScreenContext::MoveFocusLeft,
- ScreenInstruction::MoveFocusDown => ScreenContext::MoveFocusDown,
- ScreenInstruction::MoveFocusUp => ScreenContext::MoveFocusUp,
- ScreenInstruction::MoveFocusRight => ScreenContext::MoveFocusRight,
- ScreenInstruction::Quit => ScreenContext::Quit,
- ScreenInstruction::ScrollUp => ScreenContext::ScrollUp,
- ScreenInstruction::ScrollDown => ScreenContext::ScrollDown,
- ScreenInstruction::ClearScroll => ScreenContext::ClearScroll,
- ScreenInstruction::CloseFocusedPane => ScreenContext::CloseFocusedPane,
- ScreenInstruction::ToggleActiveTerminalFullscreen => {
- ScreenContext::ToggleActiveTerminalFullscreen
- }
- ScreenInstruction::SetSelectable(..) => ScreenContext::SetSelectable,
- ScreenInstruction::SetInvisibleBorders(..) => ScreenContext::SetInvisibleBorders,
- ScreenInstruction::SetMaxHeight(..) => ScreenContext::SetMaxHeight,
- ScreenInstruction::ClosePane(_) => ScreenContext::ClosePane,
- ScreenInstruction::ApplyLayout(_) => ScreenContext::ApplyLayout,
- ScreenInstruction::NewTab(_) => ScreenContext::NewTab,
- ScreenInstruction::SwitchTabNext => ScreenContext::SwitchTabNext,
- ScreenInstruction::SwitchTabPrev => ScreenContext::SwitchTabPrev,
- ScreenInstruction::CloseTab => ScreenContext::CloseTab,
- }
- }
-}
-
-#[derive(Debug, Clone, Copy, PartialEq)]
-pub enum PtyContext {
- SpawnTerminal,
- SpawnTerminalVertically,
- SpawnTerminalHorizontally,
- NewTab,
- ClosePane,
- CloseTab,
- Quit,
-}
-
-impl From<&PtyInstruction> for PtyContext {
- fn from(pty_instruction: &PtyInstruction) -> Self {
- match *pty_instruction {
- PtyInstruction::SpawnTerminal(_) => PtyContext::SpawnTerminal,
- PtyInstruction::SpawnTerminalVertically(_) => PtyContext::SpawnTerminalVertically,
- PtyInstruction::SpawnTerminalHorizontally(_) => PtyContext::SpawnTerminalHorizontally,
- PtyInstruction::ClosePane(_) => PtyContext::ClosePane,
- PtyInstruction::CloseTab(_) => PtyContext::CloseTab,
- PtyInstruction::NewTab => PtyContext::NewTab,
- PtyInstruction::Quit => PtyContext::Quit,
- }
- }
-}
-
-// FIXME: This whole pattern *needs* a macro eventually, it's soul-crushing to write
-
-use crate::wasm_vm::PluginInstruction;
-
-#[derive(Debug, Clone, Copy, PartialEq)]
-pub enum PluginContext {
- Load,
- Draw,
- Input,
- GlobalInput,
- Unload,
- Quit,
-}
-
-impl From<&PluginInstruction> for PluginContext {
- fn from(plugin_instruction: &PluginInstruction) -> Self {
- match *plugin_instruction {
- PluginInstruction::Load(..) => PluginContext::Load,
- PluginInstruction::Draw(..) => PluginContext::Draw,
- PluginInstruction::Input(..) => PluginContext::Input,
- PluginInstruction::GlobalInput(_) => PluginContext::GlobalInput,
- PluginInstruction::Unload(_) => PluginContext::Unload,
- PluginInstruction::Quit => PluginContext::Quit,
- }
- }
-}
-
-#[derive(Debug, Clone, Copy, PartialEq)]
-pub enum AppContext {
- GetState,
- SetState,
- Exit,
- Error,
-}
-
-impl From<&AppInstruction> for AppContext {
- fn from(app_instruction: &AppInstruction) -> Self {
- match *app_instruction {
- AppInstruction::GetState(_) => AppContext::GetState,
- AppInstruction::SetState(_) => AppContext::SetState,
- AppInstruction::Exit => AppContext::Exit,
- AppInstruction::Error(_) => AppContext::Error,
- }
- }
-}
diff --git a/src/common/input/actions.rs b/src/common/input/actions.rs
deleted file mode 100644
index 6f4a4521..00000000
--- a/src/common/input/actions.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-/// This module is for defining the set of actions that can be taken in response to a keybind
-/// and also passing actions back to the handler for dispatch.
-use super::handler;
-
-#[derive(Clone)]
-pub enum Direction {
- Left,
- Right,
- Up,
- Down,
-}
-
-#[derive(Clone)]
-pub enum Action {
- /// Quit mosaic
- Quit,
- /// Write to terminal
- Write(Vec<u8>),
- /// Switch to the specified input mode
- SwitchToMode(handler::InputMode),
- TogglePersistentMode,
- /// Resize focus pane in specified direction
- Resize(Direction),
- /// Switch focus to next pane in specified direction
- SwitchFocus(Direction),
- /// Move the focus pane in specified direction
- MoveFocus(Direction),
- /// Scroll up in focus pane
- ScrollUp,
- /// Scroll down in focus pane
- ScrollDown,
- /// Toggle focus pane between fullscreen and normal layout
- ToggleFocusFullscreen,
- /// Open a new pane in specified direction (relative to focus)
- /// If no direction is specified, will try to use the biggest available space
- NewPane(Option<Direction>),
- /// Close focus pane
- CloseFocus,
- // Create a new tab
- NewTab,
- // Go to next tab
- GoToNextTab,
- // Go to previous tab
- GoToPreviousTab,
- // Close the current tab
- CloseTab,
-}
diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs
deleted file mode 100644
index 2a1c2c72..00000000
--- a/src/common/input/handler.rs
+++ /dev/null
@@ -1,349 +0,0 @@
-use super::actions::Action;
-use super::keybinds::get_default_keybinds;
-use crate::common::{update_state, AppInstruction, AppState, SenderWithContext, OPENCALLS};
-/// Module for handling input
-use crate::errors::ContextType;
-use crate::os_input_output::OsApi;
-use crate::pty_bus::PtyInstruction;
-use crate::screen::ScreenInstruction;
-use crate::wasm_vm::PluginInstruction;
-use crate::CommandIsExecuting;
-
-use serde::{Deserialize, Serialize};
-use strum_macros::EnumIter;
-use termion::input::TermReadEventsAndRaw;
-
-use super::keybinds::key_to_action;
-
-struct InputHandler {
- mode: InputMode,
- mode_is_persistent: bool,
- os_input: Box<dyn OsApi>,
- command_is_executing: CommandIsExecuting,
- send_screen_instructions: SenderWithContext<ScreenInstruction>,
- send_pty_instructions: SenderWithContext<PtyInstruction>,
- send_plugin_instructions: SenderWithContext<PluginInstruction>,
- send_app_instructions: SenderWithContext<AppInstruction>,
-}
-
-impl InputHandler {
- fn new(
- os_input: Box<dyn OsApi>,
- command_is_executing: CommandIsExecuting,
- send_screen_instructions: SenderWithContext<ScreenInstruction>,
- send_pty_instructions: SenderWithContext<PtyInstruction>,
- send_plugin_instructions: SenderWithContext<PluginInstruction>,
- send_app_instructions: SenderWithContext<AppInstruction>,
- ) -> Self {
- InputHandler {
- mode: InputMode::Normal,
- mode_is_persistent: false,
- os_input,
- command_is_executing,
- send_screen_instructions,
- send_pty_instructions,
- send_plugin_instructions,
- send_app_instructions,
- }
- }
-
- /// Main event loop
- fn get_input(&mut self) {
- let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
- err_ctx.add_call(ContextType::StdinHandler);
- self.send_pty_instructions.update(err_ctx);
- self.send_app_instructions.update(err_ctx);
- self.send_screen_instructions.update(err_ctx);
- if let Ok(keybinds) = get_default_keybinds() {
- 'input_loop: loop {
- let entry_mode = self.mode;
- //@@@ I think this should actually just iterate over stdin directly
- let stdin_buffer = self.os_input.read_from_stdin();
- drop(
- self.send_plugin_instructions
- .send(PluginInstruction::GlobalInput(stdin_buffer.clone())),
- );
- for key_result in stdin_buffer.events_and_raw() {
- match key_result {
- Ok((event, raw_bytes)) => match event {
- termion::event::Event::Key(key) => {
- let should_break = self.dispatch_action(key_to_action(
- &key, raw_bytes, &self.mode, &keybinds,
- ));
- //@@@ This is a hack until we dispatch more than one action per key stroke
- if entry_mode == self.mode && !self.mode_is_persistent {
- self.mode = InputMode::Normal;
- update_state(&self.send_app_instructions, |_| AppState {
- input_state: InputState {
- mode: self.mode,
- persistent: self.mode_is_persistent,
- },
- });
- }
- if should_break {
- break 'input_loop;
- }
- }
- termion::event::Event::Mouse(_)
- | termion::event::Event::Unsupported(_) => {
- unimplemented!("Mouse and unsupported events aren't supported!");
- }
- },
- Err(err) => panic!("Encountered read error: {:?}", err),
- }
- }
- }
- } else {
- //@@@ Error handling?
- self.exit();
- }
- }
-
- fn dispatch_action(&mut self, action: Action) -> bool {
- let mut interrupt_loop = false;
-
- match action {
- Action::Write(val) => {
- self.send_screen_instructions
- .send(ScreenInstruction::ClearScroll)
- .unwrap();
- self.send_screen_instructions
- .send(ScreenInstruction::WriteCharacter(val))
- .unwrap();
- }
- Action::Quit => {
- self.exit();
- interrupt_loop = true;
- }
- Action::SwitchToMode(mode) => {
- self.mode = mode;
- if mode == InputMode::Normal {
- self.mode_is_persistent = false;
- }
- update_state(&self.send_app_instructions, |_| AppState {
- input_state: InputState {
- mode: self.mode,
- persistent: self.mode_is_persistent,
- },
- });
- self.send_screen_instructions
- .send(ScreenInstruction::Render)
- .unwrap();
- }
- Action::TogglePersistentMode => {
- self.mode_is_persistent = !self.mode_is_persistent;
- update_state(&self.send_app_instructions, |_| AppState {
- input_state: InputState {
- mode: self.mode,
- persistent: self.mode_is_persistent,
- },
- });
- }
- Action::Resize(direction) => {
- let screen_instr = match direction {
- super::actions::Direction::Left => ScreenInstruction::ResizeLeft,
- super::actions::Direction::Right => ScreenInstruction::ResizeRight,
- super::actions::Direction::Up => ScreenInstruction::ResizeUp,
- super::actions::Direction::Down => ScreenInstruction::ResizeDown,
- };
- self.send_screen_instructions.send(screen_instr).unwrap();
- }
- Action::SwitchFocus(_) => {
- self.send_screen_instructions
- .send(ScreenInstruction::MoveFocus)
- .unwrap();
- }
- Action::MoveFocus(direction) => {
- let screen_instr = match direction {
- super::actions::Direction::Left => ScreenInstruction::MoveFocusLeft,
- super::actions::Direction::Right => ScreenInstruction::MoveFocusRight,
- super::actions::Direction::Up => ScreenInstruction::MoveFocusUp,
- super::actions::Direction::Down => ScreenInstruction::MoveFocusDown,
- };
- self.send_screen_instructions.send(screen_instr).unwrap();
- }
- Action::ScrollUp => {
- self.send_screen_instructions
- .send(ScreenInstruction::ScrollUp)
- .unwrap();
- }
- Action::ScrollDown => {
- self.send_screen_instructions
- .send(ScreenInstruction::ScrollDown)
- .unwrap();
- }
- Action::ToggleFocusFullscreen => {
- self.send_screen_instructions
- .send(ScreenInstruction::ToggleActiveTerminalFullscreen)
- .unwrap();
- }
- Action::NewPane(direction) => {
- let pty_instr = match direction {
- Some(super::actions::Direction::Left) => {
- PtyInstruction::SpawnTerminalVertically(None)
- }
- Some(super::actions::Direction::Right) => {
- PtyInstruction::SpawnTerminalVertically(None)
- }
- Some(super::actions::Direction::Up) => {
- PtyInstruction::SpawnTerminalHorizontally(None)
- }
- Some(super::actions::Direction::Down) => {
- PtyInstruction::SpawnTerminalHorizontally(None)
- }
- // No direction specified - try to put it in the biggest available spot
- None => PtyInstruction::SpawnTerminal(None),
- };
- self.command_is_executing.opening_new_pane();
- self.send_pty_instructions.send(pty_instr).unwrap();
- self.command_is_executing.wait_until_new_pane_is_opened();
- }
- Action::CloseFocus => {
- self.command_is_executing.closing_pane();
- self.send_screen_instructions
- .send(ScreenInstruction::CloseFocusedPane)
- .unwrap();
- self.command_is_executing.wait_until_pane_is_closed();
- }
- Action::NewTab => {
- self.command_is_executing.opening_new_pane();
- self.send_pty_instructions
- .send(PtyInstruction::NewTab)
- .unwrap();
- self.command_is_executing.wait_until_new_pane_is_opened();
- }
- Action::GoToNextTab => {
- self.send_screen_instructions
- .send(ScreenInstruction::SwitchTabNext)
- .unwrap();
- }
- Action::GoToPreviousTab => {
- self.send_screen_instructions
- .send(ScreenInstruction::SwitchTabPrev)
- .unwrap();
- }
- Action::CloseTab => {
- self.command_is_executing.closing_pane();
- self.send_screen_instructions
- .send(ScreenInstruction::CloseTab)
- .unwrap();
- self.command_is_executing.wait_until_pane_is_closed();
- }
- }
-
- interrupt_loop
- }
-
- /// Routine to be called when the input handler exits (at the moment this is the
- /// same as quitting mosaic)
- fn exit(&mut self) {
- self.send_app_instructions
- .send(AppInstruction::Exit)
- .unwrap();
- }
-}
-
-#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
-pub struct InputState {
- mode: InputMode,
- persistent: bool,
-}
-
-impl Default for InputState {
- fn default() -> InputState {
- InputState {
- mode: InputMode::Normal,
- persistent: false,
- }
- }
-}
-
-/// Dictates whether we're in command mode, persistent command mode, normal mode or exiting:
-/// - Normal mode either writes characters to the terminal, or switches to command mode
-/// using a particular key control
-/// - Command mode intercepts characters to control mosaic itself, before switching immediately
-/// back to normal mode
-/// - Persistent command mode is the same as command mode, but doesn't return automatically to
-/// normal mode
-#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter, Serialize, Deserialize)]
-pub enum InputMode {
- Normal,
- Command,
- Resize,
- Pane,
- Tab,
- Scroll,
- Exiting,
-}
-
-#[derive(Default, Debug, Clone, Serialize, Deserialize)]
-pub struct Help {
- pub mode: InputMode,
- pub mode_is_persistent: bool,
- pub keybinds: Vec<(String, String)>, // <shortcut> => <shortcut description>
-}
-
-impl Default for InputMode {
- fn default() -> InputMode {
- InputMode::Normal
- }
-}
-
-pub fn get_help(input_state: &InputState) -> Help {
- let mut keybinds: Vec<(String, String)> = vec![];
- match input_state.mode {
- InputMode::Normal | InputMode::Command | InputMode::Exiting => {
- keybinds.push((format!("p"), format!("Pane mode")));
- keybinds.push((format!("t"), format!("Tab mode")));
- keybinds.push((format!("r"), format!("Resize mode")));
- }
- InputMode::Resize => {
- keybinds.push((format!("←↓↑→"), format!("resize pane")));
- }
- InputMode::Pane => {
- keybinds.push((format!("←↓↑→"), format!("move focus")));
- keybinds.push((format!("p"), format!("next pane")));
- keybinds.push((format!("n"), format!("new pane")));
- keybinds.push((format!("d"), format!("down split")));
- keybinds.push((format!("r"), format!("right split")));
- keybinds.push((format!("x"), format!("exit pane")));
- keybinds.push((format!("f"), format!("fullscreen pane")));
- }
- InputMode::Tab => {
- keybinds.push((format!("←↓↑→"), format!("move tab focus")));
- keybinds.push((format!("n"), format!("new tab")));
- keybinds.push((format!("x"), format!("exit tab")));
- }
- InputMode::Scroll => {
- keybinds.push((format!("↓↑"), format!("scroll up/down")));
- }
- }
- keybinds.push((format!("ESC"), format!("Back")));
- keybinds.push((format!("q"), format!("Quit")));
- Help {
- mode: input_state.mode,
- mode_is_persistent: input_state.persistent,
- keybinds,
- }
-}
-
-/// Entry point to the module that instantiates a new InputHandler and calls its
-/// reading loop
-pub fn input_loop(
- os_input: Box<dyn OsApi>,
- command_is_executing: CommandIsExecuting,
- send_screen_instructions: SenderWithContext<ScreenInstruction>,
- send_pty_instructions: SenderWithContext<PtyInstruction>,
- send_plugin_instructions: SenderWithContext<PluginInstruction>,
- send_app_instructions: SenderWithContext<AppInstruction>,
-) {
- let _handler = InputHandler::new(
- os_input,
- command_is_executing,
- send_screen_instructions,
- send_pty_instructions,
- send_plugin_instructions,
- send_app_instructions,
- )
- .get_input();
-}
diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs
deleted file mode 100644
index d6ace68d..00000000
--- a/src/common/input/keybinds.rs
+++ /dev/null
@@ -1,143 +0,0 @@
-// This module is for mapping particular input keys to their corresponding actions.
-
-use super::actions::{Action, Direction};
-use super::handler::InputMode;
-
-use std::collections::HashMap;
-
-use strum::IntoEnumIterator;
-use termion::event::Key;
-
-type Keybinds = HashMap<InputMode, ModeKeybinds>;
-type ModeKeybinds = HashMap<Key, Action>;
-
-/// Populate the default hashmap of keybinds
-/// @@@khs26 What about an input config file?
-pub fn get_default_keybinds() -> Result<Keybinds, String> {
- let mut defaults = Keybinds::new();
-
- for mode in InputMode::iter() {
- defaults.insert(mode, get_defaults_for_mode(&mode)?);
- }
-
- Ok(defaults)
-}
-
-fn get_defaults_for_mode(mode: &InputMode) -> Result<ModeKeybinds, String> {
- let mut defaults = ModeKeybinds::new();
-
- match *mode {
- InputMode::Normal => {
- defaults.insert(Key::Ctrl('g'), Action::SwitchToMode(InputMode::Command));
- }
- InputMode::Command => {
- defaults.insert(Key::Char('r'), Action::SwitchToMode(InputMode::Resize));
- defaults.insert(Key::Char('p'), Action::SwitchToMode(InputMode::Pane));
- defaults.insert(Key::Char('t'), Action::SwitchToMode(InputMode::Tab));
- defaults.insert(Key::Char('s'), Action::SwitchToMode(InputMode::Scroll));
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
- defaults.insert(Key::Char('q'), Action::Quit);
- }
- InputMode::Resize => {
- defaults.insert(Key::Char('h'), Action::Resize(Direction::Left));
- defaults.insert(Key::Char('j'), Action::Resize(Direction::Down));
- defaults.insert(Key::Char('k'), Action::Resize(Direction::Up));
- defaults.insert(Key::Char('l'), Action::Resize(Direction::Right));
-
- defaults.insert(Key::Left, Action::Resize(Direction::Left));
- defaults.insert(Key::Down, Action::Resize(Direction::Down));
- defaults.insert(Key::Up, Action::Resize(Direction::Up));
- defaults.insert(Key::Right, Action::Resize(Direction::Right));
-
- defaults.insert(Key::Ctrl('b'), Action::Resize(Direction::Left));
- defaults.insert(Key::Ctrl('n'), Action::Resize(Direction::Down));
- defaults.insert(Key::Ctrl('p'), Action::Resize(Direction::Up));
- defaults.insert(Key::Ctrl('f'), Action::Resize(Direction::Right));
-
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
- }
- InputMode::Pane => {
- defaults.insert(Key::Char('h'), Action::MoveFocus(Direction::Left));
- defaults.insert(Key::Char('j'), Action::MoveFocus(Direction::Down));
- defaults.insert(Key::Char('k'), Action::MoveFocus(Direction::Up));
- defaults.insert(Key::Char('l'), Action::MoveFocus(Direction::Right));
-
- defaults.insert(Key::Left, Action::MoveFocus(Direction::Left));
- defaults.insert(Key::Down, Action::MoveFocus(Direction::Down));
- defaults.insert(Key::Up, Action::MoveFocus(Direction::Up));
- defaults.insert(Key::Right, Action::MoveFocus(Direction::Right));
-
- defaults.insert(Key::Ctrl('b'), Action::MoveFocus(Direction::Left));
- defaults.insert(Key::Ctrl('n'), Action::MoveFocus(Direction::Down));
- defaults.insert(Key::Ctrl('p'), Action::MoveFocus(Direction::Up));
- defaults.insert(Key::Ctrl('f'), Action::MoveFocus(Direction::Right));
-
- defaults.insert(Key::Char('p'), Action::SwitchFocus(Direction::Right));
- defaults.insert(Key::Char('n'), Action::NewPane(None));
- defaults.insert(Key::Char('d'), Action::NewPane(Some(Direction::Down)));
- defaults.insert(Key::Char('r'), Action::NewPane(Some(Direction::Right)));
- defaults.insert(Key::Char('x'), Action::CloseFocus);
-
- defaults.insert(Key::Char('f'), Action::ToggleFocusFullscreen);
-
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
- }
- InputMode::Tab => {
- defaults.insert(Key::Char('h'), Action::GoToPreviousTab);
- defaults.insert(Key::Char('j'), Action::GoToNextTab);
- defaults.insert(Key::Char('k'), Action::GoToPreviousTab);
- defaults.insert(Key::Char('l'), Action::GoToNextTab);
-
- defaults.insert(Key::Left, Action::GoToPreviousTab);
- defaults.insert(Key::Down, Action::GoToNextTab);
- defaults.insert(Key::Up, Action::GoToPreviousTab);
- defaults.insert(Key::Right, Action::GoToNextTab);
-
- defaults.insert(Key::Ctrl('b'), Action::GoToPreviousTab);
- defaults.insert(Key::Ctrl('n'), Action::GoToNextTab);
- defaults.insert(Key::Ctrl('p'), Action::GoToPreviousTab);
- defaults.insert(Key::Ctrl('f'), Action::GoToNextTab);
-
- defaults.insert(Key::Char('n'), Action::NewTab);
- defaults.insert(Key::Char('x'), Action::CloseTab);
-
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
- }
- InputMode::Scroll => {
- defaults.insert(Key::Char('j'), Action::ScrollDown);
- defaults.insert(Key::Char('k'), Action::ScrollUp);
-
- defaults.insert(Key::Down, Action::ScrollDown);
- defaults.insert(Key::Up, Action::ScrollUp);
-
- defaults.insert(Key::Ctrl('n'), Action::ScrollDown);
- defaults.insert(Key::Ctrl('p'), Action::ScrollUp);
-
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
- }
- InputMode::Exiting => {}
- }
-
- Ok(defaults)
-}
-
-pub fn key_to_action(key: &Key, input: Vec<u8>, mode: &InputMode, keybinds: &Keybinds) -> Action {
- if let Some(mode_keybinds) = keybinds.get(mode) {
- mode_keybinds
- .get(key)
- .cloned()
- .unwrap_or(Action::Write(input))
- } else {
- // Unrecognized mode - panic?
- panic!("Unrecognized mode: {:?}", mode);
- }
-}
diff --git a/src/common/input/mod.rs b/src/common/input/mod.rs
deleted file mode 100644
index 094054e8..00000000
--- a/src/common/input/mod.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-pub mod actions;
-pub mod handler;
-pub mod keybinds;
diff --git a/src/common/ipc.rs b/src/common/ipc.rs
deleted file mode 100644
index 425d81d6..00000000
--- a/src/common/ipc.rs
+++ /dev/null
@@ -1,46 +0,0 @@
-// IPC stuff for starting to split things into a client and server model
-use serde::{Deserialize, Serialize};
-use std::collections::HashSet;
-
-type SessionID = u64;
-
-#[derive(PartialEq, Eq, Serialize, Deserialize, Hash)]
-pub struct Session {
- // Unique ID for this session
- id: SessionID,
- // Identifier for the underlying IPC primitive (socket, pipe)
- conn_name: String,
- // User configured alias for the session
- alias: String,
-}
-
-// How do we want to connect to a session?
-#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
-pub enum ClientType {
- Reader,
- Writer,
-}
-
-// Types of messages sent from the client to the server
-#[derive(Serialize, Deserialize)]
-pub enum ClientToServerMsg {
- // List which sessions are available
- ListSessions,
- // Create a new session
- CreateSession,
- // Attach to a running session
- AttachToSession(SessionID, ClientType),
- // Force detach
- DetachSession(SessionID),
- // Disconnect from the session we're connected to
- DisconnectFromSession,
-}
-
-// Types of messages sent from the server to the client
-// @@@ Implement Serialize and Deserialize for this...
-pub enum ServerToClientMsg {
- // Info about a particular session
- SessionInfo(Session),
- // A list of sessions
- SessionList(HashSet<Session>),
-}
diff --git a/src/common/mod.rs b/src/common/mod.rs
deleted file mode 100644
index de0b9f76..00000000
--- a/src/common/mod.rs
+++ /dev/null
@@ -1,659 +0,0 @@
-pub mod command_is_executing;
-pub mod errors;
-pub mod input;
-pub mod ipc;
-pub mod os_input_output;
-pub mod pty_bus;
-pub mod screen;
-pub mod utils;
-pub mod wasm_vm;
-
-use std::io::Write;
-use std::path::{Path, PathBuf};
-use std::sync::mpsc::{channel, sync_channel, Receiver, SendError, Sender, SyncSender};
-use std::thread;
-use std::{cell::RefCell, sync::mpsc::TrySendError};
-use std::{collections::HashMap, fs};
-
-use crate::panes::PaneId;
-use directories_next::ProjectDirs;
-use input::handler::InputState;
-use serde::{Deserialize, Serialize};
-use termion::input::TermRead;
-use wasm_vm::PluginEnv;
-use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
-use wasmer_wasi::{Pipe, WasiState};
-
-use crate::cli::CliArgs;
-use crate::layout::Layout;
-use command_is_executing::CommandIsExecuting;
-use errors::{AppContext, ContextType, ErrorContext, PluginContext, PtyContext, ScreenContext};
-use input::handler::input_loop;
-use os_input_output::OsApi;
-use pty_bus::{PtyBus, PtyInstruction};
-use screen::{Screen, ScreenInstruction};
-use utils::consts::{MOSAIC_IPC_PIPE, MOSAIC_ROOT_PLUGIN_DIR};
-use wasm_vm::{mosaic_imports, wasi_stdout, wasi_write_string, PluginInstruction};
-
-#[derive(Serialize, Deserialize, Debug)]
-pub enum ApiCommand {
- OpenFile(PathBuf),
- SplitHorizontally,
- SplitVertically,
- MoveFocus,
-}
-// FIXME: It would be good to add some more things to this over time
-#[derive(Debug, Clone, Default)]
-pub struct AppState {
- pub input_state: InputState,
-}
-
-// FIXME: Make this a method on the big `Communication` struct, so that app_tx can be extracted
-// from self instead of being explicitly passed here
-pub fn update_state(
- app_tx: &SenderWithContext<AppInstruction>,
- update_fn: impl FnOnce(AppState) -> AppState,
-) {
- let (state_tx, state_rx) = channel();
-
- drop(app_tx.send(AppInstruction::GetState(state_tx)));
- let state = state_rx.recv().unwrap();
-
- drop(app_tx.send(AppInstruction::SetState(update_fn(state))))
-}
-
-pub type ChannelWithContext<T> = (Sender<(T, ErrorContext)>, Receiver<(T, ErrorContext)>);
-pub type SyncChannelWithContext<T> = (SyncSender<(T, ErrorContext)>, Receiver<(T, ErrorContext)>);
-
-#[derive(Clone)]
-enum SenderType<T: Clone> {
- Sender(Sender<(T, ErrorContext)>),
- SyncSender(SyncSender<(T, ErrorContext)>),
-}
-
-#[derive(Clone)]
-pub struct SenderWithContext<T: Clone> {
- err_ctx: ErrorContext,
- sender: SenderType<T>,
-}
-
-impl<T: Clone> SenderWithContext<T> {
- fn new(err_ctx: ErrorContext, sender: SenderType<T>) -> Self {
- Self { err_ctx, sender }
- }
-
- pub fn send(&self, event: T) -> Result<(), SendError<(T, ErrorContext)>> {
- match self.sender {
- SenderType::Sender(ref s) => s.send((event, self.err_ctx)),
- SenderType::SyncSender(ref s) => s.send((event, self.err_ctx)),
- }
- }
-
- pub fn try_send(&self, event: T) -> Result<(), TrySendError<(T, ErrorContext)>> {
- if let SenderType::SyncSender(ref s) = self.sender {
- s.try_send((event, self.err_ctx))
- } else {
- panic!("try_send can only be called on SyncSenders!")
- }
- }
-
- pub fn update(&mut self, new_ctx: ErrorContext) {
- self.err_ctx = new_ctx;
- }
-}
-
-unsafe impl<T: Clone> Send for SenderWithContext<T> {}
-unsafe impl<T: Clone> Sync for SenderWithContext<T> {}
-
-thread_local!(static OPENCALLS: RefCell<ErrorContext> = RefCell::default());
-
-#[derive(Clone)]
-pub enum AppInstruction {
- GetState(Sender<AppState>),
- SetState(AppState),
- Exit,
- Error(String),
-}
-
-pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
- let take_snapshot = "\u{1b}[?1049h";
- os_input.unset_raw_mode(0);
- let _ = os_input
- .get_stdout_writer()
- .write(take_snapshot.as_bytes())
- .unwrap();
- let mut app_state = AppState::default();
-
- let command_is_executing = CommandIsExecuting::new();
-
- let full_screen_ws = os_input.get_terminal_size_using_fd(0);
- os_input.set_raw_mode(0);
- let (send_screen_instructions, receive_screen_instructions): ChannelWithContext<
- ScreenInstruction,
- > = channel();
- let err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
- let mut send_screen_instructions =
- SenderWithContext::new(err_ctx, SenderType::Sender(send_screen_instructions));
-
- let (send_pty_instructions, receive_pty_instructions): ChannelWithContext<PtyInstruction> =
- channel();
- let mut send_pty_instructions =
- SenderWithContext::new(err_ctx, SenderType::Sender(send_pty_instructions));
-
- let (send_plugin_instructions, receive_plugin_instructions): ChannelWithContext<
- PluginInstruction,
- > = channel();
- let send_plugin_instructions =
- SenderWithContext::new(err_ctx, SenderType::Sender(send_plugin_instructions));
-
- let (send_app_instructions, receive_app_instructions): SyncChannelWithContext<AppInstruction> =
- sync_channel(0);
- let send_app_instructions =
- SenderWithContext::new(err_ctx, SenderType::SyncSender(send_app_instructions));
-
- let mut pty_bus = PtyBus::new(
- receive_pty_instructions,
- send_screen_instructions.clone(),
- send_plugin_instructions.clone(),
- os_input.clone(),
- opts.debug,
- );
-
- // Don't use default layouts in tests, but do everywhere else
- #[cfg(not(test))]
- let default_layout = Some(PathBuf::from("default"));
- #[cfg(test)]
- let default_layout = None;
- let maybe_layout = opts.layout.or(default_layout).map(Layout::new);
-
- #[cfg(not(test))]
- std::panic::set_hook({
- use crate::errors::handle_panic;
- let send_app_instructions = send_app_instructions.clone();
- Box::new(move |info| {
- handle_panic(info, &send_app_instructions);
- })
- });
-
- let pty_thread = thread::Builder::new()
- .name("pty".to_string())
- .spawn({
- let mut command_is_executing = command_is_executing.clone();
- send_pty_instructions.send(PtyInstruction::NewTab).unwrap();
- move || loop {
- let (event, mut err_ctx) = pty_bus
- .receive_pty_instructions
- .recv()
- .expect("failed to receive event on channel");
- err_ctx.add_call(ContextType::Pty(PtyContext::from(&event)));
- pty_bus.send_screen_instructions.update(err_ctx);
- match event {
- PtyInstruction::SpawnTerminal(file_to_open) => {
- let pid = pty_bus.spawn_terminal(file_to_open);
- pty_bus
- .send_screen_instructions
- .send(ScreenInstruction::NewPane(PaneId::Terminal(pid)))
- .unwrap();
- }
- PtyInstruction::SpawnTerminalVertically(file_to_open) => {
- let pid = pty_bus.spawn_terminal(file_to_open);
- pty_bus
- .send_screen_instructions
- .send(ScreenInstruction::VerticalSplit(PaneId::Terminal(pid)))
- .unwrap();
- }
- PtyInstruction::SpawnTerminalHorizontally(file_to_open) => {
- let pid = pty_bus.spawn_terminal(file_to_open);
- pty_bus
- .send_screen_instructions
- .send(ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)))
- .unwrap();
- }
- PtyInstruction::NewTab => {
- if let Some(layout) = maybe_layout.clone() {
- pty_bus.spawn_terminals_for_layout(layout);
- } else {
- let pid = pty_bus.spawn_terminal(None);
- pty_bus
- .send_screen_instructions
- .send(ScreenInstruction::NewTab(pid))
- .unwrap();
- }
- }
- PtyInstruction::ClosePane(id) => {
- pty_bus.close_pane(id);
- command_is_executing.done_closing_pane();
- }
- PtyInstruction::CloseTab(ids) => {
- pty_bus.close_tab(ids);
- command_is_executing.done_closing_pane();
- }
- PtyInstruction::Quit => {
- break;
- }
- }
- }
- })
- .unwrap();
-
- let screen_thread = thread::Builder::new()
- .name("screen".to_string())
- .spawn({
- let mut command_is_executing = command_is_executing.clone();
- let os_input = os_input.clone();
- let send_pty_instructions = send_pty_instructions.clone();
- let send_plugin_instructions = send_plugin_instructions.clone();
- let send_app_instructions = send_app_instructions.clone();
- let max_panes = opts.max_panes;
-
- move || {
- let mut screen = Screen::new(
- receive_screen_instructions,
- send_pty_instructions,
- send_plugin_instructions,
- send_app_instructions,
- &full_screen_ws,
- os_input,
- max_panes,
- );
- loop {
- let (event, mut err_ctx) = screen
- .receiver
- .recv()
- .expect("failed to receive event on channel");
- err_ctx.add_call(ContextType::Screen(ScreenContext::from(&event)));
- screen.send_app_instructions.update(err_ctx);
- screen.send_pty_instructions.update(err_ctx);
- match event {
- ScreenInstruction::Pty(pid, vte_event) => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .handle_pty_event(pid, vte_event);
- }
- ScreenInstruction::Render => {
- screen.render();
- }
- ScreenInstruction::NewPane(pid) => {
- screen.get_active_tab_mut().unwrap().new_pane(pid);
- command_is_executing.done_opening_new_pane();
- }
- ScreenInstruction::HorizontalSplit(pid) => {
- screen.get_active_tab_mut().unwrap().horizontal_split(pid);
- command_is_executing.done_opening_new_pane();
- }
- ScreenInstruction::VerticalSplit(pid) => {
- screen.get_active_tab_mut().unwrap().vertical_split(pid);
- command_is_executing.done_opening_new_pane();
- }
- ScreenInstruction::WriteCharacter(bytes) => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .write_to_active_terminal(bytes);
- }
- ScreenInstruction::ResizeLeft => {
- screen.get_active_tab_mut().unwrap().resize_left();
- }
- ScreenInstruction::ResizeRight => {
- screen.get_active_tab_mut().unwrap().resize_right();
- }
- ScreenInstruction::ResizeDown => {
- screen.get_active_tab_mut().unwrap().resize_down();
- }
- ScreenInstruction::ResizeUp => {
- screen.get_active_tab_mut().unwrap().resize_up();
- }
- ScreenInstruction::MoveFocus => {
- screen.get_active_tab_mut().unwrap().move_focus();
- }
- ScreenInstruction::MoveFocusLeft => {
- screen.get_active_tab_mut().unwrap().move_focus_left();
- }
- ScreenInstruction::MoveFocusDown => {
- screen.get_active_tab_mut().unwrap().move_focus_down();
- }
- ScreenInstruction::MoveFocusRight => {
- screen.get_active_tab_mut().unwrap().move_focus_right();
- }
- ScreenInstruction::MoveFocusUp => {
- screen.get_active_tab_mut().unwrap().move_focus_up();
- }
- ScreenInstruction::ScrollUp => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .scroll_active_terminal_up();
- }
- ScreenInstruction::ScrollDown => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .scroll_active_terminal_down();
- }
- ScreenInstruction::ClearScroll => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .clear_active_terminal_scroll();
- }
- ScreenInstruction::CloseFocusedPane => {
- screen.get_active_tab_mut().unwrap().close_focused_pane();
- screen.render();
- }
- ScreenInstruction::SetSelectable(id, selectable) => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .set_pane_selectable(id, selectable);
- // FIXME: Is this needed?
- screen.render();
- }
- ScreenInstruction::SetMaxHeight(id, max_height) => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .set_pane_max_height(id, max_height);
- }
- ScreenInstruction::SetInvisibleBorders(id, invisible_borders) => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .set_pane_invisible_borders(id, invisible_borders);
- screen.render();
- }
- ScreenInstruction::ClosePane(id) => {
- screen.get_active_tab_mut().unwrap().close_pane(id);
- screen.render();
- }
- ScreenInstruction::ToggleActiveTerminalFullscreen => {
- screen
- .get_active_tab_mut()
- .unwrap()
- .toggle_active_pane_fullscreen();
- }
- ScreenInstruction::NewTab(pane_id) => {
- screen.new_tab(pane_id);
- command_is_executing.done_opening_new_pane();
- }
- ScreenInstruction::SwitchTabNext => screen.switch_tab_next(),
- ScreenInstruction::SwitchTabPrev => screen.switch_tab_prev(),
- ScreenInstruction::CloseTab => screen.close_tab(),
- ScreenInstruction::ApplyLayout((layout, new_pane_pids)) => {
- screen.apply_layout(layout, new_pane_pids);
- command_is_executing.done_opening_new_pane();
- }
- ScreenInstruction::Quit => {
- break;
- }
- }
- }
- }
- })
- .unwrap();
-
- let wasm_thread = thread::Builder::new()
- .name("wasm".to_string())
- .spawn({
- let mut send_pty_instructions = send_pty_instructions.clone();
- let mut send_screen_instructions = send_screen_instructions.clone();
- let mut send_app_instructions = send_app_instructions.clone();
-
- let store = Store::default();
- let mut plugin_id = 0;
- let mut plugin_map = HashMap::new();
-
- move || loop {
- let (event, mut err_ctx) = receive_plugin_instructions
- .recv()
- .expect("failed to receive event on channel");
- err_ctx.add_call(ContextType::Plugin(PluginContext::from(&event)));
- send_screen_instructions.update(err_ctx);
- send_pty_instructions.update(err_ctx);
- send_app_instructions.update(err_ctx);
- match event {
- PluginInstruction::Load(pid_tx, path) => {
- let project_dirs =
- ProjectDirs::from("org", "Mosaic Contributors", "Mosaic").unwrap();
- let plugin_dir = project_dirs.data_dir().join("plugins/");
- let root_plugin_dir = Path::new(MOSAIC_ROOT_PLUGIN_DIR);
- let wasm_bytes = fs::read(&path)
- .or_else(|_| fs::read(&path.with_extension("wasm")))
- .or_else(|_| fs::read(&plugin_dir.join(&path).with_extension("wasm")))
- .or_else(|_| {
- fs::read(&root_plugin_dir.join(&path).with_extension("wasm"))
- })
- .unwrap_or_else(|_| panic!("cannot find plugin {}", &path.display()));
-
- // FIXME: Cache this compiled module on disk. I could use `(de)serialize_to_file()` for that
- let module = Module::new(&store, &wasm_bytes).unwrap();
-
- let output = Pipe::new();
- let input = Pipe::new();
- let mut wasi_env = WasiState::new("mosaic")
- .env("CLICOLOR_FORCE", "1")
- .preopen(|p| {
- p.directory(".") // FIXME: Change this to a more meaningful dir
- .alias(".")
- .read(true)
- .write(true)
- .create(true)
- })
- .unwrap()
- .stdin(Box::new(input))
- .stdout(Box::new(output))
- .finalize()
- .unwrap();
-
- let wasi = wasi_env.import_object(&module).unwrap();
-
- let plugin_env = PluginEnv {
- plugin_id,
- send_pty_instructions: send_pty_instructions.clone(),
- send_screen_instructions: send_screen_instructions.clone(),
- send_app_instructions: send_app_instructions.clone(),
- wasi_env,
- };
-
- let mosaic = mosaic_imports(&store, &plugin_env);
- let instance = Instance::new(&module, &mosaic.chain_back(wasi)).unwrap();
-
- let start = instance.exports.get_function("_start").unwrap();
-
- // This eventually calls the `.init()` method
- start.call(&[]).unwrap();
-
- plugin_map.insert(plugin_id, (instance, plugin_env));
- pid_tx.send(plugin_id).unwrap();
- plugin_id += 1;
- }
- PluginInstruction::Draw(buf_tx, pid, rows, cols) => {
- let (instance, plugin_env) = plugin_map.get(&pid).unwrap();
-
- let draw = instance.exports.get_function("draw").unwrap();
-
- draw.call(&[Value::I32(rows as i32), Value::I32(cols as i32)])
- .unwrap();
-
- buf_tx.send(wasi_stdout(&plugin_env.wasi_env)).unwrap();
- }
- // FIXME: Deduplicate this with the callback below!
- PluginInstruction::Input(pid, input_bytes) => {
- let (instance, plugin_env) = plugin_map.get(&pid).unwrap();
-
- let handle_key = instance.exports.get_function("handle_key").unwrap();
- for key in input_bytes.keys() {
- if let Ok(key) = key {
- wasi_write_string(
- &plugin_env.wasi_env,
- &serde_json::to_string(&key).unwrap(),
- );
- handle_key.call(&[]).unwrap();
- }
- }
-
- drop(send_screen_instructions.send(ScreenInstruction::Render));
- }
- PluginInstruction::GlobalInput(input_bytes) => {
- // FIXME: Set up an event subscription system, and timed callbacks
- for (instance, plugin_env) in plugin_map.values() {
- let handler =
- instance.exports.get_function("handle_global_key").unwrap();
- for key in input_bytes.keys() {
- if let Ok(key) = key {
- wasi_write_string(
- &plugin_env.wasi_env,
- &serde_json::to_string(&key).unwrap(),
- );
- handler.call(&[]).unwrap();
- }
- }
- }
-
- drop(send_screen_instructions.send(ScreenInstruction::Render));
- }
- PluginInstruction::Unload(pid) => drop(plugin_map.remove(&pid)),
- PluginInstruction::Quit => break,
- }
- }
- })
- .unwrap();
-
- // TODO: currently we don't wait for this to quit
- // because otherwise the app will hang. Need to fix this so it both
- // listens to the ipc-bus and is able to quit cleanly
- #[cfg(not(test))]
- let _ipc_thread = thread::Builder::new()
- .name("ipc_server".to_string())
- .spawn({
- use std::io::Read;
- let mut send_pty_instructions = send_pty_instructions.clone();
- let mut send_screen_instructions = send_screen_instructions.clone();
- move || {
- std::fs::remove_file(MOSAIC_IPC_PIPE).ok();
- let listener = std::os::unix::net::UnixListener::bind(MOSAIC_IPC_PIPE)
- .expect("could not listen on ipc socket");
- let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
- err_ctx.add_call(ContextType::IPCServer);
- send_pty_instructions.update(err_ctx);
- send_screen_instructions.update(err_ctx);
-
- for stream in listener.incoming() {
- match stream {
- Ok(mut stream) => {
- let mut buffer = [0; 65535]; // TODO: more accurate
- let _ = stream
- .read(&mut buffer)
- .expect("failed to parse ipc message");
- let decoded: ApiCommand = bincode::deserialize(&buffer)
- .expect("failed to deserialize ipc message");
- match &decoded {
- ApiCommand::OpenFile(file_name) => {
- let path = PathBuf::from(file_name);
- send_pty_instructions
- .send(PtyInstruction::SpawnTerminal(Some(path)))
- .unwrap();
- }
- ApiCommand::SplitHorizontally => {
- send_pty_instructions
- .send(PtyInstruction::SpawnTerminalHorizontally(None))
- .unwrap();
- }
- ApiCommand::SplitVertically => {
- send_pty_instructions
- .send(PtyInstruction::SpawnTerminalVertically(None))
- .unwrap();
- }
- ApiCommand::MoveFocus => {
- send_screen_instructions
- .send(ScreenInstruction::MoveFocus)
- .unwrap();
- }
- }
- }
- Err(err) => {
- panic!("err {:?}", err);
- }
- }
- }
- }
- })
- .unwrap();
-
- let _stdin_thread = thread::Builder::new()
- .name("stdin_handler".to_string())
- .spawn({
- let send_screen_instructions = send_screen_instructions.clone();
- let send_pty_instructions = send_pty_instructions.clone();
- let send_plugin_instructions = send_plugin_instructions.clone();
- let os_input = os_input.clone();
- move || {
- input_loop(
- os_input,
- command_is_executing,
- send_screen_instructions,
- send_pty_instructions,
- send_plugin_instructions,
- send_app_instructions,
- )
- }
- });
-
- #[warn(clippy::never_loop)]
- loop {
- let (app_instruction, mut err_ctx) = receive_app_instructions
- .recv()
- .expect("failed to receive app instruction on channel");
-
- err_ctx.add_call(ContextType::App(AppContext::from(&app_instruction)));
- send_screen_instructions.update(err_ctx);
- send_pty_instructions.update(err_ctx);
- match app_instruction {
- AppInstruction::GetState(state_tx) => drop(state_tx.send(app_state.clone())),
- AppInstruction::SetState(state) => app_state = state,
- AppInstruction::Exit => {
- break;
- }
- AppInstruction::Error(backtrace) => {
- let _ = send_screen_instructions.send(ScreenInstruction::Quit);
- let _ = screen_thread.join();
- let _ = send_pty_instructions.send(PtyInstruction::Quit);
- let _ = pty_thread.join();
- let _ = send_plugin_instructions.send(PluginInstruction::Quit);
- let _ = wasm_thread.join();
- os_input.unset_raw_mode(0);
- let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.rows, 1);
- let error = format!("{}\n{}", goto_start_of_last_line, backtrace);
- let _ = os_input
- .get_stdout_writer()
- .write(error.as_bytes())
- .unwrap();
- std::process::exit(1);
- }
- }
- }
-
- let _ = send_pty_instructions.send(PtyInstruction::Quit);
- pty_thread.join().unwrap();
- let _ = send_screen_instructions.send(ScreenInstruction::Quit);
- screen_thread.join().unwrap();
- let _ = send_plugin_instructions.send(PluginInstruction::Quit);
- wasm_thread.join().unwrap();
-
- // cleanup();
- let reset_style = "\u{1b}[m";
- let show_cursor = "\u{1b}[?25h";
- let restore_snapshot = "\u{1b}[?1049l";
- let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.rows, 1);
- let goodbye_message = format!(
- "{}\n{}{}{}Bye from Mosaic!",
- goto_start_of_last_line, restore_snapshot, reset_style, show_cursor
- );
-
- os_input.unset_raw_mode(0);
- let _ = os_input
- .get_stdout_writer()
- .write(goodbye_message.as_bytes())
- .unwrap();
- os_input.get_stdout_writer().flush().unwrap();
-}
diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs
deleted file mode 100644
index 41ee3b3c..00000000
--- a/src/common/os_input_output.rs
+++ /dev/null
@@ -1,217 +0,0 @@
-use crate::panes::PositionAndSize;
-use nix::fcntl::{fcntl, FcntlArg, OFlag};
-use nix::pty::{forkpty, Winsize};
-use nix::sys::signal::{kill, Signal};
-use nix::sys::termios::{cfmakeraw, tcdrain, tcgetattr, tcsetattr, SetArg, Termios};
-use nix::sys::wait::waitpid;
-use nix::unistd::{read, write, ForkResult, Pid};
-use std::io::prelude::*;
-use std::io::{stdin, Write};
-use std::os::unix::io::RawFd;
-use std::path::PathBuf;
-use std::process::{Child, Command};
-use std::sync::{Arc, Mutex};
-
-use std::env;
-
-fn into_raw_mode(pid: RawFd) {
- let mut tio = tcgetattr(pid).expect("could not get terminal attribute");
- cfmakeraw(&mut tio);
- match tcsetattr(pid, SetArg::TCSANOW, &tio) {
- Ok(_) => {}
- Err(e) => panic!("error {:?}", e),
- };
-}
-
-fn unset_raw_mode(pid: RawFd, orig_termios: Termios) {
- match tcsetattr(pid, SetArg::TCSANOW, &orig_termios) {
- Ok(_) => {}
- Err(e) => panic!("error {:?}", e),
- };
-}
-
-pub fn get_terminal_size_using_fd(fd: RawFd) -> PositionAndSize {
- // TODO: do this with the nix ioctl
- use libc::ioctl;
- use libc::TIOCGWINSZ;
-
- let mut winsize = Winsize {
- ws_row: 0,
- ws_col: 0,
- ws_xpixel: 0,
- ws_ypixel: 0,
- };
-
- unsafe { ioctl(fd, TIOCGWINSZ, &mut winsize) };
- PositionAndSize::from(winsize)
-}
-
-pub fn set_terminal_size_using_fd(fd: RawFd, columns: u16, rows: u16) {
- // TODO: do this with the nix ioctl
- use libc::ioctl;
- use libc::TIOCSWINSZ;
-
- let winsize = Winsize {
- ws_col: columns,
- ws_row: rows,
- ws_xpixel: 0,
- ws_ypixel: 0,
- };
- unsafe { ioctl(fd, TIOCSWINSZ, &winsize) };
-}
-
-fn handle_command_exit(mut child: Child) {
- let signals = ::signal_hook::iterator::Signals::new(&[::signal_hook::SIGINT]).unwrap();
- 'handle_exit: loop {
- match child.try_wait() {
- Ok(Some(_status)) => {
- // TODO: handle errors?
- break;
- }
- Ok(None) => {
- ::std::thread::sleep(::std::time::Duration::from_millis(100));
- }
- Err(e) => panic!("error attempting to wait: {}", e),
- }
-
- for signal in signals.pending() {
- // FIXME: We need to handle more signals here!
- match signal {
- signal_hook::SIGINT => {
- child.kill().unwrap();
- child.wait().unwrap();
- break 'handle_exit;
- }
- _ => {}
- }
- }
- }
-}
-
-fn spawn_terminal(file_to_open: Option<PathBuf>, orig_termios: Termios) -> (RawFd, RawFd) {
- let (pid_primary, pid_secondary): (RawFd, RawFd) = {
- match forkpty(None, Some(&orig_termios)) {
- Ok(fork_pty_res) => {
- let pid_primary = fork_pty_res.master;
- let pid_secondary = match fork_pty_res.fork_result {
- ForkResult::Parent { child } => {
- // fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::empty())).expect("could not fcntl");
- fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))
- .expect("could not fcntl");
- child
- }
- ForkResult::Child => match file_to_open {
- Some(file_to_open) => {
- if env::var("EDITOR").is_err() && env::var("VISUAL").is_err() {
- panic!("Can't edit files if an editor is not defined. To fix: define the EDITOR or VISUAL environment variables with the path to your editor (eg. /usr/bin/vim)");
- }
- let editor =
- env::var("EDITOR").unwrap_or_else(|_| env::var("VISUAL").unwrap());
-
- let child = Command::new(editor)
- .args(&[file_to_open])
- .spawn()
- .expect("failed to spawn");
- handle_command_exit(child);
- ::std::process::exit(0);
- }
- None => {
- let child = Command::new(env::var("SHELL").unwrap())
- .spawn()
- .expect("failed to spawn");
- handle_command_exit(child);
- ::std::process::exit(0);
- }
- },
- };
- (pid_primary, pid_secondary.as_raw())
- }
- Err(e) => {
- panic!("failed to fork {:?}", e);
- }
- }
- };
- (pid_primary, pid_secondary)
-}
-
-#[derive(Clone)]
-pub struct OsInputOutput {
- orig_termios: Arc<Mutex<Termios>>,
-}
-
-pub trait OsApi: Send + Sync {
- fn get_terminal_size_using_fd(&self, pid: RawFd) -> PositionAndSize;
- fn set_terminal_size_using_fd(&mut self, pid: RawFd, cols: u16, rows: u16);
- fn set_raw_mode(&mut self, pid: RawFd);
- fn unset_raw_mode(&mut self, pid: RawFd);
- fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> (RawFd, RawFd);
- fn read_from_tty_stdout(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error>;
- fn write_to_tty_stdin(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error>;
- fn tcdrain(&mut self, pid: RawFd) -> Result<(), nix::Error>;
- fn kill(&mut self, pid: RawFd) -> Result<(), nix::Error>;
- fn read_from_stdin(&self) -> Vec<u8>;
- fn get_stdout_writer(&self) -> Box<dyn Write>;
- fn box_clone(&self) -> Box<dyn OsApi>;
-}
-
-impl OsApi for OsInputOutput {
- fn get_terminal_size_using_fd(&self, pid: RawFd) -> PositionAndSize {
- get_terminal_size_using_fd(pid)
- }
- fn set_terminal_size_using_fd(&mut self, pid: RawFd, cols: u16, rows: u16) {
- set_terminal_size_using_fd(pid, cols, rows);
- }
- fn set_raw_mode(&mut self, pid: RawFd) {
- into_raw_mode(pid);
- }
- fn unset_raw_mode(&mut self, pid: RawFd) {
- let orig_termios = self.orig_termios.lock().unwrap();
- unset_raw_mode(pid, orig_termios.clone());
- }
- fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> (RawFd, RawFd) {
- let orig_termios = self.orig_termios.lock().unwrap();
- spawn_terminal(file_to_open, orig_termios.clone())
- }
- fn read_from_tty_stdout(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error> {
- read(pid, buf)
- }
- fn write_to_tty_stdin(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error> {
- write(pid, buf)
- }
- fn tcdrain(&mut self, pid: RawFd) -> Result<(), nix::Error> {
- tcdrain(pid)
- }
- fn box_clone(&self) -> Box<dyn OsApi> {
- Box::new((*self).clone())
- }
- fn read_from_stdin(&self) -> Vec<u8> {
- let stdin = stdin();
- let mut stdin = stdin.lock();
- let buffer = stdin.fill_buf().unwrap();
- let length = buffer.len();
- let read_bytes = Vec::from(buffer);
- stdin.consume(length);
- read_bytes
- }
- fn get_stdout_writer(&self) -> Box<dyn Write> {
- let stdout = ::std::io::stdout();
- Box::new(stdout)
- }
- fn kill(&mut self, fd: RawFd) -> Result<(), nix::Error> {
- kill(Pid::from_raw(fd), Some(Signal::SIGINT)).unwrap();
- waitpid(Pid::from_raw(fd), None).unwrap();
- Ok(())
- }
-}
-
-impl Clone for Box<dyn OsApi> {
- fn clone(&self) -> Box<dyn OsApi> {
- self.box_clone()
- }
-}
-
-pub fn get_os_input() -> OsInputOutput {
- let current_termios = tcgetattr(0).unwrap();
- let orig_termios = Arc::new(Mutex::new(current_termios));
- OsInputOutput { orig_termios }
-}
diff --git a/src/common/pty_bus.rs b/src/common/pty_bus.rs
deleted file mode 100644
index 9a00d1de..00000000
--- a/src/common/pty_bus.rs
+++ /dev/null
@@ -1,326 +0,0 @@
-use ::async_std::stream::*;
-use ::async_std::task;
-use ::async_std::task::*;
-use ::std::collections::HashMap;
-use ::std::os::unix::io::RawFd;
-use ::std::pin::*;
-use ::std::sync::mpsc::Receiver;
-use ::std::time::{Duration, Instant};
-use ::vte;
-use std::path::PathBuf;
-
-use super::{ScreenInstruction, SenderWithContext, OPENCALLS};
-use crate::os_input_output::OsApi;
-use crate::utils::logging::debug_to_file;
-use crate::{
- errors::{ContextType, ErrorContext},
- panes::PaneId,
-};
-use crate::{layout::Layout, wasm_vm::PluginInstruction};
-
-pub struct ReadFromPid {
- pid: RawFd,
- os_input: Box<dyn OsApi>,
-}
-
-impl ReadFromPid {
- pub fn new(pid: &RawFd, os_input: Box<dyn OsApi>) -> ReadFromPid {
- ReadFromPid {
- pid: *pid,
- os_input,
- }
- }
-}
-
-impl Stream for ReadFromPid {
- type Item = Vec<u8>;
- fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
- let mut read_buffer = [0; 65535];
- let pid = self.pid;
- let read_result = &self.os_input.read_from_tty_stdout(pid, &mut read_buffer);
- match read_result {
- Ok(res) => {
- if *res == 0 {
- // indicates end of file
- Poll::Ready(None)
- } else {
- let res = Some(read_buffer[..=*res].to_vec());
- Poll::Ready(res)
- }
- }
- Err(e) => {
- match e {
- nix::Error::Sys(errno) => {
- if *errno == nix::errno::Errno::EAGAIN {
- Poll::Ready(Some(vec![])) // TODO: better with timeout waker somehow
- } else {
- Poll::Ready(None)
- }
- }
- _ => Poll::Ready(None),
- }
- }
- }
- }
-}
-
-#[derive(Debug, Clone)]
-pub enum VteEvent {
- // TODO: try not to allocate Vecs
- Print(char),
- Execute(u8), // byte
- Hook(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char
- Put(u8), // byte
- Unhook,
- OscDispatch(Vec<Vec<u8>>, bool), // params, bell_terminated
- CsiDispatch(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char
- EscDispatch(Vec<u8>, bool, u8), // intermediates, ignore, byte
-}
-
-struct VteEventSender {
- id: RawFd,
- sender: SenderWithContext<ScreenInstruction>,
-}
-
-impl VteEventSender {
- pub fn new(id: RawFd, sender: SenderWithContext<ScreenInstruction>) -> Self {
- VteEventSender { id, sender }
- }
-}
-
-impl vte::Perform for VteEventSender {
- fn print(&mut self, c: char) {
- let _ = self
- .sender
- .send(ScreenInstruction::Pty(self.id, VteEvent::Print(c)));
- }
- fn execute(&mut self, byte: u8) {
- let _ = self
- .sender
- .send(ScreenInstruction::Pty(self.id, VteEvent::Execute(byte)));
- }
-
- fn hook(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) {
- let params = params.iter().copied().collect();
- let intermediates = intermediates.iter().copied().collect();
- let instruction =
- ScreenInstruction::Pty(self.id, VteEvent::Hook(params, intermediates, ignore, c));
- let _ = self.sender.send(instruction);
- }
-
- fn put(&mut self, byte: u8) {
- let _ = self
- .sender
- .send(ScreenInstruction::Pty(self.id, VteEvent::Put(byte)));
- }
-
- fn unhook(&mut self) {
- let _ = self
- .sender
- .send(ScreenInstruction::Pty(self.id, VteEvent::Unhook));
- }
-
- fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
- let params = params.iter().map(|p| p.to_vec()).collect();
- let instruction =
- ScreenInstruction::Pty(self.id, VteEvent::OscDispatch(params, bell_terminated));
- let _ = self.sender.send(instruction);
- }
-
- fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) {
- let params = params.iter().copied().collect();
- let intermediates = intermediates.iter().copied().collect();
- let instruction = ScreenInstruction::Pty(
- self.id,
- VteEvent::CsiDispatch(params, intermediates, ignore, c),
- );
- let _ = self.sender.send(instruction);
- }
-
- fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
- let intermediates = intermediates.iter().copied().collect();
- let instruction =
- ScreenInstruction::Pty(self.id, VteEvent::EscDispatch(intermediates, ignore, byte));
- let _ = self.sender.send(instruction);
- }
-}
-
-#[derive(Clone, Debug)]
-pub enum PtyInstruction {
- SpawnTerminal(Option<PathBuf>),
- SpawnTerminalVertically(Option<PathBuf>),
- SpawnTerminalHorizontally(Option<PathBuf>),
- NewTab,
- ClosePane(PaneId),
- CloseTab(Vec<PaneId>),
- Quit,
-}
-
-pub struct PtyBus {
- pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
- pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
- pub receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>,
- pub id_to_child_pid: HashMap<RawFd, RawFd>,
- os_input: Box<dyn OsApi>,
- debug_to_file: bool,
- task_handles: HashMap<RawFd, JoinHandle<()>>,
-}
-
-fn stream_terminal_bytes(
- pid: RawFd,
- mut send_screen_instructions: SenderWithContext<ScreenInstruction>,
- os_input: Box<dyn OsApi>,
- debug: bool,
-) -> JoinHandle<()> {
- let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
- task::spawn({
- async move {
- err_ctx.add_call(ContextType::AsyncTask);
- send_screen_instructions.update(err_ctx);
- let mut vte_parser = vte::Parser::new();
- let mut vte_event_sender = VteEventSender::new(pid, send_screen_instructions.clone());
- let mut terminal_bytes = ReadFromPid::new(&pid, os_input);
-
- let mut last_byte_receive_time: Option<Instant> = None;
- let mut pending_render = false;
- let max_render_pause = Duration::from_millis(30);
-
- while let Some(bytes) = terminal_bytes.next().await {
- let bytes_is_empty = bytes.is_empty();
- for byte in bytes {
- if debug {
- debug_to_file(byte, pid).unwrap();
- }
- vte_parser.advance(&mut vte_event_sender, byte);
- }
- if !bytes_is_empty {
- // for UX reasons, if we got something on the wire, we only send the render notice if:
- // 1. there aren't any more bytes on the wire afterwards
- // 2. a certain period (currently 30ms) has elapsed since the last render
- // (otherwise if we get a large amount of data, the display would hang
- // until it's done)
- // 3. the stream has ended, and so we render 1 last time
- match last_byte_receive_time.as_mut() {
- Some(receive_time) => {
- if receive_time.elapsed() > max_render_pause {
- pending_render = false;
- let _ = send_screen_instructions.send(ScreenInstruction::Render);
- last_byte_receive_time = Some(Instant::now());
- } else {
- pending_render = true;
- }
- }
- None => {
- last_byte_receive_time = Some(Instant::now());
- pending_render = true;
- }
- };
- } else {
- if pending_render {
- pending_render = false;
- let _ = send_screen_instructions.send(ScreenInstruction::Render);
- }
- last_byte_receive_time = None;
- task::sleep(::std::time::Duration::from_millis(10)).await;
- }
- }
- send_screen_instructions
- .send(ScreenInstruction::Render)
- .unwrap();
- #[cfg(not(test))]
- // this is a little hacky, and is because the tests end the file as soon as
- // we read everything, rather than hanging until there is new data
- // a better solution would be to fix the test fakes, but this will do for now
- send_screen_instructions
- .send(ScreenInstruction::ClosePane(PaneId::Terminal(pid)))
- .unwrap();
- }
- })
-}
-
-impl PtyBus {
- pub fn new(
- receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>,
- send_screen_instructions: SenderWithContext<ScreenInstruction>,
- send_plugin_instructions: SenderWithContext<PluginInstruction>,
- os_input: Box<dyn OsApi>,
- debug_to_file: bool,
- ) -> Self {
- PtyBus {
- send_screen_instructions,
- send_plugin_instructions,
- receive_pty_instructions,
- os_input,
- id_to_child_pid: HashMap::new(),
- debug_to_file,
- task_handles: HashMap::new(),
- }
- }
- pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> RawFd {
- let (pid_primary, pid_secondary): (RawFd, RawFd) =
- self.os_input.spawn_terminal(file_to_open);
- let task_handle = stream_terminal_bytes(
- pid_primary,
- self.send_screen_instructions.clone(),
- self.os_input.clone(),
- self.debug_to_file,
- );
- self.task_handles.insert(pid_primary, task_handle);
- self.id_to_child_pid.insert(pid_primary, pid_secondary);
- pid_primary
- }
- pub fn spawn_terminals_for_layout(&mut self, layout: Layout) {
- let total_panes = layout.total_terminal_panes();
- let mut new_pane_pids = vec![];
- for _ in 0..total_panes {
- let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(None);
- self.id_to_child_pid.insert(pid_primary, pid_secondary);
- new_pane_pids.push(pid_primary);
- }
- self.send_screen_instructions
- .send(ScreenInstruction::ApplyLayout((
- layout,
- new_pane_pids.clone(),
- )))
- .unwrap();
- for id in new_pane_pids {
- let task_handle = stream_terminal_bytes(
- id,
- self.send_screen_instructions.clone(),
- self.os_input.clone(),
- self.debug_to_file,
- );
- self.task_handles.insert(id, task_handle);
- }
- }
- pub fn close_pane(&mut self, id: PaneId) {
- match id {
- PaneId::Terminal(id) => {
- let child_pid = self.id_to_child_pid.remove(&id).unwrap();
- let handle = self.task_handles.remove(&id).unwrap();
- self.os_input.kill(child_pid).unwrap();
- task::block_on(async {
- handle.cancel().await;
- });
- }
- PaneId::Plugin(pid) => drop(
- self.send_plugin_instructions
- .send(PluginInstruction::Unload(pid)),
- ),
- }
- }
- pub fn close_tab(&mut self, ids: Vec<PaneId>) {
- ids.iter().for_each(|&id| {
- self.close_pane(id);
- });
- }
-}
-
-impl Drop for PtyBus {
- fn drop(&mut self) {
- let child_ids: Vec<RawFd> = self.id_to_child_pid.keys().copied().collect();
- for id in child_ids {
- self.close_pane(PaneId::Terminal(id));
- }
- }
-}
diff --git a/src/common/screen.rs b/src/common/screen.rs
deleted file mode 100644
index 6ae89ddc..00000000
--- a/src/common/screen.rs
+++ /dev/null
@@ -1,201 +0,0 @@
-use std::collections::BTreeMap;
-use std::os::unix::io::RawFd;
-use std::sync::mpsc::Receiver;
-
-use super::{AppInstruction, SenderWithContext};
-use crate::os_input_output::OsApi;
-use crate::panes::PositionAndSize;
-use crate::pty_bus::{PtyInstruction, VteEvent};
-use crate::tab::Tab;
-use crate::{errors::ErrorContext, wasm_vm::PluginInstruction};
-use crate::{layout::Layout, panes::PaneId};
-
-/*
- * Screen
- *
- * this holds multiple tabs, each one holding multiple panes
- * it tracks the active tab and controls tab switching, all the rest
- * is performed in Tab
- *
- */
-
-#[derive(Debug, Clone)]
-pub enum ScreenInstruction {
- Pty(RawFd, VteEvent),
- Render,
- NewPane(PaneId),
- HorizontalSplit(PaneId),
- VerticalSplit(PaneId),
- WriteCharacter(Vec<u8>),
- ResizeLeft,
- ResizeRight,
- ResizeDown,
- ResizeUp,
- MoveFocus,
- MoveFocusLeft,
- MoveFocusDown,
- MoveFocusUp,
- MoveFocusRight,
- Quit,
- ScrollUp,
- ScrollDown,
- ClearScroll,
- CloseFocusedPane,
- ToggleActiveTerminalFullscreen,
- SetSelectable(PaneId, bool),
- SetMaxHeight(PaneId, usize),
- SetInvisibleBorders(PaneId, bool),
- ClosePane(PaneId),
- ApplyLayout((Layout, Vec<RawFd>)),
- NewTab(RawFd),
- SwitchTabNext,
- SwitchTabPrev,
- CloseTab,
-}
-
-pub struct Screen {
- pub receiver: Receiver<(ScreenInstruction, ErrorContext)>,
- max_panes: Option<usize>,
- tabs: BTreeMap<usize, Tab>,
- pub send_pty_instructions: SenderWithContext<PtyInstruction>,
- pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
- pub send_app_instructions: SenderWithContext<AppInstruction>,
- full_screen_ws: PositionAndSize,
- active_tab_index: Option<usize>,
- os_api: Box<dyn OsApi>,
-}
-
-impl Screen {
- pub fn new(
- receive_screen_instructions: Receiver<(ScreenInstruction, ErrorContext)>,
- send_pty_instructions: SenderWithContext<PtyInstruction>,
- send_plugin_instructions: SenderWithContext<PluginInstruction>,
- send_app_instructions: SenderWithContext<AppInstruction>,
- full_screen_ws: &PositionAndSize,
- os_api: Box<dyn OsApi>,
- max_panes: Option<usize>,
- ) -> Self {
- Screen {
- receiver: receive_screen_instructions,
- max_panes,
- send_pty_instructions,
- send_plugin_instructions,
- send_app_instructions,
- full_screen_ws: *full_screen_ws,
- active_tab_index: None,
- tabs: BTreeMap::new(),
- os_api,
- }
- }
- pub fn new_tab(&mut self, pane_id: RawFd) {
- let tab_index = self.get_next_tab_index();
- let tab = Tab::new(
- tab_index,
- &self.full_screen_ws,
- self.os_api.clone(),
- self.send_pty_instructions.clone(),
- self.send_plugin_instructions.clone(),
- self.send_app_instructions.clone(),
- self.max_panes,
- Some(PaneId::Terminal(pane_id)),
- );
- self.active_tab_index = Some(tab_index);
- self.tabs.insert(tab_index, tab);
- self.render();
- }
- fn get_next_tab_index(&self) -> usize {
- if let Some(index) = self.tabs.keys().last() {
- *index + 1
- } else {
- 0
- }
- }
- pub fn switch_tab_next(&mut self) {
- let active_tab_id = self.get_active_tab().unwrap().index;
- let tab_ids: Vec<usize> = self.tabs.keys().copied().collect();
- let first_tab = tab_ids.get(0).unwrap();
- let active_tab_id_position = tab_ids.iter().position(|id| id == &active_tab_id).unwrap();
- if let Some(next_tab) = tab_ids.get(active_tab_id_position + 1) {
- self.active_tab_index = Some(*next_tab);
- } else {
- self.active_tab_index = Some(*first_tab);
- }
- self.render();
- }
- pub fn switch_tab_prev(&mut self) {
- let active_tab_id = self.get_active_tab().unwrap().index;
- let tab_ids: Vec<usize> = self.tabs.keys().copied().collect();
- let first_tab = tab_ids.get(0).unwrap();
- let last_tab = tab_ids.last().unwrap();
-
- let active_tab_id_position = tab_ids.iter().position(|id| id == &active_tab_id).unwrap();
- if active_tab_id == *first_tab {
- self.active_tab_index = Some(*last_tab)
- } else if let Some(prev_tab) = tab_ids.get(active_tab_id_position - 1) {
- self.active_tab_index = Some(*prev_tab)
- }
- self.render();
- }
- pub fn close_tab(&mut self) {
- let active_tab_index = self.active_tab_index.unwrap();
- if self.tabs.len() > 1 {
- self.switch_tab_prev();
- }
- let active_tab = self.tabs.remove(&active_tab_index).unwrap();
- let pane_ids = active_tab.get_pane_ids();
- // below we don't check the result of sending the CloseTab instruction to the pty thread
- // because this might be happening when the app is closing, at which point the pty thread
- // has already closed and this would result in an error
- let _ = self
- .send_pty_instructions
- .send(PtyInstruction::CloseTab(pane_ids));
- if self.tabs.is_empty() {
- self.active_tab_index = None;
- self.send_app_instructions
- .send(AppInstruction::Exit)
- .unwrap();
- }
- }
- pub fn render(&mut self) {
- if let Some(active_tab) = self.get_active_tab_mut() {
- if active_tab.get_active_pane().is_some() {
- active_tab.render();
- } else {
- self.close_tab();
- }
- };
- }
-
- pub fn get_active_tab(&self) -> Option<&Tab> {
- match self.active_tab_index {
- Some(tab) => self.tabs.get(&tab),
- None => None,
- }
- }
- pub fn get_tabs_mut(&mut self) -> &mut BTreeMap<usize, Tab> {
- &mut self.tabs
- }
- pub fn get_active_tab_mut(&mut self) -> Option<&mut Tab> {
- let tab = match self.active_tab_index {
- Some(tab) => self.get_tabs_mut().get_mut(&tab),
- None => None,
- };
- tab
- }
- pub fn apply_layout(&mut self, layout: Layout, new_pids: Vec<RawFd>) {
- let tab_index = self.get_next_tab_index();
- let mut tab = Tab::new(
- tab_index,
- &self.full_screen_ws,
- self.os_api.clone(),
- self.send_pty_instructions.clone(),
- self.send_plugin_instructions.clone(),
- self.send_app_instructions.clone(),
- self.max_panes,
- None,
- );
- tab.apply_layout(layout, new_pids);
- self.active_tab_index = Some(tab_index);
- self.tabs.insert(tab_index, tab);
- }
-}
diff --git a/src/common/utils/consts.rs b/src/common/utils/consts.rs
deleted file mode 100644
index 7a0c9ddf..00000000
--- a/src/common/utils/consts.rs
+++ /dev/null
@@ -1,6 +0,0 @@
-pub const MOSAIC_TMP_DIR: &str = "/tmp/mosaic";
-pub const MOSAIC_TMP_LOG_DIR: &str = "/tmp/mosaic/mosaic-log";
-pub const MOSAIC_TMP_LOG_FILE: &str = "/tmp/mosaic/mosaic-log/log.txt";
-pub const MOSAIC_IPC_PIPE: &str = "/tmp/mosaic/ipc";
-pub const MOSAIC_ROOT_PLUGIN_DIR: &str = "/usr/share/mosaic/plugins";
-pub const MOSAIC_ROOT_LAYOUT_DIR: &str = "/usr/share/mosaic/layouts";
diff --git a/src/common/utils/logging.rs b/src/common/utils/logging.rs
deleted file mode 100644
index 05af455c..00000000
--- a/src/common/utils/logging.rs
+++ /dev/null
@@ -1,76 +0,0 @@
-use std::{
- fs,
- io::{self, prelude::*},
- os::unix::io::RawFd,
- path::PathBuf,
-};
-
-use crate::utils::consts::{MOSAIC_TMP_LOG_DIR, MOSAIC_TMP_LOG_FILE};
-
-pub fn atomic_create_file(file_name: &str) {
- let _ = fs::OpenOptions::new().create(true).open(file_name);
-}
-
-pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> {
- if let Err(e) = fs::create_dir(dir_name) {
- if e.kind() == std::io::ErrorKind::AlreadyExists {
- Ok(())
- } else {
- Err(e)
- }
- } else {
- Ok(())
- }
-}
-
-pub fn debug_log_to_file(mut message: String) -> io::Result<()> {
- message.push('\n');
- debug_log_to_file_without_newline(message)
-}
-
-pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> {
- atomic_create_file(MOSAIC_TMP_LOG_FILE);
- let mut file = fs::OpenOptions::new()
- .append(true)
- .create(true)
- .open(MOSAIC_TMP_LOG_FILE)?;
- file.write_all(message.as_bytes())
-}
-
-pub fn _debug_log_to_file_pid_3(message: String, pid: RawFd) -> io::Result<()> {
- if pid == 3 {
- debug_log_to_file(message)
- } else {
- Ok(())
- }
-}
-
-#[allow(dead_code)]
-pub fn delete_log_file() -> io::Result<()> {
- if fs::metadata(MOSAIC_TMP_LOG_FILE).is_ok() {
- fs::remove_file(MOSAIC_TMP_LOG_FILE)
- } else {
- Ok(())
- }
-}
-
-#[allow(dead_code)]
-pub fn delete_log_dir() -> io::Result<()> {
- if fs::metadata(MOSAIC_TMP_LOG_DIR).is_ok() {
- fs::remove_dir_all(MOSAIC_TMP_LOG_DIR)
- } else {
- Ok(())
- }
-}
-
-pub fn debug_to_file(message: u8, pid: RawFd) -> io::Result<()> {
- let mut path = PathBuf::new();
- path.push(MOSAIC_TMP_LOG_DIR);
- path.push(format!("mosaic-{}.log", pid.to_string()));
-
- let mut file = fs::OpenOptions::new()
- .append(true)
- .create(true)
- .open(path)?;
- file.write_all(&[message])
-}
diff --git a/src/common/utils/mod.rs b/src/common/utils/mod.rs
deleted file mode 100644
index 0ea4c76f..00000000
--- a/src/common/utils/mod.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-pub mod consts;
-pub mod logging;
-pub mod shared;
diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs
deleted file mode 100644
index 1d8ca5c3..00000000
--- a/src/common/utils/shared.rs
+++ /dev/null
@@ -1,19 +0,0 @@
-use std::{iter, str::from_utf8};
-
-use strip_ansi_escapes::strip;
-
-pub fn ansi_len(s: &str) -> usize {
- from_utf8(&strip(s.as_bytes()).unwrap())
- .unwrap()
- .chars()
- .count()
-}
-
-pub fn pad_to_size(s: &str, rows: usize, columns: usize) -> String {
- s.lines()
- .map(|l| [l, &str::repeat(" ", columns - ansi_len(l))].concat())
- .chain(iter::repeat(str::repeat(" ", columns)))
- .take(rows)
- .collect::<Vec<_>>()
- .join("\n\r")
-}
diff --git a/src/common/wasm_vm.rs b/src/common/wasm_vm.rs
deleted file mode 100644
index 758d5e8a..00000000
--- a/src/common/wasm_vm.rs
+++ /dev/null
@@ -1,121 +0,0 @@
-use std::{
- path::PathBuf,
- sync::mpsc::{channel, Sender},
-};
-use wasmer::{imports, Function, ImportObject, Store, WasmerEnv};
-use wasmer_wasi::WasiEnv;
-
-use crate::utils::logging::debug_log_to_file;
-
-use super::{
- input::handler::get_help, pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction,
- PaneId, SenderWithContext,
-};
-
-#[derive(Clone, Debug)]
-pub enum PluginInstruction {
- Load(Sender<u32>, PathBuf),
- Draw(Sender<String>, u32, usize, usize), // String buffer, plugin id, rows, cols
- Input(u32, Vec<u8>), // plugin id, input bytes
- GlobalInput(Vec<u8>), // input bytes
- Unload(u32),
- Quit,
-}
-
-#[derive(WasmerEnv, Clone)]
-pub struct PluginEnv {
- pub plugin_id: u32,
- pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
- pub send_app_instructions: SenderWithContext<AppInstruction>,
- pub send_pty_instructions: SenderWithContext<PtyInstruction>, // FIXME: This should be a big bundle of all of the channels
- pub wasi_env: WasiEnv,
-}
-
-// Plugin API ---------------------------------------------------------------------------------------------------------
-
-pub fn mosaic_imports(store: &Store, plugin_env: &PluginEnv) -> ImportObject {
- imports! {
- "mosaic" => {
- "host_open_file" => Function::new_native_with_env(store, plugin_env.clone(), host_open_file),
- "host_set_invisible_borders" => Function::new_native_with_env(store, plugin_env.clone(), host_set_invisible_borders),
- "host_set_max_height" => Function::new_native_with_env(store, plugin_env.clone(), host_set_max_height),
- "host_set_selectable" => Function::new_native_with_env(store, plugin_env.clone(), host_set_selectable),
- "host_get_help" => Function::new_native_with_env(store, plugin_env.clone(), host_get_help),
- }
- }
-}
-
-// FIXME: Bundle up all of the channels! Pair that with WasiEnv?
-fn host_open_file(plugin_env: &PluginEnv) {
- let path = PathBuf::from(wasi_stdout(&plugin_env.wasi_env).lines().next().unwrap());
- plugin_env
- .send_pty_instructions
- .send(PtyInstruction::SpawnTerminal(Some(path)))
- .unwrap();
-}
-
-// FIXME: Think about these naming conventions – should everything be prefixed by 'host'?
-fn host_set_selectable(plugin_env: &PluginEnv, selectable: i32) {
- let selectable = selectable != 0;
- plugin_env
- .send_screen_instructions
- .send(ScreenInstruction::SetSelectable(
- PaneId::Plugin(plugin_env.plugin_id),
- selectable,
- ))
- .unwrap()
-}
-
-fn host_set_max_height(plugin_env: &PluginEnv, max_height: i32) {
- let max_height = max_height as usize;
- plugin_env
- .send_screen_instructions
- .send(ScreenInstruction::SetMaxHeight(
- PaneId::Plugin(plugin_env.plugin_id),
- max_height,
- ))
- .unwrap()
-}
-
-fn host_set_invisible_borders(plugin_env: &PluginEnv, invisible_borders: i32) {
- let invisible_borders = invisible_borders != 0;
- plugin_env
- .send_screen_instructions
- .send(ScreenInstruction::SetInvisibleBorders(
- PaneId::Plugin(plugin_env.plugin_id),
- invisible_borders,
- ))
- .unwrap()
-}
-
-fn host_get_help(plugin_env: &PluginEnv) {
- let (state_tx, state_rx) = channel();
- // FIXME: If I changed the application so that threads were sent the termination
- // signal and joined one at a time, there would be an order to shutdown, so I
- // could get rid of this .is_ok() check and the .try_send()
- if plugin_env
- .send_app_instructions
- .try_send(AppInstruction::GetState(state_tx))
- .is_ok()
- {
- let help = get_help(&state_rx.recv().unwrap().input_state);
- wasi_write_string(&plugin_env.wasi_env, &serde_json::to_string(&help).unwrap());
- }
-}
-
-// Helper Functions ---------------------------------------------------------------------------------------------------
-
-// FIXME: Unwrap city
-pub fn wasi_stdout(wasi_env: &WasiEnv) -> String {
- let mut state = wasi_env.state();
- let wasi_file = state.fs.stdout_mut().unwrap().as_mut().unwrap();
- let mut buf = String::new();
- wasi_file.read_to_string(&mut buf).unwrap();
- buf
-}
-
-pub fn wasi_write_string(wasi_env: &WasiEnv, buf: &str) {
- let mut state = wasi_env.state();
- let wasi_file = state.fs.stdin_mut().unwrap().as_mut().unwrap();
- writeln!(wasi_file, "{}\r", buf).unwrap();
-}