1extern crate colored;
2extern crate dirs;
3
4use colored::*;
5use dirs::home_dir;
6use std::fs::create_dir_all;
7use std::fs::File;
8use std::io::prelude::*;
9use std::path::PathBuf;
10
11pub fn setup_thefuck() {
19 pub const THEFUCK_STRING: &str = include_str!("../py/clap-rs.py");
20 let home_dir = match home_dir() {
21 Some(p) => p,
22 None => PathBuf::from("."),
23 };
24
25 let mut config_path = home_dir;
26 config_path.push(".config");
27 config_path.push("thefuck");
28 config_path.push("rules");
29 if config_path.exists() && config_path.is_dir() {
30 config_path.push("clap-rs");
31 config_path.set_extension("py");
32 if let Ok(mut f) = File::create(&config_path) {
33 let thefuck_string = THEFUCK_STRING.to_string();
34 match f.write(thefuck_string.as_bytes()) {
35 Ok(_) => (),
36 _ => eprintln!("{}: file write failed", "Warning".yellow()),
37 };
38 } else {
39 eprintln!(
40 "{}: failed to open file at {}",
41 "Warning".yellow(),
42 &config_path.display()
43 );
44 };
45 }
46}
47
48pub fn write_shell_config(shell_cfg: &str) {
49 let home_dir = match home_dir() {
50 Some(p) => p,
51 None => PathBuf::from("."),
52 };
53
54 let mut cfg_file = home_dir;
55 cfg_file.push(shell_cfg);
56 if let Ok(mut f) = File::open(&cfg_file) {
57 let mut contents = String::new();
58 match f.read_to_string(&mut contents) {
59 Ok(_) => (),
60 Err(_) => eprintln!(
61 "{}: failed to open file, not installing manual pages",
62 "Warning".yellow()
63 ),
64 }
65 let should_write: bool = contents
66 .lines()
67 .all(|line| line != "\n#manpath updated by cli-setup");
68
69 let mut contents_saved = contents;
70
71 if !should_write {
72 contents_saved.push_str(
73 "\n#manpath updated by cli-setup\nexport MANPATH=\"$HOME\"/.local/share:$MANPATH",
74 );
75 match File::create(&cfg_file) {
76 Ok(mut file) => match file.write(contents_saved.as_bytes()) {
77 Ok(_) => (),
78 Err(_) => {
79 eprintln!("{}: failed to open file, not modifying", "Warning".yellow())
80 }
81 },
82 _ => eprintln!(
83 "{}: failed to open file at {}, modifying",
84 "Warning".yellow(),
85 &cfg_file.display()
86 ),
87 };
88 };
89 };
90}
91
92pub fn setup_manpages(man: &str, exe_name: &str) {
102 let home_dir = match home_dir() {
103 Some(p) => p,
104 None => PathBuf::from("."),
105 };
106
107 write_shell_config(".bashrc");
108 write_shell_config(".zshrc");
109
110 let mut man_dir = home_dir;
111 man_dir.push(".local");
112 man_dir.push("share");
113 man_dir.push("man");
114 man_dir.push("man1");
115
116 let _ = create_dir_all(&man_dir);
117 let mut man_path = man_dir;
118
119 man_path.push(exe_name);
120 man_path.set_extension("1");
121
122 let pre_f = File::create(man_path);
123 match pre_f {
124 Ok(mut f) => {
125 let res = f.write(man.as_bytes());
126 match res {
127 Ok(_) => (),
128 Err(_) => eprintln!(
129 "{}: failed to open file, not installing manual pages",
130 "Warning".yellow()
131 ),
132 }
133 }
134 Err(_) => eprintln!(
135 "{}: failed to open file, not installing manual pages",
136 "Warning".yellow()
137 ),
138 }
139}