1use std::fmt::Display;
16use std::process::Command;
17use std::sync::LazyLock;
18
19use indicatif::{ProgressBar, ProgressStyle};
20use itertools::Itertools;
21
22pub fn get_program_name(cmd: &Command) -> String {
23 let program_path = cmd.get_program().to_string_lossy();
24 match program_path.rsplit_once('/') {
25 Some((_, rest)) => rest.to_owned(),
26 None => program_path.to_string(),
27 }
28}
29
30pub fn get_program_args(cmd: &Command) -> String {
31 cmd.get_args().map(|x| x.to_string_lossy()).join(" \\\n ")
32}
33
34pub fn get_program_env_cmd(cmd: &Command) -> String {
35 cmd.get_envs()
36 .map(|(k, v)| {
37 format!(
38 "export {}={}",
39 k.to_string_lossy(),
40 v.map(|v| v.to_string_lossy()).unwrap_or_default()
41 )
42 })
43 .join("\n")
44}
45
46pub fn new_spinner() -> ProgressBar {
47 let pb = ProgressBar::new(0);
48 pb.set_style(
49 ProgressStyle::default_spinner()
50 .template("🟡 {prefix}: {msg}")
51 .unwrap(),
52 );
53 pb
54}
55
56pub fn begin_spin(pb: &ProgressBar) {
57 pb.set_style(
58 ProgressStyle::default_spinner()
59 .template("{spinner} {prefix}: {msg}")
60 .unwrap(),
61 );
62}
63
64pub fn complete_spin(pb: &ProgressBar) {
65 pb.set_style(
66 ProgressStyle::default_spinner()
67 .template("✅ {prefix}: {msg}")
68 .unwrap(),
69 );
70}
71
72pub fn fail_spin(pb: &ProgressBar) {
73 pb.set_style(
74 ProgressStyle::default_spinner()
75 .template("❗ {prefix}: {msg}")
76 .unwrap(),
77 );
78}
79
80pub fn is_env_set(var: &str) -> bool {
81 if let Ok(val) = std::env::var(var) {
82 if let Ok(true) = val.parse() {
83 return true;
84 } else if let Ok(x) = val.parse::<usize>() {
85 if x != 0 {
86 return true;
87 }
88 }
89 }
90 false
91}
92
93pub fn is_enable_backtrace() -> bool {
94 !is_env_set("DISABLE_BACKTRACE")
95}
96
97pub fn risedev_cmd() -> &'static str {
98 static RISEDEV_CMD: LazyLock<String> = LazyLock::new(|| {
99 if let Ok(val) = std::env::var("RISEDEV_CMD") {
100 val
101 } else {
102 "./risedev".to_owned()
103 }
104 });
105
106 RISEDEV_CMD.as_str()
107}
108
109pub fn stylized_risedev_subcmd(subcmd: &str) -> impl Display + use<> {
110 console::style(format!("{} {}", risedev_cmd(), subcmd))
111 .blue()
112 .bold()
113}