cleaning upp

main
Jordan Orelli 11 months ago
parent 5505cb48c6
commit a847b56b05

@ -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<Value, ExecError> {
fn call(&self, _ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
match args.len() {
0 => {
todo!()

@ -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);
}
}
}

@ -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<Status> {
self.display.newline()?;
let text = self.buffer.pop();

@ -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
*/

@ -1,7 +0,0 @@
use anyhow::Result;
pub trait Command {
fn name() -> String;
fn create() -> Self;
fn exec(&mut self, args: Vec<&str>) -> Result<bool>;
}

@ -536,7 +536,7 @@ pub enum ControlCharacter {
Tab,
Linefeed,
VTab,
FormFeed,
Formfeed,
CarriageReturn,
ShiftOut,
ShiftIn,
@ -580,7 +580,7 @@ impl TryFrom<Console::KEY_EVENT_RECORD> 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),

@ -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<bool> {
// 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<E: Error>(&mut self, e: E) -> io::Result<()> {
self.render_error_helper(e, 0)?;
Ok(())

@ -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");

@ -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);

@ -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<bool, ParseError> {
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<Option<Rc<Node>>, 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> {

@ -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<builtin::Builtin> {
self.builtins.get(name).copied()
}

Loading…
Cancel
Save