Compare commits
2 commits
01e3c3aade
...
2d70d8994e
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d70d8994e | |||
| 2552adbd3e |
4 changed files with 120 additions and 24 deletions
14
Taskfile.yml
14
Taskfile.yml
|
|
@ -25,12 +25,26 @@ tasks:
|
||||||
- task: cmd-build
|
- task: cmd-build
|
||||||
vars:
|
vars:
|
||||||
CMD: convids
|
CMD: convids
|
||||||
|
build-cool-down:
|
||||||
|
desc: Builds the cool-down command
|
||||||
|
source:
|
||||||
|
- cmd/cool-down/**/*.go
|
||||||
|
- chill/*.go
|
||||||
|
- cli/err/*.go
|
||||||
|
- cli/spin/*.go
|
||||||
|
generates:
|
||||||
|
- build/cool-down
|
||||||
|
cmds:
|
||||||
|
- task: cmd-build
|
||||||
|
vars:
|
||||||
|
CMD: cool-down
|
||||||
build-all:
|
build-all:
|
||||||
desc: Builds all available commands
|
desc: Builds all available commands
|
||||||
sources:
|
sources:
|
||||||
- "**/*.go"
|
- "**/*.go"
|
||||||
generates:
|
generates:
|
||||||
- build/convids
|
- build/convids
|
||||||
|
- build/cool-down
|
||||||
cmds:
|
cmds:
|
||||||
- task: cmd-build
|
- task: cmd-build
|
||||||
vars:
|
vars:
|
||||||
|
|
|
||||||
47
chill/chill.go
Normal file
47
chill/chill.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package chill
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var TempFile string = "/sys/class/thermal/thermal_zone2/temp"
|
||||||
|
var MaxTemp int = 72500
|
||||||
|
var Sleep = time.Second * 10
|
||||||
|
var ErrChilledOut = errors.New("temperature dropped below threshold")
|
||||||
|
|
||||||
|
func Chill(baseCtx context.Context) context.Context {
|
||||||
|
ctx, cancel := context.WithCancelCause(baseCtx)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
var by []byte
|
||||||
|
var err error
|
||||||
|
current := MaxTemp + 1
|
||||||
|
|
||||||
|
for current >= MaxTemp {
|
||||||
|
by, err = os.ReadFile(TempFile)
|
||||||
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
by = bytes.TrimSpace(by)
|
||||||
|
current, err = strconv.Atoi(string(by))
|
||||||
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if current < MaxTemp {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(Sleep)
|
||||||
|
}
|
||||||
|
cancel(ErrChilledOut)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
@ -58,28 +58,47 @@ func (m model) View() string {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
func Spin(ctx context.Context, message string) (chan string, chan error) {
|
type Spinner interface {
|
||||||
p := tea.NewProgram(newModel(message), tea.WithContext(ctx))
|
Wait() error
|
||||||
|
SetMessage(string)
|
||||||
msgChan := make(chan string, 10)
|
Err() error
|
||||||
errChan := make(chan error)
|
}
|
||||||
|
|
||||||
go func() {
|
var _ Spinner = new(spin)
|
||||||
for {
|
|
||||||
m := <-msgChan
|
type spin struct {
|
||||||
p.Send(textMessage(m))
|
p *tea.Program
|
||||||
}
|
err error
|
||||||
}()
|
finished chan struct{}
|
||||||
go func() {
|
}
|
||||||
_, err := p.Run()
|
|
||||||
if errors.Is(err, context.Canceled) {
|
func (s *spin) Wait() error {
|
||||||
err = nil
|
<-s.finished
|
||||||
} else if errors.Is(err, context.DeadlineExceeded) {
|
return s.Err()
|
||||||
err = nil
|
}
|
||||||
} else if errors.Is(err, tea.ErrProgramKilled) {
|
|
||||||
err = nil
|
func (s *spin) SetMessage(msg string) {
|
||||||
}
|
s.p.Send(textMessage(msg))
|
||||||
errChan <- err
|
}
|
||||||
}()
|
|
||||||
return msgChan, errChan
|
func (s *spin) Err() error {
|
||||||
|
if errors.Is(s.err, context.Canceled) {
|
||||||
|
return nil
|
||||||
|
} else if errors.Is(s.err, context.DeadlineExceeded) {
|
||||||
|
return nil
|
||||||
|
} else if errors.Is(s.err, tea.ErrProgramKilled) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Spin(ctx context.Context, message string) Spinner {
|
||||||
|
p := tea.NewProgram(newModel(message), tea.WithContext(ctx))
|
||||||
|
s := &spin{p: p, finished: make(chan struct{}, 1)}
|
||||||
|
go func() {
|
||||||
|
_, err := s.p.Run()
|
||||||
|
s.err = err
|
||||||
|
s.finished <- struct{}{}
|
||||||
|
}()
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
cmd/cool-down/main.go
Normal file
16
cmd/cool-down/main.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"codeberg.org/danjones000/utils/chill"
|
||||||
|
e "codeberg.org/danjones000/utils/cli/err"
|
||||||
|
"codeberg.org/danjones000/utils/cli/spin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx := chill.Chill(context.Background())
|
||||||
|
s := spin.Spin(ctx, "Waiting for CPU to cool...")
|
||||||
|
err := s.Wait()
|
||||||
|
e.HandleErr(err)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue