func PadAndColorInstanceState(instanceInfo app_examiner.InstanceInfo) string { padLength := 0 if len(ColorInstanceState(instanceInfo)) < len(colors.NoColor("UNCLAIMED")) { padLength = len("UNCLAIMED") - len(instanceInfo.State) } return fmt.Sprintf("%s%s", ColorInstanceState(instanceInfo), strings.Repeat(" ", padLength)) }
func (factory *AppExaminerCommandFactory) printInstanceSummary(writer io.Writer, actualInstances []app_examiner.InstanceInfo) { w := tabwriter.NewWriter(writer, minColumnWidth, 8, 1, '\t', 0) printHorizontalRule(w, "=") fmt.Fprintf(w, fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s\n", "Instance", colors.NoColor("State")+" ", "Crashes", "CPU", "Memory", "Uptime"), ) printHorizontalRule(w, "-") for _, instance := range actualInstances { metricsSlice := []string{"N/A", "N/A"} if instance.HasMetrics { metricsSlice = []string{ fmt.Sprintf("%.2f%%", instance.Metrics.CpuPercentage), fmt.Sprintf("%s", bytefmt.ByteSize(instance.Metrics.MemoryBytes)), } } if instance.PlacementError == "" && instance.State != "CRASHED" { uptime := factory.clock.Now().Sub(time.Unix(0, instance.Since)) roundedUptime := uptime - (uptime % time.Second) fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", strconv.Itoa(instance.Index), presentation.PadAndColorInstanceState(instance), strconv.Itoa(instance.CrashCount), strings.Join(metricsSlice, "\t"), fmt.Sprint(roundedUptime), ) } else { fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", strconv.Itoa(instance.Index), presentation.PadAndColorInstanceState(instance), strconv.Itoa(instance.CrashCount), strings.Join(metricsSlice, "\t"), "N/A", ) } } w.Flush() }
State: "COMPLETED", }, } fakeTaskExaminer.ListTasksReturns(listTasks, nil) test_helpers.ExecuteCommandWithArgs(listAppsCommand, []string{}) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("App Name"))) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("Instances"))) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("DiskMB"))) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("MemoryMB"))) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("Route"))) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("process1"))) Expect(outputBuffer).To(test_helpers.Say(colors.Red("0/21"))) Expect(outputBuffer).To(test_helpers.Say(colors.NoColor("100"))) Expect(outputBuffer).To(test_helpers.Say(colors.NoColor("50"))) Expect(outputBuffer).To(test_helpers.Say("alldaylong.com => 54321")) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("process2"))) Expect(outputBuffer).To(test_helpers.Say(colors.Yellow("9/8"))) Expect(outputBuffer).To(test_helpers.Say(colors.NoColor("400"))) Expect(outputBuffer).To(test_helpers.Say(colors.NoColor("30"))) Expect(outputBuffer).To(test_helpers.Say("never.io => 1234")) Expect(outputBuffer).To(test_helpers.Say(colors.Bold("process3"))) Expect(outputBuffer).To(test_helpers.Say(colors.Green("5/5"))) Expect(outputBuffer).To(test_helpers.Say(colors.NoColor("600"))) Expect(outputBuffer).To(test_helpers.Say(colors.NoColor("90"))) Expect(outputBuffer).To(test_helpers.Say("allthetime.com => 1234, herewego.org => 1234"))
func (factory *AppExaminerCommandFactory) listApps(context *cli.Context) { appList, err := factory.appExaminer.ListApps() if err == nil { w := &tabwriter.Writer{} w.Init(factory.ui, 10+colors.ColorCodeLength, 8, 1, '\t', 0) appTableHeader := strings.Repeat("-", 30) + "= Apps =" + strings.Repeat("-", 31) fmt.Fprintln(w, appTableHeader) if len(appList) != 0 { header := fmt.Sprintf("%s\t%s\t%s\t%s\t%s", colors.Bold("App Name"), colors.Bold("Instances"), colors.Bold("DiskMB"), colors.Bold("MemoryMB"), colors.Bold("Route")) fmt.Fprintln(w, header) for _, appInfo := range appList { displayedRoutes := factory.getRoutes(appInfo) fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", colors.Bold(appInfo.ProcessGuid), colorInstances(appInfo), colors.NoColor(strconv.Itoa(appInfo.DiskMB)), colors.NoColor(strconv.Itoa(appInfo.MemoryMB)), colors.Cyan(displayedRoutes)) } } else { fmt.Fprintf(w, "No apps to display."+"\n") } w.Flush() } else { factory.ui.SayLine("Error listing apps: " + err.Error()) factory.exitHandler.Exit(exit_codes.CommandFailed) } taskList, err := factory.taskExaminer.ListTasks() if err == nil { wTask := &tabwriter.Writer{} wTask.Init(factory.ui, 10+colors.ColorCodeLength, 8, 1, '\t', 0) factory.ui.SayNewLine() taskTableHeader := strings.Repeat("-", 30) + "= Tasks =" + strings.Repeat("-", 30) fmt.Fprintln(wTask, taskTableHeader) if len(taskList) != 0 { taskHeader := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t", colors.Bold("Task Name"), colors.Bold("Cell ID"), colors.Bold("Status"), colors.Bold("Result"), colors.Bold("Failure Reason")) fmt.Fprintln(wTask, taskHeader) for _, taskInfo := range taskList { if taskInfo.CellID == "" { taskInfo.CellID = "N/A" } if taskInfo.Result == "" { taskInfo.Result = "N/A" } if taskInfo.FailureReason == "" { taskInfo.FailureReason = "N/A" } coloumnInfo := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t", colors.Bold(taskInfo.TaskGuid), colors.NoColor(taskInfo.CellID), colors.NoColor(taskInfo.State), colors.NoColor(taskInfo.Result), colors.NoColor(taskInfo.FailureReason)) fmt.Fprintln(wTask, coloumnInfo) } } else { fmt.Fprintf(wTask, "No tasks to display.\n") } wTask.Flush() } else { factory.ui.SayLine("Error listing tasks: " + err.Error()) factory.exitHandler.Exit(exit_codes.CommandFailed) } }
}) itShouldNotColorizeWhitespace(colors.Gray) }) Describe("Bold", func() { It("adds the bold color code", func() { Expect(colors.Bold("Bold")).To(Equal("\x1b[1mBold\x1b[0m")) }) itShouldNotColorizeWhitespace(colors.Bold) }) Describe("PurpleUnderline", func() { It("adds the purple underlined color code", func() { Expect(colors.PurpleUnderline("PURPLE UNDERLINE")).To(Equal("\x1b[35;4mPURPLE UNDERLINE\x1b[0m")) }) itShouldNotColorizeWhitespace(colors.PurpleUnderline) }) Describe("NoColor", func() { It("adds no color code", func() { Expect(colors.NoColor("None")).To(Equal("\x1b[0mNone\x1b[0m")) }) itShouldNotColorizeWhitespace(colors.NoColor) }) })