2 Commits

9 changed files with 200 additions and 183 deletions

View File

@@ -1,28 +1,12 @@
version: "2"
run:
tests: false
skip-dirs:
- data
- vendor
go: "1.24"
modules-download-mode: readonly
tests: false
allow-parallel-runners: true
go: '1.21'
output:
sort-results: true
linters-settings:
gosec:
excludes:
# not relevant here
- G404 # Insecure random number source (rand)
- G306 # Expect WriteFile permissions to be 0600 or less (gosec)
linters:
disable-all: true
default: none
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- asasalint
- asciicheck
- bodyclose
@@ -30,20 +14,20 @@ linters:
- contextcheck
- decorder
- durationcheck
- err113
- errcheck
- errchkjson
- errorlint
- exportloopref
- ginkgolinter
- gocheckcompilerdirectives
- gocritic
- godot
- goerr113
- gofmt
- goimports
- gosec
- gosmopolitan
- govet
- grouper
- importas
- ineffassign
- ireturn
- makezero
- mirror
@@ -59,8 +43,39 @@ linters:
- reassign
- revive
- rowserrcheck
- stylecheck
- staticcheck
- unconvert
- unused
- wastedassign
- whitespace
- wsl
settings:
gosec:
excludes:
- G404
- G306
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
paths:
- data
- vendor
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
- data
- vendor
- third_party$
- builtin$
- examples$

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env gmake -f
BUILDOPTS=-ldflags="-s -w" -a -gcflags=all=-l -trimpath
BUILDOPTS=-ldflags="-s -w" -a -gcflags=all=-l -trimpath -buildvcs=false
BINARY=i3status-go
TEST1=battery-test
@@ -13,6 +13,7 @@ build:
go build ${BUILDOPTS} -o ${BINARY} ./cmd/${BINARY}
battery-test:
rm -rf ${TEST1}
go build ${BUILDOPTS} -o ${TEST1} ./cmd/${TEST1}
## Remove binary with golang compiler' means

View File

