mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
feat: add experimental rust lib binding
This commit is contained in:
parent
9822227a5e
commit
121c0ab089
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -9,6 +9,7 @@
|
||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
*.h
|
||||||
# Initialization scripts generated by https://github.com/kevinburke/go-bindata
|
# Initialization scripts generated by https://github.com/kevinburke/go-bindata
|
||||||
init.go
|
init.go
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo CFLAGS: -I.
|
||||||
|
#cgo LDFLAGS: -L. -lposh3
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <posh3.h>
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -14,6 +18,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/distatus/battery"
|
"github.com/distatus/battery"
|
||||||
"github.com/shirou/gopsutil/host"
|
"github.com/shirou/gopsutil/host"
|
||||||
|
@ -155,61 +160,28 @@ func (env *environment) getPlatform() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) runCommand(command string, args ...string) (string, error) {
|
func (env *environment) runCommand(command string, args ...string) (string, error) {
|
||||||
getOutputString := func(io io.ReadCloser) string {
|
cleanOutput := func(output string) string {
|
||||||
output := new(bytes.Buffer)
|
return strings.TrimSuffix(output, "\n")
|
||||||
defer output.Reset()
|
|
||||||
buf := bufio.NewReader(io)
|
|
||||||
multiline := false
|
|
||||||
for {
|
|
||||||
line, _, _ := buf.ReadLine()
|
|
||||||
if line == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if multiline {
|
|
||||||
output.WriteString("\n")
|
|
||||||
}
|
|
||||||
output.Write(line)
|
|
||||||
multiline = true
|
|
||||||
}
|
|
||||||
return output.String()
|
|
||||||
}
|
}
|
||||||
cmd := exec.Command(command, args...)
|
commandC := C.CString(command)
|
||||||
stdout, err := cmd.StdoutPipe()
|
defer C.free(unsafe.Pointer(commandC))
|
||||||
if err != nil {
|
var argsC *C.char
|
||||||
|
if args != nil {
|
||||||
|
argsJoined := strings.Join(args, ";")
|
||||||
|
argsC = C.CString(argsJoined)
|
||||||
|
defer C.free(unsafe.Pointer(argsC))
|
||||||
|
}
|
||||||
|
response := C.getCommandOutput(commandC, argsC)
|
||||||
|
defer C.DestroyResponse(response)
|
||||||
|
output := C.GoString(response.output)
|
||||||
|
err := C.GoString(response.err)
|
||||||
|
if err != "" {
|
||||||
return "", &commandError{
|
return "", &commandError{
|
||||||
err: err.Error(),
|
err: cleanOutput(err),
|
||||||
exitCode: 666,
|
exitCode: int(response.status_code),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stderr, err := cmd.StderrPipe()
|
return cleanOutput(output), nil
|
||||||
if err != nil {
|
|
||||||
return "", &commandError{
|
|
||||||
err: err.Error(),
|
|
||||||
exitCode: 667,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return "", &commandError{
|
|
||||||
err: err.Error(),
|
|
||||||
exitCode: 668,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = cmd.Process.Kill()
|
|
||||||
}()
|
|
||||||
stdoutString := getOutputString(stdout)
|
|
||||||
stderrString := getOutputString(stderr)
|
|
||||||
if stderrString != "" {
|
|
||||||
// only wait in case of error reduces the lead time on successful
|
|
||||||
// commands on windows due to not calling process.Wait()
|
|
||||||
_ = cmd.Wait()
|
|
||||||
return "", &commandError{
|
|
||||||
err: stderrString,
|
|
||||||
exitCode: cmd.ProcessState.ExitCode(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return stdoutString, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) runShellCommand(shell, command string) string {
|
func (env *environment) runShellCommand(shell, command string) string {
|
||||||
|
|
Loading…
Reference in a new issue