Josh Bicking hace 4 años
padre
commit
d3d48b1e02
Se han modificado 1 ficheros con 25 adiciones y 18 borrados
  1. 25 18
      src/main.rs

+ 25 - 18
src/main.rs

@@ -6,6 +6,7 @@ use std::fs::{File};
 use std::io::prelude::*;
 use std::process::exit;
 use std::{thread, time};
+use std::env;
 
 use argparse::{ArgumentParser, Store};
 use pancurses::{
@@ -13,12 +14,18 @@ use pancurses::{
     Window,
     COLOR_GREEN, COLOR_BLACK
 };
-use yaml_rust::{YamlLoader, YamlEmitter};
+use yaml_rust::{YamlLoader, YamlEmitter, Yaml};
 
 fn main() {
 
     // Argument handling
     let mut difficulty = String::from("");
+    // TODO can't use ? ?
+    let foo = env::var("HOME")?.add(".config/");
+    let config_dir = match env::var("XDG_CONFIG_HOME") {
+        Ok(dir) => dir,
+        Err(_) => String::from("foo"),
+    };
     let mut config_location = String::from("~/.config/fallout-terminal.yaml");
     let config_help = format!("Config file location, default {}", config_location);
     {
@@ -48,37 +55,37 @@ fn main() {
     // Config file handling
     let mut config_file = match File::open(config_location.as_str()) {
         Ok(config_file) => config_file,
-        Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
-            // Write the file if it doesn't exist
-            let mut new_config_file = match File::create(config_location.as_str()) {
-                Ok(f) => f,
-                Err(e) => {
-                    println!("Could not create {}: {}", config_location, e);
-                    exit(1);
-                }
-            };
-            // TODO write yaml
-            new_config_file.write_all(b"");
-            new_config_file
-        }
         Err(e) => {
             println!("Could not open {}: {}", config_location, e);
             exit(1);
         }
     };
 
-    let mut config_content = String::new();
-    match config_file.read_to_string(&mut config_content) {
-        Ok(_) => (),
+    let mut config_str = String::new();
+    let config: Vec<Yaml>;
+    match config_file.read_to_string(&mut config_str) {
+        Ok(_) => match YamlLoader::load_from_str(&config_str) {
+            Ok(conf) => (config = conf),
+            Err(e) => {
+                println!("Could not parse YAML in {}: {}", config_location, e);
+                exit(1);
+            }
+        }
         Err(e) => {
             println!("Could not read {}: {}", config_location, e);
             exit(1);
         }
     };
 
+    let mut out_str = String::new();
+    let mut emitter = YamlEmitter::new(&mut out_str);
+    for x in &config {
+        println!("{:?}", emitter.dump(x).unwrap());
+    }
+
     let window = initscr();
     noecho();
-    // window.printw("Hello from Rust");
+    window.printw("Hello from Rust");
     window_prep();
     intro(&window);
     window.refresh();