@@ -11,13 +11,12 @@ import (
// Program entry point.
func main() {
for {
batteries, err := battery.GetAll()
batteries, _ := battery.GetAll()
if err != nil {
fmt.Printf("Unable to get battery info: %w", err)
for index, battery := range batteries {
fmt.Printf("Battery %d: %s\n", index, spew.Sdump(battery))
}
spew.Dump(batteries)
time.Sleep(1 * time.Second)
}
}

2
go.mod
View File

@@ -1,6 +1,6 @@
module i3status-go
go 1.22.6
go 1.24.2
require (
github.com/adrg/xdg v0.5.0

View File

@@ -11,155 +11,159 @@ import (
// UpdateBatteryInfo updates info about battery charge.
func (c *MyConfig) UpdateBatteryInfo() {
var (
ch int
status string
Batts string
ch int
status string
Batts string
InitialDelay = 100 * time.Millisecond
LoopIterationDelay = 5 * time.Second
)
for {
batteries, err := battery.GetAll()
Delay := InitialDelay
ticker := time.NewTicker(Delay)
if err != nil {
Batts = fmt.Sprintf(
"<span font='%s' size='%s'>%s</span><span font='%s' size='%s'> no batt</span>",
for range ticker.C {
if Delay == InitialDelay {
Delay = LoopIterationDelay
ticker.Reset(Delay)
}
batteries, _ := battery.GetAll()
// In theory, this module should give for each entry its separate err, but in practice it gives
// one single err for all entries, so we cannot detemine whist exatly entry errored.
var (
battsInfo string
chStr string
)
for i, b := range batteries {
switch b.State.Raw {
case battery.Charging:
status = `▲`
case battery.Discharging:
status = `▼`
case battery.Empty:
status = `✘`
default:
status = `•`
}
// N.B. there can be case when battery is overcharged and shows >100%. It also can indicate that
// calibration data is out of date and battery should be re-calibrated.
ch = int(math.Round((b.Full - (b.Full - b.Current)) * (100 / b.Full)))
switch {
case ch <= 500 && ch >= 84:
if c.Battery.ChargeColor.Full == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.Full,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
case ch < 85 && ch > 40:
if c.Battery.ChargeColor.AlmostFull == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.AlmostFull,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
case ch <= 40 && ch >= 10:
if c.Battery.ChargeColor.AlmostEmpty == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.AlmostEmpty,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
case ch < 10 && ch >= 0:
if c.Battery.ChargeColor.Empty == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.Empty,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
default:
continue
}
battsInfo += fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>%s</span><span color='%s' background='%s' font='%s' size='%s'>B%d </span>%s<span color='%s' background='%s' font='%s' size='%s'> %s</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.SymbolFont,
c.Battery.SymbolFontSize,
c.Battery.Symbol,
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
i,
chStr,
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
status,
)
} else {
var (
battsInfo string
chStr string
)
}
for i, b := range batteries {
switch b.State.Raw {
case battery.Charging:
status = `▲`
case battery.Discharging:
status = `▼`
case battery.Empty:
status = `✘`
default:
status = `•`
}
// N.B. there can be case when battery is overcharged and shows >100%. It also can indicate that
// calibration data is out of date and battery should be re-calibrated.
ch = int(math.Round((b.Full - (b.Full - b.Current)) * (100 / b.Full)))
switch {
case ch > 85:
if c.Battery.ChargeColor.Full == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.Full,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
case ch < 85 && ch > 40:
if c.Battery.ChargeColor.AlmostFull == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.AlmostFull,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
case ch <= 40 && ch >= 10:
if c.Battery.ChargeColor.AlmostEmpty == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.AlmostEmpty,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
case ch < 10:
if c.Battery.ChargeColor.Empty == "" {
chStr = fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
} else {
chStr = fmt.Sprintf(
`<span color='%s' background='%s' font='%s' size='%s'>% 3d%%</span>`,
c.Battery.ChargeColor.Empty,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
ch,
)
}
}
battsInfo += fmt.Sprintf(
"<span color='%s' background='%s' font='%s' size='%s'>%s</span><span color='%s' background='%s' font='%s' size='%s'>B%d </span>%s<span color='%s' background='%s' font='%s' size='%s'> %s</span>",
c.Battery.Color,
c.Battery.Background,
c.Battery.SymbolFont,
c.Battery.SymbolFontSize,
c.Battery.Symbol,
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
i,
chStr,
c.Battery.Color,
c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
status,
)
}
if battsInfo != "" {
Batts = battsInfo
}
if battsInfo != "" {
Batts = battsInfo
}
if c.Values.BatteryString != Batts {

View File

@@ -252,7 +252,7 @@ type MyConfig struct {
// readConf reads and validates config if config does not exist, it puts default config to the same dir where i3 config
// is located.
func ReadConf(DefaultConfig []byte) (*MyConfig, error) {
func ReadConf(defaultConfig []byte) (*MyConfig, error) {
var (
path string
config *MyConfig
@@ -272,7 +272,7 @@ func ReadConf(DefaultConfig []byte) (*MyConfig, error) {
// about it for now.
if err != nil {
if errors.Is(err, os.ErrNotExist) {
buf = DefaultConfig
buf = defaultConfig
if err := os.WriteFile(path, buf, 0644); err != nil {
return config, err
@@ -283,7 +283,7 @@ func ReadConf(DefaultConfig []byte) (*MyConfig, error) {
} else {
// Config file looks too long for config...
if fileInfo.Size() > 65535 {
err := fmt.Errorf("config file %s is too long for config", path) //nolint: goerr113
err := fmt.Errorf("config file %s is too long for config", path) //nolint: err113
return config, err
}
@@ -1530,13 +1530,11 @@ func ReadConf(DefaultConfig []byte) (*MyConfig, error) {
for num, app := range sampleConfig.Apps {
// app.Instance can be missing.
// app.Class can be missing.
if app.Name == "" {
app.Name = fmt.Sprintf("app%d", num)
}
// app.Args can be empty slice. In that case command will be run without aruments.
if app.Cmd == "" {
app.Cmd = "true"
}
@@ -1557,12 +1555,11 @@ func ReadConf(DefaultConfig []byte) (*MyConfig, error) {
app.BorderActive = sampleConfig.Color
}
// app.Separator can be omitted, in that case it is false
// app.SeparatorBlockWidth can be missing
if app.FullText == "" {
app.FullText = fmt.Sprintf(" %d ", num)
}
// app.Separator can be omitted, in that case it is false
// app.SeparatorBlockWidth can be missing
}
}

View File

@@ -152,10 +152,10 @@ func HasWindows(className string, instanceName string) bool {
func FindWindowInstance(regexpStr string) bool {
var found = false
f := func(key interface{}, value interface{}) bool {
f := func(key any, value any) bool { //nolint: revive
keyStr := reflect.ValueOf(key).String()
re, err := regexp.Compile(regexpStr) //nolint:nolintlint, gocritic, we do not want shit our pants and panic on
re, err := regexp.Compile(regexpStr) // we do not want shit our pants and panic on
// erroneous regexp, we want log it to stderr, that it.
if err != nil {
@@ -183,10 +183,10 @@ func FindWindowInstance(regexpStr string) bool {
func FindWindowClass(regexpStr string) bool {
var found = false
f := func(key interface{}, value interface{}) bool {
f := func(key any, value any) bool { //nolint: revive
keyStr := reflect.ValueOf(key).String()
re, err := regexp.Compile(regexpStr) //nolint:nolintlint, gocritic, we do not want shit our pants and panic on
re, err := regexp.Compile(regexpStr) // we do not want shit our pants and panic on
// erroneous regexp, we want log it to stderr, that it.
if err != nil {

View File

@@ -135,7 +135,7 @@ func (c *MyConfig) PaReinit() error {
}
if cnt >= 5 {
return errors.New("timeout waiting pulseaudio to exit") //nolint: goerr113
return errors.New("timeout waiting pulseaudio to exit") //nolint: err113
}
log.Print("Waiting for pulseaudio to exit")

View File

@@ -69,6 +69,7 @@ func (c *MyConfig) VPNTCPCheck() bool {
if err == nil {
_ = conn.Close()
return true
}