From 004f95413a85bce68c424f65e58ad79d590244da Mon Sep 17 00:00:00 2001 From: amtoine Date: Fri, 8 Dec 2023 10:03:44 +0100 Subject: [PATCH 1/3] refactor "getting Git status" in `git/lib/lib.nu` --- src/nu-git-manager-sugar/git/lib/lib.nu | 10 ++++++++++ src/nu-git-manager-sugar/git/mod.nu | 10 ++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/nu-git-manager-sugar/git/lib/lib.nu b/src/nu-git-manager-sugar/git/lib/lib.nu index 88e06d8b..3ba9200a 100644 --- a/src/nu-git-manager-sugar/git/lib/lib.nu +++ b/src/nu-git-manager-sugar/git/lib/lib.nu @@ -111,3 +111,13 @@ export def git-action []: nothing -> string { } } +export def get-status [ + repo: path, # the path to the repo +]: nothing -> record, unstaged: list, untracked: list> { + let status = ^git -C $repo status --short | lines + { + staged: ($status | parse --regex '^\w. (?.*)' | get file), + unstaged: ($status | parse --regex '^.\w (?.*)' | get file), + untracked: ($status | parse --regex '^\?\? (?.*)' | get file), + } +} diff --git a/src/nu-git-manager-sugar/git/mod.nu b/src/nu-git-manager-sugar/git/mod.nu index 0a7d4f0a..1d703670 100644 --- a/src/nu-git-manager-sugar/git/mod.nu +++ b/src/nu-git-manager-sugar/git/mod.nu @@ -1,5 +1,7 @@ use std log +use ../git/lib/lib.nu [get-status] + # get the commit hash of any revision # # # Examples @@ -234,7 +236,7 @@ export def "gm repo ls" [ repo?: path, # the path to the repo (defaults to `.`) ]: nothing -> record, 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), @@ -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. (?.*)' | get file), - unstaged: ($status | parse --regex '^.\w (?.*)' | get file), - untracked: ($status | parse --regex '^\?\? (?.*)' | get file), + staged: $status.staged, + unstaged: $status.unstaged, + untracked: $status.untracked, last_commit: $last_commit, branch: (^git -C $repo branch --show-current), } From c71981d665f766531aab648f787230b42802e8cf Mon Sep 17 00:00:00 2001 From: amtoine Date: Fri, 8 Dec 2023 10:04:13 +0100 Subject: [PATCH 2/3] fix the signature of `gm repo ls` --- src/nu-git-manager-sugar/git/mod.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nu-git-manager-sugar/git/mod.nu b/src/nu-git-manager-sugar/git/mod.nu index 1d703670..45069737 100644 --- a/src/nu-git-manager-sugar/git/mod.nu +++ b/src/nu-git-manager-sugar/git/mod.nu @@ -234,7 +234,7 @@ 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, branch: string> { +]: nothing -> record, unstaged: list, untracked: list, last_commit: record, branch: string> { let repo = $repo | default (pwd) let status = get-status $repo From 15b069be00d8c33a34c842bf0bcf89987293d37a Mon Sep 17 00:00:00 2001 From: amtoine Date: Fri, 8 Dec 2023 10:13:03 +0100 Subject: [PATCH 3/3] add a "Git changes segment" to the left prompt --- src/nu-git-manager-sugar/git/lib/prompt.nu | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/nu-git-manager-sugar/git/lib/prompt.nu b/src/nu-git-manager-sugar/git/lib/prompt.nu index f949f2ba..ab68ab9d 100644 --- a/src/nu-git-manager-sugar/git/lib/prompt.nu +++ b/src/nu-git-manager-sugar/git/lib/prompt.nu @@ -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 @@ -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 { @@ -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,