Rosettea / Hilbish

🌓 The Moon-powered shell! A comfy and extensible shell for Lua fans! 🌺 ✨
https://rosettea.github.io/Hilbish/
MIT License
505 stars 22 forks source link

get stdout of hilbish.run #196

Closed AlphaTechnolog closed 2 years ago

AlphaTechnolog commented 2 years ago

Hello! :wave:

I'm trying to make support to the git branch in my prompt, but i tried using hilbish.run and trying to get the stdout and don't works :c

here is my code:

local function get_git_branch()
    local status, stdout, stderr = hilbish.run('git branch', true)

    if stdout ~= "" then
        return stdout
    end

    return nil
end

but that function always returns nil, and print the stdout in the terminal without catching it in my variable, here is the demonstration:

Screenshot from 2022-08-12 12-27-54

Idk if i'm doing smth wrong, please tell me if i'm doing something wrong lol

TorchedSammy commented 2 years ago

you are using the function correctly and it also works on my end. you have to be using at least commit 0fc5f457adf1c8d8b8fefc94ab9d940cacb275b6 for stdout/stderr returns

TorchedSammy commented 2 years ago

and for this usage specifically, here is how delta does it, which is my own prompt

AlphaTechnolog commented 2 years ago

ah lol, well now i tried with this code, and seems that it's working more: (based in delta xd)

local hilbish = require 'hilbish'

local function branch ()
    local _, gitbranch = hilbish.run('git rev-parse --abbrev-ref HEAD', false)

    return gitbranch:gsub('\n', '')
end

print(branch())

Screenshot from 2022-08-12 19-28-06

but i don't really know how to remove that 1 (that changes to 0 when the command fails), do you know how can i remove it?

AlphaTechnolog commented 2 years ago

if i remove gitbranch:gsub('\n', ''), it hide the number, but when i remove it, it doesn't trim the last line space, but if i add it (the string.gsub), for some reason, it adds the 0 or the 1

TorchedSammy commented 2 years ago

gsub returns 2 arguments: the resulting string and the amount of matches just wrap the return in a () like return (str:gsub('h', 'ay')) and that will only take the 1st return value. in the context of where it is used in delta it only takes the 1st but i should maybe change it to be bug prone

but also: can i recommend promptua, it would actually be nice to get some more stuff added to that.

AlphaTechnolog commented 2 years ago

tysm