Merge pull request #3673 from niten94/status-pass-repodir
Some checks failed
Build and Test / test (1.19.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.19.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.19.x, windows-latest) (push) Has been cancelled
Build and Test / test (1.23.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.23.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.23.x, windows-latest) (push) Has been cancelled

`status.lua`: Display commit and branch of repository where file is located
This commit is contained in:
Dmytro Maluka 2025-03-04 20:15:23 +01:00 committed by GitHub
commit 2ae9812f47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 19 deletions

View File

@ -8,8 +8,10 @@ those options (`> help options`) for more information.
This plugin provides functions that can be used in the status line format: This plugin provides functions that can be used in the status line format:
* `status.branch`: returns the name of the current git branch. * `status.branch`: returns the name of the current git branch in the repository
* `status.hash`: returns the hash of the current git commit. where the file is located.
* `status.hash`: returns the hash of the current git commit in the repository
where the file is located.
* `status.paste`: returns "" if the paste option is disabled and "PASTE" * `status.paste`: returns "" if the paste option is disabled and "PASTE"
if it is enabled. if it is enabled.
* `status.lines`: returns the number of lines in the buffer. * `status.lines`: returns the number of lines in the buffer.

View File

@ -3,7 +3,10 @@ VERSION = "1.0.0"
local micro = import("micro") local micro = import("micro")
local buffer = import("micro/buffer") local buffer = import("micro/buffer")
local config = import("micro/config") local config = import("micro/config")
local shell = import("micro/shell")
local filepath = import("filepath")
local humanize = import("humanize") local humanize = import("humanize")
local strings = import("strings")
function init() function init()
micro.SetStatusInfoFn("status.branch") micro.SetStatusInfoFn("status.branch")
@ -32,30 +35,23 @@ function size(b)
return humanize.Bytes(b:Size()) return humanize.Bytes(b:Size())
end end
function branch(b) local function parseRevision(b, opt)
if b.Type.Kind ~= buffer.BTInfo then if b.Type.Kind ~= buffer.BTInfo then
local shell = import("micro/shell") local dir = filepath.Dir(b.Path)
local strings = import("strings") local str, err = shell.ExecCommand("git", "-C", dir, "rev-parse", opt, "HEAD")
local branch, err = shell.ExecCommand("git", "rev-parse", "--abbrev-ref", "HEAD")
if err == nil then if err == nil then
return strings.TrimSpace(branch) return strings.TrimSpace(str)
end end
return ""
end end
return ""
end
function branch(b)
return parseRevision(b, "--abbrev-ref")
end end
function hash(b) function hash(b)
if b.Type.Kind ~= 5 then return parseRevision(b, "--short")
local shell = import("micro/shell")
local strings = import("strings")
local hash, err = shell.ExecCommand("git", "rev-parse", "--short", "HEAD")
if err == nil then
return strings.TrimSpace(hash)
end
return ""
end
end end
function paste(b) function paste(b)