Support for styles and using default terminal colors
This commit is contained in:
parent
fcb8e33ed1
commit
79cc3bd560
1 changed files with 65 additions and 14 deletions
79
gprompt.pl
79
gprompt.pl
|
@ -111,31 +111,31 @@ sub default_theme
|
||||||
# Extra colors for transition strings
|
# Extra colors for transition strings
|
||||||
'transition' => [ 7 ] ,
|
'transition' => [ 7 ] ,
|
||||||
# Left side of top line
|
# Left side of top line
|
||||||
'bg_left' => 0,
|
'bg_left' => -2,
|
||||||
'fg_left' => 7,
|
'fg_left' => -2,
|
||||||
'left_prefix' => '' ,
|
'left_prefix' => '' ,
|
||||||
'left_separator' => ' ' ,
|
'left_separator' => ' ' ,
|
||||||
'left_suffix' => '\f2 | ' ,
|
'left_suffix' => '\f2 | ' ,
|
||||||
# Middle of top line
|
# Middle of top line
|
||||||
'bg_middle' => 0,
|
'bg_middle' => -2,
|
||||||
'fg_middle' => 7,
|
'fg_middle' => -2,
|
||||||
'middle_prefix' => ' ',
|
'middle_prefix' => ' ',
|
||||||
'middle_separator' => ' ',
|
'middle_separator' => ' ',
|
||||||
'middle_suffix' => ' ',
|
'middle_suffix' => ' ',
|
||||||
# Right side of top line
|
# Right side of top line
|
||||||
'bg_right' => 0,
|
'bg_right' => -2,
|
||||||
'fg_right' => 7,
|
'fg_right' => -2,
|
||||||
'right_prefix' => '\f2 | ' ,
|
'right_prefix' => '\f2 | ' ,
|
||||||
'right_separator' => ' ' ,
|
'right_separator' => ' ' ,
|
||||||
'right_suffix' => '' ,
|
'right_suffix' => '' ,
|
||||||
# Input line
|
# Input line
|
||||||
'bg_input' => 0,
|
'bg_input' => -2,
|
||||||
'fg_input' => 7,
|
'fg_input' => -2,
|
||||||
'input_prefix' => '' ,
|
'input_prefix' => '' ,
|
||||||
'input_separator' => '\f2:' ,
|
'input_separator' => '\f2:' ,
|
||||||
'input_suffix' => '\f2 ; ' ,
|
'input_suffix' => '\f2 ; ' ,
|
||||||
# Secondary prompt
|
# Secondary prompt
|
||||||
'bg_ps2' => 0,
|
'bg_ps2' => -2,
|
||||||
ps2_suffix => ' : ' ,
|
ps2_suffix => ' : ' ,
|
||||||
|
|
||||||
# Current working directory - Truncation string
|
# Current working directory - Truncation string
|
||||||
|
@ -236,6 +236,14 @@ our %TCCACHE = ();
|
||||||
our %TLEN = ();
|
our %TLEN = ();
|
||||||
our %SCACHE = ();
|
our %SCACHE = ();
|
||||||
our %THEME = ();
|
our %THEME = ();
|
||||||
|
our %STYLES = (
|
||||||
|
none => '' ,
|
||||||
|
b => 'bold' ,
|
||||||
|
u => 'smul' ,
|
||||||
|
i => 'sitm' ,
|
||||||
|
bu => 'bold,smul' ,
|
||||||
|
iu => 'sitm,smul' ,
|
||||||
|
);
|
||||||
|
|
||||||
# Terminal commands ---------------------------------------------------------{{{
|
# Terminal commands ---------------------------------------------------------{{{
|
||||||
|
|
||||||
|
@ -260,6 +268,9 @@ sub set_color
|
||||||
|
|
||||||
sub thref($) { bless {r=>$_[0]}, 'ThemeRef'; }
|
sub thref($) { bless {r=>$_[0]}, 'ThemeRef'; }
|
||||||
|
|
||||||
|
sub TERM_DEFAULT() { -2 }
|
||||||
|
sub SECTION_DEFAULT() { -1 }
|
||||||
|
|
||||||
sub load_theme
|
sub load_theme
|
||||||
{
|
{
|
||||||
my $theme = $CONFIG{layout_theme};
|
my $theme = $CONFIG{layout_theme};
|
||||||
|
@ -462,6 +473,20 @@ sub add_transitions
|
||||||
return @out;
|
return @out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub apply_style
|
||||||
|
{
|
||||||
|
my ( $bg , $fg , $style ) = @_;
|
||||||
|
my $out = tput_sequence( 'sgr0' );
|
||||||
|
$out .= set_color( 'b' , $bg ) unless $bg == -2;
|
||||||
|
$out .= set_color( 'f' , $fg ) unless $fg == -2;
|
||||||
|
if ( $style ne 'none' ) {
|
||||||
|
foreach my $ctrl ( split /,/, $STYLES{ $style } ) {
|
||||||
|
$out .= tput_sequence( $ctrl );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
sub render
|
sub render
|
||||||
{
|
{
|
||||||
my $name = shift;
|
my $name = shift;
|
||||||
|
@ -469,27 +494,53 @@ sub render
|
||||||
my $out = '';
|
my $out = '';
|
||||||
my $mustSetFg = undef;
|
my $mustSetFg = undef;
|
||||||
my $mustSetBg = undef;
|
my $mustSetBg = undef;
|
||||||
|
my $mustSetStyle = undef;
|
||||||
my $bgDefault = themed( 'bg_' . $name );
|
my $bgDefault = themed( 'bg_' . $name );
|
||||||
my $fgDefault = themed( 'fg_' . $name );
|
my $fgDefault = themed( 'fg_' . $name );
|
||||||
|
my ( $fg , $bg , $style ) = ( -2 , -2 , 'none' );
|
||||||
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} } ) {
|
||||||
if ( ref $part ) {
|
if ( ref $part ) {
|
||||||
$mustSetBg = $part->{bg} if exists $part->{bg};
|
$mustSetBg = $part->{bg} if exists $part->{bg};
|
||||||
$mustSetFg = $part->{fg} if exists $part->{fg};
|
$mustSetFg = $part->{fg} if exists $part->{fg};
|
||||||
|
$mustSetStyle = $part->{style} if exists $part->{style};
|
||||||
} else {
|
} else {
|
||||||
|
# Check background color changes
|
||||||
if ( defined $mustSetBg ) {
|
if ( defined $mustSetBg ) {
|
||||||
$mustSetBg = $bgDefault if $mustSetBg < 0;
|
$mustSetBg = $bgDefault if $mustSetBg == -1;
|
||||||
$out .= set_color( 'b' , $mustSetBg );
|
$mustSetBg = undef if $mustSetBg == $bg;
|
||||||
}
|
}
|
||||||
|
# Check foreground color changes
|
||||||
if ( defined $mustSetFg ) {
|
if ( defined $mustSetFg ) {
|
||||||
$mustSetFg = $fgDefault if $mustSetFg < 0;
|
$mustSetFg = $fgDefault if $mustSetFg == -1;
|
||||||
$out .= set_color( 'f' , $mustSetFg );
|
$mustSetFg = undef if $mustSetFg == $fg;
|
||||||
|
}
|
||||||
|
# Check style changes
|
||||||
|
if ( defined( $mustSetStyle ) && ( $mustSetStyle eq $style
|
||||||
|
|| !exists( $STYLES{ $style } ) ) ) {
|
||||||
|
$mustSetStyle = undef;
|
||||||
|
}
|
||||||
|
# Change style and colors if necessary
|
||||||
|
if ( defined( $mustSetBg ) || defined( $mustSetFg )
|
||||||
|
|| defined( $mustSetStyle ) ) {
|
||||||
|
if ( defined $mustSetBg ) {
|
||||||
|
$bg = $mustSetBg;
|
||||||
|
$mustSetBg = undef;
|
||||||
|
}
|
||||||
|
if ( defined $mustSetFg ) {
|
||||||
|
$fg = $mustSetFg;
|
||||||
|
$mustSetFg = undef;
|
||||||
|
}
|
||||||
|
if ( defined $mustSetStyle ) {
|
||||||
|
$style = $mustSetStyle;
|
||||||
|
$mustSetStyle = undef;
|
||||||
|
}
|
||||||
|
$out .= apply_style( $bg , $fg , $style );
|
||||||
}
|
}
|
||||||
$part =~ s/\\/\\\\/g;
|
$part =~ s/\\/\\\\/g;
|
||||||
$part =~ s/"/\\\"/g;
|
$part =~ s/"/\\\"/g;
|
||||||
$out .= $part;
|
$out .= $part;
|
||||||
$mustSetBg = $mustSetFg = undef;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue