progress.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // Update is a convenience function to write a progress update to the channel.
  37. func Update(out Output, id, action string) {
  38. out.WriteProgress(Progress{ID: id, Action: action})
  39. }
  40. // Updatef is a convenience function to write a printf-formatted progress update
  41. // to the channel.
  42. func Updatef(out Output, id, format string, a ...interface{}) {
  43. Update(out, id, fmt.Sprintf(format, a...))
  44. }
  45. // Message is a convenience function to write a progress message to the channel.
  46. func Message(out Output, id, message string) {
  47. out.WriteProgress(Progress{ID: id, Message: message})
  48. }
  49. // Messagef is a convenience function to write a printf-formatted progress
  50. // message to the channel.
  51. func Messagef(out Output, id, format string, a ...interface{}) {
  52. Message(out, id, fmt.Sprintf(format, a...))
  53. }
  54. // Aux sends auxiliary information over a progress interface, which will not be
  55. // formatted for the UI. This is used for things such as push signing.
  56. func Aux(out Output, a interface{}) {
  57. out.WriteProgress(Progress{Aux: a})
  58. }