1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-02 23:27:45 +00:00
nu_scripts/modules/prompt/async_git_prompt/async-git-prompt.nu
Mel Massadian c47ccd42b8
refactor: (#418)
* refactor:  move in one commit

Eveything in modules should probably be changed to `exported` defs.
The idea is to move everything first to keep proper history.

* refactor: 📝 add modules readme (wip)

* refactor:  small move

* refactor: 📝 changed nestring, updated modules readme

* refactor: 📝 to document or not to document

* fix: 🐛 themes

replaced the template to use `main` and regenerated them
from lemnos themes.

* Revert "fix: 🐛 themes"

This reverts commit 4918d3633c8d2d81950a0ed0cfd9eb84241bc886.

* refactor:  introduce sourced

- Created a source `root` in which sourcable demos are stored.
  Some might get converted to modules later on.
- Moved some files to bin too.

* fix: 🐛 fehbg.nu

* fix: 🐛 modules/after.nu

* moved some other stuff around

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-04-25 17:56:25 -05:00

77 lines
2.2 KiB
Text

# This module exports commands for creating a nushell prompt that computes git status (staged and
# unstaged changes) asynchronously. This can be useful in large git repos when it is slow to obtain
# this information synchronously.
# See README.md for usage.
def unstaged-symbol [] { 'અ' }
def staged-symbol [] { 'જ' }
def in-progress-symbol [] { '…' }
def cached-result-symbol [] { $"·" } #〈
def cache-file [] { '.nu-async-git-prompt-cache'}
def do-async [commands: string] {
bash -c $"nu -c '($commands)' &"
}
export def async-git-prompt-string [] {
let cache_path = (cache-path)
if ($cache_path | is-empty) {
""
} else if ($cache_path | path exists) {
$"(cached-result-symbol)(open $cache_path | str trim)"
} else {
async-git-prompt-compute-sync
}
}
export def async-git-prompt-compute-sync [] {
let unstaged = {
let symbol = if ((git diff --quiet | complete).exit_code == 1) {
(unstaged-symbol)
} else {
''
}
{ unstaged: $symbol}
}
let staged = {
let symbol = if ((git diff --cached --quiet | complete).exit_code == 1) {
(staged-symbol)
} else {
''
}
{ staged: $symbol}
}
# Execute the two slow git commands in parallel and merge the results into a single record
let symbols = ([ $unstaged $staged ] | par-each { |it| do $it } | reduce {|a b| $a | merge {$b}})
$"($symbols | get 'unstaged') ($symbols | get 'staged')" | str trim
}
export def async-git-prompt-refresh-cache [] {
let cache_path = (cache-path)
if ($cache_path != null) {
echo (in-progress-symbol) | save $cache_path
do-async $"use ($nu.config-path | path expand | path dirname)/async-git-prompt.nu *; async-git-prompt-compute-sync | save ($cache_path)"
}
}
export def async-git-prompt-delete-cache [] {
let cache_path = (cache-path)
if ($cache_path != null) {
rm -f $cache_path
}
}
def cache-path [] {
let dir = if ('.git' | path exists) {
'.'
} else {
do -i { git rev-parse --show-toplevel | str trim -r }
}
if ($dir | is-empty) {
null
} else {
$dir | path join (cache-file)
}
}