@@ -10,11 +10,11 @@ import (
1010 "io"
1111 "os"
1212 "path/filepath"
13+ "text/tabwriter"
1314
1415 "github.com/checkpoint-restore/checkpointctl/internal"
1516 metadata "github.com/checkpoint-restore/checkpointctl/lib"
1617 "github.com/checkpoint-restore/go-criu/v7/crit"
17- "github.com/olekukonko/tablewriter"
1818 "github.com/spf13/cobra"
1919)
2020
@@ -116,21 +116,16 @@ func memparse(cmd *cobra.Command, args []string) error {
116116
117117// Display processes memory sizes within the given container checkpoints.
118118func showProcessMemorySizeTables (tasks []internal.Task ) error {
119- // Initialize the table
120- table := tablewriter .NewWriter (os .Stdout )
121119 header := []string {
122120 "PID" ,
123121 "Process name" ,
124122 "Memory size" ,
125123 "Shared memory size" ,
126124 }
127- table .SetHeader (header )
128- table .SetAutoMergeCells (false )
129- table .SetRowLine (true )
130125
131126 // Function to recursively traverse the process tree and populate the table rows
132- var traverseTree func (* crit.PsTree , string ) error
133- traverseTree = func (root * crit.PsTree , checkpointOutputDir string ) error {
127+ var traverseTree func (* crit.PsTree , string , * [][] string ) error
128+ traverseTree = func (root * crit.PsTree , checkpointOutputDir string , rows * [][] string ) error {
134129 memReader , err := crit .NewMemoryReader (
135130 filepath .Join (checkpointOutputDir , metadata .CheckpointDirectory ),
136131 root .PID , pageSize ,
@@ -152,24 +147,25 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
152147 return err
153148 }
154149
155- table . Append ( []string {
150+ row := []string {
156151 fmt .Sprintf ("%d" , root .PID ),
157152 root .Comm ,
158153 metadata .ByteToString (memSize ),
159154 metadata .ByteToString (shmemSize ),
160- })
155+ }
156+ * rows = append (* rows , row )
161157
162158 for _ , child := range root .Children {
163- if err := traverseTree (child , checkpointOutputDir ); err != nil {
159+ if err := traverseTree (child , checkpointOutputDir , rows ); err != nil {
164160 return err
165161 }
166162 }
167163 return nil
168164 }
169165
170166 for _ , task := range tasks {
171- // Clear the table before processing each checkpoint task
172- table . ClearRows ()
167+ w := tabwriter . NewWriter ( os . Stdout , 0 , 0 , 2 , ' ' , 0 )
168+ var rows [][] string
173169
174170 c := crit .New (nil , nil , filepath .Join (task .OutputDir , "checkpoint" ), false , false )
175171 psTree , err := c .ExplorePs ()
@@ -178,12 +174,26 @@ func showProcessMemorySizeTables(tasks []internal.Task) error {
178174 }
179175
180176 // Populate the table rows
181- if err := traverseTree (psTree , task .OutputDir ); err != nil {
177+ if err := traverseTree (psTree , task .OutputDir , & rows ); err != nil {
182178 return err
183179 }
184180
185181 fmt .Printf ("\n Displaying processes memory sizes from %s\n \n " , task .CheckpointFilePath )
186- table .Render ()
182+
183+ internal .WriteTableHeader (w , header )
184+
185+ // Print rows
186+ for _ , row := range rows {
187+ for i , cell := range row {
188+ if i > 0 {
189+ fmt .Fprint (w , "\t " )
190+ }
191+ fmt .Fprint (w , cell )
192+ }
193+ fmt .Fprintln (w )
194+ }
195+
196+ w .Flush ()
187197 }
188198
189199 return nil
@@ -348,21 +358,27 @@ func printMemorySearchResultForPID(task internal.Task) error {
348358 return nil
349359 }
350360
351- table := tablewriter .NewWriter (os .Stdout )
352- table . SetHeader ( []string {"Address" , "Match" , "Instance" })
353- table . SetAutoMergeCells ( false )
354- table . SetRowLine ( true )
361+ w := tabwriter .NewWriter (os .Stdout , 0 , 0 , 2 , ' ' , 0 )
362+ header := []string {"Address" , "Match" , "Instance" }
363+
364+ internal . WriteTableHeader ( w , header )
355365
366+ // Print rows
356367 for i , result := range results {
357- table .Append ([]string {
358- fmt .Sprintf (
359- "%016x" , result .Vaddr ),
368+ row := []string {
369+ fmt .Sprintf ("%016x" , result .Vaddr ),
360370 result .Match ,
361371 fmt .Sprintf ("%d" , i + 1 ),
362- })
372+ }
373+ for j , cell := range row {
374+ if j > 0 {
375+ fmt .Fprint (w , "\t " )
376+ }
377+ fmt .Fprint (w , cell )
378+ }
379+ fmt .Fprintln (w )
363380 }
364381
365- table .Render ()
366-
382+ w .Flush ()
367383 return nil
368384}
0 commit comments