Skip to content

Commit

Permalink
added Interrupt method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kura-lab committed Oct 14, 2019
1 parent 8848bed commit 4b07e50
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func main() {
time.Sleep(20 * time.Millisecond)
bw.Pop(i)
bw.Redisplay()

// you can call when need to interrupt
// bw.Interrupt()
}
}
```
Expand Down
24 changes: 23 additions & 1 deletion bw.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type BubbleWrap struct {
after string
beforeColor color.Color
afterColor color.Color
interrupted bool
completed bool
}

const cursorHide = "\033[?25l"
Expand Down Expand Up @@ -58,10 +60,17 @@ func (bw *BubbleWrap) ChangeBubbleColor(before *color.Color, after *color.Color)
}

func (bw *BubbleWrap) Pop(i int) {
if bw.interrupted || bw.completed {
return
}
bw.bubbles[i] = true
}

func (bw *BubbleWrap) Display() {
if bw.interrupted || bw.completed {
return
}

// display bubbles
var c, comp int
for _, v := range bw.bubbles {
Expand Down Expand Up @@ -111,11 +120,16 @@ func (bw *BubbleWrap) Display() {

// output result
if trunc == 100 {
bw.completed = true
fmt.Printf(cursorShow + "\n\nBubble Wrape finished.\n")
}
}

func (bw *BubbleWrap) Clear() {
if bw.interrupted || bw.completed {
return
}

var c, r int
r = int(bw.numbers / bw.column)
c = bw.numbers + 2
Expand All @@ -129,10 +143,18 @@ func (bw *BubbleWrap) Redisplay() {
bw.Display()
}

func (bw *BubbleWrap) Interrupt() {
if bw.interrupted || bw.completed {
return
}
bw.interrupted = true
fmt.Printf(cursorShow + "\n\nBubble Wrape interrupted.\n")
}

func (bw *BubbleWrap) printCopyRight() {
fmt.Printf("Bubble Wrap %v by kura.\n\n", bw.version())
}

func (bw *BubbleWrap) version() string {
return "1.0.6"
return "1.0.7"
}
32 changes: 32 additions & 0 deletions bw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,35 @@ func TestChangeBubbleColor(t *testing.T) {
bw.Redisplay()
}
}

func TestInterrupt(t *testing.T) {
numbers := 220

bw := NewBubbleWrap(numbers)
bw.Display()

for i := 0; i < numbers; i++ {
time.Sleep(10 * time.Millisecond)
bw.Pop(i)
bw.Redisplay()
if i == 120 {
bw.Interrupt()
}
}
}

func TestCompleted(t *testing.T) {
numbers := 220

bw := NewBubbleWrap(numbers)
bw.Display()

for i := 0; i < numbers; i++ {
time.Sleep(10 * time.Millisecond)
bw.Pop(i)
bw.Redisplay()
}
bw.Display()
bw.Clear()
bw.Redisplay()
}

0 comments on commit 4b07e50

Please sign in to comment.