Skip to content

Commit

Permalink
Code cleanup & decoupling
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Sep 25, 2020
1 parent 36efd9c commit 2f274da
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
38 changes: 23 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var (
jsonOutput = flag.Bool("json", false, "output all devices in JSON format")
)

func renderTables(m []Mount, sortCol int) error {
// renderTables renders all tables.
func renderTables(m []Mount, columns []int, sortCol int) error {
var local, network, fuse, special []Mount

// sort/filter devices
Expand Down Expand Up @@ -72,19 +73,6 @@ func renderTables(m []Mount, sortCol int) error {
local = append(local, v)
}

columns, err := parseColumns(*output)
if err != nil {
return err
}
if len(columns) == 0 {
// no columns supplied, use defaults
if *inodes {
columns = []int{1, 6, 7, 8, 9, 10, 11}
} else {
columns = []int{1, 2, 3, 4, 5, 10, 11}
}
}

// print tables
if !*hideLocal || *all {
printTable("local", local, sortCol, columns)
Expand All @@ -101,6 +89,7 @@ func renderTables(m []Mount, sortCol int) error {
return nil
}

// renderJSON encodes the JSON output and prints it.
func renderJSON(m []Mount) error {
output, err := json.MarshalIndent(m, "", " ")
if err != nil {
Expand All @@ -111,6 +100,7 @@ func renderJSON(m []Mount) error {
return nil
}

// parseColumns parses the supplied output flag into a slice of column indices.
func parseColumns(cols string) ([]int, error) {
var i []int

Expand All @@ -135,6 +125,21 @@ func parseColumns(cols string) ([]int, error) {
func main() {
flag.Parse()

// validate flags
columns, err := parseColumns(*output)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(columns) == 0 {
// no columns supplied, use defaults
if *inodes {
columns = []int{1, 6, 7, 8, 9, 10, 11}
} else {
columns = []int{1, 2, 3, 4, 5, 10, 11}
}
}

sortCol, err := stringToSortIndex(*sortBy)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -160,10 +165,12 @@ func main() {
os.Exit(1)
}

// print out warnings
for _, warning := range warnings {
fmt.Fprintln(os.Stderr, warning)
}

// print JSON
if *jsonOutput {
err := renderJSON(m)
if err != nil {
Expand All @@ -173,7 +180,8 @@ func main() {
return
}

err = renderTables(m, sortCol)
// print tables
err = renderTables(m, columns, sortCol)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
Expand Down
1 change: 1 addition & 0 deletions mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"golang.org/x/sys/unix"
)

// Mount contains all metadata for a single filesystem mount.
type Mount struct {
Device string `json:"device"`
DeviceType string `json:"device_type"`
Expand Down
15 changes: 12 additions & 3 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
colorCyan = term.Color("#66C2CD")
)

// printTable prints an individual table of mounts.
func printTable(title string, m []Mount, sortBy int, cols []int) {
tab := table.NewWriter()
tab.SetAllowedRowLength(int(*width))
Expand Down Expand Up @@ -138,10 +139,12 @@ func printTable(title string, m []Mount, sortBy int, cols []int) {
tab.Render()
}

// sizeTransformer makes a size human-readable.
func sizeTransformer(val interface{}) string {
return sizeToString(val.(uint64))
}

// spaceTransformer makes a size human-readable and applies a color coding.
func spaceTransformer(val interface{}) string {
free := val.(uint64)

Expand All @@ -158,6 +161,7 @@ func spaceTransformer(val interface{}) string {
return s.String()
}

// barTransformer transforms a percentage into a progress-bar.
func barTransformer(val interface{}) string {
usage := val.(float64)
s := termenv.String()
Expand Down Expand Up @@ -187,16 +191,19 @@ func barTransformer(val interface{}) string {
return s.String()
}

func inColumns(i []int, j int) bool {
for _, v := range i {
if v == j {
// inColumns return true if the column with index i is in the slice of visible
// columns cols.
func inColumns(cols []int, i int) bool {
for _, v := range cols {
if v == i {
return true
}
}

return false
}

// barWidth returns the width of progress-bars for the given render width.
func barWidth() int {
switch {
case *width < 100:
Expand All @@ -208,6 +215,7 @@ func barWidth() int {
}
}

// tableWidth returns the required minimum table width for the given columns.
func tableWidth(cols []int, separators bool) int {
var sw int
if separators {
Expand Down Expand Up @@ -274,6 +282,7 @@ func stringToSortIndex(s string) (int, error) {
return 0, fmt.Errorf("unknown column: %s (valid: %s)", s, strings.Join(columnIDs(), ", "))
}

// columnsIDs returns a slice of all column IDs.
func columnIDs() []string {
s := make([]string, len(columns))
for i, v := range columns {
Expand Down

0 comments on commit 2f274da

Please sign in to comment.