Projects STRLCPY termdash Commits 1aba2803
🤬
  • ■ ■ ■ ■ ■
    widgets/gauge/gauge.go
    skipped 135 lines
    136 136   return nil
    137 137  }
    138 138   
    139  -// width determines the required width of the gauge drawn on the provided area
    140  -// in order to represent the current progress.
    141  -func (g *Gauge) width(ar image.Rectangle) int {
    142  - mult := float32(g.current) / float32(g.total)
     139 +// width determines the X coordinate that represents point w in rectangle ar.
     140 +// This is used to calculate the width of the gauge drawn on the provided area
     141 +// in order to represent the current progress or to figure out the coordinate
     142 +// for the threshold line.
     143 +func (g *Gauge) width(ar image.Rectangle, w int) int {
     144 + mult := float32(w) / float32(g.total)
    143 145   width := float32(ar.Dx()) * mult
    144 146   return int(width)
    145 147  }
    skipped 127 lines
    273 275   progress := image.Rect(
    274 276   usable.Min.X,
    275 277   usable.Min.Y,
    276  - usable.Min.X+g.width(usable),
     278 + usable.Min.X+g.width(usable, g.current),
    277 279   usable.Max.Y,
    278 280   )
    279 281   if progress.Dx() > 0 {
    skipped 4 lines
    284 286   return err
    285 287   }
    286 288   }
     289 + if g.opts.threshold > 0 && g.opts.threshold < g.total {
     290 + line := draw.HVLine{
     291 + Start: image.Point{
     292 + X: usable.Min.X + g.width(usable, g.opts.threshold),
     293 + Y: cvs.Area().Min.Y,
     294 + },
     295 + End: image.Point{
     296 + X: usable.Min.X + g.width(usable, g.opts.threshold),
     297 + Y: cvs.Area().Max.Y - 1,
     298 + },
     299 + }
     300 + if err := draw.HVLines(cvs, []draw.HVLine{line},
     301 + draw.HVLineStyle(g.opts.thresholdLineStyle),
     302 + draw.HVLineCellOpts(g.opts.thresholdCellOpts...),
     303 + ); err != nil {
     304 + return err
     305 + }
     306 + }
     307 + 
    287 308   return g.drawText(cvs, progress)
    288 309  }
    289 310   
    skipped 44 lines
  • ■ ■ ■ ■ ■ ■
    widgets/gauge/gauge_test.go
    skipped 840 lines
    841 841   return ft
    842 842   },
    843 843   },
     844 + {
     845 + desc: "threshold with border percentage",
     846 + opts: []Option{
     847 + Char('o'),
     848 + Threshold(20, linestyle.Double),
     849 + },
     850 + percent: &percentCall{p: 35},
     851 + canvas: image.Rect(0, 0, 10, 3),
     852 + want: func(size image.Point) *faketerm.Terminal {
     853 + ft := faketerm.MustNew(size)
     854 + c := testcanvas.MustNew(ft.Area())
     855 + 
     856 + testdraw.MustRectangle(c, image.Rect(0, 0, 3, 3),
     857 + draw.RectChar('o'),
     858 + draw.RectCellOpts(cell.BgColor(cell.ColorGreen)),
     859 + )
     860 + testdraw.MustText(c, "35%", image.Point{3, 1})
     861 + testdraw.MustHVLines(c, []draw.HVLine{{
     862 + Start: image.Point{X: 2, Y: 0},
     863 + End: image.Point{X: 2, Y: 2},
     864 + }}, draw.HVLineStyle(linestyle.Double))
     865 + testcanvas.MustApply(c, ft)
     866 + return ft
     867 + },
     868 + },
     869 + {
     870 + desc: "threshold without border absolute",
     871 + opts: []Option{
     872 + Char('o'),
     873 + Threshold(3, linestyle.Light, cell.BgColor(cell.ColorRed)),
     874 + Border(linestyle.None),
     875 + HideTextProgress(),
     876 + },
     877 + absolute: &absoluteCall{done: 4, total: 10},
     878 + canvas: image.Rect(0, 0, 10, 3),
     879 + want: func(size image.Point) *faketerm.Terminal {
     880 + ft := faketerm.MustNew(size)
     881 + c := testcanvas.MustNew(ft.Area())
     882 + 
     883 + testdraw.MustRectangle(c, image.Rect(0, 0, 3, 3),
     884 + draw.RectChar('o'),
     885 + draw.RectCellOpts(cell.BgColor(cell.ColorGreen)),
     886 + )
     887 + testdraw.MustHVLines(c, []draw.HVLine{{
     888 + Start: image.Point{X: 3, Y: 0},
     889 + End: image.Point{X: 3, Y: 2},
     890 + }}, draw.HVLineStyle(linestyle.Light),
     891 + draw.HVLineCellOpts(cell.BgColor(cell.ColorRed)))
     892 + testcanvas.MustApply(c, ft)
     893 + return ft
     894 + },
     895 + },
     896 + {
     897 + desc: "threshold outside of bounds (negative)",
     898 + opts: []Option{
     899 + Char('o'),
     900 + HideTextProgress(),
     901 + Threshold(-1, linestyle.Light), // ignored
     902 + },
     903 + percent: &percentCall{p: 35},
     904 + canvas: image.Rect(0, 0, 10, 3),
     905 + want: func(size image.Point) *faketerm.Terminal {
     906 + ft := faketerm.MustNew(size)
     907 + c := testcanvas.MustNew(ft.Area())
     908 + 
     909 + testdraw.MustRectangle(c, image.Rect(0, 0, 3, 3),
     910 + draw.RectChar('o'),
     911 + draw.RectCellOpts(cell.BgColor(cell.ColorGreen)),
     912 + )
     913 + testcanvas.MustApply(c, ft)
     914 + return ft
     915 + },
     916 + },
     917 + {
     918 + desc: "threshold outside of bounds (>=max)",
     919 + opts: []Option{
     920 + Char('o'),
     921 + HideTextProgress(),
     922 + Threshold(100, linestyle.Light), // ignored
     923 + },
     924 + percent: &percentCall{p: 35},
     925 + canvas: image.Rect(0, 0, 10, 3),
     926 + want: func(size image.Point) *faketerm.Terminal {
     927 + ft := faketerm.MustNew(size)
     928 + c := testcanvas.MustNew(ft.Area())
     929 + 
     930 + testdraw.MustRectangle(c, image.Rect(0, 0, 3, 3),
     931 + draw.RectChar('o'),
     932 + draw.RectCellOpts(cell.BgColor(cell.ColorGreen)),
     933 + )
     934 + testcanvas.MustApply(c, ft)
     935 + return ft
     936 + },
     937 + },
    844 938   }
    845 939   
    846 940   for _, tc := range tests {
    skipped 157 lines
  • ■ ■ ■ ■ ■ ■
    widgets/gauge/gaugedemo/gaugedemo.go
    skipped 100 lines
    101 101   gauge.Color(cell.ColorNumber(33)),
    102 102   gauge.Border(linestyle.Light),
    103 103   gauge.BorderTitle("Absolute progress"),
     104 + gauge.Threshold(43, linestyle.Light, cell.FgColor(cell.ColorRed)),
    104 105   )
    105 106   if err != nil {
    106 107   panic(err)
    skipped 17 lines
    124 125   gauge.Color(cell.ColorRed),
    125 126   gauge.FilledTextColor(cell.ColorBlack),
    126 127   gauge.EmptyTextColor(cell.ColorYellow),
     128 + gauge.Threshold(20, linestyle.Double),
    127 129   )
    128 130   if err != nil {
    129 131   panic(err)
    skipped 50 lines
  • ■ ■ ■ ■ ■ ■
    widgets/gauge/options.go
    skipped 46 lines
    47 47   borderCellOpts []cell.Option
    48 48   borderTitle string
    49 49   borderTitleHAlign align.Horizontal
     50 + // If set draws a vertical line representing the threshold.
     51 + threshold int
     52 + thresholdCellOpts []cell.Option
     53 + thresholdLineStyle linestyle.LineStyle
    50 54  }
    51 55   
    52 56  // newOptions returns options with the default values set.
    skipped 149 lines
    202 206   })
    203 207  }
    204 208   
     209 +// Threshold configures the Gauge to display a vertical threshold line at value
     210 +// t. If the progress is set by a call to Percent(), t represents a percentage,
     211 +// e.g. "40" means line is displayed at 40%. If the progress is set by a call to
     212 +// Absolute(), the threshold is also considered an absolute number.
     213 +// Threshold needs to be configured inside the valid values of a gauge to be
     214 +// displayed, if it's set to <=0 or >=total it won't have any effect.
     215 +func Threshold(t int, ls linestyle.LineStyle, cOpts ...cell.Option) Option {
     216 + return option(func(opts *options) {
     217 + opts.threshold = t
     218 + opts.thresholdLineStyle = ls
     219 + opts.thresholdCellOpts = cOpts
     220 + })
     221 +}
     222 + 
Please wait...
Page is in error, reload to recover