Skip to content

Commit 4b55b6f

Browse files
committed
add plain and json output formats
1 parent 3aba087 commit 4b55b6f

8 files changed

Lines changed: 202 additions & 121 deletions

File tree

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ should not be an exception.
88

99
In case you didn't know, `free` shows your memory usage on Unix-based systems.
1010
`frei` obtains memory data from `/proc/meminfo` and represents it in a colored
11-
bar chart using the same color-coding for as `htop`.
11+
bar chart (or other formats) using the same color-coding for as `htop`.
1212

1313
## Installation
1414

@@ -35,12 +35,12 @@ go install . # builds and install a binary to your $GOPATH
3535

3636
## Command-line options
3737

38-
| option | description |
39-
|------------|---------------------------------------------------|
40-
| `-help` | show list of options |
41-
| `-h` | human-readable numbers (implies `-table`) |
42-
| `-table` | print table with numbers in addition to the chart |
43-
| `-version` | print version and exit |
38+
| option | description |
39+
|--------------------|---------------------------------------------------|
40+
| `-help` | show list of options |
41+
| `-h` | human-readable numbers |
42+
| `-format=FORMAT` | output format (chart/table/charttable/plain/json) |
43+
| `-version` | print version and exit |
4444

4545
## Contributing
4646

@@ -58,4 +58,3 @@ currently not in use. "Available" (`MemAvailable` in `/proc/meminfo`), on the
5858
other side, is a more sophisticated estimation of how much memory is available
5959
for starting new applications, without swapping. It takes into account the page
6060
size and how much of reclaimable memory can actually be reclaimed.
61-

