summaryrefslogtreecommitdiff
path: root/lib/textui
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:43:58 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:43:58 -0700
commitd675f41242c043ddc4c6c1a1fb8aabcfd324aae2 (patch)
tree4f2afbce761eb377ad0b0ab2e4fb2f478ff844f5 /lib/textui
parent9971e38110d5f90d15c7b78f396f2638b3952a96 (diff)
parent6e1a9fbb1e9a943e04902ed3a4958f6821e39456 (diff)
Merge branch 'lukeshu/lint'
Diffstat (limited to 'lib/textui')
-rw-r--r--lib/textui/log.go15
-rw-r--r--lib/textui/log_memstats.go2
-rw-r--r--lib/textui/log_test.go5
-rw-r--r--lib/textui/progress.go5
-rw-r--r--lib/textui/text.go12
-rw-r--r--lib/textui/text_test.go5
-rw-r--r--lib/textui/tunable.go13
7 files changed, 38 insertions, 19 deletions
diff --git a/lib/textui/log.go b/lib/textui/log.go
index 5ddc7a8..d2373f8 100644
--- a/lib/textui/log.go
+++ b/lib/textui/log.go
@@ -1,5 +1,5 @@
-// Copyright (C) 2019-2022 Ambassador Labs
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2019-2022-2023 Ambassador Labs
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: Apache-2.0
//
@@ -134,21 +134,21 @@ func (l *logger) Log(lvl dlog.LogLevel, msg string) {
// UnformattedLog implements dlog.OptimizedLogger.
func (l *logger) UnformattedLog(lvl dlog.LogLevel, args ...any) {
l.log(lvl, func(w io.Writer) {
- printer.Fprint(w, args...)
+ _, _ = printer.Fprint(w, args...)
})
}
// UnformattedLogln implements dlog.OptimizedLogger.
func (l *logger) UnformattedLogln(lvl dlog.LogLevel, args ...any) {
l.log(lvl, func(w io.Writer) {
- printer.Fprintln(w, args...)
+ _, _ = printer.Fprintln(w, args...)
})
}
// UnformattedLogf implements dlog.OptimizedLogger.
func (l *logger) UnformattedLogf(lvl dlog.LogLevel, format string, args ...any) {
l.log(lvl, func(w io.Writer) {
- printer.Fprintf(w, format, args...)
+ _, _ = printer.Fprintf(w, format, args...)
})
}
@@ -159,6 +159,7 @@ var (
)
func init() {
+ //nolint:dogsled // I can't change the signature of the stdlib.
_, file, _, _ := runtime.Caller(0)
thisModDir = filepath.Dir(filepath.Dir(filepath.Dir(file)))
}
@@ -171,8 +172,8 @@ func (l *logger) log(lvl dlog.LogLevel, writeMsg func(io.Writer)) {
// This is optimized for mostly-single-threaded usage. If I cared more
// about multi-threaded performance, I'd trade in some
// memory-use/allocations and (1) instead of using a static `logBuf`,
- // I'd have a `logBufPool` `sync.Pool`, and (2) have the the final call
- // to `l.out.Write()` be the only thing protected by `logMu`.
+ // I'd have a `logBufPool` `sync.Pool`, and (2) have the final call to
+ // `l.out.Write()` be the only thing protected by `logMu`.
logMu.Lock()
defer logMu.Unlock()
defer logBuf.Reset()
diff --git a/lib/textui/log_memstats.go b/lib/textui/log_memstats.go
index 39733c6..6e3c3a1 100644
--- a/lib/textui/log_memstats.go
+++ b/lib/textui/log_memstats.go
@@ -19,7 +19,7 @@ type LiveMemUse struct {
var _ fmt.Stringer = (*LiveMemUse)(nil)
-const liveMemUseUpdateInterval = 1 * time.Second
+var liveMemUseUpdateInterval = Tunable(1 * time.Second)
func (o *LiveMemUse) String() string {
o.mu.Lock()
diff --git a/lib/textui/log_test.go b/lib/textui/log_test.go
index 08eb38c..514d96e 100644
--- a/lib/textui/log_test.go
+++ b/lib/textui/log_test.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -20,6 +20,7 @@ func logLineRegexp(inner string) string {
}
func TestLogFormat(t *testing.T) {
+ t.Parallel()
var out strings.Builder
ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelTrace))
dlog.Debugf(ctx, "foo %d", 12345)
@@ -29,6 +30,7 @@ func TestLogFormat(t *testing.T) {
}
func TestLogLevel(t *testing.T) {
+ t.Parallel()
var out strings.Builder
ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelInfo))
dlog.Error(ctx, "Error")
@@ -54,6 +56,7 @@ func TestLogLevel(t *testing.T) {
}
func TestLogField(t *testing.T) {
+ t.Parallel()
var out strings.Builder
ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelInfo))
ctx = dlog.WithField(ctx, "foo", 12345)
diff --git a/lib/textui/progress.go b/lib/textui/progress.go
index 7b3f63a..68d986f 100644
--- a/lib/textui/progress.go
+++ b/lib/textui/progress.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -19,7 +19,7 @@ type Stats interface {
}
type Progress[T Stats] struct {
- ctx context.Context
+ ctx context.Context //nolint:containedctx // captured for separate goroutine
lvl dlog.LogLevel
interval time.Duration
@@ -56,6 +56,7 @@ func (p *Progress[T]) Done() {
}
func (p *Progress[T]) flush(force bool) {
+ //nolint:forcetypeassert // It wasn't worth it to me (yet?) to make a typed wrapper around atomic.Value.
cur := p.cur.Load().(T)
if !force && cur == p.oldStat {
return
diff --git a/lib/textui/text.go b/lib/textui/text.go
index d6a80b3..c0a3c5e 100644
--- a/lib/textui/text.go
+++ b/lib/textui/text.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -53,7 +53,7 @@ var (
// String implements fmt.Formatter.
func (h humanized) Format(f fmt.State, verb rune) {
- printer.Fprintf(f, fmtutil.FmtStateString(f, verb), h.val)
+ _, _ = printer.Fprintf(f, fmtutil.FmtStateString(f, verb), h.val)
}
// String implements fmt.Stringer.
@@ -72,9 +72,7 @@ type Portion[T constraints.Integer] struct {
N, D T
}
-var (
- _ fmt.Stringer = Portion[int]{}
-)
+var _ fmt.Stringer = Portion[int]{}
// String implements fmt.Stringer.
func (p Portion[T]) String() string {
@@ -146,7 +144,7 @@ func (v metric[T]) Format(f fmt.State, verb rune) {
if v.Val < 0 {
y = -y
}
- printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s",
+ _, _ = printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s",
y, prefix, v.Unit)
}
@@ -194,7 +192,7 @@ func (v iec[T]) Format(f fmt.State, verb rune) {
if v.Val < 0 {
y = -y
}
- printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s",
+ _, _ = printer.Fprintf(f, fmtutil.FmtStateString(f, verb)+"%s%s",
number.Decimal(y), prefix, v.Unit)
}
diff --git a/lib/textui/text_test.go b/lib/textui/text_test.go
index c4b42f6..4b93683 100644
--- a/lib/textui/text_test.go
+++ b/lib/textui/text_test.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -16,12 +16,14 @@ import (
)
func TestFprintf(t *testing.T) {
+ t.Parallel()
var out strings.Builder
textui.Fprintf(&out, "%d", 12345)
assert.Equal(t, "12,345", out.String())
}
func TestHumanized(t *testing.T) {
+ t.Parallel()
assert.Equal(t, "12,345", fmt.Sprint(textui.Humanized(12345)))
assert.Equal(t, "12,345 ", fmt.Sprintf("%-8d", textui.Humanized(12345)))
@@ -32,6 +34,7 @@ func TestHumanized(t *testing.T) {
}
func TestPortion(t *testing.T) {
+ t.Parallel()
assert.Equal(t, "100% (0/0)", fmt.Sprint(textui.Portion[int]{}))
assert.Equal(t, "0% (1/12,345)", fmt.Sprint(textui.Portion[int]{N: 1, D: 12345}))
assert.Equal(t, "100% (0/0)", fmt.Sprint(textui.Portion[btrfsvol.PhysicalAddr]{}))
diff --git a/lib/textui/tunable.go b/lib/textui/tunable.go
new file mode 100644
index 0000000..7e5f311
--- /dev/null
+++ b/lib/textui/tunable.go
@@ -0,0 +1,13 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package textui
+
+// Tunable annotates a value as something that might want to be tuned
+// as the program gets optimized.
+//
+// TODO(lukeshu): Have Tunable be runtime-configurable.
+func Tunable[T any](x T) T {
+ return x
+}