Skip to content

Commit

Permalink
Added fixed terminal width option
Browse files Browse the repository at this point in the history
There might be cases where a fixed terminal width is helpful,
especially if the terminal width detection fails
  • Loading branch information
HeavyWombat committed Apr 15, 2018
1 parent 4111d22 commit e043c1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
13 changes: 4 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ import (
"github.com/spf13/cobra"
)

// NoColor is the gobal switch to decide whether strings should be colored in the output
var NoColor = false

// Debug is the global switch to enable debug output
var Debug = false

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "dyff",
Expand All @@ -57,12 +51,13 @@ func init() {
// Here you will define your flags and configuration settings. Cobra supports
// persistent flags, which, if defined here, will be global for your
// application.
rootCmd.PersistentFlags().BoolVar(&NoColor, "no-color", false, "Disable colors in output")
rootCmd.PersistentFlags().BoolVar(&Debug, "debug", false, "Disable colors in output")
rootCmd.PersistentFlags().BoolVar(&core.NoColor, "no-color", false, "Disable colors in output")
rootCmd.PersistentFlags().BoolVar(&core.DebugMode, "debug", false, "Disable colors in output")
rootCmd.PersistentFlags().IntVar(&core.FixedTerminalWidth, "fixed-terminal-width", -1, "Disables terminal width detection by using fixed provided value")
}

func initSettings() {
if NoColor {
if core.NoColor {
color.NoColor = true
yaml.HighlightKeys = false
}
Expand Down
13 changes: 13 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

// DebugMode is the global switch to enable debug output
var DebugMode = false

// NoColor is the gobal switch to decide whether strings should be colored in the output
var NoColor = false

// FixedTerminalWidth disables terminal width detection and reset it with a fixed given value
var FixedTerminalWidth = -1

// Debug log output
var Debug = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)

Expand Down Expand Up @@ -96,6 +105,10 @@ func ExitWithError(text string, err error) {
}

func GetTerminalWidth() int {
if FixedTerminalWidth > 0 {
return FixedTerminalWidth
}

termWidth, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
return 80
Expand Down
4 changes: 4 additions & 0 deletions core/core_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func TestCore(t *testing.T) {
var _ = BeforeSuite(func() {
yaml.HighlightKeys = false
color.NoColor = true

core.NoColor = false
core.DebugMode = false
core.FixedTerminalWidth = 80
})

func yml(input string) yaml.MapSlice {
Expand Down

0 comments on commit e043c1d

Please sign in to comment.