Josh Bicking 4 ani în urmă
părinte
comite
5b88edde35
3 a modificat fișierele cu 131 adăugiri și 83 ștergeri
  1. 75 0
      src/config.rs
  2. 6 83
      src/main.rs
  3. 50 0
      src/screen.rs

+ 75 - 0
src/config.rs

@@ -0,0 +1,75 @@
+extern crate argparse;
+extern crate yaml_rust;
+
+use std::fs::{File};
+use std::io::prelude::*;
+use std::process::exit;
+use std::env;
+
+use argparse::{ArgumentParser, Store};
+use yaml_rust::{YamlLoader, YamlEmitter, Yaml};
+
+mod config {}
+
+
+// TODO
+//   Collect args
+//   Determine the config file dir
+//   Load the config file
+//   Validate
+//   Return the config
+
+fn default_config_location() -> String {
+    let config_dir = match env::var("XDG_CONFIG_HOME") {
+        Ok(dir) => dir,
+        // This will usually be $HOME/.config
+        Err(_) => match env::var("HOME") {
+            Ok(home) => [home, "/.config".into()].concat(),
+            Err(_) => {
+                println!("Could not determine config directory");
+                exit(1);
+            }
+        }
+    };
+
+    // Throw fallout-terminal.yaml onto that
+    let mut config_location = String::from(&config_dir);
+    config_location.push_str("/fallout-terminal.yaml");
+
+    config_location
+}
+
+fn load_config_file(config_dir: str) ->  {
+    
+}
+
+pub fn config_from_env() -> Config {
+    let mut config_location = default_config_location();
+    let config_help = format!("Config file location, default {}", config_location);
+
+    // Argument handling
+    let mut difficulty = String::from("");
+
+    {
+        let mut ap = ArgumentParser::new();
+        ap.set_description("A Fallout terminal minigame inspired game.");
+        ap.refer(&mut difficulty)
+            .add_option(&["--difficulty"], Store, "The game's difficulty (veryEasy [default], \
+                                                   easy, average, hard, or veryHard)");
+
+        ap.refer(&mut config_location)
+            .add_option(
+                &["--config"],
+                Store,
+                config_help.as_str()
+            );
+        ap.parse_args_or_exit();
+    }
+
+    match difficulty.as_str() {
+        "" | "veryEasy" => {
+            println!("veryEasy");
+        },
+        _ => println!("Invalid difficulty, see --help")
+    }
+}

+ 6 - 83
src/main.rs

@@ -1,57 +1,9 @@
-extern crate argparse;
-extern crate pancurses;
-extern crate yaml_rust;
 
-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::{
-    initscr, endwin, noecho, has_colors, start_color, init_pair,
-    Window,
-    COLOR_GREEN, COLOR_BLACK
-};
-use yaml_rust::{YamlLoader, YamlEmitter, Yaml};
+mod config;
+mod screen;
 
 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);
-    {
-        let mut ap = ArgumentParser::new();
-        ap.set_description("A Fallout terminal minigame inspired game.");
-        ap.refer(&mut difficulty)
-            .add_option(&["--difficulty"], Store, "The game's difficulty (veryEasy [default], \
-                                                   easy, average, hard, or veryHard)");
-
-        ap.refer(&mut config_location)
-            .add_option(
-                &["--config"],
-                Store,
-                config_help.as_str()
-            );
-        ap.parse_args_or_exit();
-    }
-
-    match difficulty.as_str() {
-        "" | "veryEasy" => {
-            println!("veryEasy");
-        },
-        _ => println!("Invalid difficulty, see --help")
-    }
-
-
     // Config file handling
     let mut config_file = match File::open(config_location.as_str()) {
         Ok(config_file) => config_file,
@@ -77,45 +29,16 @@ fn main() {
         }
     };
 
+    // DEBUG: seeing yaml values
     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_prep();
-    intro(&window);
+    let window = screen::init_window();
+    screen::intro(&window);
     window.refresh();
     window.getch();
-    endwin();
-}
-
-fn window_prep () {
-    if has_colors() {
-        start_color();
-        init_pair(1,COLOR_GREEN, COLOR_BLACK);
-    }
-}
-
-fn intro (w: &Window) {
-    fn slow_print(w: &Window, line: i32, string: String) {
-        w.mv(line, 0);
-        for c in string.chars() {
-            w.addch(c);
-            w.refresh();
-            // TODO keyboard skip
-            thread::sleep(time::Duration::from_millis(20));
-        }
-    }
-
-    w.clear();
-    thread::sleep(time::Duration::from_millis(250));
-    slow_print(&w, 0, String::from("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK"));
-
-    w.mv(1, 0);
-    w.refresh();
-    thread::sleep(time::Duration::from_millis(30));
+    screen::end_window();
 }

+ 50 - 0
src/screen.rs

@@ -0,0 +1,50 @@
+extern crate pancurses;
+use self::pancurses::{
+    initscr, endwin, noecho, has_colors, start_color, init_pair,
+    Window,
+    COLOR_GREEN, COLOR_BLACK
+};
+use std::{thread, time};
+
+// Screen control functionality. Essentially just handling the heavy lifting
+// pancurses involves.
+
+mod screen {}
+
+pub fn init_window() -> Window {
+    let window = initscr();
+    noecho();
+    window_enable_colors();
+    window
+}
+
+pub fn end_window() {
+    endwin();
+}
+
+pub fn intro (w: &Window) {
+    fn slow_print(w: &Window, line: i32, string: String) {
+        w.mv(line, 0);
+        for c in string.chars() {
+            w.addch(c);
+            w.refresh();
+            // TODO keyboard skip
+            thread::sleep(time::Duration::from_millis(20));
+        }
+    }
+
+    w.clear();
+    thread::sleep(time::Duration::from_millis(250));
+    slow_print(&w, 0, String::from("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK"));
+
+    w.mv(1, 0);
+    w.refresh();
+    thread::sleep(time::Duration::from_millis(30));
+}
+
+fn window_enable_colors() {
+    if has_colors() {
+        start_color();
+        init_pair(1, COLOR_GREEN, COLOR_BLACK);
+    }
+}