From 4de4a28d182181fb34a30d545c92728f84b37be7 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Fri, 27 Sep 2019 15:40:03 -0500 Subject: [PATCH 1/3] Add phase banners to the tops of each phase --- cmd/exec/exec.go | 1 + cmd/models/onebuild-execution-plan.go | 37 ++++++++++++++++++++++-- testing/fixtures/execute_cmd_fixtures.go | 11 +++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 4efb7fac..9ebab327 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -41,6 +41,7 @@ func ExecutePlan(commands ...string) { } func executeAndStopIfFailed(command *models.CommandContext) { + command.PrintBanner() err := command.CommandSession.Run() if err != nil { exitCode := (err.Error())[12:] diff --git a/cmd/models/onebuild-execution-plan.go b/cmd/models/onebuild-execution-plan.go index 5a907b5e..92540556 100644 --- a/cmd/models/onebuild-execution-plan.go +++ b/cmd/models/onebuild-execution-plan.go @@ -2,11 +2,13 @@ package models import ( "fmt" - "github.com/codeskyblue/go-sh" - "github.com/gopinath-langote/1build/cmd/utils" "os" "strings" "text/tabwriter" + + "github.com/codeskyblue/go-sh" + "github.com/gopinath-langote/1build/cmd/utils" + "github.com/logrusorgru/aurora" ) // OneBuildExecutionPlan holds all information for the execution strategy @@ -23,6 +25,13 @@ type CommandContext struct { CommandSession *sh.Session } +const ( + commandBannerLength = 72 + + bannerOpen = "[ " + bannerClose = " ]" +) + // HasBefore return true if plan contains before section else false func (executionPlan *OneBuildExecutionPlan) HasBefore() bool { return executionPlan.Before != nil @@ -90,3 +99,27 @@ func longestPhaseAndCommandValue(executionPlan *OneBuildExecutionPlan) (string, func dashesOfLength(text string) string { return strings.Repeat("-", len(text)) } + +// PrintBanner prints the CommandContext's name in a 72-character banner +func (c *CommandContext) PrintBanner() { + centreLength := len(c.Name) + len(bannerOpen) + len(bannerClose) + totalDashes := commandBannerLength - centreLength + + // Intentional integer division + numDashesLeft := totalDashes / 2 + numDashesRight := totalDashes / 2 + + // If we need an extra dash, let's add it on the right. + // This way, similar length aliases will line up + if totalDashes%2 == 1 { + numDashesRight++ + } + + fmt.Printf("%s%s%s%s%s\n", + strings.Repeat("-", numDashesLeft), + bannerOpen, + aurora.BrightCyan(c.Name), + bannerClose, + strings.Repeat("-", numDashesRight), + ) +} diff --git a/testing/fixtures/execute_cmd_fixtures.go b/testing/fixtures/execute_cmd_fixtures.go index 24615f49..d4e4bbde 100644 --- a/testing/fixtures/execute_cmd_fixtures.go +++ b/testing/fixtures/execute_cmd_fixtures.go @@ -35,6 +35,7 @@ Phase Command build echo building project +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -94,7 +95,9 @@ before echo running pre-command build echo building project +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- running pre-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -127,7 +130,9 @@ build echo building project after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project +-------------------------------[ ` + aurora.BrightCyan("after").String() + ` ]-------------------------------- running post-command ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -162,8 +167,11 @@ build echo building project after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- running pre-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project +-------------------------------[ ` + aurora.BrightCyan("after").String() + ` ]-------------------------------- running post-command ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -198,6 +206,7 @@ build echo building project after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- ----------------------------------------------------------------------------------------------------------- ` + aurora.Red("Execution failed during phase \"before\" - Execution of the script \"exit 10\" returned non-zero exit code : 10").Bold().String() + ` @@ -233,7 +242,9 @@ build invalid_command after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- running pre-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- ------------------------------------------------------------------------------------------------------------------- ` + aurora.Red("Execution failed during phase \"build\" - Execution of the script \"invalid_command\" returned non-zero exit code : 127").Bold().String() + ` From 148fbd4ebea4e92428ffc8192e60a97f1f699a2c Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Sat, 28 Sep 2019 15:00:17 -0500 Subject: [PATCH 2/3] Move output width constant into utils --- cmd/models/onebuild-execution-plan.go | 6 ++---- cmd/utils/utils.go | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/models/onebuild-execution-plan.go b/cmd/models/onebuild-execution-plan.go index 92540556..cc1e1f60 100644 --- a/cmd/models/onebuild-execution-plan.go +++ b/cmd/models/onebuild-execution-plan.go @@ -26,8 +26,6 @@ type CommandContext struct { } const ( - commandBannerLength = 72 - bannerOpen = "[ " bannerClose = " ]" ) @@ -100,10 +98,10 @@ func dashesOfLength(text string) string { return strings.Repeat("-", len(text)) } -// PrintBanner prints the CommandContext's name in a 72-character banner +// PrintBanner prints the CommandContext's name in a banner of the standard length func (c *CommandContext) PrintBanner() { centreLength := len(c.Name) + len(bannerOpen) + len(bannerClose) - totalDashes := commandBannerLength - centreLength + totalDashes := utils.MaxOutputWidth - centreLength // Intentional integer division numDashesLeft := totalDashes / 2 diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index 23fa3356..da02993b 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -5,6 +5,11 @@ import ( "strconv" ) +const ( + // MaxOutputWidth is the number of spaces to use on a console + MaxOutputWidth = 72 +) + // Exit exits the current process with specified exit code func Exit(code int) { os.Exit(code) From d370578384b140007b6500484075cae64449d781 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Sat, 28 Sep 2019 15:12:37 -0500 Subject: [PATCH 3/3] Switch from len to utf8.RuneCountInString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need the literal number of spaces a command *is* taking up, as opposed to the width of its unicode character, or the width that it *should* take up on the screen. E.g. The smiley emoji, described by the 3 bytes [0xE2 0x98 0xBA] takes up 2 columns on the screen, but only occupies 1 column, as seen below 1234567 ###☺### --- cmd/models/onebuild-execution-plan.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/models/onebuild-execution-plan.go b/cmd/models/onebuild-execution-plan.go index cc1e1f60..8a1f017d 100644 --- a/cmd/models/onebuild-execution-plan.go +++ b/cmd/models/onebuild-execution-plan.go @@ -5,6 +5,7 @@ import ( "os" "strings" "text/tabwriter" + "unicode/utf8" "github.com/codeskyblue/go-sh" "github.com/gopinath-langote/1build/cmd/utils" @@ -100,7 +101,9 @@ func dashesOfLength(text string) string { // PrintBanner prints the CommandContext's name in a banner of the standard length func (c *CommandContext) PrintBanner() { - centreLength := len(c.Name) + len(bannerOpen) + len(bannerClose) + centreLength := utf8.RuneCountInString(c.Name) + + utf8.RuneCountInString(bannerOpen) + + utf8.RuneCountInString(bannerClose) totalDashes := utils.MaxOutputWidth - centreLength // Intentional integer division