disk.go (1125B)
1 package disk 2 3 import ( 4 "context" 5 "fmt" 6 "os/exec" 7 "strings" 8 ) 9 10 // Config holds the configuration for the disk block. 11 type Config struct { 12 Mounts []string `json:"mounts"` 13 } 14 15 type Block struct { 16 mounts []string 17 } 18 19 func New(mounts ...string) *Block { 20 return &Block{mounts: mounts} 21 } 22 23 func (b *Block) Name() string { 24 return "disk" 25 } 26 27 func (b *Block) Render(ctx context.Context) (string, error) { 28 out, err := exec.CommandContext(ctx, "df", "-h").Output() 29 if err != nil { 30 return "", fmt.Errorf("exec df failed: %v", err) 31 } 32 33 var mounts = map[string]string{} 34 for i, l := range strings.Split(string(out), "\n") { 35 if i == 0 || len(l) == 0 { 36 continue 37 } 38 39 fields := strings.Fields(l) 40 if len(fields) < 6 { 41 continue 42 } 43 44 var icon rune = '' 45 switch fields[5] { 46 case "/": 47 icon = '/' 48 case "/home": 49 icon = '' 50 } 51 52 mounts[fields[5]] = fmt.Sprintf("%c %s", icon, fields[4]) 53 } 54 55 var output strings.Builder 56 for i, m := range b.mounts { 57 if s, exists := mounts[m]; exists { 58 output.WriteString(s) 59 if i < len(b.mounts)-1 { 60 output.WriteRune(' ') 61 } 62 } 63 } 64 65 return output.String(), nil 66 }