@@ -16,6 +16,7 @@ use termcolor::{ColorChoice, StandardStream, WriteColor as _};
16
16
use terminal_size:: { terminal_size, Height } ;
17
17
18
18
const DEFAULT_UPSTREAM_BRANCHES : & [ & str ] = & [ "main" , "master" , "develop" , "trunk" ] ;
19
+ pub const DEFAULT_THEME : & str = "base16-ocean.dark" ;
19
20
20
21
pub struct Config {
21
22
/// Change the commit message that you amend, instead of using the original commit message
@@ -27,11 +28,13 @@ pub struct Config {
27
28
pub default_upstream_branch : Option < String > ,
28
29
/// Require a newline when confirming y/n questions
29
30
pub require_newline : bool ,
31
+ /// Which theme to use
32
+ pub theme : String ,
30
33
}
31
34
32
35
pub fn instafix ( c : Config ) -> Result < ( ) , anyhow:: Error > {
33
36
let repo = Repository :: open ( "." ) . context ( "opening repo" ) ?;
34
- let diff = create_diff ( & repo, c. require_newline ) . context ( "creating diff" ) ?;
37
+ let diff = create_diff ( & repo, & c . theme , c. require_newline ) . context ( "creating diff" ) ?;
35
38
let head = repo. head ( ) . context ( "finding head commit" ) ?;
36
39
let head_branch = Branch :: wrap ( head) ;
37
40
let upstream = get_merge_base ( & repo, & head_branch, c. default_upstream_branch . as_deref ( ) )
@@ -254,7 +257,11 @@ fn get_merge_base<'a>(
254
257
}
255
258
256
259
/// Get a diff either from the index or the diff from the index to the working tree
257
- fn create_diff ( repo : & Repository , require_newline : bool ) -> Result < Diff , anyhow:: Error > {
260
+ fn create_diff < ' a > (
261
+ repo : & ' a Repository ,
262
+ theme : & str ,
263
+ require_newline : bool ,
264
+ ) -> Result < Diff < ' a > , anyhow:: Error > {
258
265
let head = repo. head ( ) ?;
259
266
let head_tree = head. peel_to_tree ( ) ?;
260
267
let staged_diff = repo. diff_tree_to_index ( Some ( & head_tree) , None , None ) ?;
@@ -269,7 +276,7 @@ fn create_diff(repo: &Repository, require_newline: bool) -> Result<Diff, anyhow:
269
276
if total_change >= cutoff_height {
270
277
print_diffstat ( "Unstaged" , & dirty_diff) ?;
271
278
} else {
272
- let diff_lines = native_diff ( & dirty_diff) ?;
279
+ let diff_lines = native_diff ( & dirty_diff, theme ) ?;
273
280
if diff_lines. len ( ) >= cutoff_height {
274
281
print_diffstat ( "Unstaged" , & dirty_diff) ?;
275
282
} else {
@@ -439,13 +446,26 @@ fn format_ref(rf: &git2::Reference<'_>) -> Result<String, anyhow::Error> {
439
446
Ok ( format ! ( "{} ({})" , shorthand, & sha[ ..10 ] ) )
440
447
}
441
448
449
+ /// A vec of all built-in theme names
450
+ pub fn print_themes ( ) {
451
+ println ! ( "Available themes:" ) ;
452
+ for theme in ThemeSet :: load_defaults ( ) . themes . keys ( ) {
453
+ println ! ( " {}" , theme) ;
454
+ }
455
+ }
456
+
442
457
// diff helpers
443
458
444
- fn native_diff ( diff : & Diff < ' _ > ) -> Result < Vec < String > , anyhow:: Error > {
459
+ fn native_diff ( diff : & Diff < ' _ > , theme : & str ) -> Result < Vec < String > , anyhow:: Error > {
445
460
let ss = SyntaxSet :: load_defaults_newlines ( ) ;
446
461
let ts = ThemeSet :: load_defaults ( ) ;
447
462
let syntax = ss. find_syntax_by_extension ( "patch" ) . unwrap ( ) ;
448
- let mut h = HighlightLines :: new ( syntax, & ts. themes [ "base16-ocean.dark" ] ) ;
463
+ let mut h = HighlightLines :: new (
464
+ syntax,
465
+ ts. themes
466
+ . get ( theme)
467
+ . unwrap_or_else ( || & ts. themes [ DEFAULT_THEME ] ) ,
468
+ ) ;
449
469
450
470
let mut inner_err = None ;
451
471
let mut diff_lines = Vec :: new ( ) ;
0 commit comments