forked from tarantool/tt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
status: make the output of the command as a table
Made the output of the status command as a table. Have done it using tabwriter from the standard library. Since tabwriter does not support ANSI escape codes, I need to remove extra spaces from the table header to use colored text in the second column of the table. Closes tarantool#197
- Loading branch information
Showing
8 changed files
with
146 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package status | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/fatih/color" | ||
"github.com/tarantool/tt/cli/process_utils" | ||
"github.com/tarantool/tt/cli/running" | ||
) | ||
|
||
const ( | ||
// colorBytesNumber is the number of bytes added to the colorized string. | ||
colorBytesNumber = 9 | ||
|
||
// padding is the padding between two columns of the table. | ||
padding = 5 | ||
) | ||
|
||
var header = []string{"INSTANCE", "STATUS", "PID"} | ||
|
||
// Status writes the status as a table. | ||
func Status(runningCtx running.RunningCtx) error { | ||
instColWidth := 0 | ||
sb := strings.Builder{} | ||
tw := tabwriter.NewWriter(&sb, 0, 1, padding, ' ', 0) | ||
|
||
fmt.Fprintln(tw, strings.Join(header, "\t")) | ||
for _, run := range runningCtx.Instances { | ||
fullInstanceName := running.GetAppInstanceName(run) | ||
procStatus := running.Status(&run) | ||
if len(fullInstanceName) > instColWidth { | ||
instColWidth = len(fullInstanceName) | ||
} | ||
|
||
fmt.Fprintf(tw, "%s\t%s\t", fullInstanceName, | ||
procStatus.ColorSprint(procStatus.Status)) | ||
if procStatus.Code == process_utils.ProcessRunningCode { | ||
fmt.Fprintf(tw, "%d", procStatus.PID) | ||
} | ||
fmt.Fprintf(tw, "\n") | ||
} | ||
|
||
if err := tw.Flush(); err != nil { | ||
return err | ||
} | ||
|
||
rawOutput := sb.String() | ||
rawHeader, rest, _ := strings.Cut(rawOutput, "\n") | ||
|
||
// Calculating the position of the `status` end in the header. | ||
statusOffset := instColWidth + padding + len(header[1]) | ||
fmt.Print(rawHeader[:statusOffset]) | ||
|
||
var toSkip int | ||
if len(runningCtx.Instances) > 0 && !color.NoColor { | ||
// We need to skip the spaces that appear | ||
// as a result of using color bytes, if any. | ||
toSkip = colorBytesNumber | ||
} | ||
fmt.Println(rawHeader[statusOffset+toSkip:]) | ||
fmt.Print(rest) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters