]> OzVa Git service - gn-editor/commitdiff
Moved more logic to utils
authorNil Gradisnik <nil@layer.com>
Fri, 8 Dec 2017 03:08:11 +0000 (19:08 -0800)
committerNil Gradisnik <nil@layer.com>
Fri, 8 Dec 2017 03:08:11 +0000 (19:08 -0800)
src/main.rs
src/utils.rs

index d0e433f70e6314244715fb705c18c67fe695d0a9..30eaff4e1d708482d780e5a4542099914217f378 100644 (file)
@@ -11,15 +11,12 @@ mod preview;
 mod utils;
 
 use std::env::args;
-use std::fs::File;
-use std::io::prelude::*;
-use std::io::BufReader;
 
 use gio::prelude::*;
 use gtk::prelude::*;
 use gtk::Builder;
 
-use utils::{buffer_to_string, set_title, configure_sourceview};
+use utils::{buffer_to_string, open_file, set_title, configure_sourceview};
 
 const NAME: &str = env!("CARGO_PKG_NAME");
 const VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -83,17 +80,9 @@ fn build_ui(application: &gtk::Application) {
 
         if file_chooser.run() == gtk::ResponseType::Ok.into() {
             let filename = file_chooser.get_filename().expect("Couldn't get filename");
-            let file = File::open(&filename).expect("Couldn't open file");
-
-            let mut reader = BufReader::new(file);
-            let mut contents = String::new();
-            let _ = reader.read_to_string(&mut contents);
+            let contents = open_file(&filename);
 
             set_title(&header_bar, &filename);
-            if let Some(parent) = filename.parent() {
-                let subtitle: &str = &parent.to_string_lossy();
-                header_bar.set_subtitle(subtitle);
-            }
 
             text_buffer.set_text(&contents);
             markdown_view.get_buffer().unwrap().set_text(&preview::render(&contents));
index 21a9210d6ef3535849cf959fc06a82bb46297303..964291a00c1eee9cdfd206166670011a4269aaf4 100644 (file)
@@ -1,11 +1,21 @@
+
+use std::fs::File;
+use std::io::prelude::*;
+use std::io::BufReader;
+
 use gtk::*;
 use sourceview::*;
-use std::path::Path;
+use std::path::{PathBuf};
 
-pub fn set_title(headerbar: &HeaderBar, path: &Path) {
-    if let Some(filename) = path.file_name() {
-        let filename: &str = &filename.to_string_lossy();
-        headerbar.set_title(filename);
+pub fn set_title(header_bar: &HeaderBar, path: &PathBuf) {
+    if let Some(file_name) = path.file_name() {
+        let file_name: &str = &file_name.to_string_lossy();
+        header_bar.set_title(file_name);
+
+        if let Some(parent) = path.parent() {
+            let subtitle: &str = &parent.to_string_lossy();
+            header_bar.set_subtitle(subtitle);
+        }
     }
 }
 
@@ -14,6 +24,16 @@ pub fn buffer_to_string(buffer: &Buffer) -> Option<String> {
     buffer.get_text(&start, &end, false)
 }
 
+pub fn open_file(filename: &PathBuf) -> String {
+    let file = File::open(&filename).expect("Couldn't open file");
+
+    let mut reader = BufReader::new(file);
+    let mut contents = String::new();
+    let _ = reader.read_to_string(&mut contents);
+
+    contents
+}
+
 pub fn configure_sourceview(buff: &Buffer) {
     LanguageManager::new()
         .get_language("markdown")