2025-10-17 00:01:04 +00:00
# include <cstdint>
# include <format>
# include <regex>
Support HSL, HSV, CMYK, short options, minor I/O fix, and add a manual page (#12)
* use getopt_long(3) and support short options
Now instead of `--format`, `--no-fancy`, and `--help` you can also use
`-f`, `-n`, and `-h`. Additionally instead of just being able to do
`--format <fmt>` you can now do the standard `--format=<fmt>`.
* remove initial space when m_bFancyOutput is true
* add the HSL format
* add the HSV format
* add a manual page
* silence compiler warning
* make all the default target, and add install rule
* add the CMYK format
* add new formats to the README
2022-11-19 18:02:11 +01:00
# include <strings.h>
2023-03-31 17:41:40 +01:00
2022-09-02 18:06:00 +02:00
# include <iostream>
2023-03-31 17:41:40 +01:00
2022-09-02 18:06:00 +02:00
# include "hyprpicker.hpp"
2025-10-17 00:01:04 +00:00
# include "src/debug/Log.hpp"
2022-09-02 18:06:00 +02:00
2025-02-22 01:32:08 +05:00
static void help ( ) {
2023-03-31 17:41:40 +01:00
std : : cout < < " Hyprpicker usage: hyprpicker [arg [...]]. \n \n Arguments: \n "
2024-11-28 21:39:39 +05:30
< < " -a | --autocopy | Automatically copies the output to the clipboard (requires wl-clipboard) \n "
< < " -f | --format=fmt | Specifies the output format (cmyk, hex, rgb, hsl, hsv) \n "
2025-10-17 00:01:04 +00:00
< < " -o | --output-format=fmt | Specifies how the output color should be formatted e.g. rgb({0}, {1}, {2}) would output rgb(red, green, blue) if --format=rgb \n "
2025-06-10 19:39:45 +03:00
< < " -n | --notify | Sends a desktop notification when a color is picked (requires notify-send and a notification daemon like dunst) \n "
< < " -b | --no-fancy | Disables the \" fancy \" (aka. colored) outputting \n "
2024-11-28 21:39:39 +05:30
< < " -h | --help | Show this help message \n "
< < " -r | --render-inactive | Render (freeze) inactive displays \n "
< < " -z | --no-zoom | Disable the zoom lens \n "
< < " -q | --quiet | Disable most logs (leaves errors) \n "
< < " -v | --verbose | Enable more logs \n "
< < " -t | --no-fractional | Disable fractional scaling support \n "
2025-05-18 16:03:24 +00:00
< < " -d | --disable-preview | Disable live preview of color \n "
2025-01-17 21:32:10 +05:30
< < " -l | --lowercase-hex | Outputs the hexcode in lowercase \n "
2025-09-16 05:45:32 +08:00
< < " -s | --scale=scale | Set the zoom scale (between 1 and 10) \n "
< < " -u | --radius=radius | Set the circle radius (between 1 and 1000) \n "
2024-11-28 21:39:39 +05:30
< < " -V | --version | Print version info \n " ;
Support HSL, HSV, CMYK, short options, minor I/O fix, and add a manual page (#12)
* use getopt_long(3) and support short options
Now instead of `--format`, `--no-fancy`, and `--help` you can also use
`-f`, `-n`, and `-h`. Additionally instead of just being able to do
`--format <fmt>` you can now do the standard `--format=<fmt>`.
* remove initial space when m_bFancyOutput is true
* add the HSL format
* add the HSV format
* add a manual page
* silence compiler warning
* make all the default target, and add install rule
* add the CMYK format
* add new formats to the README
2022-11-19 18:02:11 +01:00
}
2022-09-02 18:06:00 +02:00
int main ( int argc , char * * argv , char * * envp ) {
g_pHyprpicker = std : : make_unique < CHyprpicker > ( ) ;
2022-09-02 20:44:42 +02:00
Support HSL, HSV, CMYK, short options, minor I/O fix, and add a manual page (#12)
* use getopt_long(3) and support short options
Now instead of `--format`, `--no-fancy`, and `--help` you can also use
`-f`, `-n`, and `-h`. Additionally instead of just being able to do
`--format <fmt>` you can now do the standard `--format=<fmt>`.
* remove initial space when m_bFancyOutput is true
* add the HSL format
* add the HSV format
* add a manual page
* silence compiler warning
* make all the default target, and add install rule
* add the CMYK format
* add new formats to the README
2022-11-19 18:02:11 +01:00
while ( true ) {
2023-03-31 17:41:40 +01:00
int option_index = 0 ;
2025-02-22 01:32:08 +05:00
static struct option long_options [ ] = { { " autocopy " , no_argument , nullptr , ' a ' } ,
{ " format " , required_argument , nullptr , ' f ' } ,
2025-10-17 00:01:04 +00:00
{ " output-format " , required_argument , nullptr , ' o ' } ,
2025-02-22 01:32:08 +05:00
{ " help " , no_argument , nullptr , ' h ' } ,
2025-06-10 19:39:45 +03:00
{ " no-fancy " , no_argument , nullptr , ' b ' } ,
{ " notify " , no_argument , nullptr , ' n ' } ,
2025-02-22 01:32:08 +05:00
{ " render-inactive " , no_argument , nullptr , ' r ' } ,
{ " no-zoom " , no_argument , nullptr , ' z ' } ,
{ " no-fractional " , no_argument , nullptr , ' t ' } ,
{ " quiet " , no_argument , nullptr , ' q ' } ,
{ " verbose " , no_argument , nullptr , ' v ' } ,
2025-05-18 16:03:24 +00:00
{ " disable-preview " , no_argument , nullptr , ' d ' } ,
2025-02-22 01:32:08 +05:00
{ " lowercase-hex " , no_argument , nullptr , ' l ' } ,
{ " version " , no_argument , nullptr , ' V ' } ,
2025-09-16 05:45:32 +08:00
{ " scale " , required_argument , nullptr , ' s ' } ,
{ " radius " , required_argument , nullptr , ' u ' } ,
2025-02-22 01:32:08 +05:00
{ nullptr , 0 , nullptr , 0 } } ;
Support HSL, HSV, CMYK, short options, minor I/O fix, and add a manual page (#12)
* use getopt_long(3) and support short options
Now instead of `--format`, `--no-fancy`, and `--help` you can also use
`-f`, `-n`, and `-h`. Additionally instead of just being able to do
`--format <fmt>` you can now do the standard `--format=<fmt>`.
* remove initial space when m_bFancyOutput is true
* add the HSL format
* add the HSV format
* add a manual page
* silence compiler warning
* make all the default target, and add install rule
* add the CMYK format
* add new formats to the README
2022-11-19 18:02:11 +01:00
2025-10-17 00:01:04 +00:00
int c = getopt_long ( argc , argv , " :f:o:hnbarzqvtdlVs:u: " , long_options , & option_index ) ;
Support HSL, HSV, CMYK, short options, minor I/O fix, and add a manual page (#12)
* use getopt_long(3) and support short options
Now instead of `--format`, `--no-fancy`, and `--help` you can also use
`-f`, `-n`, and `-h`. Additionally instead of just being able to do
`--format <fmt>` you can now do the standard `--format=<fmt>`.
* remove initial space when m_bFancyOutput is true
* add the HSL format
* add the HSV format
* add a manual page
* silence compiler warning
* make all the default target, and add install rule
* add the CMYK format
* add new formats to the README
2022-11-19 18:02:11 +01:00
if ( c = = - 1 )
break ;
switch ( c ) {
case ' f ' :
if ( strcasecmp ( optarg , " cmyk " ) = = 0 )
g_pHyprpicker - > m_bSelectedOutputMode = OUTPUT_CMYK ;
else if ( strcasecmp ( optarg , " hex " ) = = 0 )
g_pHyprpicker - > m_bSelectedOutputMode = OUTPUT_HEX ;
else if ( strcasecmp ( optarg , " rgb " ) = = 0 )
g_pHyprpicker - > m_bSelectedOutputMode = OUTPUT_RGB ;
else if ( strcasecmp ( optarg , " hsl " ) = = 0 )
g_pHyprpicker - > m_bSelectedOutputMode = OUTPUT_HSL ;
else if ( strcasecmp ( optarg , " hsv " ) = = 0 )
g_pHyprpicker - > m_bSelectedOutputMode = OUTPUT_HSV ;
else {
Debug : : log ( NONE , " Unrecognized format %s " , optarg ) ;
exit ( 1 ) ;
}
break ;
2025-10-17 00:01:04 +00:00
case ' o ' : g_pHyprpicker - > m_sOutputFormat = optarg ; break ;
2023-03-31 17:41:40 +01:00
case ' h ' : help ( ) ; exit ( 0 ) ;
2025-06-10 19:39:45 +03:00
case ' b ' : g_pHyprpicker - > m_bFancyOutput = false ; break ;
case ' n ' : g_pHyprpicker - > m_bNotify = true ; break ;
2023-03-31 17:41:40 +01:00
case ' a ' : g_pHyprpicker - > m_bAutoCopy = true ; break ;
case ' r ' : g_pHyprpicker - > m_bRenderInactive = true ; break ;
case ' z ' : g_pHyprpicker - > m_bNoZoom = true ; break ;
2024-09-29 18:12:50 +01:00
case ' t ' : g_pHyprpicker - > m_bNoFractional = true ; break ;
case ' q ' : Debug : : quiet = true ; break ;
case ' v ' : Debug : : verbose = true ; break ;
2025-05-18 16:03:24 +00:00
case ' d ' : g_pHyprpicker - > m_bDisablePreview = true ; break ;
2025-01-17 21:32:10 +05:30
case ' l ' : g_pHyprpicker - > m_bUseLowerCase = true ; break ;
2024-09-29 18:24:57 +01:00
case ' V ' : {
std : : cout < < " hyprpicker v " < < HYPRPICKER_VERSION < < " \n " ;
exit ( 0 ) ;
}
2025-09-16 05:45:32 +08:00
case ' s ' : {
float value ;
auto result = std : : from_chars ( optarg , optarg + strlen ( optarg ) , value ) ;
2023-03-31 17:41:40 +01:00
2025-09-16 05:45:32 +08:00
if ( result . ec ! = std : : errc ( ) | | result . ptr ! = optarg + strlen ( optarg ) ) {
std : : cerr < < " Invalid scale value: " < < optarg < < " \n " ;
exit ( 1 ) ;
}
if ( value < 1.0f | | value > 10.0f ) {
std : : cerr < < " Scale must be between 1 and 10! \n " ;
exit ( 1 ) ;
}
g_pHyprpicker - > m_fZoomScale = value ;
break ;
}
case ' u ' : {
int value ;
auto result = std : : from_chars ( optarg , optarg + strlen ( optarg ) , value ) ;
if ( result . ec ! = std : : errc ( ) | | result . ptr ! = optarg + strlen ( optarg ) ) {
std : : cerr < < " Invalid radius value: " < < optarg < < " \n " ;
exit ( 1 ) ;
}
if ( value < 1 | | value > 1000 ) {
std : : cerr < < " Radius must be between 1 and 1000! \n " ;
exit ( 1 ) ;
}
g_pHyprpicker - > m_iCircleRadius = value ;
break ;
}
2023-03-31 17:41:40 +01:00
default : help ( ) ; exit ( 1 ) ;
}
2022-09-02 20:44:42 +02:00
}
2022-09-02 21:14:44 +02:00
if ( ! isatty ( fileno ( stdout ) ) | | getenv ( " NO_COLOR " ) )
g_pHyprpicker - > m_bFancyOutput = false ;
2025-10-17 00:01:04 +00:00
if ( g_pHyprpicker - > m_sOutputFormat . empty ( ) ) {
switch ( g_pHyprpicker - > m_bSelectedOutputMode ) {
case OUTPUT_CMYK : g_pHyprpicker - > m_sOutputFormat = " {}% {}% {}% {}% " ; break ;
case OUTPUT_HEX : g_pHyprpicker - > m_sOutputFormat = " #{}{}{} " ; break ;
case OUTPUT_RGB : g_pHyprpicker - > m_sOutputFormat = " {} {} {} " ; break ;
case OUTPUT_HSL : g_pHyprpicker - > m_sOutputFormat = " {} {}% {}% " ; break ;
case OUTPUT_HSV : g_pHyprpicker - > m_sOutputFormat = " {} {}% {}% " ; break ;
}
}
try {
std : : array < uint8_t , 4 > dummy = { 0 , 0 , 0 , 0 } ;
( void ) std : : vformat ( g_pHyprpicker - > m_sOutputFormat , std : : make_format_args ( dummy [ 0 ] , dummy [ 1 ] , dummy [ 2 ] , dummy [ 3 ] ) ) ;
} catch ( const std : : format_error & e ) {
Debug : : log ( NONE , " Invalid --output-format: %s " , e . what ( ) ) ;
exit ( 1 ) ;
}
2022-09-02 18:06:00 +02:00
g_pHyprpicker - > init ( ) ;
return 0 ;
2022-09-04 22:48:37 -04:00
}