-
Notifications
You must be signed in to change notification settings - Fork 6
Repl #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Repl #68
Changes from all commits
f2cc993
d7e3917
c5f7f9f
6a0d41a
dbdde99
97af5fb
34ae3fa
d38b964
877ea4c
1463ae3
7750c4b
351a331
d920425
3ccf026
0f863ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,32 +6,23 @@ use termion::{ | |
| cursor::DetectCursorPos, | ||
| raw::RawTerminal, | ||
| }; | ||
| use docopt::ArgvMap; | ||
| use crate::program::{Runtime, parse_and_run}; | ||
| use crate::process::{IO, Jobs}; | ||
| use crate::repl::prompt; | ||
|
|
||
| #[cfg(feature = "history")] | ||
| use super::history::History; | ||
|
|
||
| #[cfg(feature = "completion")] | ||
| use super::completion::*; | ||
| use super::completion::{self, *}; | ||
|
|
||
|
|
||
| pub struct Action; | ||
|
|
||
| pub struct ActionContext<'a> { | ||
| pub struct ActionContext<'a, 'b> { | ||
| pub stdout: &'a mut RawTerminal<Stdout>, | ||
| pub io: &'a mut IO, | ||
| pub jobs: &'a mut Jobs, | ||
| pub args: &'a mut ArgvMap, | ||
| pub runtime: &'a mut Runtime<'b>, | ||
| // TODO: Remove this field. | ||
| #[cfg(feature = "raw")] | ||
| pub prompt_length: u16, | ||
| #[cfg(feature = "raw")] | ||
| pub text: &'a mut String, | ||
| #[cfg(feature = "history")] | ||
| pub history: &'a mut History, | ||
| } | ||
|
|
||
| #[cfg(feature = "raw")] | ||
|
|
@@ -43,30 +34,24 @@ impl Action { | |
|
|
||
| // Run the command. | ||
| context.stdout.suspend_raw_mode().unwrap(); | ||
| let mut runtime = Runtime { | ||
| background: false, | ||
| io: context.io.clone(), | ||
| jobs: context.jobs, | ||
| args: context.args, | ||
| prompt::ps0(&mut context.stdout); | ||
| if parse_and_run(context.text, context.runtime).is_ok() { | ||
| #[cfg(feature = "history")] | ||
| history: context.history, | ||
| }; | ||
| if parse_and_run(context.text, &mut runtime).is_ok() { | ||
| #[cfg(feature = "history")] | ||
| context.history.add(&context.text, 1); | ||
| context.runtime.history.add(context.text, 1); | ||
| } | ||
| context.stdout.activate_raw_mode().unwrap(); | ||
|
|
||
| // Reset for the next program. | ||
| context.text.clear(); | ||
| #[cfg(feature = "history")] | ||
| context.history.reset_index(); | ||
| context.runtime.history.reset_index(); | ||
|
|
||
| prompt::ps1(&mut context.stdout); | ||
| } | ||
|
|
||
| pub fn insert(context: &mut ActionContext, c: char) { | ||
| if let Ok((x, y)) = context.stdout.cursor_pos() { | ||
| // XXX: Why did this panic? | ||
| let i = (x - context.prompt_length) as usize; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth looking into this again for a bit. |
||
| context.text.insert(i, c); | ||
| print!("{}{}", | ||
|
|
@@ -108,7 +93,7 @@ impl Action { | |
|
|
||
| // Save history to file in $HOME. | ||
| #[cfg(feature = "history")] | ||
| context.history.save(); | ||
| context.runtime.history.save().unwrap(); | ||
|
|
||
| // Manually drop the raw terminal. | ||
| // TODO: Needed? | ||
|
|
@@ -164,9 +149,9 @@ impl Action { | |
| print!("{}{}", | ||
| termion::cursor::Left(1000), // XXX | ||
| termion::clear::CurrentLine); | ||
| context.prompt.display(&mut context.stdout); | ||
| prompt::ps1(&mut context.stdout); | ||
|
|
||
| if let Some(history_text) = context.history.get_up() { | ||
| if let Some(history_text) = context.runtime.history.get_up() { | ||
| *context.text = history_text; | ||
| print!("{}", context.text); | ||
| } | ||
|
|
@@ -178,9 +163,9 @@ impl Action { | |
| print!("{}{}", | ||
| termion::cursor::Left(1000), // XXX | ||
| termion::clear::CurrentLine); | ||
| context.prompt.display(&mut context.stdout); | ||
| prompt::ps1(&mut context.stdout); | ||
|
|
||
| if let Some(history_text) = context.history.get_down() { | ||
| if let Some(history_text) = context.runtime.history.get_down() { | ||
| *context.text = history_text; | ||
| print!("{}", context.text); | ||
| context.stdout.flush().unwrap(); | ||
|
|
@@ -191,20 +176,23 @@ impl Action { | |
|
|
||
| #[cfg(feature = "completion")] | ||
| pub fn complete(context: &mut ActionContext) { | ||
| match complete(&context.text) { | ||
| match complete(context.text) { | ||
| Completion::Partial(possibilities) => { | ||
| if possibilities.len() > 25 { | ||
| print!("\n\r"); | ||
| for possibility in possibilities { | ||
| print!("{}\n\r", possibility); | ||
| } | ||
| print!("\n\r"); | ||
| } else { | ||
| print!("\n\r{}\n\r", possibilities.join("\t")); | ||
| } | ||
| println!(); | ||
| print!("{}{}", | ||
| termion::cursor::Left(1000), // XXX | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth fixing this now? |
||
| termion::clear::CurrentLine); | ||
| context.stdout.flush().unwrap(); | ||
| context.stdout.suspend_raw_mode().unwrap(); | ||
| completion::write_table(&mut context.stdout, &possibilities); | ||
| context.stdout.activate_raw_mode().unwrap(); | ||
| print!("{}{}", | ||
| termion::cursor::Left(1000), // XXX | ||
| termion::clear::CurrentLine); | ||
| prompt::ps1(&mut context.stdout); | ||
| print!("{}", context.text); | ||
| context.stdout.flush().unwrap(); | ||
|
|
||
| }, | ||
| Completion::Complete(t) => { | ||
| *context.text = t; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,9 @@ use std::{ | |
| fs, | ||
| cmp::Ordering::Equal, | ||
| os::unix::fs::PermissionsExt, | ||
| io::Write, | ||
| }; | ||
| use tabwriter::TabWriter; | ||
|
|
||
| /// The result of a query for text completion. | ||
| /// | ||
|
|
@@ -134,9 +136,10 @@ pub fn executable_completions(text: &str) -> Completion { | |
| 0 => Completion::None, | ||
| 1 => Completion::Complete(matches.remove(0)), | ||
| _ => { | ||
| // TODO: Support POSIX style lexicographical order? | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm torn on this, as am I torn on row major vs column major order. |
||
| matches.sort_by(|a, b| { | ||
| match a.len().cmp(&b.len()) { | ||
| Equal => b.cmp(&a), | ||
| Equal => b.cmp(a), | ||
| o => o | ||
| } | ||
| }); | ||
|
|
@@ -168,6 +171,38 @@ pub fn path_complete(text: &str) -> Completion { | |
| } | ||
| } | ||
|
|
||
| pub fn write_table(writer: impl Write, words: &[String]) { | ||
| // TODO: Handle empty case better. | ||
| let max_length = words.iter().max_by(|a,b| a.len().cmp(&b.len())).unwrap().len() as u16; | ||
| // TODO: Can this function be called from outside a terminal environment? | ||
| let (width, _height) = termion::terminal_size().unwrap(); | ||
| let padding = 5; | ||
| let columns = width / (max_length + padding); | ||
| let mut tw = TabWriter::new(writer).padding(padding as usize); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to ditch this albeit neat method of table generation and go for something that will produce better results in our case. Issues at the moment:
|
||
| // TODO: Determine table width/height and calculate iteration better | ||
| let mut _row = 0; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a note here about what we are planning on using |
||
| let mut col = 0; | ||
| let mut needs_newline = false; | ||
| for word in words { | ||
| let _r = tw.write(word.as_bytes()).unwrap(); | ||
|
|
||
| if col == columns-1 { | ||
| col = 0; | ||
| _row += 1; | ||
| let _r = tw.write(b"\n").unwrap(); | ||
| needs_newline = false; | ||
| } else { | ||
| col += 1; | ||
| let _r = tw.write(b"\t").unwrap(); | ||
| needs_newline = true; | ||
| } | ||
| } | ||
| if needs_newline { | ||
| let _r = tw.write(b"\n").unwrap(); | ||
| } | ||
| tw.flush().unwrap(); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,12 @@ impl History { | |
| return; | ||
| } | ||
|
|
||
| // HACK: There's got to be a cleaner way. | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
| let mut index = 0; | ||
| if self.1.iter().enumerate().find(|(i, (t, _))| { | ||
| index = *i; | ||
| text == t | ||
| }).is_some() { | ||
| if self.1.iter().enumerate() | ||
| .any(|(_,(t, i))| { | ||
| index = *i; | ||
| text == t }) | ||
| { | ||
| self.1[index].1 += count; | ||
| let text = self.1.remove(index); | ||
| self.1.insert(0, text); | ||
|
|
@@ -86,7 +86,7 @@ impl History { | |
| // println!("{:?}", s); | ||
| // }) | ||
| // }).collect::<Vec<String, usize>>(); | ||
| let hist = contents.split("\n").map(|s| { | ||
| let hist = contents.split('\n').map(|s| { | ||
| (String::from(s), 0) | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As always, I think documentation will mark the end of the PR.
Generally in a collaborative environment (which this is currently not), I like this, writing a summary gives people time to review as well as giving future developers the needed information.