Skip to content

Commit

Permalink
Speedup execution and normalize configuration (#77)
Browse files Browse the repository at this point in the history
This is a rewrite of how we handle environment variables and subshells. This means that old configuration will no longer work. To fix this backup and remove ${HOME}/.config/sbp and change the casing of 'sbp_path' to 'SBP_PATH' in your ${HOME}/.bashrc. These changes were nessesary to make the code readable when moving away from using subshells.
  • Loading branch information
brujoand authored Jul 7, 2020
1 parent aeafc22 commit 3c9b999
Show file tree
Hide file tree
Showing 138 changed files with 5,007 additions and 1,631 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/main.yml

This file was deleted.

20 changes: 13 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
language: bash
dist: trusty
sudo: required
dist: bionic
email: false
addons:
apt:
packages:
- bc
script: ./test
before_install:
- sudo apt-get install npm -y
- sudo npm install -g bats
script: ./bin/run_tests && ./bin/package
deploy:
provider: releases
api_key:
secure: c5h0E4QkBWe7Bka6mkowP/z6QEiqwKGV5/E9MhX9COzxhtQGlDa7rYJ7AlS43o9sXOqJR7P8Hq5KJAXqYoOamKpXB7M+1IGlDjeVB8uHBKZ/53Iwj6ZUrnXvmIrbY3X6Cr2cK2JVkmKeHx4MhT3HZKCv+MGbA3EfcIghEk/KSi8GcP84oqMLLvOhXStvVgOh2n80w4KUlqXct7DWBTc8kZAhlp2jFXBXOTdk7ihwGzS98qvHyFEO+asW63PwqpCAj74Qmj9KECIStTWGr/ieArpIuoWMVB3yvmmmZkAcm0hckwdgnwcwS47MWo3PDoUO/QuAy0CoVdWGmkz+NekudXrN7xTuTgP0afA5VF0hX7DtDsu18BoiQonauAfImNz5dYVonqMXqHSVZXcwCuyAs/zfbpFVjrngUnPrZN+rD/NwdAB3EbTIFHdQz3dqsrFhKbeHzg2dQv6NNUn9FY/hD7kol2xODONLk0A8tnZwEVRLEKSA/aBPcjh1gq63im4DpONUpf2EjtHSWVJ/kaqiKgc+kQGeF/KdH36O57qP5ttiZuYmjftiNS2n6vlu2ODgQiBrwVlbTXOdR2+lYLrQKF26L7IvslLX7PJUG9S79OjSxob/+NyuFgKYbD0SUT7nN6enwS1UVi3vO+LOJkWmq3fFULjy3stbcreH9L48iTs=
file: sbp-v${TRAVIS_TAG}.tar.gz
on:
tags: true
skip_cleanup: 'true'
26 changes: 23 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
FROM ubuntu

RUN apt-get update && apt-get install -y git bash curl
ARG DEBIAN_FRONTEND=noninteractive

ADD . /sbp

ENV USER root
RUN apt-get update && \
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
apt-get install -y git bash curl apt-utils dialog

RUN adduser --system --shell /bin/bash --disabled-password sbp && \
apt-get install -y locales && \
locale-gen en_US.UTF-8 && \
dpkg-reconfigure locales && \
update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8

copy . /sbp

RUN chown -R sbp /sbp

USER sbp

ENV USER sbp
ENV LC_ALL en_US.UTF-8

WORKDIR /home/sbp

RUN touch .bashrc && /sbp/bin/install
150 changes: 65 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,126 +17,106 @@ here](https://asciinema.org/a/0efgJrqQJY2vH1XguXjX3xV1c)
I wanted to add support for trueculors instead of relying on "just" 256 colors.
To do this I had to break the configuration, and when the flood gates had been
opened, a lot of things started changing. Suddenly layout changes was pretty
easy too.
easy too. After a while speed became and issue, and I had to reduce the number
of subshells. This led to return by reference which is now the standard way of
returning results in the code base. So if you're upgrading from an older version
it's probably a good idea to make a copy of your sbp config and start fresh.

## Soft Requirements
If you want the fancy pointy segment separators, you need the powerline fonts _installed_ and _enabled_. Both.
You can get them [here](https://github.com/powerline/fonts).
Then run the install.sh script. Now the hard_to_remember part. Change the settings of your terminal emulator.
You can get them [here](https://github.com/powerline/fonts) which also has
installation instructions
Now the hard_to_remember part. Change the settings of your terminal emulator.
Something like "Settings" and then "Fonts" will probably be the right place.
If you don't like powerline then use the 'plain' theme or create your own.
If you don't like powerline then use the 'plain' or 'lines' theme or create your
own. If you are using Kitty as a terminal then everything should work out of the
box.

## Installing
There is an install script. It will copy the default
settings to `~/.config/sbp/settings.conf` along with the default color assignments
to `~/.config/sbp/colors.conf` and add two lines to your `$HOME/.bashrc`:

### With brew:
`brew install brujoand/sbp/sbp`
This will get you the latest release.

### With git and the install script
When you clone this repo, there is an install script located at ´bin/install´.
It will add two lines to `$HOME/.bashrc`:
```
sbp_path=/the/path/to/sbp/
source ${sbp_path}/sbp.bash
SBP_PATH=/the/path/to/sbp/
source ${SBP_PATH}/sbp.bash
```
You could also just add these two lines to some bash config file of your own
choosing manually. Keep in mind that this approach will use the master branch
by default, so expect less stability.

## Usage
So you're ready to go. Now you do nothing. Just use it. But you could. If you want. Change stuff up a bit.
Edit your config by running `sbp config` and run `sbp reload` if you changed
Edit your config by running `sbp edit config` and run `sbp reload` if you changed
something substantial. Most changes will be effective immediately.
You can use the `sbp` command for a lot of things:
```
sbp
Usage: sbp [command]
Commands:
segments - List all available segments
hooks - List all available hooks
peekaboo - Toggle visibility of [segment] or [hook]
color - Set [color] for the current session
layout - Set [layout] for the current session
themes - List all available color themes and layouts
reload - Reload SBP and user settings
debug - Toggle debug mode
status - Show the current configuration
config - Opens the config in $EDITOR
reload - Reload SBP and user settings
status - Show the current configuration
help - Show this help text
list
segments - List all available segments
hooks - List all available hooks
themes - List all available color themes and layouts
edit
config - Opens the sbp config in \$EDITOR (${EDITOR:-'not set'})
colors - Opens the colors config in \$EDITOR (${EDITOR:-'not set'})
set
color - Set [color] for the current session
layout - Set [layout] for the current session
toggle
peekaboo - Toggle execution of [segment] or [hook]
debug - Toggle debug mode
sbp
```

## Features
### Segments
Segments can be configured, moved, and hidden depending on your mood, or
environment. Read more about those and how to make your own in the [Segments
Folder](/segments).
Folder](/src/segments).

### Hooks
Hooks let's you execute scripts asynchronously to either alert you, populate
data for segments or whatever you want really. Read more about those and how to
make your own in the [Hooks Folder](/hooks).
Hooks let's you execute scripts asynchronously to either alert you, or prepare
data in some way. Whatever you want really. Read more about those and how to
make your own in the [Hooks Folder](/src/hooks).

### Themes
Themes let you decide how the prompt is drawn, both in terms of layout and the
colors used. Read more about those and how to make your own in the [Themes
Folder](/themes). SBP supports both truecolors through RGB values and 256 colors
by using ansi codes.
### Colors and Layouts
Colors and layouts let you decide how the prompt is drawn. Read more about those
and how to make your own in the [Colors](/src/colors) and
[Layouts](/src/layouts). SBP supports both truecolors through RGB values and 256 colors
by using ansi codes. Many will probably just want to rely on the configuration
set in Xresources, by using the xresources color setting.

#### Beta - VI mode
The setting `settings_prompt_ready_vi_mode=1` will use the `prompt_ready` icon
~~The setting `settings_prompt_ready_vi_mode=1` will use the `prompt_ready` icon
with the configured colors and change it's color depending on the current VI
mode if enabled. The cursor will also change from blinking to solid block if
your terminal supports it.


### Speed
I've been comparing the speed of SBP to fancy prompts like
[powerline9k](https://github.com/Powerlevel9k/powerlevel9k), but there has been
an improvement recently in the form of
[powerlevel10k](https://github.com/romkatv/powerlevel10k). The latter version
feels a lot snappier and does deliver on speed. However in my benchmarks SBP is
still faster. I ran 50 invocation of the prompt in succession, with the
following enabled for all prompts: host, path, git, time, last command time and
last command exit status. I measured the display time on my trusted old mac mini
running Ubuntu using the following function:

```
precmd() {
export second=$(date +'%s%3N')
if [[ -n "$first" ]]; then
echo $(( first - second ))ms >> ${HOME}/${ZSH_THEME##*/}.stats
fi
export first=$(date +'%s%3N')
}
```
Since bash doesn't support the `precmd` function I used `$PROMPT_COMMAND` instead, and by
taking the average of each stats file and ignoring the first unrealistic values
I ended up with pretty similar results.

| prompt | Ubuntu mac mini| Raspbian | MacOS Catalina|
|--------------|----------------|------------------|---------------|
| SBP | 48ms | 254ms | 105ms |
| Powerlevel10k| 81ms | 87ms | 77ms |
| Powerlevel9k | 179ms | 554ms | 180ms |

I'm not quite sure why these numbers differ so much from the ones in the
Powerlevel10k readme, but I suspect it might be because the benchmarking script
used doesn't actually measure rendering but rather generation time. Another
point of course is the different hardware used, which will affect the time spent
but shouldn't affect the relation between the times much. There will also be a
ton of other things that will slow each prompt down differently depending on
implementation. SBP for instance caches data to files which can be slower on
some systems like the rpi. Also feel free to educate me if there are better ways of measuring
the speed difference here. On a related note though, on thing that tends to slow
these prompts down are large git repositories. Powerlevel10k has a special
binary for this and I expect it will perform much better in these cases.
But in the end, even if SBP is faster on some setups, the consistency in speed
for Powerlevel10k is mind blowing. I'll have to dig into how they've done that.
your terminal supports it.~~
The VI mode support has been removed as it is not possible to predictably place
the VI mode indicator on a multiline prompt. PR's are very welcome if you find a
way to do this.

### FAQ

#### Is this really just bash?
Yes, but actually no. At the time of writing there are 4 sed
invocations in addition to a few date calls. I want to get rid of awk and sed,
but I haven't been able to yet.
Yes, but actually no. At the time of writing the main implementation has
just a few calls do date, while some segments touch grep and sed but these
are being removed. Sometimes we need to talk to other CLI applications though like
git.

#### My prompt doesn't show any colors, whats wrong?
You are using a terminal that doesn't support truecolors. You can write your own
ansi theme, or use one of the two provided ones, default-256 & xresources. The
latter only uses the 16 base colors.
You are using a terminal that doesn't support truecolors, the OSX Terminal.app maybe?
You can write your own ansi theme, or use one of the two provided ones, default-256 or xresources.

#### I don't want to install any fancy fonts, can I still have nice things?
Why yes! Simply use the 'plain' layout. No fonts needed.
Why yes! Simply use the 'plain' layout. No fonts needed. Or use the
[Kitty](https://sw.kovidgoyal.net/kitty/) terminal which will draw most of the
missing characters for you.
8 changes: 4 additions & 4 deletions install → bin/install
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#! /usr/bin/env bash

base_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
base_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
bashrc_path="$HOME"/.bashrc

if ! grep -qE '^sbp_path=*' "$bashrc_path"; then
if ! grep -qE '^SBP_PATH=*' "$bashrc_path"; then
echo "SBP: adding sbp_path=${base_path} to ~/.bashrc"
echo "sbp_path=${base_path}" >> "$bashrc_path"
echo "SBP_PATH=${base_path}" >> "$bashrc_path"
else
# shellcheck disable=SC2154
echo "SBP: sbp_path has already been set to '$sbp_path'"
echo "SBP: sbp_path has already been set to '$SBP_PATH'"
fi

if ! grep -qE "^(source|.) ${base_path}" "$bashrc_path"; then
Expand Down
7 changes: 7 additions & 0 deletions bin/package
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /usr/bin/env bash

base_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )

cd "$base_path"

tar -zcvf "sbp-v${TRAVIS_TAG}.tar.gz" sbp.bash src config README.md LICENSE
10 changes: 10 additions & 0 deletions bin/run_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /usr/bin/env bash

set -e

SBP_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
export SBP_PATH

shellcheck -x --severity=error sbp.bash src/main.bash

bats -r test/
8 changes: 8 additions & 0 deletions bin/try_me
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /usr/bin/env bash

set -e

base_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )

sudo docker build -t brujoand/sbp:local "$base_path"
sudo docker run -it brujoand/sbp:local bash
69 changes: 35 additions & 34 deletions config/colors.conf
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
#!/usr/bin/env bash

# Color configuration for the segments.
# Check the actualy colors ofr the current theme
# Using 'sbp colors'
# See src/colors/*.bash for the color values

settings_command_color_primary=$color04
settings_command_color_secondary=$color01
settings_command_color_primary_error=$color08
settings_command_color_secondary_error=$color04
SEGMENTS_COMMAND_COLOR_PRIMARY=$color4
SEGMENTS_COMMAND_COLOR_SECONDARY=$color1
SEGMENTS_COMMAND_COLOR_PRIMARY_HIGHLIGHT=$color8
SEGMENTS_COMMAND_COLOR_SECONDARY_HIGHLIGHT=$color4

settings_git_color_primary=$color0A
settings_git_color_secondary=$color01
SEGMENTS_GIT_COLOR_PRIMARY=$color10
SEGMENTS_GIT_COLOR_SECONDARY=$color1

settings_host_color_primary=$color02
settings_host_color_secondary=$color05
settings_host_root_color_primary=$color09
settings_host_root_color_secondary=$color00
SEGMENTS_HOST_COLOR_PRIMARY=$color2
SEGMENTS_HOST_COLOR_SECONDARY=$color5
SEGMENTS_HOST_COLOR_PRIMARY_HIGHLIGHT=$color9
SEGMENTS_HOST_COLOR_SECONDARY_HIGHLIGHT=$color0

settings_path_color_primary=$color0E
settings_path_color_secondary=$color07
settings_path_splitter_color=$color04
SEGMENTS_PATH_COLOR_PRIMARY=$color14
SEGMENTS_PATH_COLOR_SECONDARY=$color7
SEGMENTS_PATH_SPLITTER_COLOR=$color4

settings_path_readonly_color_secondary=$color0F
settings_path_readonly_color_primary=$color1
SEGMENTS_PATH_READONLY_COLOR_SECONDARY=$color15
SEGMENTS_PATH_READONLY_COLOR_PRIMARY=$color1

settings_prompt_ready_color_primary=''
settings_prompt_ready_color_secondary=$color04
settings_prompt_ready_vi_insert_color=$color04
settings_prompt_ready_vi_command_color=$color0E
SEGMENTS_PROMPT_READY_COLOR_PRIMARY=''
SEGMENTS_PROMPT_READY_COLOR_SECONDARY=$color4

settings_python_virtual_env_color_primary=$color09
settings_python_virtual_env_color_secondary=$color0F
SEGMENTS_PYTHON_ENV_COLOR_PRIMARY=$color9
SEGMENTS_PYTHON_ENV_COLOR_SECONDARY=$color15

settings_return_code_color_primary=$color1
settings_return_code_color_secondary=$color0F
SEGMENTS_RETURN_CODE_COLOR_PRIMARY=$color1
SEGMENTS_RETURN_CODE_COLOR_SECONDARY=$color15

settings_timestamp_color_primary=$color02
settings_timestamp_color_secondary=$color05
SEGMENTS_TIMESTAMP_COLOR_PRIMARY=$color2
SEGMENTS_TIMESTAMP_COLOR_SECONDARY=$color5

settings_aws_color_primary=$color08
settings_aws_color_secondary=$color09
SEGMENTS_AWS_COLOR_PRIMARY=$color8
SEGMENTS_AWS_COLOR_SECONDARY=$color9

settings_openshift_color_primary=$color03
settings_openshift_color_secondary=$color07
SEGMENTS_K8S_COLOR_PRIMARY=$color3
SEGMENTS_K8S_COLOR_SECONDARY=$color7

settings_rescuetime_color_primary=$color09
settings_rescuetime_color_secondary=$color0F
settings_rescuetime_splitter_color=$color07
SEGMENTS_RESCUETIME_COLOR_PRIMARY=$color11
SEGMENTS_RESCUETIME_COLOR_SECONDARY=$color4
SEGMENTS_RESCUETIME_SPLITTER_COLOR=$color7

SEGMENTS_WTTR_COLOR_PRIMARY=$color11
SEGMENTS_WTTR_COLOR_SECONDARY=$color4
SEGMENTS_WTTR_SPLITTER_COLOR=$color7
Loading

0 comments on commit 3c9b999

Please sign in to comment.