@@ -3,6 +3,7 @@ package main
33import (
44 "fmt"
55 "strings"
6+ "time"
67
78 "github.com/jroimartin/gocui"
89 "github.com/willf/pad"
@@ -72,48 +73,79 @@ func viewPods(g *gocui.Gui, lMaxX int, lMaxY int) error {
7273 // Set as current view
7374 g .SetCurrentView (v .Name ())
7475
75- // Content: Add column
76- //viewPodsAddLine(v, lMaxX, "NAME", "CPU", "MEMORY", "READY", "STATUS", "RESTARTS", "AGE") // TODO CPU + Memory
77- viewPodsAddLine (v , lMaxX , "NAME" , "READY" , "STATUS" , "RESTARTS" , "AGE" )
78- fmt .Fprintln (v , strings .Repeat ("─" , lMaxX ))
76+ // Content
77+ go viewPodsActualize (g , lMaxX )
78+ go viewPodsAutoRefresh (g , lMaxX )
79+ }
80+
81+ return nil
82+ }
83+
84+ // Auto refresh view pods
85+ func viewPodsAutoRefresh (g * gocui.Gui , lMaxX int ) {
86+ c := getConfig ()
87+ t := time .NewTicker (time .Duration (c .frequency ) * time .Second )
88+ for {
89+ select {
90+ case <- t .C :
91+ debug (g , fmt .Sprintf ("View pods: Refreshing (%ds)" , c .frequency ))
92+ go viewPodsActualize (g , lMaxX )
93+ }
94+ }
95+ }
96+
97+ // Actualize pods view
98+ func viewPodsActualize (g * gocui.Gui , lMaxX int ) {
99+ g .Execute (func (g * gocui.Gui ) error {
100+ debug (g , "View pods: Actualize" )
101+ v , err := g .View ("pods" )
102+ if err != nil {
103+ return err
104+ }
79105
80- // Content: Add lines
81- // TODO Use goroutine
82106 pods , err := getPods ()
83107 if err != nil {
84108 panic (err .Error ())
85109 }
110+
111+ v .Clear ()
112+
113+ // Content: Add column
114+ //viewPodsAddLine(v, lMaxX, "NAME", "CPU", "MEMORY", "READY", "STATUS", "RESTARTS", "AGE") // TODO CPU + Memory #20
115+ viewPodsAddLine (v , lMaxX , "NAME" , "READY" , "STATUS" , "RESTARTS" , "AGE" )
116+ fmt .Fprintln (v , strings .Repeat ("─" , lMaxX ))
117+
86118 if len (pods .Items ) > 0 {
87- debug (g , fmt .Sprintf ("There are %d pods in the cluster " , len (pods .Items )))
119+ debug (g , fmt .Sprintf ("View pods: %d pods found " , len (pods .Items )))
88120 for _ , pod := range pods .Items {
89121 n := pod .GetName ()
90- //c := "?" // TODO CPU + Memory
91- //m := "?" // TODO CPU + Memory
122+ //c := "?" // TODO CPU + Memory #20
123+ //m := "?" // TODO CPU + Memory #20
92124 s := columnHelperStatus (pod .Status )
93125 r := columnHelperRestarts (pod .Status .ContainerStatuses )
94126 a := columnHelperAge (pod .CreationTimestamp )
95127 cr := columnHelperReady (pod .Status .ContainerStatuses )
96128 viewPodsAddLine (v , lMaxX , n , cr , s , r , a )
97- //viewPodsAddLine(v, lMaxX, n, c, m, cr, s, r, a) // TODO CPU + Memory
129+ //viewPodsAddLine(v, lMaxX, n, c, m, cr, s, r, a) // TODO CPU + Memory #20
98130 }
99131 } else {
100- debug (g , "Pods not found. " )
132+ debug (g , "View pods: Pods not found" )
101133 }
102- }
103134
104- return nil
135+ return nil
136+ })
105137}
106138
107139// Add line to view pods
108- //func viewPodsAddLine(v *gocui.View, maxX int, name, cpu, memory, ready, status, restarts, age string) { // TODO CPU + Memory
140+ //func viewPodsAddLine(v *gocui.View, maxX int, name, cpu, memory, ready, status, restarts, age string) { // TODO CPU + Memory #20
109141func viewPodsAddLine (v * gocui.View , maxX int , name , ready , status , restarts , age string ) {
110- wN := maxX - 34 // 54 // TODO CPU + Memory
142+ wN := maxX - 34 // 54 // TODO CPU + Memory #20
111143 if wN < 45 {
112144 wN = 45
113145 }
114146 line := pad .Right (name , wN , " " ) +
115- //pad.Right(cpu, 10, " ") + // TODO CPU + Memory
116- //pad.Right(memory, 10, " ") + // TODO CPU + Memory
147+ //pad.Right(cpu, 10, " ") + // TODO CPU + Memory #20
148+ //pad.Right(memory, 10, " ") + // TODO CPU + Memory #20
117149 pad .Right (ready , 10 , " " ) +
118150 pad .Right (status , 10 , " " ) +
119151 pad .Right (restarts , 10 , " " ) +
0 commit comments