mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-23 22:50:41 +02:00
Switched to channels instaid of a mutex
This commit is contained in:
parent
1fd8cadd9e
commit
5ae0e75e5e
@ -33,19 +33,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
|
|||||||
return errorMessage, err
|
return errorMessage, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// canAsk makes sure there are no data races in go
|
stopAsking := make(chan struct{})
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
var waitForBufio sync.WaitGroup
|
var waitForBufio sync.WaitGroup
|
||||||
waitForBufio.Add(1)
|
waitForBufio.Add(1)
|
||||||
@ -57,9 +45,12 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
|
|||||||
go func() {
|
go func() {
|
||||||
scanner := bufio.NewScanner(tty)
|
scanner := bufio.NewScanner(tty)
|
||||||
scanner.Split(scanWordsWithNewLines)
|
scanner.Split(scanWordsWithNewLines)
|
||||||
|
loop:
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
// canAsk prefrents calls to output when the program is already closed
|
select {
|
||||||
if canAsk() {
|
case <-stopAsking:
|
||||||
|
break loop
|
||||||
|
default:
|
||||||
toOutput := strings.Trim(scanner.Text(), " ")
|
toOutput := strings.Trim(scanner.Text(), " ")
|
||||||
cmdOutput = append(cmdOutput, toOutput)
|
cmdOutput = append(cmdOutput, toOutput)
|
||||||
toWrite := output(toOutput)
|
toWrite := output(toOutput)
|
||||||
@ -72,7 +63,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if err = cmd.Wait(); err != nil {
|
if err = cmd.Wait(); err != nil {
|
||||||
stopCanAsk()
|
stopAsking <- struct{}{}
|
||||||
waitForBufio.Wait()
|
waitForBufio.Wait()
|
||||||
return strings.Join(cmdOutput, " "), err
|
return strings.Join(cmdOutput, " "), err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user