Skip to content

Commit

Permalink
Split code + remove cpu usage for zsh 5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
MickaelBlet committed Jun 4, 2024
1 parent c62fed5 commit 740a00c
Show file tree
Hide file tree
Showing 30 changed files with 1,135 additions and 681 deletions.
57 changes: 26 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Promptor

Customize your zsh prompt with powerline or nerd font.
Customize your zsh prompt with powerline or nerd font.
Full examples at [examples.md](docs/examples.md).

<p align="center">
<img src="./images/promptor.drawio.png" >
Expand Down Expand Up @@ -35,19 +36,21 @@ source "$HOME/.zshrc.d/promptor/promptor.zsh"
<img src="./images/colors.drawio.png" >
</p>

## Sections
## Section

Add section in **promptor_config::prompt** or **promptor_config::rprompt**.
The sections are separate by `[{unicode_gliph}]`
The sections are separate by `[<glyph>]`, the **Glyph** can be set by `name` or `unicode` character.

A **section** can be contain a [Function](#function) or 3 Arguments (`background`, `foreground`, `content`).
A **section** can be contain a `{{`[Function](#function)`}}` or 3 Arguments (`background`, `foreground`, `content`).

### Examples:

Full examples at [examples.md](docs/examples.md).

#### Change colors of prompt

```bash
promptor_config::prompt "[\ue0b6]248 232 %~ [\ue0b0] unwritten [\ue0b0] exit_code [\ue0b0]"
promptor_config::prompt '[left_half_circle_thick]248 231 %~ [left_hard_divider] {{unwritten}} [left_hard_divider] {{exit_code}} [left_hard_divider]'
```

<p align="center">
Expand All @@ -57,16 +60,16 @@ promptor_config::prompt "[\ue0b6]248 232 %~ [\ue0b0] unwritten [\ue0b0] exit_cod
#### Bullet prompt

```bash
promptor_config::prompt "[\ue0b6]237 231 %~[\ue0b4][\ue0b6]unwritten[\ue0b4][\ue0b6]exit_code[\ue0b4]"
promptor_config::prompt '[\ue0b6]237 231 %~[\ue0b4][\ue0b6]{{unwritten}}[\ue0b4][\ue0b6]{{exit_code}}[\ue0b4]'
```

<p align="center">
<img src="./images/example_bullet.drawio.png" >
</p>

## Function
Call functions in **promptor_config::prompt** and **promptor_config::rprompt** with `[{unicode_gliph}]{function_name}[{unicode_gliph}]`
The functions take a agrument "**prompt**" or "**rprompt**"
Call functions in **promptor_config::prompt** and **promptor_config::rprompt** with `{{<function_name>}}`
Function can be take an arguments (`{{<function_name> <arguments...>}}`).

### Git
With `git` or `git_async` functions you can show a color status.
Expand All @@ -78,16 +81,15 @@ You can change or disable information piority sequence with: `promptor_config::g
</p>

### Custion function
~/.zshrc.d/promptor/promptor_functions/example
~/.zshrc.d/promptor/functions/example
``` bash
# default configuration
promptor_config[example.bg]=231
promptor_config[example.fg]=232
promptor_config[example.value]="foo bar"
promptor_config[example.value]="foobar"

# must be prefix by promptor_function_
promptor_function_example() {
local prompt="$1"
# the function must be print at least 3 arguments
echo "${promptor_config[example.bg]}" # BACKGROUND
echo "${promptor_config[example.fg]}" # FOREGROUND
Expand All @@ -105,25 +107,21 @@ Create automaticaly the manage function configurations:
</p>

### Custion async function
~/.zshrc.d/promptor/promptor_functions/example_async
~/.zshrc.d/promptor/functions/example_async
``` bash
# default configuration
promptor_config[example.bg]=231
promptor_config[example.fg]=232
promptor_config[example.value]="foo bar"
promptor_config[example.value]="foobar"

# call by end of __my_example_slow_function (not required)
__my_example_callback() {
local prompt="$1"
local answer="$2"
promptor_reload_prompt_from_function \
example_async \
"$answer"
local answer="$1"
promptor_reload_prompt_from_function example_async "$answer"
}

# slow example function
__my_example_slow_function() {
local prompt="$1"
sleep 1
echo "${promptor_config[example.bg]}" # BACKGROUND
echo "${promptor_config[example.fg]}" # FOREGROUND
Expand All @@ -132,18 +130,13 @@ __my_example_slow_function() {

# must be prefix by promptor_worker_
promptor_worker_example_async() {
local prompt="$1"
promptor_launch_worker_job \
example_async \
"$prompt" \
__my_example_slow_function \
__my_example_callback
promptor_create_worker_callback example_async __my_example_callback
promptor_launch_worker_job example_async __my_example_slow_function "$@"
}

# execute before callback (not required)
# must be prefix by promptor_function_
promptor_function_example_async() {
local prompt="$1"
# the function must be print at least 3 arguments
echo "${promptor_config[example.bg]}" # BACKGROUND
echo "${promptor_config[example.fg]}" # FOREGROUND
Expand All @@ -153,16 +146,19 @@ promptor_function_example_async() {

## Configuration

You can edit the configuration file `~/.zshrc.d/promptor/promptor.conf`.
Use the `promptor_reload` function for take your change(s).

Default configuration:
``` bash
$ promptor_config_list
example.bg = "231"
example.fg = "232"
example.value = "foo bar"
example.value = "foobar"
exit_code.bg = "125"
exit_code.fg = "231"
git = "true"
git.async.wait.bg = "234"
git.async.wait.bg = "238"
git.async.wait.character = "\uf250" ()
git.async.wait.fg = "231"
git.character.added = "A"
Expand Down Expand Up @@ -209,10 +205,9 @@ git.powerline.character.tag = "\uf02b" ()
git.powerline.character.untracked = "U"
git.powerline.character.upstream.left = "\u2b63" (⭣)
git.powerline.character.upstream.right = "\u2b61" (⭡)
git.show.upstream = "verbose"
powerline = "true"
prompt = "[\ue0b6]237 231 %~ [\ue0b0] unwritten [\ue0b0] exit_code [\ue0b0]" ([]237 231 %~ [] unwritten [] exit_code [])
rprompt = "[\ue0b2] git_async [\ue0b2] 25 231 %n@%m [\ue0b2] 237 231 %D{%H:%M}[\ue0b4]" ([] git_async [] 25 231 %n@%m [] 237 231 %D{%H:%M}[])
prompt = "[left_half_circle_thick]237 231 %~ [left_hard_divider] {{unwritten}} [left_hard_divider] {{exit_code}} [left_hard_divider]"
rprompt = "[right_hard_divider] {{git_async}} [right_hard_divider] 25 231 %n@%m [right_hard_divider] 237 231 %D{%H:%M}[right_half_circle_thick]"
title = "%n: %~"
title.command.max_size = "100"
unwritten.bg = "124"
Expand Down
55 changes: 55 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Examples

## Git async on left

```bash
promptor_config::prompt '[left_half_circle_thick]237 231 %~ [left_hard_divider] {{unwritten}} [left_hard_divider] {{exit_code}} [left_hard_divider] {{git_async}} [left_hard_divider]'
promptor_config::rprompt '[right_hard_divider] 25 231 %n@%m [right_hard_divider] 237 231 %D{%H:%M}[right_half_circle_thick]'
```

<p align="center">
<img src="images/left_git.drawio.png" >
</p>

## Bullets

```bash
promptor_config::prompt '[left_half_circle_thick]237 231 %~[right_half_circle_thick][left_half_circle_thick]{{unwritten}}[right_half_circle_thick][left_half_circle_thick]{{exit_code}}[right_half_circle_thick]'
promptor_config::rprompt '[left_half_circle_thick]{{git_async}}[right_half_circle_thick][left_half_circle_thick]25 231 %n@%m[right_half_circle_thick][left_half_circle_thick]237 231 %D{%H:%M}[right_half_circle_thick]'
```

<p align="center">
<img src="images/bullets.drawio.png" >
</p>

## Multiline

```bash
promptor_config::prompt '[left_half_circle_thick]240 231 %D{%H:%M} [left_hard_divider] {{git_async}} [left_hard_divider] 237 231 %~[right_half_circle_thick][\n][left_half_circle_thick]25 231 %n@%m [left_hard_divider] {{unwritten}} [left_hard_divider] {{exit_code}} [left_hard_divider]'
promptor_config::rprompt ''
```

<p align="center">
<img src="images/multiline.drawio.png" >
</p>

## Multiline2

```bash
promptor_config::prompt '[left_half_circle_thick]39 232 %D{%H:%M:%S} [left_hard_divider] 204 232 %1d [left_hard_divider] {{unwritten}} [left_hard_divider] {{exit_code}} [left_hard_divider] {{git_async}}[right_half_circle_thick][\n]-1 2 \ue285'
promptor_config::rprompt ''
```

<p align="center">
<img src="images/multiline2.drawio.png" >
</p>

## Multiline3

```bash
promptor_config::prompt '[left_half_circle_thick]39 232 %D{%H:%M:%S} [left_hard_divider][left_hard_divider_inverse] 204 232 %1d [left_hard_divider] {{unwritten}} [left_hard_divider] {{exit_code}} [left_hard_divider][left_hard_divider_inverse] {{git_async}}[right_half_circle_thick][\n]-1 2 \ue285'
promptor_config::rprompt ''
```
<p align="center">
<img src="images/multiline3.drawio.png" >
</p>
Binary file added docs/images/bullets.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/left_git.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/multiline.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/multiline2.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/multiline3.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/colors.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/example_bullet.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/example_change_color.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/function.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/git.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/glyphs.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/promptor.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions promptor/functions/echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
promptor_function_echo() {
local bg="$1"
local fg="$2"
local content="$3"

echo "$bg" "$fg" "$content"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# default configuration
promptor_config[example.bg]=231
promptor_config[example.fg]=232
promptor_config[example.value]="foo bar"
promptor_config[example.value]="foobar"

# must be prefix by promptor_function_
promptor_function_example() {
local prompt="$1"
# the function must be print at least 3 arguments
echo "${promptor_config[example.bg]}" # BACKGROUND
echo "${promptor_config[example.fg]}" # FOREGROUND
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# default configuration
promptor_config[example.bg]=231
promptor_config[example.fg]=232
promptor_config[example.value]="foo bar"
promptor_config[example.value]="foobar"

# call by end of __my_example_slow_function (not required)
__my_example_callback() {
local prompt="$1"
local answer="$2"
local answer="$1"
promptor_reload_prompt_from_function \
example_async \
"$answer"
}

# slow example function
__my_example_slow_function() {
local prompt="$1"
sleep 1
echo "${promptor_config[example.bg]}" # BACKGROUND
echo "${promptor_config[example.fg]}" # FOREGROUND
Expand All @@ -23,18 +21,13 @@ __my_example_slow_function() {

# must be prefix by promptor_worker_
promptor_worker_example_async() {
local prompt="$1"
promptor_launch_worker_job \
example_async \
"$prompt" \
__my_example_slow_function \
__my_example_callback
promptor_create_worker_callback example_async __my_example_callback
promptor_launch_worker_job example_async __my_example_slow_function "$@"
}

# execute before callback (not required)
# must be prefix by promptor_function_
promptor_function_example_async() {
local prompt="$1"
# the function must be print at least 3 arguments
echo "${promptor_config[example.bg]}" # BACKGROUND
echo "${promptor_config[example.fg]}" # FOREGROUND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ promptor_config[exit_code.fg]=231
# must be prefix by promptor_function_
promptor_function_exit_code() {
if [ "$__promptor_last_exit_code" -ne 0 ]; then
echo "${promptor_config[exit_code.bg]}"
echo "${promptor_config[exit_code.fg]}"
echo "$__promptor_last_exit_code"
echo "${promptor_config[exit_code.bg]}" \
"${promptor_config[exit_code.fg]}" \
"$__promptor_last_exit_code"
fi
}
10 changes: 1 addition & 9 deletions promptor/promptor_functions/git → promptor/functions/git
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ promptor_config[git.information.sequence]="dirty added untracked stash upstream"
# - describe
# - *
promptor_config[git.describe.style]="tag"
# show upstream options list:
# - git | svn choose your repo type
# - verbose show number up or down stream
# - legacy use legacy command
# - name show the name of bramch or commit
promptor_config[git.show.upstream]="verbose"
promptor_config[git.character.dirty]="M"
promptor_config[git.character.added]="A"
promptor_config[git.character.untracked]="U"
Expand Down Expand Up @@ -78,8 +72,6 @@ promptor_function_git() {
return
fi

local prompt_side="$1"

__promptor_function_git_command() {
GIT_OPTIONAL_LOCKS=0 command git "$@"
}
Expand Down Expand Up @@ -293,7 +285,7 @@ promptor_function_git() {
local character_separator_prompt="${promptor_config[git.${powerline}character.separator.prompt]}"
local character_separator_rprompt="${promptor_config[git.${powerline}character.separator.rprompt]}"

if [ "$prompt_side" = "rprompt" ]; then
if [ "${__promptor_prompt:-}" = "rprompt" ]; then
character_separator_prompt="$character_separator_rprompt"
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@ __promptor_old_git_prompt=""

# must be prefix by promptor_worker_
promptor_worker_git_async() {
promptor_launch_worker_job \
git_async \
"$1" \
__promptor_function_git \
__promptor_worker_git_async_callback
promptor_create_worker_callback git_async __promptor_worker_git_async_callback
promptor_launch_worker_job git_async __promptor_function_git "$@"
}

# call by end of __promptor_function_git
__promptor_worker_git_async_callback() {
__promptor_old_git_prompt="$2"
promptor_reload_prompt_from_function git_async "$2"
__promptor_old_git_prompt="$1"
promptor_reload_prompt_from_function git_async "$1"
}

# must be prefix by promptor_function_
promptor_function_git_async() {
# remove colors
local old_git_prompt="${__promptor_old_git_prompt#* * }"
if [ -n "$old_git_prompt" ]; then
echo -en "${promptor_config[git.async.wait.bg]}" \
"${promptor_config[git.async.wait.fg]}" \
"${old_git_prompt:0:-1}${promptor_config[git.async.wait.character]}" 2> /dev/null
echo -e "${promptor_config[git.async.wait.bg]}" \
"${promptor_config[git.async.wait.fg]}" \
"${old_git_prompt:0:-1}${promptor_config[git.async.wait.character]}" 2> /dev/null
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ promptor_config[unwritten.fg]=231
promptor_function_unwritten() {
if [ ! -w "$(pwd)" ]; then
# font character
local character_lock='X'
local character_lock
if [ "${promptor_config[powerline]}" = true ]; then
character_lock=$'\ue0a2' &> /dev/null
else
character_lock='X'
fi
echo "${promptor_config[unwritten.bg]}"
echo "${promptor_config[unwritten.fg]}"
echo "$character_lock"
echo "${promptor_config[unwritten.bg]}" \
"${promptor_config[unwritten.fg]}" \
"$character_lock"
fi
}
Loading

0 comments on commit 740a00c

Please sign in to comment.