draw.go renamed to charts.go

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ func drawBar(width int, number float64) string {
4949
// }}}
5050

5151
// printCharts() {{{
52-
func printCharts(data DrawData, chartWidth int, barWidth int, stats MemData) {
52+
func printCharts(stats MemData) {
53+
chartWidth := getTerminalWidth() - 2
54+
barWidth := chartWidth - 4 - 5
55+
data := calcDrawData(stats, barWidth)
56+
5357
// head
5458
fmt.Println(" ╭" + strings.Repeat("─", chartWidth-2) + "╮")
5559

@@ -82,67 +86,3 @@ func printCharts(data DrawData, chartWidth int, barWidth int, stats MemData) {
8286
}
8387

8488
// }}}
85-
86-
// printNumbers() {{{
87-
func printTable(data MemData, chartWidth int, human bool) {
88-
if chartWidth > 40 {
89-
chartWidth = 40
90-
}
91-
92-
labels := [10]string{
93-
"Total",
94-
"Used",
95-
"Shared",
96-
"Buffers",
97-
"Cache",
98-
"Available",
99-
"Free",
100-
"Swap Total",
101-
"Swap Used",
102-
"Swap Free",
103-
}
104-
values := [10]string{
105-
toHumanStr(data.MemTotal, human),
106-
toHumanStr(data.MemUsed, human),
107-
toHumanStr(data.MemShared, human),
108-
toHumanStr(data.MemBuffers, human),
109-
toHumanStr(data.MemCached, human),
110-
toHumanStr(data.MemAvailable, human),
111-
toHumanStr(data.MemFree, human),
112-
toHumanStr(data.SwapTotal, human),
113-
toHumanStr(data.SwapUsed, human),
114-
toHumanStr(data.SwapFree, human),
115-
}
116-
colors := [10]string{
117-
"\033[1m",
118-
"\033[32m",
119-
"\033[35m",
120-
"\033[34m",
121-
"\033[33m",
122-
"",
123-
"",
124-
"",
125-
"",
126-
"",
127-
}
128-
129-
// head
130-
fmt.Println(" ╭─────────────┬" + strings.Repeat("─", chartWidth-16) + "╮")
131-
132-
// body
133-
for i := 0; i < len(labels); i++ {
134-
// separator before swap
135-
if i == len(labels)-3 {
136-
fmt.Println(" ├─────────────┼" + strings.Repeat("─", chartWidth-16) + "┤")
137-
}
138-
139-
spacesNumberLabel := 10 - len(labels[i])
140-
spacesNumberValue := chartWidth - 19 - len(values[i])
141-
fmt.Printf(" │ %s%s\033[0m%s │ %s %s │\n", colors[i], labels[i], strings.Repeat(" ", spacesNumberLabel), strings.Repeat(" ", spacesNumberValue), values[i])
142-
}
143-
144-
// tail
145-
fmt.Println(" ╰─────────────┴" + strings.Repeat("─", chartWidth-16) + "╯")
146-
}
147-
148-
// }}}

frei.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ frei [OPTIONS...]
1212

1313
.SS "-h"
1414
.PP
15-
Display human-readable numbers (implies `-table`)
15+
Display human-readable numbers
1616

1717
.SS "-help"
1818
.PP
1919
Print help message and exit
2020

21-
.SS "-table"
21+
.SS "-format=FORMAT"
2222
.PP
23-
Print table with numbers in addition to the chart
23+
Specifies the output format. Possible values: chart, table, charttable, plain, json. Default: chart
2424

2525
.SS "-version"
2626
.PP

json.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
func printJsonOutput(data MemData, human bool) {
9+
dataMap := map[string]string{
10+
"Total": toHumanStr(data.MemTotal, human),
11+
"Used": toHumanStr(data.MemUsed, human),
12+
"Shared": toHumanStr(data.MemShared, human),
13+
"Buffers": toHumanStr(data.MemBuffers, human),
14+
"Cache": toHumanStr(data.MemCached, human),
15+
"Available": toHumanStr(data.MemAvailable, human),
16+
"Free": toHumanStr(data.MemFree, human),
17+
"Swap Total": toHumanStr(data.SwapTotal, human),
18+
"Swap Used": toHumanStr(data.SwapUsed, human),
19+
"Swap Free": toHumanStr(data.SwapFree, human),
20+
}
21+
22+
dataJson, err := json.MarshalIndent(dataMap, "", " ")
23+
if err != nil {
24+
panic("Error marshaling json")
25+
}
26+
27+
fmt.Println(string(dataJson))
28+
}

main.go

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,13 @@ import (
77
"path/filepath"
88
)
99

10-
// types {{{
11-
type MemData struct {
12-
MemTotal float64
13-
MemUsed float64
14-
MemShared float64
15-
MemBuffers float64
16-
MemCached float64
17-
MemAvailable float64
18-
MemFree float64
19-
20-
SwapFree float64
21-
SwapUsed float64
22-
SwapTotal float64
23-
}
24-
25-
type DrawData struct {
26-
Buffers int
27-
Cache int
28-
Free int
29-
Shared int
30-
Used int
31-
32-
SwapFree int
33-
SwapUsed int
34-
}
35-
36-
type winsize struct {
37-
Row uint16
38-
Col uint16
39-
Xpixel uint16
40-
Ypixel uint16
41-
}
42-
43-
// }}}
44-
4510
// args {{{
4611
var (
4712
Version = "[built from source]"
4813
CommitSHA = ""
4914

50-
dispHuman = flag.Bool("h", false, "display human-readable numbers (implies -table)")
51-
dispTable = flag.Bool("table", false, "print table with numbers in addition to the chart")
15+
format = flag.String("format", "chart", "output format (chart/table/charttable/plain/json)")
16+
dispHuman = flag.Bool("h", false, "display human-readable numbers")
5217
dispVersion = flag.Bool("version", false, "display version and exit")
5318
)
5419

@@ -75,13 +40,19 @@ func main() {
7540
panic("Cannot get memory info")
7641
}
7742

78-
chartWidth := getTerminalWidth() - 2
79-
barWidth := chartWidth - 4 - 5
80-
drawData := calcDrawData(data, barWidth)
81-
82-
printCharts(drawData, chartWidth, barWidth, data)
83-
84-
if *dispTable || *dispHuman {
85-
printTable(data, chartWidth, *dispHuman)
43+
switch *format {
44+
case "chart":
45+
printCharts(data)
46+
case "table":
47+
printTable(data, *dispHuman)
48+
case "charttable":
49+
printCharts(data)
50+
printTable(data, *dispHuman)
51+
case "plain":
52+
printPlainTextOutput(data, *dispHuman)
53+
case "json":
54+
printJsonOutput(data, *dispHuman)
55+
default:
56+
panic("Invalid format specified. Check -help for usage")
8657
}
8758
}

plain.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func printPlainTextOutput(data MemData, human bool) {
9+
labels := []string{
10+
"Total",
11+
"Used",
12+
"Shared",
13+
"Buffers",
14+
"Cache",
15+
"Available",
16+
"Free",
17+
"Swap Total",
18+
"Swap Used",
19+
"Swap Free",
20+
}
21+
values := []string{
22+
toHumanStr(data.MemTotal, human),
23+
toHumanStr(data.MemUsed, human),
24+
toHumanStr(data.MemShared, human),
25+
toHumanStr(data.MemBuffers, human),
26+
toHumanStr(data.MemCached, human),
27+
toHumanStr(data.MemAvailable, human),
28+
toHumanStr(data.MemFree, human),
29+
toHumanStr(data.SwapTotal, human),
30+
toHumanStr(data.SwapUsed, human),
31+
toHumanStr(data.SwapFree, human),
32+
}
33+
34+
for i := 0; i < len(labels); i++ {
35+
fmt.Printf("%s:%s%s\n", labels[i], strings.Repeat(" ", (11-len(labels[i]))+(8-len(values[i]))), values[i])
36+
}
37+
}

table.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// printTable() {{{
9+
func printTable(data MemData, human bool) {
10+
chartWidth := getTerminalWidth() - 2
11+
if chartWidth > 40 {
12+
chartWidth = 40
13+
}
14+
15+
labels := [10]string{
16+
"Total",
17+
"Used",
18+
"Shared",
19+
"Buffers",
20+
"Cache",
21+
"Available",
22+
"Free",
23+
"Swap Total",
24+
"Swap Used",
25+
"Swap Free",
26+
}
27+
values := [10]string{
28+
toHumanStr(data.MemTotal, human),
29+
toHumanStr(data.MemUsed, human),
30+
toHumanStr(data.MemShared, human),
31+
toHumanStr(data.MemBuffers, human),
32+
toHumanStr(data.MemCached, human),
33+
toHumanStr(data.MemAvailable, human),
34+
toHumanStr(data.MemFree, human),
35+
toHumanStr(data.SwapTotal, human),
36+
toHumanStr(data.SwapUsed, human),
37+
toHumanStr(data.SwapFree, human),
38+
}
39+
colors := [10]string{
40+
"\033[1m",
41+
"\033[32m",
42+
"\033[35m",
43+
"\033[34m",
44+
"\033[33m",
45+
"",
46+
"",
47+
"",
48+
"",
49+
"",
50+
}
51+
52+
// head
53+
fmt.Println(" ╭─────────────┬" + strings.Repeat("─", chartWidth-16) + "╮")
54+
55+
// body
56+
for i := 0; i < len(labels); i++ {
57+
// separator before swap
58+
if i == len(labels)-3 {
59+
fmt.Println(" ├─────────────┼" + strings.Repeat("─", chartWidth-16) + "┤")
60+
}
61+
62+
spacesNumberLabel := 10 - len(labels[i])
63+
spacesNumberValue := chartWidth - 19 - len(values[i])
64+
fmt.Printf(" │ %s%s\033[0m%s │ %s %s │\n", colors[i], labels[i], strings.Repeat(" ", spacesNumberLabel), strings.Repeat(" ", spacesNumberValue), values[i])
65+
}
66+
67+
// tail
68+
fmt.Println(" ╰─────────────┴" + strings.Repeat("─", chartWidth-16) + "╯")
69+
}
70+
71+
// }}}

utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@ import (
77
"unsafe"
88
)
99

10+
// types {{{
11+
type MemData struct {
12+
MemTotal float64
13+
MemUsed float64
14+
MemShared float64
15+
MemBuffers float64
16+
MemCached float64
17+
MemAvailable float64
18+
MemFree float64
19+
20+
SwapFree float64
21+
SwapUsed float64
22+
SwapTotal float64
23+
}
24+
25+
type DrawData struct {
26+
Buffers int
27+
Cache int
28+
Free int
29+
Shared int
30+
Used int
31+
32+
SwapFree int
33+
SwapUsed int
34+
}
35+
36+
type winsize struct {
37+
Row uint16
38+
Col uint16
39+
Xpixel uint16
40+
Ypixel uint16
41+
}
42+
43+
// }}}
44+
1045
// getTerminalWidth() {{{
1146
func getTerminalWidth() int {
1247
ws := &winsize{}

0 commit comments

Comments
 (0)