Skip to content

Commit

Permalink
add a "Git changes" segment to the prompt (#133)
Browse files Browse the repository at this point in the history
wait for
- #122 (to include a test and make sure the changes appear in the
prompt)

## changelog
- refactor the short Git status into `git/lib get-status`
- adds a segment containing Git changes

## details
- if there are no changes in the index, the prompt won't add anything.
- if there are changes, they will appear after the `(branch:commit)`
segment inside `[...]`
- in order
  - staged changes will show up as one `_`
  - unstaged changes will show up as one `!`
  - untracked changes will show up as one `?`
  • Loading branch information
amtoine authored Dec 13, 2023
1 parent b333280 commit 2dc21d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/nu-git-manager-sugar/git/lib/lib.nu
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@ export def git-action []: nothing -> string {
}
}

export def get-status [
repo: path, # the path to the repo
]: nothing -> record<staged: list<string>, unstaged: list<string>, untracked: list<string>> {
let status = ^git -C $repo status --short | lines
{
staged: ($status | parse --regex '^\w. (?<file>.*)' | get file),
unstaged: ($status | parse --regex '^.\w (?<file>.*)' | get file),
untracked: ($status | parse --regex '^\?\? (?<file>.*)' | get file),
}
}
22 changes: 21 additions & 1 deletion src/nu-git-manager-sugar/git/lib/prompt.nu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ../../git/lib/lib.nu [get-revision, git-action]
use ../../git/lib/lib.nu [get-revision, git-action, get-status]
use ../../git/lib/style.nu [color, simplify-path]

# /!\ the PWD will be sanitized
Expand Down Expand Up @@ -60,6 +60,25 @@ export def get-left-prompt [duration_threshold: duration]: nothing -> string {
null
}

let git_changes_segment = if $is_git_repo {
let status = get-status .

let markers = [
(if not ($status.staged | is-empty) { "_" }),
(if not ($status.unstaged | is-empty) { "!" }),
(if not ($status.untracked | is-empty) { "?" }),
]
let markers = $markers | compact | str join ""

if $markers == "" {
null
} else {
$"[($markers)]" | color default_dimmed
}
} else {
null
}

let admin_segment = if (is-admin) {
"!!" | color "red_bold"
} else {
Expand Down Expand Up @@ -93,6 +112,7 @@ export def get-left-prompt [duration_threshold: duration]: nothing -> string {
$pwd,
$git_branch_segment,
$git_action_segment,
$git_changes_segment,
$duration_segment,
$command_failed_segment,
$login_segment,
Expand Down
12 changes: 7 additions & 5 deletions src/nu-git-manager-sugar/git/mod.nu
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std log

use ../git/lib/lib.nu [get-status]

# get the commit hash of any revision
#
# # Examples
Expand Down Expand Up @@ -232,9 +234,9 @@ export def "gm repo switch" []: nothing -> nothing {
# get some information about a repo
export def "gm repo ls" [
repo?: path, # the path to the repo (defaults to `.`)
]: nothing -> record<path: path, name: string, staged: int, unstaged: int, untracked: int, last_commit: record<date: datetime, title: string, hash: string>, branch: string> {
]: nothing -> record<path: path, name: string, staged: list<string>, unstaged: list<string>, untracked: list<string>, last_commit: record<date: datetime, title: string, hash: string>, branch: string> {
let repo = $repo | default (pwd)
let status = ^git -C $repo status --short | lines
let status = get-status $repo

let last_commit = if (do --ignore-errors { git -C $repo log -1 } | complete).exit_code == 0 { {
date: (^git -C $repo log -1 --format=%cd | into datetime),
Expand All @@ -248,9 +250,9 @@ export def "gm repo ls" [
# FIXME: should be using `path sanitize` defined in `nu-git-manager`
path: ($repo | str replace --regex '^.:' '' | str replace --all '\' '/'),
name: ($repo | path basename),
staged: ($status | parse --regex '^\w. (?<file>.*)' | get file),
unstaged: ($status | parse --regex '^.\w (?<file>.*)' | get file),
untracked: ($status | parse --regex '^\?\? (?<file>.*)' | get file),
staged: $status.staged,
unstaged: $status.unstaged,
untracked: $status.untracked,
last_commit: $last_commit,
branch: (^git -C $repo branch --show-current),
}
Expand Down

0 comments on commit 2dc21d5

Please sign in to comment.