Commit 4e3f26c
Changed files (4)
cmd/list/list_volumes.go
@@ -2,38 +2,77 @@ package list
import (
"fmt"
+ "os"
+ "strings"
+
+ "github.com/jedib0t/go-pretty/v6/table"
human "github.com/dustin/go-humanize"
+ "github.com/kahnwong/swissknife/color"
"github.com/shirou/gopsutil/v4/disk"
"github.com/spf13/cobra"
)
-func listVolumes() string {
+func listVolumes() {
// ref: <https://stackoverflow.com/a/64141403>
- formatter := "%-14s %7s %7s %7s %4s %s\n"
- fmt.Printf(formatter, "Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on")
+ // layout inspired by `duf`
+
+ // init table
+ t := table.NewWriter()
+ t.SetOutputMirror(os.Stdout)
+ t.AppendHeader(table.Row{"Mounted on", "Size", "Used", "Avail", "Use%", "Type", "Filesystem"})
+ // get volumes info
partitions, _ := disk.Partitions(false)
for _, partition := range partitions {
- device := partition.Mountpoint
- stats, _ := disk.Usage(device)
+ if partition.Fstype != "squashfs" && !strings.Contains(partition.Mountpoint, "snap") { // for example: snap partitions on ubuntu
+ device := partition.Mountpoint
+ stats, _ := disk.Usage(device)
- if stats.Total == 0 {
- continue
- }
+ if stats.Total == 0 {
+ continue
+ }
- percent := fmt.Sprintf("%2.f%%", stats.UsedPercent)
+ // disk available
+ diskAvail := stats.Free
+ diskAvailStr := ""
+ if diskAvail < 50*1024*1024*1024 { // if free space less than 50 GB
+ diskAvailStr = color.Red(human.Bytes(diskAvail))
+ } else if diskAvail < 100*1024*1024*1024 { // if free space less than 100 GB
+ diskAvailStr = color.Yellow(human.Bytes(diskAvail))
+ } else {
+ diskAvailStr = color.Green(human.Bytes(diskAvail))
+ }
- fmt.Printf(formatter,
- stats.Fstype,
- human.Bytes(stats.Total),
- human.Bytes(stats.Used),
- human.Bytes(stats.Free),
- percent,
- partition.Mountpoint,
- )
+ // disk use percent
+ percent := fmt.Sprintf("%2.f%%", stats.UsedPercent)
+ percentRaw := stats.UsedPercent
+ percentStr := ""
+ if percentRaw > 80 {
+ percentStr = color.Red(percent)
+ } else if percentRaw > 70 {
+ percentStr = color.Yellow(percent)
+ } else {
+ percentStr = color.Green(percent)
+ }
+
+ // append info to table
+ t.AppendRows([]table.Row{
+ {
+ color.Blue(partition.Mountpoint),
+ human.Bytes(stats.Total),
+ human.Bytes(stats.Used),
+ diskAvailStr,
+ percentStr,
+ color.Magenta(partition.Fstype),
+ color.Magenta(stats.Fstype),
+ },
+ })
+ }
}
- return "foo"
+
+ // render
+ t.Render()
}
var listVolumesCmd = &cobra.Command{
color/constants.go
@@ -3,7 +3,9 @@ package color
import "github.com/fatih/color"
var (
- Blue = color.New(color.FgBlue).SprintFunc()
- Green = color.New(color.FgHiGreen).SprintFunc()
- Red = color.New(color.FgRed).SprintFunc()
+ Blue = color.New(color.FgBlue).SprintFunc()
+ Green = color.New(color.FgHiGreen).SprintFunc()
+ Magenta = color.New(color.FgHiMagenta).SprintFunc()
+ Red = color.New(color.FgRed).SprintFunc()
+ Yellow = color.New(color.FgYellow).SprintFunc()
)
go.mod
@@ -10,6 +10,7 @@ require (
github.com/charmbracelet/lipgloss v1.0.0
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.18.0
+ github.com/jedib0t/go-pretty/v6 v6.6.2
github.com/libp2p/go-netroute v0.2.2
github.com/rs/zerolog v1.33.0
github.com/sethvargo/go-diceware v0.4.0
go.sum
@@ -38,6 +38,8 @@ github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/jedib0t/go-pretty/v6 v6.6.2 h1:27bLj3nRODzaiA7tPIxy9UVWHoPspFfME9XxgwiiNsM=
+github.com/jedib0t/go-pretty/v6 v6.6.2/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/libp2p/go-netroute v0.2.2 h1:Dejd8cQ47Qx2kRABg6lPwknU7+nBnFRpko45/fFPuZ8=
github.com/libp2p/go-netroute v0.2.2/go.mod h1:Rntq6jUAH0l9Gg17w5bFGhcC9a+vk4KNXs6s7IljKYE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=