Theming system

This commit is contained in:
Emmanuel BENOîT 2020-10-19 23:42:19 +02:00
parent 59903d01aa
commit a6672ed393

91
spp.pl
View file

@ -10,6 +10,9 @@ use POSIX qw(strftime);
our %CONFIG = ( our %CONFIG = (
# LAYOUT # LAYOUT
# - Theme and local overrides
layout_theme => 'powerline_yb' ,
layout_theme_overrides => {} ,
# - Section generators for the left side of the top bar # - Section generators for the left side of the top bar
layout_left => [ layout_left => [
'datetime' , 'datetime' ,
@ -73,7 +76,14 @@ our %CONFIG = (
load_min => 0 , load_min => 0 ,
); );
our %THEME = (
#-------------------------------------------------------------------------------
# THEMES
our %THEMES = ();
# Powerline based, using yellow and blue
$THEMES{powerline_yb} = {
# Padding character # Padding character
padding => ' ' , padding => ' ' ,
# Left side of top line # Left side of top line
@ -150,8 +160,8 @@ our %THEME = (
load_med_bg => 135 , load_med_bg => 135 ,
# Load average - High load colors # Load average - High load colors
load_high_fg => 231 , load_high_fg => 231 ,
load_high_bg => 130 load_high_bg => 130 ,
); };
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# MAIN PROGRAM # MAIN PROGRAM
@ -175,6 +185,19 @@ sub set_color
return tput_sequence( "seta$type $index" ); return tput_sequence( "seta$type $index" );
} }
our $THEME = undef;
sub themed($)
{
my $k = shift;
unless ( defined $THEME ) {
$THEME = {
%{ $THEMES{ $CONFIG{layout_theme} } } ,
%{ $CONFIG{layout_theme_overrides} } ,
};
}
return $THEME->{ $k };
}
sub get_section_length sub get_section_length
{ {
my $section = shift; my $section = shift;
@ -199,7 +222,7 @@ sub get_length
sub gen_transition sub gen_transition
{ {
my $transition = shift; my $transition = shift;
my @colors = ( @_ , @{ $THEME{transition} } ); my @colors = ( @_ , @{ themed 'transition' } );
my $state = 0; my $state = 0;
my $pc; my $pc;
my $out = [ ]; my $out = [ ];
@ -232,7 +255,7 @@ sub compute_trans_lengths
foreach my $type ( qw( prefix separator suffix ) ) { foreach my $type ( qw( prefix separator suffix ) ) {
my $k = $side . '_' . $type; my $k = $side . '_' . $type;
$out{ $k } = get_section_length( gen_transition( $out{ $k } = get_section_length( gen_transition(
$THEME{ $k } , 1 , 2 ) ); themed $k , 1 , 2 ) );
} }
} }
return %out; return %out;
@ -282,9 +305,9 @@ sub add_transitions
my $cBefore = shift; my $cBefore = shift;
my $cAfter = shift; my $cAfter = shift;
my $prefix = $THEME{$name . '_prefix'}; my $prefix = themed( $name . '_prefix' );
my $separator = $THEME{$name . '_separator'}; my $separator = themed( $name . '_separator' );
my $suffix = $THEME{$name . '_suffix'}; my $suffix = themed( $name . '_suffix' );
my $prevBg = undef; my $prevBg = undef;
my $curBg = $cBefore; my $curBg = $cBefore;
@ -310,7 +333,7 @@ sub render
my $out = ''; my $out = '';
my $mustSetFg = undef; my $mustSetFg = undef;
my $mustSetBg = undef; my $mustSetBg = undef;
my $cDefault = $THEME{'bg_' . $name}; my $cDefault = themed( 'bg_' . $name );
foreach my $section ( @_ ) { foreach my $section ( @_ ) {
$mustSetBg = $section->{bg} if exists $section->{bg}; $mustSetBg = $section->{bg} if exists $section->{bg};
foreach my $part ( @{ $section->{content} } ) { foreach my $part ( @{ $section->{content} } ) {
@ -345,16 +368,16 @@ sub gen_top_line
# Generate content # Generate content
my ( @lm , @middle , @mr ) = ( ); my ( @lm , @middle , @mr ) = ( );
my $mc = $THEME{bg_middle}; my $mc = themed 'bg_middle';
@left = gen_prompt_sections( @left ); @left = gen_prompt_sections( @left );
if ( defined $midGen ) { if ( defined $midGen ) {
@middle = ( gen_prompt_section( $midGen ) ); @middle = ( gen_prompt_section( $midGen ) );
if ( @middle ) { if ( @middle ) {
@lm = ( @lm = (
gen_transition( $THEME{middle_prefix} , $mc , $mc ) , gen_transition( themed('middle_prefix') , $mc , $mc ) ,
{ bg => $THEME{bg_middle} } , { bg => themed('bg_middle') } ,
); );
@mr = gen_transition( $THEME{middle_suffix} , $mc , $mc ); @mr = gen_transition( themed('middle_suffix') , $mc , $mc );
foreach my $entry ( @middle ) { foreach my $entry ( @middle ) {
delete $entry->{bg}; delete $entry->{bg};
} }
@ -371,7 +394,7 @@ sub gen_top_line
my @mpad = (); my @mpad = ();
if ( $len < $COLUMNS ) { if ( $len < $COLUMNS ) {
push @mpad , { push @mpad , {
content => [ $THEME{padding} x ( $COLUMNS - $len ) ] content => [ themed('padding') x ( $COLUMNS - $len ) ]
}; };
} }
@ -397,11 +420,11 @@ sub gen_input_line
sub gen_ps2 sub gen_ps2
{ {
my $ill = shift; my $ill = shift;
my @line = gen_transition( $THEME{ps2_suffix} , $THEME{bg_ps2} , 0 ); my @line = gen_transition( themed('ps2_suffix') , themed('bg_ps2') , 0 );
my $len = get_length( @line ); my $len = get_length( @line );
if ( $len < $ill ) { if ( $len < $ill ) {
unshift @line , { unshift @line , {
bg => $THEME{bg_ps2} , bg => themed('bg_ps2') ,
content => [ ' ' x ( $ill - $len ) ] content => [ ' ' x ( $ill - $len ) ]
}; };
} }
@ -423,15 +446,15 @@ sub render_datetime
my @cur_time = localtime time; my @cur_time = localtime time;
my @out = ( ); my @out = ( );
if ( $CONFIG{dt_show_date} ) { if ( $CONFIG{dt_show_date} ) {
push @out , {fg=>$THEME{dt_date_fg}}; push @out , {fg=>themed 'dt_date_fg'};
push @out , ( strftime $CONFIG{dt_date_fmt}, @cur_time ) push @out , ( strftime $CONFIG{dt_date_fmt}, @cur_time )
} }
if ( $CONFIG{dt_show_time} ) { if ( $CONFIG{dt_show_time} ) {
push @out, ' ' if @out; push @out, ' ' if @out;
push @out , {fg=>$THEME{dt_time_fg}}; push @out , {fg=>themed 'dt_time_fg'};
push @out , ( strftime $CONFIG{dt_time_fmt}, @cur_time ) push @out , ( strftime $CONFIG{dt_time_fmt}, @cur_time )
} }
return { bg => $THEME{dt_bg} , content => [@out] }; return { bg => themed 'dt_bg' , content => [@out] };
} }
sub render_cwd sub render_cwd
@ -447,12 +470,13 @@ sub render_cwd
my $offset = length( $dir ) - $max_len; my $offset = length( $dir ) - $max_len;
if ( $offset > 0 ) { if ( $offset > 0 ) {
$dir = substr $dir , $offset , $max_len; $dir = substr $dir , $offset , $max_len;
$dir =~ s!^[^/]*/!$THEME{cwd_trunc}/!; my $t = themed 'cwd_trunc';
$dir =~ s!^[^/]*/!$t/!;
} }
return { return {
bg => $THEME{cwd_bg_color} , bg => themed 'cwd_bg_color' ,
content => [ {fg=>$THEME{cwd_fg_color}} , $dir ] content => [ {fg=>themed 'cwd_fg_color'} , $dir ]
}; };
} }
@ -483,14 +507,14 @@ sub render_userhost
$str .= hostname; $str .= hostname;
} }
if ( $rm && $is_remote ) { if ( $rm && $is_remote ) {
$str .= $THEME{uh_remote_symbol}; $str .= themed 'uh_remote_symbol';
} }
return () unless $str; return () unless $str;
return { return {
bg => $THEME{ ( $> == 0 ) ? 'uh_root_bg' : 'uh_user_bg' } , bg => themed( ( $> == 0 ) ? 'uh_root_bg' : 'uh_user_bg' ) ,
content => [ content => [
{ fg => $THEME{ ( $> == 0 ) ? 'uh_root_fg' : 'uh_user_fg' } } , { fg => themed( ( $> == 0 ) ? 'uh_root_fg' : 'uh_user_fg' ) } ,
$str $str
] , ] ,
}; };
@ -505,20 +529,19 @@ sub render_prevcmd
$sc = ( $sc == 1 || ( $sc == 2 && $status ) ); $sc = ( $sc == 1 || ( $sc == 2 && $status ) );
return () unless $sc || $ss; return () unless $sc || $ss;
my $col = ( $status == 0 ) ? $THEME{pcmd_ok_fg} : $THEME{pcmd_err_fg}; my $col = themed( ( $status == 0 ) ? 'pcmd_ok_fg' : 'pcmd_err_fg' );
my @out = (); my @out = ();
if ( $ss ) { if ( $ss ) {
push @out , { fg => ( $cl & 1 != 0 ) ? $col : -1 }; push @out , { fg => ( $cl & 1 != 0 ) ? $col : -1 };
push @out , ( ( $status == 0 ) push @out , themed( ( $status == 0 ) ? 'pcmd_ok_sym' : 'pcmd_err_sym' );
? $THEME{pcmd_ok_sym}
: $THEME{pcmd_err_sym} );
} }
if ( $sc ) { if ( $sc ) {
push @out , ' ' if @out; push @out , ' ' if @out;
push @out , { fg => ( $cl & 2 != 0 ) ? $col : -1 }; push @out , { fg => ( $cl & 2 != 0 ) ? $col : -1 };
if ( $pc ) { if ( $pc ) {
my $str = sprintf '%' . ( $pc * 3 ) . 's' , $status; my $str = sprintf '%' . ( $pc * 3 ) . 's' , $status;
$str =~ s/ /$THEME{padding}/g; my $pad = themed 'padding';
$str =~ s/ /$pad/g;
push @out , $str; push @out , $str;
} else { } else {
push @out , $status; push @out , $status;
@ -526,7 +549,7 @@ sub render_prevcmd
} }
return { return {
bg => ( $status == 0 ) ? $THEME{pcmd_ok_bg} : $THEME{pcmd_err_bg} , bg => themed( ( $status == 0 ) ? 'pcmd_ok_bg' : 'pcmd_err_bg' ) ,
content => [ @out ] , content => [ @out ] ,
}; };
} }
@ -560,9 +583,9 @@ sub render_load
$cat = 'high'; $cat = 'high';
} }
$load = $THEME{load_title} . $load . '%'; $load = (themed 'load_title') . $load . '%';
return { return {
bg => $THEME{ 'load_' . $cat . '_bg' } , bg => themed( 'load_' . $cat . '_bg' ) ,
content => [ {fg=>$THEME{ 'load_' . $cat . '_fg' }}, $load ] content => [ {fg=>themed( 'load_' . $cat . '_fg' )}, $load ]
}; };
} }