github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/progress/progress.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package progress
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  
    26  	"golang.org/x/crypto/ssh/terminal"
    27  
    28  	"github.com/snapcore/snapd/osutil"
    29  )
    30  
    31  // Meter is an interface to show progress to the user
    32  type Meter interface {
    33  	// Start progress with max "total" steps
    34  	Start(label string, total float64)
    35  
    36  	// set progress to the "current" step
    37  	Set(current float64)
    38  
    39  	// set "total" steps needed
    40  	SetTotal(total float64)
    41  
    42  	// Finish the progress display
    43  	Finished()
    44  
    45  	// Indicate indefinite activity by showing a spinner
    46  	Spin(msg string)
    47  
    48  	// interface for writer
    49  	Write(p []byte) (n int, err error)
    50  
    51  	// notify the user of miscellaneous events
    52  	Notify(string)
    53  }
    54  
    55  // NullMeter is a Meter that does nothing
    56  type NullMeter struct{}
    57  
    58  // Null is a default NullMeter instance
    59  var Null = NullMeter{}
    60  
    61  func (NullMeter) Start(string, float64)       {}
    62  func (NullMeter) Set(float64)                 {}
    63  func (NullMeter) SetTotal(float64)            {}
    64  func (NullMeter) Finished()                   {}
    65  func (NullMeter) Write(p []byte) (int, error) { return len(p), nil }
    66  func (NullMeter) Notify(string)               {}
    67  func (NullMeter) Spin(msg string)             {}
    68  
    69  // QuietMeter is a Meter that _just_ shows Notify()s.
    70  type QuietMeter struct{ NullMeter }
    71  
    72  func (QuietMeter) Notify(msg string) {
    73  	fmt.Fprintln(stdout, msg)
    74  }
    75  
    76  // testMeter, if set, is returned by MakeProgressBar; set it from tests.
    77  var testMeter Meter
    78  
    79  func MockMeter(meter Meter) func() {
    80  	testMeter = meter
    81  	return func() {
    82  		testMeter = nil
    83  	}
    84  }
    85  
    86  var inTesting bool = (osutil.IsTestBinary()) || os.Getenv("SPREAD_SYSTEM") != ""
    87  
    88  // MakeProgressBar creates an appropriate progress.Meter for the environ in
    89  // which it is called:
    90  //
    91  // * if MockMeter has been called, return that.
    92  // * if no terminal is attached, or we think we're running a test, a
    93  //   minimalistic QuietMeter is returned.
    94  // * otherwise, an ANSIMeter is returned.
    95  //
    96  // TODO: instead of making the pivot at creation time, do it at every call.
    97  func MakeProgressBar() Meter {
    98  	if testMeter != nil {
    99  		return testMeter
   100  	}
   101  	if !inTesting && terminal.IsTerminal(int(os.Stdin.Fd())) {
   102  		return &ANSIMeter{}
   103  	}
   104  
   105  	return QuietMeter{}
   106  }