Commit 81367a83 authored by Liam Naddell's avatar Liam Naddell

tidy up comments

parent 6e1c0aa2
Pipeline #142 passed with stage
in 1 minute and 59 seconds
......@@ -10,7 +10,7 @@ use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter};
use termion::color;
use regex::Regex;
//this prints out the text back onto the screen
// This prints out the text back onto the screen
fn highlight_dodo(s: &str) -> String {
let reg_exp = Regex::new("(?P<k>dodo)").unwrap();
let format = format!("{}$k{}", color::Fg(color::Red), color::Fg(color::Reset));
......@@ -18,10 +18,8 @@ fn highlight_dodo(s: &str) -> String {
}
fn main() {
//create a new context
let mut con = Context::new();
//get the 1st arg, and use it as the history file
let history_file = args().nth(1);
match history_file {
Some(ref file_name) => println!("History file: {}", file_name),
......@@ -29,58 +27,55 @@ fn main() {
}
con.history.set_file_name(history_file);
//we set the file name, then check if we set it
// We set the file name, then check if we set it, and if we set it properly, we load it in
if con.history.file_name().is_some() {
con.history.load_history().unwrap();
}
//while true: read line and do stuff
loop {
//reads the line, the first arg is the prompt, the second arg is a function called on every keypress, and the last does something I don't understand
// Reads the line, the first arg is the prompt, the second arg is a function called on every bit of text leaving liner, and the third is called on every key press
// Basically highlight_dodo(read_line()), where on every keypress, the lambda is called
let res = con.read_line("[prompt]$ ",
Some(Box::new(highlight_dodo)),
&mut |Event { editor, kind }| {
//if this is a BeforeComplete
if let EventKind::BeforeComplete = kind {
let (_, pos) = editor.get_words_and_cursor_position();
// Figure out of we are completing a command (the first word) or a filename.
let filename = match pos {
//if we are inside of a word(i is the index inside of the text, and if that
//position is over zero, we return true
// If we are inside of a word(i is the index inside of the text, and if that
// position is over zero, we return true
CursorPosition::InWord(i) => i > 0,
//if we are in a space like this `cat | cart` or cat |
//checks if there is a word to our left(indicated by there being Some value)
// If we are in a space like this `cat | cart` or cat |
// checks if there is a word to our left(indicated by there being Some value)
CursorPosition::InSpace(Some(_), _) => true,
//checks if there is no word to our left(indicated by there being None value)
// Checks if there is no word to our left(indicated by there being None value)
CursorPosition::InSpace(None, _) => false,
//If we are on the left edge of a word, and the position of the cursor is
//greater than or equal to 1, return true
// If we are on the left edge of a word, and the position of the cursor is
// greater than or equal to 1, return true
CursorPosition::OnWordLeftEdge(i) => i >= 1,
//if we are on the right edge of the word
// If we are on the right edge of the word
CursorPosition::OnWordRightEdge(i) => i >= 1,
};
//If we are not in a word with pos over zero, or in a space with text beforehand,
//or on the left edge of a word with pos >= to 1, or on the Right edge of a word
//under the same condition
//This condition is only false under the predicate that we are in a space with no
//word to the left
// If we are not in a word with pos over zero, or in a space with text beforehand,
// or on the left edge of a word with pos >= to 1, or on the Right edge of a word
// under the same condition, then
// This condition is only false under the predicate that we are in a space with no
// word to the left
if filename {
//create a filename completer in the current directory
let completer = FilenameCompleter::new(Some(current_dir().unwrap()));
//replace the editor.context.completer with a box containing our completer
replace(&mut editor.context().completer, Some(Box::new(completer)));
} else {
//otherwise delete the completer
// Delete the completer
replace(&mut editor.context().completer, None);
}
}
});
//we are out of the lambda, and res is the result from read_line which is an Into<String>
// We are out of the lambda, and res is the result from read_line which is an Into<String>
match res {
//if user typed vi, use vim bindings, if they typed emacs, use emacs, and if exit, leave
Ok(res) => {
match res.as_str() {
"emacs" => {
......@@ -95,19 +90,19 @@ fn main() {
println!("exiting...");
break;
}
//if all else fails, do nothing
// If all else fails, do nothing
_ => {}
}
//If we typed nothing, don't continue down to pushing to history
// If we typed nothing, don't continue down to pushing to history
if res.is_empty() {
break;
}
con.history.push(res.into()).unwrap();
}
//if there was an error, get what type it was( remember, we still are in the match{}
//from waaay above
// If there was an error, get what type it was(remember, we still are in the match{}
// from waaay above)
Err(e) => {
match e.kind() {
// ctrl-c pressed
......@@ -127,7 +122,7 @@ fn main() {
}
}
//end loop
// End loop
}
// Ensure that all writes to the history file are written before exiting.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment