1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-03 15:02:52 +02:00
lazygit/pkg/commands/exec_live_default.go

133 lines
3.2 KiB
Go
Raw Normal View History

// +build !windows
package commands
import (
"bufio"
"os"
"os/exec"
"regexp"
2018-10-29 08:23:56 +01:00
"strings"
2018-11-01 07:06:34 +01:00
"sync"
2018-11-06 20:24:10 +01:00
"unicode/utf8"
"github.com/kr/pty"
)
// RunCommandWithOutputLiveWrapper runs a command and return every word that gets written in stdout
// Output is a function that executes by every word that gets read by bufio
// As return of output you need to give a string that will be written to stdin
// NOTE: If the return data is empty it won't written anything to stdin
// NOTE: You don't have to include a enter in the return data this function will do that for you
2018-10-29 08:23:56 +01:00
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) (errorMessage string, codeError error) {
cmdOutput := []string{}
splitCmd := ToArgv(command)
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "LANG=en_US.utf8", "LC_ALL=en_US.UTF-8")
tty, err := pty.Start(cmd)
if err != nil {
2018-10-29 08:23:56 +01:00
return errorMessage, err
}
2018-11-06 20:24:10 +01:00
// canAsk makes sure there are no data races in go
2018-11-03 09:36:38 +01:00
var canAskLock sync.Mutex
canAskValue := true
canAsk := func() bool {
canAskLock.Lock()
defer canAskLock.Unlock()
return canAskValue
}
stopCanAsk := func() {
canAskLock.Lock()
defer canAskLock.Unlock()
canAskValue = false
}
2018-11-01 07:06:34 +01:00
var waitForBufio sync.WaitGroup
waitForBufio.Add(1)
2018-10-31 19:25:52 +01:00
defer func() {
2018-11-01 07:06:34 +01:00
_ = tty.Close()
2018-10-31 19:25:52 +01:00
}()
go func() {
// Regex to cleanup the command output
// sometimes the output words include unneeded spaces at eatch end of the string
re := regexp.MustCompile(`(^\s*)|(\s*$)`)
scanner := bufio.NewScanner(tty)
2018-11-06 20:24:10 +01:00
scanner.Split(scanWordsWithNewLines)
for scanner.Scan() {
2018-11-03 09:12:45 +01:00
// canAsk prefrents calls to output when the program is already closed
2018-11-03 09:36:38 +01:00
if canAsk() {
2018-11-03 09:12:45 +01:00
toOutput := re.ReplaceAllString(scanner.Text(), "")
cmdOutput = append(cmdOutput, toOutput)
toWrite := output(toOutput)
if len(toWrite) > 0 {
_, _ = tty.Write([]byte(toWrite + "\n"))
}
}
}
2018-11-01 07:06:34 +01:00
waitForBufio.Done()
}()
2018-11-06 20:24:10 +01:00
if err = cmd.Wait(); err != nil {
2018-11-03 09:36:38 +01:00
stopCanAsk()
2018-11-01 07:06:34 +01:00
waitForBufio.Wait()
2018-10-29 08:23:56 +01:00
return strings.Join(cmdOutput, " "), err
}
2018-10-29 08:23:56 +01:00
return errorMessage, nil
}
2018-11-06 20:24:10 +01:00
// scanWordsWithNewLines is a copy of bufio.ScanWords but this also captures new lines
func scanWordsWithNewLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
start := 0
for width := 0; start < len(data); start += width {
var r rune
r, width = utf8.DecodeRune(data[start:])
if !isSpace(r) {
break
}
}
for width, i := 0, start; i < len(data); i += width {
var r rune
r, width = utf8.DecodeRune(data[i:])
if isSpace(r) {
return i + width, data[start:i], nil
}
}
if atEOF && len(data) > start {
return len(data), data[start:], nil
}
return start, nil, nil
}
// isSpace is also copied form bufio.ScanWords and has been modiefied to also captures new lines
func isSpace(r rune) bool {
if r <= '\u00FF' {
// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
switch r {
case ' ', '\t', '\v', '\f':
return true
case '\u0085', '\u00A0':
return true
}
return false
}
// High-valued ones.
if '\u2000' <= r && r <= '\u200a' {
return true
}
switch r {
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
return true
}
return false
}