From a847b56b058bb5709677cf2db923e4ee057ef3fc Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 7 Feb 2024 22:01:43 -0600 Subject: [PATCH] cleaning upp --- src/builtin/cd.rs | 4 ++-- src/builtin/tail.rs | 2 +- src/edit.rs | 8 ++++++- src/ext.rs | 58 --------------------------------------------- src/ext/command.rs | 7 ------ src/input.rs | 4 ++-- src/interactive.rs | 43 +-------------------------------- src/main.rs | 41 -------------------------------- src/output.rs | 12 +--------- src/parse.rs | 34 -------------------------- src/run.rs | 4 ---- 11 files changed, 14 insertions(+), 203 deletions(-) delete mode 100644 src/ext.rs delete mode 100644 src/ext/command.rs diff --git a/src/builtin/cd.rs b/src/builtin/cd.rs index e1f0406..8401302 100644 --- a/src/builtin/cd.rs +++ b/src/builtin/cd.rs @@ -2,12 +2,12 @@ use crate::{ error::ExecError, run::{Call, Context, Value}, }; -use std::{env, io::Write, os, process}; +use std::env; pub struct Changedir; impl Call for Changedir { - fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { + fn call(&self, _ctx: &mut Context, args: &[Value]) -> Result { match args.len() { 0 => { todo!() diff --git a/src/builtin/tail.rs b/src/builtin/tail.rs index 1975421..9087261 100644 --- a/src/builtin/tail.rs +++ b/src/builtin/tail.rs @@ -34,7 +34,7 @@ impl Call for Tail { } } Err(e) => { - write!(ctx.stderr, "failed to open file: {}", e); + _ = write!(ctx.stderr, "failed to open file: {}", e); } } } diff --git a/src/edit.rs b/src/edit.rs index 260fe1c..1bff22a 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -259,13 +259,19 @@ impl Editor { StartOfHeading => self.seek_left()?, EndOfTransmission => return Ok(Status::Done), Enquiry => self.seek_right()?, - FormFeed => self.clear_screen()?, + Formfeed => self.clear_screen()?, + Linefeed => self.insert_dot()?, NegativeAcknowledge => self.clear_left()?, cc => debug!("ignored control character: {cc:?}"), } Ok(Status::Ongoing) } + fn insert_dot(&mut self) -> anyhow::Result<()> { + self.insert('\u{2022}')?; + Ok(()) + } + fn submit(&mut self) -> anyhow::Result { self.display.newline()?; let text = self.buffer.pop(); diff --git a/src/ext.rs b/src/ext.rs deleted file mode 100644 index d62d87a..0000000 --- a/src/ext.rs +++ /dev/null @@ -1,58 +0,0 @@ -mod command; - -pub use command::Command; - -/* -Posix Shell builtins: - - alias bg cd command false fc fg getopts hash jobs kill - newgrp pwd read true type ulimit umask unalias wait - -Bourne Shell builtins: - - : - do nothing except expand arguments and perform redirections - . - read and execute a file in the current shell context - break - exit from a loop - cd - change the current working directory to directory - continue - resume the next iteration of a loop - eval - evaluate the arguments as a single command - exec - replace the shell without creating a new process - exit - exits the process with a status - export - exports environment variables - getopts - used by shell scripts to parse arguments - hash - displays the hash table used by the shell to map command names to files - pwd - prints the absolute path of the current directory - readonly - mark each name as readonly so that they cannot be reassigned - return - causes a shell function to stop execution and return a value - shift - shifts the position parameters to the left by n - test - evaluate a conditional expression and return a status of 0 or 1 - times - print out the user and system times used by the shell and its children. sorry, what the fuck is this - trap - execute commands when a given signal is received - umask - set the shell process's file creation mask - unset - clears variable or function names - -Bash Builtins: - - alias - creates an alias for a command - bind - adds key bindings - builtin - access a builtin function even if it has been masked by another function - caller - tells you what line number you're executing? - command - runs a command with a given name, ignoring shell functions - declare - declare variables and give them attributes - echo - echos the outputs - enable - enable and disable builtin shell commands - help - display help text about builtin commands - let - perform arithmetic on shell variables - local - create local variables - logout - exits a login shell with a status - mapfile - read lines from stdin or a file into an array - printf - formats arguments using a format string - read - reads one line from stdin or from a file - readarray - read lines from stdin into an array or something - source - executes the script in the current shell - type - tells you if a thing is an alias, function, builtin, or file command - typeset - synonym for declare, included for korn shell compatibility - ulimit - provides control over the resources available to the processes started by the shell - unalias - clears an alias - -*/ diff --git a/src/ext/command.rs b/src/ext/command.rs deleted file mode 100644 index 8585339..0000000 --- a/src/ext/command.rs +++ /dev/null @@ -1,7 +0,0 @@ -use anyhow::Result; - -pub trait Command { - fn name() -> String; - fn create() -> Self; - fn exec(&mut self, args: Vec<&str>) -> Result; -} diff --git a/src/input.rs b/src/input.rs index cce0a5f..c15510a 100644 --- a/src/input.rs +++ b/src/input.rs @@ -536,7 +536,7 @@ pub enum ControlCharacter { Tab, Linefeed, VTab, - FormFeed, + Formfeed, CarriageReturn, ShiftOut, ShiftIn, @@ -580,7 +580,7 @@ impl TryFrom for ControlCharacter { 9 => Ok(Tab), 10 => Ok(Linefeed), 11 => Ok(VTab), - 12 => Ok(FormFeed), + 12 => Ok(Formfeed), 13 => Ok(CarriageReturn), 14 => Ok(ShiftOut), 15 => Ok(ShiftIn), diff --git a/src/interactive.rs b/src/interactive.rs index af3e97a..dfd3b7e 100644 --- a/src/interactive.rs +++ b/src/interactive.rs @@ -1,5 +1,5 @@ use crate::{ - builtin, edit, + edit, log::*, output, run::{self, Eval}, @@ -105,47 +105,6 @@ impl Session { } } - // pub fn eval(&mut self, cmd: String, args: Vec<&str>) -> Result { - // match cmd.as_str() { - // "pwd" => { - // let pb = std::env::current_dir()?; - // println!("{}", pb.as_path().as_os_str().to_str().unwrap()); - // return Ok(true); - // } - // "cd" => { - // let cwd = std::env::current_dir()?; - // if args.len() > 0 { - // let target = cwd.join(args[0]); - // std::env::set_current_dir(target)?; - // } - // return Ok(true); - // } - // "printenv" => Printenv::create().exec(args), - // "which" => Which::create().exec(args), - // "tail" => Tail::create().exec(args), - // "echo" => Echo::create().exec(args), - // _ => { - // let mut proc = std::process::Command::new(cmd); - // if args.len() > 0 { - // proc.args(args); - // } - // match proc.spawn() { - // Ok(mut child) => { - // if let Err(e) = child.wait() { - // println!("error: {}", e); - // return Err(e.into()); - // } - // } - // Err(e) => { - // println!("error: {}", e); - // return Ok(false); - // } - // } - // return Ok(true); - // } - // } - // } - pub fn render_error(&mut self, e: E) -> io::Result<()> { self.render_error_helper(e, 0)?; Ok(()) diff --git a/src/main.rs b/src/main.rs index c11e34b..b61aa5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,47 +55,6 @@ fn main() -> anyhow::Result<()> { loop { match session.input.next()? { input::Event::Key(event) => { - if event.down { - if event.code.val == 0 { - debug!(" {}", event); - } else { - warn!(" {}", event); - } - continue; - } - info!(" {}", event); - - if event.code == key::ENTER { - session.stdout.newline()?; - let s = session.editor.pop(); - info!("◇ {}", s); - if let Ok(tokens) = lex::lex(&s) { - for t in tokens { - debug!(" {:?}", t); - } - } - match syntax::parse(&s) { - Ok(tree) => { - debug!(" {:?}", tree); - let mut state = runtime::State::new(); - if let Err(e) = tree.eval(&mut state) { - error!("{e:?}"); - _ = session.render_error(e); - } - } - Err(e) => { - error!("{e:?}"); - _ = session.render_error(e); - } - } - // shell.exec(tree.into())?; - // Some commands don't leave the terminal in a clean state, so we use reset - // to ensure that our input and output modes are what we expect them to be. - session.reset()?; - prompt.print(&mut session.stdout)?; - continue; - } - // CTRL-J to draw a cool little dot if event.ctrl && event.code == key::J { debug!("⎈ j: dot"); diff --git a/src/output.rs b/src/output.rs index 97b8723..c2329b9 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,10 +1,7 @@ use crate::{error::Error, log::*}; use anyhow::{Context, Result}; use std::io::{self, Write}; -use windows::Win32::{ - Foundation::{CloseHandle, HANDLE}, - System::Console, -}; +use windows::Win32::{Foundation::HANDLE, System::Console}; #[allow(dead_code)] fn log_output_mode(mode: Console::CONSOLE_MODE) { @@ -120,13 +117,6 @@ impl Writer { } } - // pub fn close(&mut self) -> Result<()> { - // unsafe { - // CloseHandle(self.output); - // } - // Ok(()) - // } - pub fn reset(&mut self) -> Result<()> { unsafe { Console::SetConsoleOutputCP(65001); diff --git a/src/parse.rs b/src/parse.rs index 5b60c74..e214b6a 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -126,40 +126,6 @@ impl Cursor { } } - /// moves the cursor horizontally in the tree, selecting the node that appears after the - /// current target node - pub fn next_sibling(&mut self) -> Result { - let next = match self.pick_next_sibling()? { - Some(node) => node, - None => return Ok(false), - }; - self.prev = self.target.id; - self.target = next; - Ok(true) - } - - fn pick_next_sibling(&mut self) -> Result>, ParseError> { - let parent = self - .target - .parent - .clone() - .ok_or_else(|| ParseError::AtRootAlready)?; - - // SAFETY: this is ok because the cursor always retains a pointer to the root of the tree, - // so we know that since we have a cursor, the parent cannot yet be dropped - let parent = parent.upgrade().unwrap(); - let mut found_self = false; - for child in parent.children.borrow().iter() { - if found_self { - return Ok(Some(child.clone())); - } - if child.id == self.target.id { - found_self = true; - } - } - Ok(None) - } - /// Adds a value to the children of the current target node, then descends to select that /// child. fn push(&mut self, v: Value) -> Result<(), ParseError> { diff --git a/src/run.rs b/src/run.rs index 3c2391e..1be32b2 100644 --- a/src/run.rs +++ b/src/run.rs @@ -62,10 +62,6 @@ impl State { } } - pub fn has_builtin(&self, name: &str) -> bool { - self.builtins.contains_key(name) - } - pub fn builtin(&self, name: &str) -> Option { self.builtins.get(name).copied() }