src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

temp.go (903B)


      1 package temp
      2 
      3 import (
      4 	"context"
      5 	"encoding/json"
      6 	"fmt"
      7 	"os/exec"
      8 )
      9 
     10 type Block struct{}
     11 
     12 func New() *Block {
     13 	return &Block{}
     14 }
     15 
     16 func (b *Block) Name() string {
     17 	return "temp"
     18 }
     19 
     20 func (b *Block) Render(ctx context.Context) (string, error) {
     21 	out, err := exec.CommandContext(ctx, "sensors", "-j").Output()
     22 	if err != nil {
     23 		return "", fmt.Errorf("exec sensors failed: %v", err)
     24 	}
     25 
     26 	var sensors = struct {
     27 		Thinkpad struct {
     28 			CPU struct {
     29 				Temp float64 `json:"temp1_input"`
     30 			} `json:"CPU"`
     31 		} `json:"thinkpad-isa-0000"`
     32 		NVME struct {
     33 			Composite struct {
     34 				Temp float64 `json:"temp1_input"`
     35 			} `json:"Composite"`
     36 		} `json:"nvme-pci-0400"`
     37 	}{}
     38 	if err := json.Unmarshal(out, &sensors); err != nil {
     39 		return "", fmt.Errorf("failed to json unmarshal: %v", err)
     40 	}
     41 
     42 	return fmt.Sprintf(
     43 		" %.0f℃  %.0f℃",
     44 		sensors.Thinkpad.CPU.Temp, sensors.NVME.Composite.Temp,
     45 	), nil
     46 }