1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use failure::Error;
use failure::ResultExt;
use failure::err_msg;
use log::{debug};
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::{DirEntry, WalkDir};

//pub fn get_home_files() -> Result<String, Error> {
    //let mut results = String::new();
    //let dir = env::var("HOME")?;
    //for entry in WalkDir::new(dir)
        //.into_iter()
        //.filter_entry(|e| !is_hidden(e))
    //{
        //if let Ok(e) = entry {
            //debug!("{}", e.path().display());
            //results.push_str(e.path().to_str().unwrap());
            //results.push_str("\n");
        //}
    //}

    //Ok(results)
//}

pub fn get_home_files() -> Option<Vec<PathBuf>> {
    match env::var("HOME") {
        Ok(dir) => {
            Some(WalkDir::new(dir)
                .into_iter()
		.filter_entry(|e| is_not_hidden(e))
                .filter_map(|entry| entry.ok())
                .filter_map(|entry| {
		    Some(entry.path().to_path_buf())
                })
                .collect())
        },
        _ => None
    }
}

pub fn open_file_in_default_app(path: &Path) -> Result<(), Error> {
    println!("Launching: xdg-open {:?}", path);
    Command::new("xdg-open")
        .arg(&path.as_os_str())
        .output()
        .with_context(|_| err_msg("Failed to run xdg-open"))?;

    Ok(())
}

fn is_hidden(entry: &DirEntry) -> bool {
    entry
        .file_name()
        .to_str()
        .map(|s| s.starts_with("."))
        .unwrap_or(false)
}


fn is_not_hidden(entry: &DirEntry) -> bool {
    entry
         .file_name()
         .to_str()
         .map(|s| entry.depth() == 0 || !s.starts_with("."))
         .unwrap_or(false)
}