use crate::config::Editor;
use ratatui::style::Color;
use std::io;
pub fn parse_color(s: &str) -> Color {
match s.to_lowercase().as_str() {
"default" | "reset" => Color::Reset,
"yellow" => Color::Yellow,
"red" => Color::Red,
"blue" => Color::Blue,
"green" => Color::Green,
"magenta" => Color::Magenta,
"cyan" => Color::Cyan,
"white" => Color::White,
"black" => Color::Black,
_ => {
if let Some(color) = s.strip_prefix('#') {
match color.len() {
6 => {
if let Ok(rgb) = u32::from_str_radix(color, 16) {
return Color::Rgb(
((rgb >> 16) & 0xFF) as u8,
((rgb >> 8) & 0xFF) as u8,
(rgb & 0xFF) as u8,
);
}
}
3 => {
let expanded = color
.chars()
.map(|c| format!("{}{}", c, c))
.collect::<String>();
if let Ok(rgb) = u32::from_str_radix(&expanded, 16) {
return Color::Rgb(
((rgb >> 16) & 0xFF) as u8,
((rgb >> 8) & 0xFF) as u8,
(rgb & 0xFF) as u8,
);
}
}
_ => {}
}
}
Color::Reset
}
}
}
pub fn open_in_editor(editor: &Editor, file_path: &std::path::Path) -> std::io::Result<()> {
use crossterm::{
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
let mut stdout = io::stdout();
disable_raw_mode()?;
execute!(stdout, LeaveAlternateScreen)?;
let status = std::process::Command::new(editor.cmd())
.arg(file_path)
.status();
execute!(io::stdout(), EnterAlternateScreen)?;
enable_raw_mode()?;
status.map(|_| ())
}