progress.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package progress
  2. import (
  3. "fmt"
  4. )
  5. // Progress represents the progress of a transfer.
  6. type Progress struct {
  7. ID string
  8. // Progress contains a Message or...
  9. Message string
  10. // ...progress of an action
  11. Action string
  12. Current int64
  13. Total int64
  14. // If true, don't show xB/yB
  15. HideCounts bool
  16. // If not empty, use units instead of bytes for counts
  17. Units string
  18. // Aux contains extra information not presented to the user, such as
  19. // digests for push signing.
  20. Aux interface{}
  21. LastUpdate bool
  22. }
  23. // Output is an interface for writing progress information. It's
  24. // like a writer for progress, but we don't call it Writer because
  25. // that would be confusing next to ProgressReader (also, because it
  26. // doesn't implement the io.Writer interface).
  27. type Output interface {
  28. WriteProgress(Progress) error
  29. }
  30. type chanOutput chan<- Progress
  31. func (out chanOutput) WriteProgress(p Progress) error {
  32. out <- p
  33. return nil
  34. }
  35. // ChanOutput returns an Output that writes progress updates to the
  36. // supplied channel.
  37. func ChanOutput(progressChan chan<- Progress) Output {
  38. return chanOutput(progressChan)
  39. }
  40. type discardOutput struct{}
  41. func (discardOutput) WriteProgress(Progress) error {
  42. return nil
  43. }
  44. // DiscardOutput returns an Output that discards progress
  45. func DiscardOutput() Output {
  46. return discardOutput{}
  47. }
  48. // Update is a convenience function to write a progress update to the channel.
  49. func Update(out Output, id, action string) {
  50. out.WriteProgress(Progress{ID: id, Action: action})
  51. }
  52. // Updatef is a convenience function to write a printf-formatted progress update
  53. // to the channel.
  54. func Updatef(out Output, id, format string, a ...interface{}) {
  55. Update(out, id, fmt.Sprintf(format, a...))
  56. }
  57. // Message is a convenience function to write a progress message to the channel.
  58. func Message(out Output, id, message string) {
  59. out.WriteProgress(Progress{ID: id, Message: message})
  60. }
  61. // Messagef is a convenience function to write a printf-formatted progress
  62. // message to the channel.
  63. func Messagef(out Output, id, format string, a ...interface{}) {
  64. Message(out, id, fmt.Sprintf(format, a...))
  65. }
  66. // Aux sends auxiliary information over a progress interface, which will not be
  67. // formatted for the UI. This is used for things such as push signing.
  68. func Aux(out Output, a interface{}) {
  69. out.WriteProgress(Progress{Aux: a})
  70. }