feat: cli implemented

This commit is contained in:
2025-08-16 13:26:32 +02:00
parent a48cce3fc6
commit c699003701
7 changed files with 777 additions and 83 deletions

View File

@@ -1,3 +1,5 @@
use std::fmt::Display;
use crate::color::{ColorHue, HSL, Percentage, RGB};
use regex::Regex;
@@ -11,6 +13,12 @@ impl HSL {
}
}
impl Display for HSL {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "hsl({}, {}%, {}%)", self.0, self.1, self.2)
}
}
impl PartialEq for HSL {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0 && self.1 == other.1 && self.2 == other.2

View File

@@ -15,20 +15,20 @@ use crate::core::ranged::RangedInt;
pub type ColorIntensity = RangedInt<0, 255>;
pub type ColorHue = RangedInt<0, 360>;
pub type Percentage = RangedInt<0, 100>;
#[derive(Debug)]
pub struct RGB(ColorIntensity, ColorIntensity, ColorIntensity);
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct RGB(pub ColorIntensity, pub ColorIntensity, pub ColorIntensity);
#[derive(Debug, Clone)]
pub struct HSL(ColorHue, Percentage, Percentage);
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct HSV(ColorHue, Percentage, Percentage);
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Color(RGB);
impl Color {
pub fn try_parse(input: String) -> Result<Color, ()> {
let input = input.replace(" ", "").to_lowercase();
// TODO: clean all of this
// TODO: clean all of this to manage errors on a clean and simple way
// - Move down to custom trait that returns a Result
let hex_regex = Regex::new(r".*(#[a-fA-F0-9]{3,6}).*").unwrap();
let rgb_regex =
@@ -101,3 +101,21 @@ impl From<HSV> for Color {
Color(RGB::from(color))
}
}
impl Into<RGB> for Color {
fn into(self) -> RGB {
self.0
}
}
impl Into<HSL> for Color {
fn into(self) -> HSL {
HSL::from(self.0)
}
}
impl Into<HSV> for Color {
fn into(self) -> HSV {
HSV::from(self.0)
}
}

View File

@@ -14,6 +14,14 @@ impl RGB {
ColorIntensity::new(b as BaseNumber),
)
}
pub fn to_u8_tuple(self) -> (u8, u8, u8) {
(
self.0.to_f32() as u8,
self.1.to_f32() as u8,
self.2.to_f32() as u8,
)
}
}
// TODO: manage error
@@ -67,7 +75,7 @@ impl Display for RGB {
impl UpperHex for RGB {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0>2X}{:0>2X}{:0>2X}", self.0, self.1, self.2)
write!(f, "#{:0>2X}{:0>2X}{:0>2X}", self.0, self.1, self.2)
}
}

View File

@@ -110,23 +110,23 @@ pub mod tests {
let green_color = Color::from(RGB::new(0, 255, 0));
let blue_color = Color::from(RGB::new(0, 0, 255));
assert_eq!(format!("{:X}", red_color), "FF0000");
assert_eq!(format!("{:X}", green_color), "00FF00");
assert_eq!(format!("{:X}", blue_color), "0000FF");
assert_eq!(format!("{:X}", red_color), "#FF0000");
assert_eq!(format!("{:X}", green_color), "#00FF00");
assert_eq!(format!("{:X}", blue_color), "#0000FF");
}
#[test]
fn test_complex_hex_convertion() {
let color = Color::from(RGB::new(255, 183, 3));
assert_eq!(format!("{:X}", color), "FFB703");
assert_eq!(format!("{:X}", color), "#FFB703");
let color = Color::from(RGB::new(88, 129, 87));
assert_eq!(format!("{:X}", color), "588157");
assert_eq!(format!("{:X}", color), "#588157");
let color = Color::from(RGB::new(251, 133, 0));
assert_eq!(format!("{:X}", color), "FB8500");
assert_eq!(format!("{:X}", color), "#FB8500");
let color = Color::from(RGB::new(131, 56, 236));
assert_eq!(format!("{:X}", color), "8338EC");
assert_eq!(format!("{:X}", color), "#8338EC");
let color = Color::from(RGB::new(157, 129, 137));
assert_eq!(format!("{:X}", color), "9D8189");
assert_eq!(format!("{:X}", color), "#9D8189");
}
#[test]

View File

@@ -1,31 +1,90 @@
use clipboard::ClipboardContext;
use clipboard::ClipboardProvider;
use arboard::Clipboard;
use colored::Colorize;
use inquire::Select;
use inquire::Text;
use std::env;
use crate::color::Color;
use crate::color::HSL;
use crate::color::HSV;
use crate::color::RGB;
mod color;
pub mod core;
fn example() {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
println!("{:?}", ctx.get_contents());
ctx.set_contents("some string".to_owned()).unwrap();
fn set_clipboard(output: String) {
let mut ctx = Clipboard::new().unwrap();
let result = ctx.set_text(output.clone());
match result {
Ok(_) => {
println!(
"{} Copied {} to clipboard!",
"".truecolor(0, 240, 0),
output
);
}
Err(_) => {
println!("{} Error on copy to clipboard!", "".truecolor(240, 0, 0),);
}
}
}
// fn read_clipboard() -> Result<String, ()> {
// let ctx: ClipboardContext = ClipboardContext::new().unwrap();
// let value = ctx.get_rich_text();
// match value {
// Ok(clipboard) => return Ok(clipboard),
// Err(_) => return Err(()),
// }
// }
/**
* Params
* --clipboard / -c : read from clipboard first
* --input <String> / -i <String> : dont read from clipboard nor input, simply try parsing the input
*/
fn main() {
println!("Hello, world!");
example();
let args: Vec<String> = env::args().collect();
let color = RGB::new(220, 10, 50);
let hsv_color = HSV::from(RGB::new(220, 10, 50));
println!("RGB color: {}, HSV Color: {}", color, hsv_color);
let parsed_color = Color::try_parse("#F2FA01".to_string());
match parsed_color {
Ok(color) => {
let options = list_color_options(color.clone());
let rgb_color: RGB = color.clone().into();
let rgb_tuple = rgb_color.clone().to_u8_tuple();
let selected_format = Select::new(
&format!(
"Encodings of color {}",
"".truecolor(rgb_tuple.0, rgb_tuple.1, rgb_tuple.2)
),
options,
)
.prompt();
Color::try_parse("#F2FA01".to_string());
Color::try_parse("rgb(1, 3,4)".to_string());
Color::try_parse("rgb(1,3 ,4)".to_string());
Color::try_parse("rgb(1,F,4)".to_string());
Color::try_parse("hsl(100,100%,40%)".to_string());
Color::try_parse("hsl(100,100%,40)".to_string());
match selected_format {
Ok(format) => {
set_clipboard(format.clone());
}
Err(_) => {
let _ = Text::new("Error on input read").prompt();
}
}
}
Err(()) => (),
}
}
fn list_color_options(color: Color) -> Vec<String> {
let mut options: Vec<String> = vec![];
let rgb_color: RGB = color.clone().into();
let hsl_color: HSL = color.clone().into();
let hsv_color: HSV = color.clone().into();
options.push(format!("{:X}", rgb_color));
options.push(format!("{}", rgb_color));
options.push(format!("{}", hsl_color));
options.push(format!("{}", hsv_color));
options
}