Home

Awesome

Trueline: Bash Powerline Style Prompt with True Color Support

<!--toc:start--> <!--toc:end-->

Trueline is a fast and extensible Powerline style bash prompt with true color (24-bit) and fancy glyph support.

The pure Bash code implementation and overall features are modelled after the excellent Pureline command prompt. However Trueline also adds the ability to use RGB color codes, expands icon/glyph usage across prompt segments (inspired by Powerlevel9k), simplifies configuration and, among other goodies, shows the current input mode (when in vi-mode).

main_image

Installation

Download the trueline.sh script in this repo and source it from within your .bashrc file:

$> git clone https://github.com/petobens/trueline ~/trueline
$> echo 'source ~/trueline/trueline.sh' >> ~/.bashrc

or alternatively

$> wget https://raw.githubusercontent.com/petobens/trueline/master/trueline.sh -P ~/
$> echo 'source ~/trueline.sh' >> ~/.bashrc

If you use a font that supports "Powerline" glyphs, such as those included in the wonderful Nerd Fonts project, then the prompt should render properly and no further configuration is necessary (as long as you like the default settings shown in the image above).

Customization

Customizing and extending the prompt is easy and there are several segment options available to do so.

All settings go inside your .bashrc and must be defined before actually sourcing the trueline.sh file (otherwise default settings will be used). To see how this works let's start with a simple configuration example:

declare -A TRUELINE_COLORS=(
    [light_blue]='75;161;207'
    [grey]='99;99;100'
    [pink]='199;88;157'
)

declare -a TRUELINE_SEGMENTS=(
    'working_dir,light_blue,black,normal'
    'git,grey,black,normal'
    'time,white,black,normal'
    'newline,pink,black,bold'
)

declare -A TRUELINE_SYMBOLS=(
    [git_modified]='*'
    [git_github]=''
    [segment_separator]=''
    [working_dir_folder]='...'
    [working_dir_separator]='/'
    [working_dir_home]='~'
    [newline]='❯'
    [clock]='🕒'
)

TRUELINE_GIT_SHOW_STATUS_NUMBERS=false
TRUELINE_GIT_MODIFIED_COLOR='grey'
TRUELINE_WORKING_DIR_SPACE_BETWEEN_PATH_SEPARATOR=false

_trueline_time_segment() {
    local prompt_time="${TRUELINE_SYMBOLS[clock]} \t"
    if [[ -n "$prompt_time" ]]; then
        local fg_color="$1"
        local bg_color="$2"
        local font_style="$3"
        local segment="$(_trueline_separator)"
        segment+="$(_trueline_content "$fg_color" "$bg_color" "$font_style" " $prompt_time ")"
        PS1+="$segment"
        _trueline_record_colors "$fg_color" "$bg_color" "$font_style"
    fi
}

source ~/trueline/trueline.sh

which generates the following prompt (that essentially replicates the minimal ZSH Pure prompt):

pure_image

You can see in the config above that there are basically 5 different/relevant settings: colors, segments, symbols, options and extensions. Let's break each of these down.

Colors

Colors are defined by means of an associative array named TRUELINE_COLORS. The keys of this array are color names and the values RGB color codes:

declare -A TRUELINE_COLORS=(
    [color_name]='red;green;blue'
)

Default colors are loosely based on Atom's One Dark theme and given by:

declare -A TRUELINE_COLORS=(
    [black]='36;39;46'
    [cursor_grey]='40;44;52'
    [green]='152;195;121'
    [grey]='171;178;191'
    [light_blue]='97;175;239'
    [mono]='130;137;151'
    [orange]='209;154;102'
    [purple]='198;120;221'
    [red]='224;108;117'
    [special_grey]='59;64;72'
    [white]='208;208;208'
)

Any TRUELINE_COLORS array defined in the bashrc file prior to sourcing the Trueline script will actually update the default array above (in the sense that it will overwrite existing keys and add non-existing ones). This basically means that default colors can always be used and the array only needs to be defined when new extra colors are truly needed.

Note: you can define any color name you want except for default_bg which is used by Trueline to obtain the default terminal background color.

Segments

Prompt segments are defined in an ordered array called TRUELINE_SEGMENTS that has the following structure:

declare -a TRUELINE_SEGMENTS=(
    'segment_name,segment_fg_color,segment_bg_color,font_style'
)

where the segment foreground and background color names are keys of the TRUELINE_COLORS array and the font style is either bold, dim, italic, normal or underlined. The order of the elements in the array define the order in which each segment is rendered in the prompt.

Trueline offers the following segments (status indicates whether they are enabled/rendered by default):

Segment NameStatusDescription
aws_profileenabledcurrent AWS profile
bg_jobsenablednumber of background jobs
cmd_durationdisabledlast command execution time
conda_envenabledcurrent anaconda environment
exit_statusenabledreturn code of last command
gitenabledgit branch/remote and repository status
newlinedisabledsplits prompt segments across multiple lines
read_onlyenabledindicator of read only directory
userenabledusername and host (conditional on ssh status)
venvenabledPython virtual environment
working_direnabledcurrent working directory
distro_icondisabledcurrent Linux distribution icon

but more segments can be easily added (see Extensions).

To enable the newline segment one could use the following config:

declare -a TRUELINE_SEGMENTS=(
    'working_dir,mono,cursor_grey,normal'
    'git,grey,special_grey,normal'
    'newline,black,orange,bold'
)

which results in: newline_image

Symbols

Symbols (i.e icons/glyphs) are defined through an associative array named TRUELINE_SYMBOLS where each entry key is a (predefined) segment symbol name and the value is the actual symbol/icon:

declare -A TRUELINE_SYMBOLS=(
    [segment_symbol_name]='|' # actual symbol
)

The following table shows the current predefined symbol names along with their default values (i.e either the actual glyph or the corresponding nerd-font unicode code):

Symbol NameGlyphSymbol NameGlyph
aws_profileU+f52cnewline_rootU+f52c
bg_jobsU+f085ps2...
distro_iconU+ebc6read_onlyU+f023
exit_statusU+ea87segment_separatorU+e0b0
git_aheadU+f55csshU+f817
git_behindU+f544timerU+fa1e
git_bitbucketU+f171venv (and conda)U+e73c
git_branchU+e0a0vimode_cmdN
git_githubU+f408vimode_insI
git_gitlabU+f296working_dir_folderU+e5fe
git_modifiedU+f44dworking_dir_homeU+f015
newlineU+f155working_dir_separatorU+e0b1

As with TRUELINE_COLORS, any TRUELINE_SYMBOLS array defined in the bashrc file prior to sourcing the Trueline script will actually update the array with the default symbols shown above (thus such array needs to be defined only when overriding some icon or adding new ones).

Options

Most Trueline settings are controlled with the 3 structures defined above. However Trueline also defines a series of variables that control some extra options. In particular we can distinguish between intra-segment and external options. These, along with their default values, are defined as follows:

Intra-segment

The next segments have (sub)settings of their own:

External

Extensions

New segments can be easily added to the prompt by following this template:

_trueline_new_segment_name_segment() {
    local some_content=$(...)
    if [[ -n "$some_content" ]]; then
        local fg_color="$1"
        local bg_color="$2"
        local font_style="$3"
        local segment="$(_trueline_separator)"
        segment+="$(_trueline_content "$fg_color" "$bg_color" "$font_style" " $some_content ")"
        PS1+="$segment"
        _trueline_record_colors "$fg_color" "$bg_color" "$font_style"
    fi
}

and then simply including the new_segment_name in your TRUELINE_SEGMENTS array.

PRs with new segments are welcome!