Save REPL config to file
This commit is contained in:
parent
a8b77848b4
commit
e47a2c7241
@ -13,4 +13,7 @@ linefeed = "0.2.2"
|
|||||||
lazy_static = "0.2.8"
|
lazy_static = "0.2.8"
|
||||||
maplit = "*"
|
maplit = "*"
|
||||||
colored = "1.5"
|
colored = "1.5"
|
||||||
|
serde = "1.0.15"
|
||||||
|
serde_derive = "1.0.15"
|
||||||
|
serde_json = "1.0.3"
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ impl TokenError {
|
|||||||
|
|
||||||
pub struct LLVMCodeString(pub String);
|
pub struct LLVMCodeString(pub String);
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
pub struct EvalOptions {
|
pub struct EvalOptions {
|
||||||
pub debug_tokens: bool,
|
pub debug_tokens: bool,
|
||||||
pub debug_parse: bool,
|
pub debug_parse: bool,
|
||||||
|
39
src/main.rs
39
src/main.rs
@ -5,6 +5,10 @@ extern crate linefeed;
|
|||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate maplit;
|
extern crate maplit;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate serde_json;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@ -121,14 +125,42 @@ impl Repl {
|
|||||||
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap();
|
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap();
|
||||||
reader.set_prompt(">> ");
|
reader.set_prompt(">> ");
|
||||||
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
||||||
|
|
||||||
Repl {
|
Repl {
|
||||||
options: EvalOptions::default(),
|
options: Repl::get_options(),
|
||||||
languages: languages,
|
languages: languages,
|
||||||
current_language_index: i,
|
current_language_index: i,
|
||||||
interpreter_directive_sigil: '.',
|
interpreter_directive_sigil: '.',
|
||||||
reader: reader,
|
reader: reader,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_options() -> EvalOptions {
|
||||||
|
File::open(".schala_repl")
|
||||||
|
.and_then(|mut file| {
|
||||||
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents)?;
|
||||||
|
Ok(contents)
|
||||||
|
})
|
||||||
|
.and_then(|contents| {
|
||||||
|
let options: EvalOptions = serde_json::from_str(&contents)?;
|
||||||
|
Ok(options)
|
||||||
|
}).unwrap_or(EvalOptions::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_options(&self) {
|
||||||
|
let ref options = self.options;
|
||||||
|
let read = File::create(".schala_repl")
|
||||||
|
.and_then(|mut file| {
|
||||||
|
let buf = serde_json::to_string(options).unwrap();
|
||||||
|
file.write_all(buf.as_bytes())
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Err(err) = read {
|
||||||
|
println!("Error saving .schala_repl file {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn run(&mut self) {
|
fn run(&mut self) {
|
||||||
use linefeed::ReadResult::*;
|
use linefeed::ReadResult::*;
|
||||||
println!("MetaInterpreter v 0.05");
|
println!("MetaInterpreter v 0.05");
|
||||||
@ -181,7 +213,10 @@ impl Repl {
|
|||||||
};
|
};
|
||||||
|
|
||||||
match cmd {
|
match cmd {
|
||||||
"exit" | "quit" => process::exit(0),
|
"exit" | "quit" => {
|
||||||
|
self.save_options();
|
||||||
|
process::exit(0)
|
||||||
|
},
|
||||||
"history" => {
|
"history" => {
|
||||||
for item in self.reader.history() {
|
for item in self.reader.history() {
|
||||||
println!("{}", item);
|
println!("{}", item);
|
||||||
|
Loading…
Reference in New Issue
Block a user