progress.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // Aux contains extra information not presented to the user, such as
  17. // digests for push signing.
  18. Aux interface{}
  19. LastUpdate bool
  20. }
  21. // Output is an interface for writing progress information. It's
  22. // like a writer for progress, but we don't call it Writer because
  23. // that would be confusing next to ProgressReader (also, because it
  24. // doesn't implement the io.Writer interface).
  25. type Output interface {
  26. WriteProgress(Progress) error
  27. }
  28. type chanOutput chan<- Progress
  29. func (out chanOutput) WriteProgress(p Progress) error {
  30. out <- p
  31. return nil
  32. }
  33. // ChanOutput returns an Output that writes progress updates to the
  34. // supplied channel.
  35. func ChanOutput(progressChan chan<- Progress) Output {
  36. return chanOutput(progressChan)
  37. }
  38. type discardOutput struct{}
  39. func (discardOutput) WriteProgress(Progress) error {
  40. return nil
  41. }
  42. // DiscardOutput returns an Output that discards progress
  43. func DiscardOutput() Output {
  44. return discardOutput{}
  45. }
  46. // Update is a convenience function to write a progress update to the channel.
  47. func Update(out Output, id, action string) {
  48. out.WriteProgress(Progress{ID: id, Action: action})
  49. }
  50. // Updatef is a convenience function to write a printf-formatted progress update
  51. // to the channel.
  52. func Updatef(out Output, id, format string, a ...interface{}) {
  53. Update(out, id, fmt.Sprintf(format, a...))
  54. }
  55. // Message is a convenience function to write a progress message to the channel.
  56. func Message(out Output, id, message string) {
  57. out.WriteProgress(Progress{ID: id, Message: message})
  58. }
  59. // Messagef is a convenience function to write a printf-formatted progress
  60. // message to the channel.
  61. func Messagef(out Output, id, format string, a ...interface{}) {
  62. Message(out, id, fmt.Sprintf(format, a...))
  63. }
  64. // Aux sends auxiliary information over a progress interface, which will not be
  65. // formatted for the UI. This is used for things such as push signing.
  66. func Aux(out Output, a interface{}) {
  67. out.WriteProgress(Progress{Aux: a})
  68. }