<commit_before>extern crate rcalc;

use std::io;
use std::io::{ BufWriter, Write };
use std::path::PathBuf;
use std::fs::File;
use std::env;
use rcalc::{ Calculator, RuntimeItem, Value };

fn main() {
    let mut calculator = Calculator::new();
    let mut precision = None;
    let mut parallel_srcs = None;

    println!("Enter ? for help");

    loop {
        print!("> ");
        io::stdout().flush().expect("stdout error");

        match read_line().trim() {
            "" => (),
            "#q" => break,
            "#:" => {
                parallel_srcs = match parallel_srcs {
                    Some(..) => None,
                    None => Some(vec![]),
                } 
            },
            "#!" => {
                if let Some(ref srcs) = parallel_srcs {
                    let item = calculator.calc_parallel(srcs.clone());
                    print_item(item, &precision);
                };
                parallel_srcs = None;
            },
            "#j" => {
                let mut buf = Vec::new();
                let _ = {
                    let mut writer = BufWriter::new(&mut buf);
                    calculator.write_json(&mut writer).expect("json output error");
                };
                println!("{}", String::from_utf8(buf).unwrap());
            },
            "#w" => {
                match write_json(&calculator) {
                    Result::Ok(ref path) => println!("Context written to '{}'", path),
                    Result::Err((ref path, ref error)) => println!("Error writing context to '{}': {:?}", path, error),
                };
            },
            "?" => {
                println!("#q - quit rcalc");
                println!("#: - enter or leave parallel mode");
                println!("#! - leave parallel mode and evaluate all input concurrently");
                println!("#j - print a JSON representation of the current value bindings");
                println!("#w - write a JSON representation of the current value bindings to '{}'", get_json_path());
                println!("#precision <DIGITS> - format floating point values with <DIGITS> decimal places");
            },
            line if line.starts_with("#precision ") => {
                if let Result::Ok(p) = line.split_whitespace().last().unwrap().parse::<usize>() {
                    precision = Some(p);
                } else {
                    precision = None;
                }
            },
            line => {
                match parallel_srcs {
                    Some(ref mut srcs) => srcs.push(line.to_string()),
                    None => {
                        match calculator.calc(line) {
                            Ok(item) => print_item(item, &precision),
                            Err(msg) => println!("{}", msg),
                        }
                    }
                }
            }
        }
    }
}

fn read_line() -> String {<commit_msg>Minor cosmetics for v0.2<commit_after>extern crate rcalc;

use std::io;
use std::io::{ BufWriter, Write };
use std::path::PathBuf;
use std::fs::File;
use std::env;
use rcalc::{ Calculator, RuntimeItem, Value };

fn main() {
    let mut calculator = Calculator::new();
    let mut precision = None;
    let mut parallel_srcs = None;

    println!("rust-calc v0.2");
    println!("http://github.com/smackem/rust-calc");
    println!("Enter ? for help");

    loop {
        print!("> ");
        io::stdout().flush().expect("stdout error");

        match read_line().trim() {
            "" => (),
            "#q" => break,
            "#:" => {
                parallel_srcs = match parallel_srcs {
                    Some(..) => None,
                    None => Some(vec![]),
                } 
            },
            "#!" => {
                if let Some(ref srcs) = parallel_srcs {
                    let item = calculator.calc_parallel(srcs.clone());
                    print_item(item, &precision);
                };
                parallel_srcs = None;
            },
            "#j" => {
                let mut buf = Vec::new();
                let _ = {
                    let mut writer = BufWriter::new(&mut buf);
                    calculator.write_json(&mut writer).expect("json output error");
                };
                println!("{}", String::from_utf8(buf).unwrap());
            },
            "#w" => {
                match write_json(&calculator) {
                    Result::Ok(ref path) => println!("Context written to '{}'", path),
                    Result::Err((ref path, ref error)) => println!("Error writing context to '{}': {:?}", path, error),
                };
            },
            "?" => {
                println!("#: - enter or leave parallel mode");
                println!("#! - leave parallel mode and evaluate all input concurrently");
                println!("#j - print a JSON representation of the current value bindings");
                println!("#w - write a JSON representation of the current value bindings to '{}'", get_json_path());
                println!("#p <DIGITS> - format floating point values with <DIGITS> decimal places");
                println!("#q - quit rcalc");
                println!("");
                println!("anything else is parsed as a statement.");
                println!("see http://github.com/smackem/rust-calc");
            },
            line if line.starts_with("#p ") => {
                if let Result::Ok(p) = line.split_whitespace().last().unwrap().parse::<usize>() {
                    precision = Some(p);
                } else {
                    precision = None;
                }
            },
            line => {
                match parallel_srcs {
                    Some(ref mut srcs) => srcs.push(line.to_string()),
                    None => {
                        match calculator.calc(line) {
                            Ok(item) => print_item(item, &precision),
                            Err(msg) => println!("{}", msg),
                        }
                    }
                }
            }
        }
    }
}

fn read_line() -> String {
{'fraction_non_alphanumeric': 0.14666666666666667, 'fraction_numerical': 0.000975609756097561, 'mean_word_length': 1.3450247807853604, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2082484', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1537, 'n_words': 378}