Skip to main content

rustbasic_cli/
utils.rs

1pub fn to_snake_case(s: &str) -> String {
2    let mut snake = String::new();
3    for (i, ch) in s.chars().enumerate() {
4        if ch.is_uppercase() && i != 0 {
5            snake.push('_');
6        }
7        snake.push(ch.to_ascii_lowercase());
8    }
9    snake
10}
11pub fn to_pascal_case(s: &str) -> String {
12    let mut pascal = String::new();
13    let mut capitalize_next = true;
14    for ch in s.chars() {
15        if ch == '_' || ch == '-' {
16            capitalize_next = true;
17        } else if capitalize_next {
18            pascal.push(ch.to_ascii_uppercase());
19            capitalize_next = false;
20        } else {
21            pascal.push(ch);
22        }
23    }
24    pascal
25}
26
27pub fn open_browser(url: &str) {
28    let _ = match std::env::consts::OS {
29        "macos" => std::process::Command::new("open").arg(url).spawn(),
30        "windows" => std::process::Command::new("cmd").args(["/C", "start", url]).spawn(),
31        _ => std::process::Command::new("xdg-open").arg(url).spawn(),
32    };
33}
34
35pub fn wait_and_open(url: String) {
36    let addr = url.replace("http://", "").replace("https://", "");
37    let addr = addr.split('/').next().unwrap_or(&addr).to_string();
38    
39    std::thread::spawn(move || {
40        // Coba hubungkan ke port selama 60 detik (120 * 500ms)
41        for _ in 0..120 {
42            if std::net::TcpStream::connect(&addr).is_ok() {
43                open_browser(&url);
44                return;
45            }
46            std::thread::sleep(std::time::Duration::from_millis(500));
47        }
48    });
49}
50
51pub fn remove_dir_all_recursive(path: &std::path::Path) -> std::io::Result<()> {
52    if path.is_dir() {
53        for entry in std::fs::read_dir(path)? {
54            let entry = entry?;
55            let path = entry.path();
56            if path.is_dir() {
57                remove_dir_all_recursive(&path)?;
58            } else {
59                #[cfg(windows)]
60                {
61                    let mut perms = std::fs::metadata(&path)?.permissions();
62                    if perms.readonly() {
63                        perms.set_readonly(false);
64                        std::fs::set_permissions(&path, perms)?;
65                    }
66                }
67                std::fs::remove_file(&path)?;
68            }
69        }
70        #[cfg(windows)]
71        {
72            let mut perms = std::fs::metadata(path)?.permissions();
73            if perms.readonly() {
74                perms.set_readonly(false);
75                std::fs::set_permissions(path, perms)?;
76            }
77        }
78        std::fs::remove_dir(path)?;
79    } else if path.exists() {
80        #[cfg(windows)]
81        {
82            let mut perms = std::fs::metadata(path)?.permissions();
83            if perms.readonly() {
84                perms.set_readonly(false);
85                std::fs::set_permissions(path, perms)?;
86            }
87        }
88        std::fs::remove_file(path)?;
89    }
90    Ok(())
91}