mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: faster command time
This commit is contained in:
parent
0250e8d5d1
commit
408b94fcd4
|
@ -1,7 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -138,17 +139,34 @@ 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) {
|
||||||
out, err := exec.Command(command, args...).Output()
|
cmd := exec.Command(command, args...)
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
var exerr *exec.ExitError
|
if err != nil {
|
||||||
if errors.As(err, &exerr) {
|
log.Fatal(err)
|
||||||
return "", &commandError{exitCode: exerr.ExitCode()}
|
|
||||||
}
|
}
|
||||||
|
err = cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
return strings.TrimSpace(string(out)), nil
|
_ = cmd.Process.Kill()
|
||||||
|
}()
|
||||||
|
output := new(bytes.Buffer)
|
||||||
|
defer output.Reset()
|
||||||
|
buf := bufio.NewReader(stdout)
|
||||||
|
multiline := false
|
||||||
|
for {
|
||||||
|
line, _, _ := buf.ReadLine()
|
||||||
|
if line == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if multiline {
|
||||||
|
output.WriteString("\n")
|
||||||
|
}
|
||||||
|
output.Write(line)
|
||||||
|
multiline = true
|
||||||
|
}
|
||||||
|
return output.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) runShellCommand(shell, command string) string {
|
func (env *environment) runShellCommand(shell, command string) string {
|
||||||
|
|
Loading…
Reference in a new issue