| 1 | package format |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/gookit/color" |
| 7 | "github.com/wagoodman/go-progress" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | LiteTheme SimpleTheme = iota |
| 12 | LiteSquashTheme |
| 13 | HeavyTheme |
| 14 | HeavySquashTheme |
| 15 | ReallyHeavySquashTheme |
| 16 | HeavyNoBarTheme |
| 17 | ) |
| 18 | |
| 19 | const ( |
| 20 | emptyPosition SimplePosition = iota |
| 21 | fullPosition |
| 22 | leftCapPosition |
| 23 | rightCapPosition |
| 24 | ) |
| 25 | |
| 26 | type SimpleTheme int |
| 27 | type SimplePosition int |
| 28 | |
| 29 | var lookup = map[SimpleTheme][]string{ |
| 30 | LiteTheme: {" ", "─", "├", "┤"}, |
| 31 | LiteSquashTheme: {" ", "─", "▕", "▏"}, |
| 32 | HeavyTheme: {"━", "━", "┝", "┥"}, |
| 33 | HeavySquashTheme: {"━", "━", "▕", "▏"}, |
| 34 | ReallyHeavySquashTheme: {"━", "━", "▐", "▌"}, |
| 35 | HeavyNoBarTheme: {"━", "━", "", ""}, |
| 36 | } |
| 37 | |
| 38 | var ( |
| 39 | ColorCompleted = color.HEX("#fcba03") |
| 40 | ColorTodo = color.HEX("#777777") |
| 41 | ) |
| 42 | |
| 43 | type Simple struct { |
| 44 | width int |
| 45 | charSet []string |
| 46 | doneColor color.RGBColor |
| 47 | todoColor color.RGBColor |
| 48 | } |
| 49 | |
| 50 | func NewSimple(width int) Simple { |
| 51 | return Simple{ |
| 52 | width: width, |
| 53 | charSet: lookup[HeavySquashTheme], |
| 54 | doneColor: ColorCompleted, |
| 55 | todoColor: ColorTodo, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func NewSimpleWithTheme(width int, theme SimpleTheme, doneHexColor, todoHexColor color.RGBColor) Simple { |
| 60 | return Simple{ |
| 61 | width: width, |
| 62 | charSet: lookup[theme], |
| 63 | doneColor: doneHexColor, |
| 64 | todoColor: todoHexColor, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func (s Simple) Format(p progress.Progress) (string, error) { |
| 69 | completedRatio := p.Ratio() |
| 70 | if completedRatio < 0 { |
| 71 | completedRatio = 0 |
| 72 | } |
| 73 | completedCount := int(completedRatio * float64(s.width)) |
| 74 | todoCount := s.width - completedCount |
| 75 | if todoCount < 0 { |
| 76 | todoCount = 0 |
| 77 | } |
| 78 | |
| 79 | completedSection := s.doneColor.Sprint(strings.Repeat(s.charSet[fullPosition], completedCount)) |
| 80 | todoSection := s.todoColor.Sprint(strings.Repeat(s.charSet[fullPosition], todoCount)) |
| 81 | |
| 82 | return s.charSet[leftCapPosition] + completedSection + todoSection + s.charSet[rightCapPosition], nil |
| 83 | } |