1use crate::{
2 ansi::{
3 arrows::RET,
4 colors::{G, U, X},
5 moves::pos_x,
6 others::{ARS, TAB},
7 },
8 git::{set_url, MOVE_DEL, POS_X_ARG},
9 misc::{confirm, input, warning},
10 proc::execute,
11};
12
13use std::{path::Path, process::exit};
14
15fn set_clone_branch(given: &str, flag: bool) -> String {
16 let branch = if flag {
17 given.to_string()
18 } else if confirm("Is it master branch") {
19 "master".to_string()
20 } else {
21 let b = input("Enter branch name");
22 if !b.is_empty() {
23 b
24 } else {
25 "master".to_string()
26 }
27 };
28 print!("{x}", x = MOVE_DEL);
29 println!(
30 "{t}{r} Remote branch set to {a}: {x}{u}{v}{x}",
31 a = pos_x(POS_X_ARG),
32 t = TAB,
33 r = RET,
34 u = U,
35 x = X,
36 v = branch
37 );
38 branch
39}
40
41fn set_clone_dir(url: &str) -> String {
42 let name: Vec<&str> = url.split('/').collect();
43 let name = name.last().unwrap();
44 let question = format!("Clone it to `{}", name);
45 let mut name = if confirm(&question) {
46 (*name).to_string()
47 } else {
48 let b = input("Enter directory name");
49 if !b.is_empty() {
50 b
51 } else {
52 (*name).to_string()
53 }
54 };
55
56 loop {
57 if Path::new(&name).exists() {
58 let string = format!("Path `{}` already exists", name);
59 warning(&string);
60 } else {
61 break;
62 }
63 let b = input("Enter directory name");
64 if !b.is_empty() {
65 name = b
66 }
67 print!("{x}{x}{x}", x = MOVE_DEL);
68 }
69 print!("{x}", x = MOVE_DEL);
70
71 println!(
72 "{t}{r} Cloning remote repository to {a}: {x}{u}{v}{x}",
73 a = pos_x(POS_X_ARG),
74 t = TAB,
75 r = RET,
76 u = U,
77 x = X,
78 v = name
79 );
80 name
81}
82
83pub fn clone(given_url: &str, given_branch: &str, given_input: bool) {
84 println!(
86 "\n{c}{a}Cloning remote repository...{x}",
87 a = ARS,
88 c = G,
89 x = X
90 );
91 let url = set_url(&given_url);
93 let branch = set_clone_branch(&given_branch, given_input);
94 let name = set_clone_dir(&url);
95 let command = format!("git clone -b {} {} {}", branch, url, name);
96 execute(&command);
97 exit(0);
98}