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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::env;
use std::path::Path;
use std::ffi::OsStr;
use std::fs;
use std::fmt::*;

///saves a string to a shared value (temp file in /tmp)
///value must be a string value
#[allow(dead_code)]
pub fn set_shared_val<V: Into<String>>(name: &str, val: V) {
    fs::write(concat_str(vec!["/tmp/",name]), &val.into());
}

///gets a shared value (temp file in /tmp)
///value must be a string value
#[allow(dead_code)]
pub fn get_shared_val(name: &str) -> String{
    let val = fs::read_to_string(concat_str(vec!["/tmp/",name])).unwrap();
    val
}


///returns a list of files/directories using path string
#[allow(dead_code)]
pub fn get_dir<D: Into<String>>(dir: D, show_hidden: bool) -> Vec<String> {
    let dir_str = String::from(dir.into());
    let mut dir_name = dir_str.clone();
    if dir_str.chars().last().unwrap()!='/'{
        dir_name = concat_str(vec![dir_str.as_str(),"/"]);
    }

    let dir = fs::read_dir(&dir_name).unwrap();
    let mut output: Vec<String> = Vec::new();

    for file in dir {
        
        let name = file.unwrap();
        let sep = "/".to_string();
        let filename = get_filename(name.path().display().to_string());
        let attrs = name.metadata().unwrap();
        if show_hidden || filename.chars().nth(0).unwrap() != '.' {
            if attrs.is_dir() {
                output.push(concat_str(vec![name.path().display().to_string(),sep]));
            }
            else {
                output.push(get_filename(name.path().display().to_string()));
            }  
        }
    }
    alphabetize(output)

    
}

#[allow(dead_code)]
///Alphabetizes a vector of strings
pub fn alphabetize<L: Into<String>>(list: Vec<L>) -> Vec<String>
where  L: Clone{
    //extract the strings from the generic value
    let mut string_list: Vec<String> = Vec::new();
    for item in list {
        string_list.push(item.into());
    }

    string_list.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
    string_list
}

///get the name of the file/directory from a file path
pub fn get_filename<D: Into<String>>(dir: D) -> String {
    let dir_t =dir.into();
    let dir_vec = dir_t.split("/");
    dir_vec.last().unwrap().to_string()
}

#[allow(dead_code)]
///gets the parent directory from a file path
pub fn get_parent<D: Into<String>>(dir: D) -> String {
    let dir_t = dir.into();
    let mut dir_vec: Vec<&str> = dir_t.split("/").collect();
    dir_vec.pop();
    let mut new_dir= String::from("");

    for (_, folder) in dir_vec.iter().enumerate(){
        for c in folder.chars(){
            new_dir.push(c);
        }
        new_dir.push('/');
    }
    new_dir
}

///capitalize the first letter in a string
pub fn capitalize<W: Into<String>>(word: W) -> String {
    let s1 = word.into();
    let mut v: Vec<char> = s1.chars().collect();

    for i in 0..s1.len(){
        if !v[i].is_digit(10){
            v[i] = v[i].to_uppercase().nth(0).unwrap();
            break;
        }
    }
    let s2: String = v.into_iter().collect();
    s2.to_string()
}

///returns the location (starting from 0) of the first instance of a character in a string
///skips any leading digits
///useful for parsing arguments
///returns the string's length if the char is not found
pub fn find_char<T: Into<String>>(text: T, sym: char) -> usize {
    let mut counter = 0;
    
    for c in text.into().chars(){
        if c == sym{
            break;
        }
        counter += 1;
    }

    counter
}

///concatenates (joins) 2 or more strings in a vector together
#[allow(dead_code)]
pub fn concat_str<S: Into<String>>(text: Vec<S>) -> String {
    let mut out = String::from("");

    for work_string in text {
        for c in work_string.into().chars(){
            out.push(c)
        }
    }
    
    out
}

///get the name of the executable
pub fn get_execname() -> String {
    env::args().next()
        .as_ref()
        .map(Path::new)
        .and_then(Path::file_name)
        .and_then(OsStr::to_str)
        .map(String::from).unwrap()
}