cli: Save history

This commit is contained in:
PaddiM8 2021-09-04 12:51:31 +02:00
parent cda0b29ed2
commit e54c6e4c0e
3 changed files with 84 additions and 4 deletions

67
Cargo.lock generated
View File

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.15" version = "0.7.15"
@ -75,6 +77,15 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "dirs"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30baa043103c9d0c2a57cf537cc2f35623889dc0d405e6c3cccfadbc81c71309"
dependencies = [
"dirs-sys",
]
[[package]] [[package]]
name = "dirs-next" name = "dirs-next"
version = "2.0.0" version = "2.0.0"
@ -85,6 +96,17 @@ dependencies = [
"dirs-sys-next", "dirs-sys-next",
] ]
[[package]]
name = "dirs-sys"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
dependencies = [
"libc",
"redox_users 0.4.0",
"winapi",
]
[[package]] [[package]]
name = "dirs-sys-next" name = "dirs-sys-next"
version = "0.1.1" version = "0.1.1"
@ -92,7 +114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99de365f605554ae33f115102a02057d4fc18b01f3284d6870be0938743cfe7d" checksum = "99de365f605554ae33f115102a02057d4fc18b01f3284d6870be0938743cfe7d"
dependencies = [ dependencies = [
"libc", "libc",
"redox_users", "redox_users 0.3.5",
"winapi", "winapi",
] ]
@ -114,7 +136,18 @@ checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 0.1.10",
"libc", "libc",
"wasi", "wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
] ]
[[package]] [[package]]
@ -163,6 +196,7 @@ version = "1.0.0"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"atty", "atty",
"dirs",
"kalk", "kalk",
"lazy_static", "lazy_static",
"regex", "regex",
@ -234,14 +268,33 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "redox_syscall"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
dependencies = [
"bitflags",
]
[[package]] [[package]]
name = "redox_users" name = "redox_users"
version = "0.3.5" version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.1.15",
"redox_syscall", "redox_syscall 0.1.57",
]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom 0.2.3",
"redox_syscall 0.2.10",
] ]
[[package]] [[package]]
@ -395,6 +448,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.69" version = "0.2.69"

View File

@ -19,6 +19,7 @@ regex = "1"
rustyline = "7.1.0" rustyline = "7.1.0"
seahorse = "1.1.1" seahorse = "1.1.1"
atty = "0.2.14" atty = "0.2.14"
dirs = "3.0.2"
[target.'cfg(windows)'.build-dependencies] [target.'cfg(windows)'.build-dependencies]
winres = "0.1" winres = "0.1"

View File

@ -4,6 +4,7 @@ use kalk::parser;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Captures; use regex::Captures;
use regex::Regex; use regex::Regex;
use rustyline::config::Configurer;
use rustyline::completion::Completer; use rustyline::completion::Completer;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::highlight::Highlighter; use rustyline::highlight::Highlighter;
@ -17,6 +18,7 @@ use std::borrow::Cow;
use std::borrow::Cow::Owned; use std::borrow::Cow::Owned;
use std::collections::HashMap; use std::collections::HashMap;
use std::process; use std::process;
use std::fs;
pub fn start(mut parser: &mut parser::Context, precision: u32) { pub fn start(mut parser: &mut parser::Context, precision: u32) {
let mut editor = Editor::<RLHelper>::new(); let mut editor = Editor::<RLHelper>::new();
@ -24,6 +26,20 @@ pub fn start(mut parser: &mut parser::Context, precision: u32) {
highlighter: LineHighlighter {}, highlighter: LineHighlighter {},
validator: MatchingBracketValidator::new(), validator: MatchingBracketValidator::new(),
})); }));
editor.set_max_history_size(30);
// Load history
let mut history_path = None;
if let Some(config_path) = dirs::config_dir() {
let mut config_path = config_path.clone();
config_path.push("kalker");
if let Ok(_) = fs::create_dir_all(config_path.as_path()) {
config_path.push("history.txt");
let history = config_path.into_os_string().into_string().unwrap();
editor.load_history(&history).ok();
history_path = Some(history)
}
}
// If in tty, print the welcome message // If in tty, print the welcome message
if atty::is(atty::Stream::Stdin) && atty::is(atty::Stream::Stdout) { if atty::is(atty::Stream::Stdin) && atty::is(atty::Stream::Stdout) {
@ -51,6 +67,10 @@ pub fn start(mut parser: &mut parser::Context, precision: u32) {
_ => break, _ => break,
} }
} }
if let Some(history_path) = history_path {
editor.save_history(&history_path).ok();
}
} }
fn eval_repl(parser: &mut parser::Context, input: &str, precision: u32) { fn eval_repl(parser: &mut parser::Context, input: &str, precision: u32) {