progress.go 2.2 KB

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