@@ -7,7 +7,7 @@ mod selection_view;
7
7
mod terminal;
8
8
9
9
use arboard:: Clipboard ;
10
- use clap:: { command, Parser } ;
10
+ use clap:: { command, Parser , ValueEnum } ;
11
11
use colors:: Colors ;
12
12
use crossterm:: event:: { read, Event , KeyCode , KeyModifiers } ;
13
13
use ratatui:: layout:: { Constraint , Layout } ;
@@ -16,7 +16,7 @@ use std::{
16
16
error:: Error ,
17
17
fs:: File ,
18
18
io:: { BufRead , BufReader , Write } ,
19
- process,
19
+ process:: { self , exit } ,
20
20
} ;
21
21
#[ cfg( unix) ]
22
22
use std:: { fs:: Permissions , os:: unix:: prelude:: PermissionsExt } ;
@@ -39,13 +39,33 @@ struct Args {
39
39
/// Run as git commit hook.
40
40
#[ arg( long, value_delimiter = ' ' , num_args = 1 ..3 ) ]
41
41
hook : Vec < String > ,
42
+
43
+ /// The color scheme to use (`GIMOJI_COLOR_SCHEME` environment variable takes precedence).
44
+ #[ arg( short, long) ]
45
+ color_scheme : Option < ColorScheme > ,
46
+ }
47
+
48
+ #[ derive( ValueEnum , Debug , Clone , Copy ) ]
49
+ enum ColorScheme {
50
+ Light ,
51
+ Dark ,
52
+ }
53
+
54
+ impl From < ColorScheme > for Colors {
55
+ fn from ( c : ColorScheme ) -> Self {
56
+ match c {
57
+ ColorScheme :: Dark => Colors :: dark ( ) ,
58
+ ColorScheme :: Light => Colors :: light ( ) ,
59
+ }
60
+ }
42
61
}
43
62
44
63
fn main ( ) -> Result < ( ) , Box < dyn Error > > {
45
64
let args = Args :: parse ( ) ;
65
+ let color_scheme = get_color_scheme ( & args) ;
46
66
47
67
if args. init {
48
- install_hook ( ) ?;
68
+ install_hook ( color_scheme ) ?;
49
69
50
70
return Ok ( ( ) ) ;
51
71
} else if args. update_cache {
@@ -82,7 +102,7 @@ fn main() -> Result<(), Box<dyn Error>> {
82
102
( None , None )
83
103
} ;
84
104
85
- let selected = match select_emoji ( ) ? {
105
+ let selected = match select_emoji ( color_scheme . into ( ) ) ? {
86
106
Some ( s) => s,
87
107
None => return Ok ( ( ) ) ,
88
108
} ;
@@ -103,14 +123,9 @@ fn main() -> Result<(), Box<dyn Error>> {
103
123
Ok ( ( ) )
104
124
}
105
125
106
- fn select_emoji ( ) -> Result < Option < String > , Box < dyn Error > > {
126
+ fn select_emoji ( colors : Colors ) -> Result < Option < String > , Box < dyn Error > > {
107
127
let emojis = & emoji:: EMOJIS ;
108
128
109
- let colors = match terminal_light:: luma ( ) {
110
- Ok ( luma) if luma > 0.6 => Colors :: light ( ) ,
111
- _ => Colors :: dark ( ) ,
112
- } ;
113
-
114
129
let mut terminal = Terminal :: setup ( ) ?;
115
130
let mut search_entry = SearchEntry :: new ( & colors) ;
116
131
let mut selection_view = SelectionView :: new ( emojis, & colors) ;
@@ -167,9 +182,14 @@ fn select_emoji() -> Result<Option<String>, Box<dyn Error>> {
167
182
Ok ( selected)
168
183
}
169
184
170
- fn install_hook ( ) -> Result < ( ) , Box < dyn Error > > {
185
+ fn install_hook ( color_scheme : ColorScheme ) -> Result < ( ) , Box < dyn Error > > {
171
186
let mut file = File :: create ( HOOK_PATH ) ?;
172
- file. write_all ( HOOK_CONTENT . as_bytes ( ) ) ?;
187
+ let color_scheme_arg = match color_scheme {
188
+ ColorScheme :: Light => "--color-scheme light" ,
189
+ ColorScheme :: Dark => "--color-scheme dark" ,
190
+ } ;
191
+ let content = HOOK_CONTENT_TEMPL . replace ( "{color_scheme_arg}" , color_scheme_arg) ;
192
+ file. write_all ( content. as_bytes ( ) ) ?;
173
193
#[ cfg( unix) ]
174
194
file. set_permissions ( Permissions :: from_mode ( 0o744 ) ) ?;
175
195
@@ -216,10 +236,36 @@ fn copy_to_clipboard(s: String) -> Result<(), Box<dyn Error>> {
216
236
std:: process:: exit ( 0 )
217
237
}
218
238
239
+ // Color scheme selection. Precedence: env, arg, detection, default.
240
+ fn get_color_scheme ( args : & Args ) -> ColorScheme {
241
+ std:: env:: var ( "GIMOJI_COLOR_SCHEME" )
242
+ . ok ( )
243
+ . and_then ( |s| match s. as_str ( ) {
244
+ "light" => Some ( ColorScheme :: Light ) ,
245
+ "dark" => Some ( ColorScheme :: Dark ) ,
246
+ _ => None ,
247
+ } )
248
+ . or ( args. color_scheme )
249
+ . unwrap_or_else ( || {
250
+ if args. hook . is_empty ( ) {
251
+ match terminal_light:: luma ( ) {
252
+ Ok ( luma) if luma > 0.6 => ColorScheme :: Light ,
253
+ _ => ColorScheme :: Dark ,
254
+ }
255
+ } else {
256
+ eprintln ! ( "{}" , NO_SCHEME_IN_HOOK_ERROR ) ;
257
+
258
+ exit ( -1 ) ;
259
+ }
260
+ } )
261
+ }
262
+
219
263
const HOOK_PATH : & str = ".git/hooks/prepare-commit-msg" ;
220
- const HOOK_CONTENT : & str = r#"
264
+ const HOOK_CONTENT_TEMPL : & str = r#"
221
265
#!/usr/bin/env bash
222
266
# gimoji as a commit hook
223
- exec < /dev/tty
224
- gimoji --hook $1 $2
267
+ gimoji {color_scheme_arg} --hook $1 $2
225
268
"# ;
269
+
270
+ const NO_SCHEME_IN_HOOK_ERROR : & str =
271
+ r#"No color scheme specified in the git hook. Please re-install it using `gimoji -i`."# ;
0 commit comments