use crate::internal_prelude::*;
use camino::Utf8PathBuf;
use clap::builder::styling::{Color, Style};
use std::borrow::Cow;
pub fn os_arch() -> Result<(&'static str, &'static str)> {
let target_os = if cfg!(target_os = "windows") {
"windows"
} else if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "linux") {
"linux"
} else {
bail!("unsupported OS")
};
let target_arch = if cfg!(target_arch = "x86_64") {
"x86_64"
} else if cfg!(target_arch = "aarch64") {
"aarch64"
} else {
bail!("unsupported target architecture")
};
Ok((target_os, target_arch))
}
pub fn is_linux_musl_env() -> bool {
cfg!(target_os = "linux") && cfg!(target_env = "musl")
}
pub trait StrAdditions {
fn with(&self, append: &str) -> String;
fn pad_left_to(&self, len: usize) -> Cow<'_, str>;
fn to_created_dir(&self) -> Result<Utf8PathBuf>;
}
impl StrAdditions for str {
fn with(&self, append: &str) -> String {
let mut s = self.to_string();
s.push_str(append);
s
}
fn pad_left_to(&self, len: usize) -> Cow<'_, str> {
let chars = self.chars().count();
if chars < len {
Cow::Owned(format!("{}{self}", " ".repeat(len - chars)))
} else {
Cow::Borrowed(self)
}
}
fn to_created_dir(&self) -> Result<Utf8PathBuf> {
let path = Utf8PathBuf::from(self);
if !path.exists() {
std::fs::create_dir_all(&path).wrap_err(format!("Could not create dir {self:?}"))?;
}
Ok(path)
}
}
impl StrAdditions for String {
fn with(&self, append: &str) -> String {
let mut s = self.clone();
s.push_str(append);
s
}
fn pad_left_to(&self, len: usize) -> Cow<'_, str> {
self.as_str().pad_left_to(len)
}
fn to_created_dir(&self) -> Result<Utf8PathBuf> {
self.as_str().to_created_dir()
}
}
pub trait Paint {
fn paint<'a>(self, text: impl Into<Cow<'a, str>>) -> String;
}
impl Paint for Color {
fn paint<'a>(self, text: impl Into<Cow<'a, str>>) -> String {
let text = text.into();
let style = Style::new().fg_color(Some(self));
format!("{style}{text}{style:#}")
}
}