Skip to content

Commit

Permalink
Do not execute command when suspended
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Oct 15, 2023
1 parent 5a6a620 commit 9047ed3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
47 changes: 41 additions & 6 deletions generator.go
Expand Up @@ -4,34 +4,57 @@ import "time"

type newSnapFunc func(int64, *Snapshot, chan<- struct{}) *Snapshot

func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)

go func() {
var s *Snapshot

t := time.Tick(interval)

for now := range t {
select {
case isSuspended = <-isSuspendedQueue:
default:
}

if isSuspended {
continue
}

finish := make(chan struct{})
id := (now.UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
c <- s
}
}()

return c
return c, isSuspendedQueue
}

func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)

go func() {
var s *Snapshot

begin := time.Now().UnixNano()

for {
select {
case isSuspended = <-isSuspendedQueue:
default:
}

if isSuspended {
time.Sleep(interval)
continue

Check failure on line 55 in generator.go

View workflow job for this annotation

GitHub Actions / lint

continue with no blank line before (nlreturn)
}

finish := make(chan struct{})
start := time.Now()
id := (start.UnixNano() - begin) / int64(time.Millisecond)
Expand All @@ -52,18 +75,30 @@ func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapsh
}
}()

return c
return c, isSuspendedQueue
}

func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)

go func() {
var s *Snapshot

begin := time.Now().UnixNano()

for {
select {
case isSuspended = <-isSuspendedQueue:
default:
}

if isSuspended {
time.Sleep(interval)
continue

Check failure on line 99 in generator.go

View workflow job for this annotation

GitHub Actions / lint

continue with no blank line before (nlreturn)
}

finish := make(chan struct{})
id := (time.Now().UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
Expand All @@ -75,5 +110,5 @@ func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Sna
}
}()

return c
return c, isSuspendedQueue
}
34 changes: 18 additions & 16 deletions viddy.go
Expand Up @@ -56,10 +56,11 @@ type Viddy struct {
statusView *tview.TextView
queryEditor *tview.InputField

snapshotQueue <-chan *Snapshot
queue chan int64
finishedQueue chan int64
diffQueue chan int64
snapshotQueue <-chan *Snapshot
isSuspendedQueue chan<- bool
queue chan int64
finishedQueue chan int64
diffQueue chan int64

currentID int64
latestFinishedID int64
Expand Down Expand Up @@ -103,15 +104,18 @@ func NewViddy(conf *config) *Viddy {
return NewSnapshot(id, conf.runtime.cmd, conf.runtime.args, conf.general.shell, conf.general.shellOptions, before, finish)
}

var snapshotQueue <-chan *Snapshot
var (
snapshotQueue <-chan *Snapshot
isSuspendedQueue chan<- bool
)

switch conf.runtime.mode {
case ViddyIntervalModeClockwork:
snapshotQueue = ClockSnapshot(begin, newSnap, conf.runtime.interval)
snapshotQueue, isSuspendedQueue = ClockSnapshot(begin, newSnap, conf.runtime.interval)
case ViddyIntervalModeSequential:
snapshotQueue = SequentialSnapshot(newSnap, conf.runtime.interval)
snapshotQueue, isSuspendedQueue = SequentialSnapshot(newSnap, conf.runtime.interval)
case ViddyIntervalModePrecise:
snapshotQueue = PreciseSnapshot(newSnap, conf.runtime.interval)
snapshotQueue, isSuspendedQueue = PreciseSnapshot(newSnap, conf.runtime.interval)
}

return &Viddy{
Expand All @@ -126,10 +130,11 @@ func NewViddy(conf *config) *Viddy {

historyRowCount: map[int64]int{},

snapshotQueue: snapshotQueue,
queue: make(chan int64),
finishedQueue: make(chan int64),
diffQueue: make(chan int64, 100),
snapshotQueue: snapshotQueue,
isSuspendedQueue: isSuspendedQueue,
queue: make(chan int64),
finishedQueue: make(chan int64),
diffQueue: make(chan int64, 100),

isRingBell: conf.general.bell,
isShowDiff: conf.general.differences,
Expand Down Expand Up @@ -285,10 +290,6 @@ func (v *Viddy) queueHandler() {
v.updateSelection()
}
case id := <-v.queue:
if v.isSuspend {
return
}

s := v.getSnapShot(id)
idCell := tview.NewTableCell(strconv.FormatInt(s.id, 10)).SetTextColor(tview.Styles.SecondaryTextColor)
additionCell := tview.NewTableCell("").SetTextColor(tcell.ColorGreen)
Expand Down Expand Up @@ -611,6 +612,7 @@ func (v *Viddy) Run() error {
switch event.Rune() {
case 's':
v.isSuspend = !v.isSuspend
v.isSuspendedQueue <- v.isSuspend
case 'b':
v.isRingBell = !v.isRingBell
case 'd':
Expand Down

0 comments on commit 9047ed3

Please sign in to comment.