Skip to content

Commit 8569606

Browse files
committed
a flag for hiding progress bar
Signed-off-by: Omri Bornstein <omribor@gmail.com>
1 parent 23ca30e commit 8569606

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

commands/root.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
var duration time.Duration
14-
var pid int
13+
var (
14+
duration time.Duration
15+
pid int
16+
quiet bool
17+
)
1518

1619
var RootCommand = &cobra.Command{
1720
Use: "cocainate",
@@ -31,7 +34,7 @@ var RootCommand = &cobra.Command{
3134
return err
3235
}
3336

34-
if err := s.Wait(); err != nil {
37+
if err := s.Wait(quiet); err != nil {
3538
return err
3639
}
3740

@@ -46,6 +49,7 @@ var RootCommand = &cobra.Command{
4649
func init() {
4750
RootCommand.Flags().DurationVarP(&duration, "duration", "d", 0, "duration with units ns, us (or µs), ms, s, m, h")
4851
RootCommand.Flags().IntVarP(&pid, "pid", "p", 0, "a running process ID, duration (used as polling interval) must be provided")
52+
RootCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, "hide progress bar")
4953
// RootCommand.MarkFlagsRequiredTogether("pid", "duration")
5054
RootCommand.SetVersionTemplate("{{.Version}}\n")
5155
}

session/session.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/AppleGamer22/cocainate/progress_bar"
1212
"github.com/AppleGamer22/cocainate/ps"
13+
tea "github.com/charmbracelet/bubbletea"
1314
)
1415

1516
/*
@@ -34,7 +35,7 @@ Wait will block further execution until the user send an interrupt signal, or un
3435
3536
A non-nil error is returned if the un-inhabitation call fails.
3637
*/
37-
func (s *Session) Wait() error {
38+
func (s *Session) Wait(quiet bool) error {
3839
if !s.Active() {
3940
return errors.New("Wait can be called only after Start has been called successfully")
4041
}
@@ -50,14 +51,19 @@ func (s *Session) Wait() error {
5051
case <-s.Signals:
5152
}
5253
} else if s.Duration > 0 {
53-
program := progress_bar.New(s.Duration, s.Signals)
54+
var program *tea.Program
55+
if !quiet {
56+
program = progress_bar.New(s.Duration, s.Signals)
57+
}
5458
// https://pkg.go.dev/time#After
5559
timer := time.NewTimer(s.Duration)
5660
select {
5761
case <-timer.C:
5862
case <-s.Signals:
5963
timer.Stop()
60-
program.Kill()
64+
if !quiet {
65+
program.Kill()
66+
}
6167
}
6268
} else {
6369
<-s.Signals

session/session_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestDuration(t *testing.T) {
1616
err := s.Start()
1717
assert.NoError(t, err)
1818

19-
err = s.Wait()
19+
err = s.Wait(true)
2020
assert.NoError(t, err)
2121
}
2222

@@ -29,7 +29,7 @@ func TestInterrupt(t *testing.T) {
2929
err = s.Kill()
3030
assert.NoError(t, err)
3131

32-
err = s.Wait()
32+
err = s.Wait(true)
3333
assert.NoError(t, err)
3434
}
3535

@@ -43,7 +43,7 @@ func TestKill(t *testing.T) {
4343
wg.Add(2)
4444

4545
go func() {
46-
err := s.Wait()
46+
err := s.Wait(false)
4747
assert.NoError(t, err)
4848
wg.Done()
4949
}()
@@ -70,7 +70,7 @@ func TestStop(t *testing.T) {
7070
// Test for when Wait is called before Start
7171
func TestErrors(t *testing.T) {
7272
s := session.New(0, 0)
73-
err := s.Wait()
73+
err := s.Wait(false)
7474
assert.Error(t, err)
7575

7676
err = s.Kill()

0 commit comments

Comments
 (0)