Show process args when ungrouped on unix

This commit is contained in:
Caleb Bassi 2018-05-16 14:38:28 -07:00
parent 0647a4b433
commit ee1c57c6fe
6 changed files with 23 additions and 7 deletions

2
Gopkg.lock generated
View File

@ -17,7 +17,7 @@
branch = "master"
name = "github.com/cjbassi/termui"
packages = ["."]
revision = "75525ee19c41f52324024b7a2bf31edcd1428494"
revision = "50e37fc8a80ca08ddca62be4537c7d49261994a3"
[[projects]]
branch = "master"

View File

@ -22,6 +22,7 @@ type Process struct {
Command string
CPU float64
Mem float64
Args string
}
type Proc struct {
@ -100,7 +101,7 @@ func (self *Proc) Sort() {
self.Header[3] += DOWN
}
self.Rows = FieldsToStrings(*processes)
self.Rows = FieldsToStrings(*processes, self.group)
}
// ColResize overrides the default ColResize in the termui table.
@ -220,6 +221,7 @@ func Group(P []Process) []Process {
val.Command,
val.CPU + process.CPU,
val.Mem + process.Mem,
"",
}
} else {
groupedP[process.Command] = Process{
@ -227,6 +229,7 @@ func Group(P []Process) []Process {
process.Command,
process.CPU,
process.Mem,
"",
}
}
}
@ -242,14 +245,18 @@ func Group(P []Process) []Process {
}
// FieldsToStrings converts a []Process to a [][]string
func FieldsToStrings(P []Process) [][]string {
func FieldsToStrings(P []Process, grouped bool) [][]string {
strings := make([][]string, len(P))
for i, p := range P {
strings[i] = make([]string, 4)
strings[i][0] = strconv.Itoa(int(p.PID))
if grouped {
strings[i][1] = p.Command
} else {
strings[i][1] = p.Args
}
strings[i][2] = fmt.Sprintf("%4s", strconv.FormatFloat(p.CPU, 'f', 1, 64))
strings[i][3] = fmt.Sprintf("%4s", strconv.FormatFloat(float64(p.Mem), 'f', 1, 32))
strings[i][3] = fmt.Sprintf("%4s", strconv.FormatFloat(float64(p.Mem), 'f', 1, 64))
}
return strings
}

View File

@ -22,7 +22,7 @@ func (self *Proc) update() {
}
func Processes() []Process {
output, _ := exec.Command("ps", "--no-headers", "-acxo", "pid,comm,pcpu,pmem").Output()
output, _ := exec.Command("ps", "--no-headers", "-axo", "pid,comm,pcpu,pmem,args").Output()
strOutput := strings.TrimSpace(string(output))
processes := []Process{}
for _, line := range strings.Split(strOutput, "\n") {
@ -35,6 +35,7 @@ func Processes() []Process {
Command: split[1],
CPU: cpu,
Mem: mem,
Args: strings.Join(split[4:], " "),
}
processes = append(processes, process)
}

View File

@ -18,6 +18,9 @@ func (self *Proc) update() {
command,
cpu / self.cpuCount,
float64(mem),
// getting command args using gopsutil's Cmdline and CmdlineSlice wasn't
// working the last time I tried it, so we're just reusing 'command'
command,
}
}

View File

@ -1,6 +1,7 @@
package termui
// Color is an integer in the range -1 to 255.
// -1 is clear, while 0-255 are xterm 256 colors.
type Color int
// ColorDefault = clear

View File

@ -106,7 +106,11 @@ func (self *Table) Buffer() *Buffer {
if width == 0 {
continue
}
r := MaxString(row[i], self.X-self.CellXPos[i])
width = self.X - self.CellXPos[i]
if len(self.CellXPos) > i+1 {
width -= self.X - self.CellXPos[i+1]
}
r := MaxString(row[i], width)
buf.SetString(self.CellXPos[i], y, r, self.Fg, bg)
}
}