Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a "Git changes" segment to the prompt #133

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -111,3 +111,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]

# TODO: write a test
Expand Down Expand Up @@ -57,6 +57,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 @@ -87,6 +106,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