Skip to content

Commit

Permalink
Use readline for input
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpitt committed Jun 12, 2022
1 parent cd68c51 commit b515b85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ edition = "2021"
[dependencies]
chrono = "0.4"
dirs = "4"
rustyline = "9"
24 changes: 15 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ mod store;

use std::env;
use std::error::Error;
use std::io::{self, Write};
use std::io;
use std::path::PathBuf;
use std::process;

use chrono::prelude::*;
use rustyline::{error::ReadlineError, Editor};

use store::Timelog;

Expand All @@ -20,11 +21,15 @@ fn clear_screen() {
print!("{esc}c", esc = 27 as char);
}

fn stdin_line() -> Result<String, io::Error> {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer)?;
buffer.truncate(buffer.trim_end().len());
Ok(buffer)
fn get_input(rl: &mut Editor<()>) -> Result<String, ReadlineError> {
match rl.readline("> ") {
Ok(mut line) => {
line.truncate(line.trim_end().len());
Ok(line)
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => Ok(":q".to_string()),
Err(e) => Err(e),
}
}

fn show_help() {
Expand Down Expand Up @@ -72,8 +77,7 @@ fn show_prompt(timelog: &Timelog) -> Result<(), io::Error> {
),
};

print!("\n{}; type command (:h for help) or entry\n> ", since_str);
io::stdout().flush()?;
println!("\n{}; type command (:h for help) or entry", since_str);
Ok(())
}

Expand All @@ -88,12 +92,14 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut timelog = Timelog::new_from_default_file();
let mut running = true;
let mut time_mode = TimeMode::Day;
let mut readline = Editor::<()>::new();

show(&timelog, &time_mode);

while running {
show_prompt(&timelog)?;
let input = stdin_line()?;

let input = get_input(&mut readline)?;
match input.as_str() {
":q" => running = false,
":h" => show_help(),
Expand Down

0 comments on commit b515b85

Please sign in to comment.