From 6a09356d47dc1b53df78ea5c5c64d764aab08081 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Fri, 1 Nov 2024 09:00:54 -0400 Subject: [PATCH 01/10] feat: add auto-styling instead of dark mode for docs rendering, add an additional parameter for specifying the width of markdown outputs, and maintain a consistent color profile --- cmd/docs.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index ba014f546..6f38264f3 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -8,6 +8,7 @@ import ( "runtime" "github.com/charmbracelet/glamour" + "github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" cfg "github.com/cloudposse/atmos/pkg/config" @@ -17,6 +18,8 @@ import ( const atmosDocsURL = "https://atmos.tools" +var width uint + // docsCmd opens the Atmos docs and can display component documentation var docsCmd = &cobra.Command{ Use: "docs", @@ -39,16 +42,12 @@ var docsCmd = &cobra.Command{ // Construct the full path to the Terraform component by combining the Atmos base path, Terraform base path, and component name componentPath := path.Join(cliConfig.BasePath, cliConfig.Components.Terraform.BasePath, info.Component) - componentPathExists, err := u.IsDirectory(componentPath) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } if !componentPathExists { - u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("Component '%s' not found in path: '%s'", - info.Component, - componentPath, - )) + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("Component '%s' not found in path: '%s'", info.Component, componentPath)) } readmePath := path.Join(componentPath, "README.md") @@ -65,7 +64,17 @@ var docsCmd = &cobra.Command{ u.LogErrorAndExit(schema.CliConfiguration{}, err) } - componentDocs, err := glamour.Render(string(readmeContent), "dark") + r, err := glamour.NewTermRenderer( + glamour.WithColorProfile(lipgloss.ColorProfile()), + glamour.WithAutoStyle(), + glamour.WithPreservedNewLines(), + glamour.WithWordWrap(int(width)), + ) + if err != nil { + u.LogErrorAndExit(schema.CliConfiguration{}, err) + } + + componentDocs, err := r.Render(string(readmeContent)) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } @@ -95,4 +104,5 @@ var docsCmd = &cobra.Command{ func init() { RootCmd.AddCommand(docsCmd) + docsCmd.Flags().UintVarP(&width, "width", "w", 80, "word-wrap at width") } From 0a5122aadcda4051e2d73fc5b52f773db9f187dd Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Fri, 1 Nov 2024 10:13:43 -0400 Subject: [PATCH 02/10] feat: dynamically detect terminal width --- cmd/docs.go | 21 ++++++++++++++++++++- go.mod | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 6f38264f3..e35b5d462 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -10,6 +10,7 @@ import ( "github.com/charmbracelet/glamour" "github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" + "golang.org/x/term" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/schema" @@ -40,6 +41,24 @@ var docsCmd = &cobra.Command{ u.LogErrorAndExit(schema.CliConfiguration{}, err) } + // Detect terminal width if width flag is not set + if !cmd.Flags().Changed("width") { + if term.IsTerminal(int(os.Stdout.Fd())) { + w, _, err := term.GetSize(int(os.Stdout.Fd())) + if err == nil { + width = uint(w) + } + + if width > 120 { + width = 120 + } + + if width == 0 { + width = 80 + } + } + } + // Construct the full path to the Terraform component by combining the Atmos base path, Terraform base path, and component name componentPath := path.Join(cliConfig.BasePath, cliConfig.Components.Terraform.BasePath, info.Component) componentPathExists, err := u.IsDirectory(componentPath) @@ -104,5 +123,5 @@ var docsCmd = &cobra.Command{ func init() { RootCmd.AddCommand(docsCmd) - docsCmd.Flags().UintVarP(&width, "width", "w", 80, "word-wrap at width") + docsCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width (set to 0 to disable)") } diff --git a/go.mod b/go.mod index c4262a9f3..1304428da 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 github.com/zclconf/go-cty v1.15.0 + golang.org/x/term v0.25.0 gopkg.in/yaml.v3 v3.0.1 mvdan.cc/sh/v3 v3.10.0 ) @@ -242,7 +243,6 @@ require ( golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect - golang.org/x/term v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect golang.org/x/time v0.6.0 // indirect golang.org/x/tools v0.22.0 // indirect From 6aaceddc4c6573efb42c313735f65c0e7a942a4f Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Fri, 1 Nov 2024 10:47:16 -0400 Subject: [PATCH 03/10] feat: add additional comment on parameter option --- cmd/docs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/docs.go b/cmd/docs.go index e35b5d462..2446d168f 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -123,5 +123,5 @@ var docsCmd = &cobra.Command{ func init() { RootCmd.AddCommand(docsCmd) - docsCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width (set to 0 to disable)") + docsCmd.Flags().UintVarP(&width, "width", "w", 0, "Set word-wrap width (0 disables word wrapping). Influences output formatting, particularly for tables.") } From 0a087a3c339f404b6f6c1250f1e5a833c174ce42 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Fri, 1 Nov 2024 11:04:14 -0400 Subject: [PATCH 04/10] feat: move width out of globals --- cmd/docs.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 2446d168f..a3f117bf0 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -19,8 +19,6 @@ import ( const atmosDocsURL = "https://atmos.tools" -var width uint - // docsCmd opens the Atmos docs and can display component documentation var docsCmd = &cobra.Command{ Use: "docs", @@ -42,6 +40,7 @@ var docsCmd = &cobra.Command{ } // Detect terminal width if width flag is not set + width, _ := cmd.Flags().GetUint("width") if !cmd.Flags().Changed("width") { if term.IsTerminal(int(os.Stdout.Fd())) { w, _, err := term.GetSize(int(os.Stdout.Fd())) @@ -123,5 +122,5 @@ var docsCmd = &cobra.Command{ func init() { RootCmd.AddCommand(docsCmd) - docsCmd.Flags().UintVarP(&width, "width", "w", 0, "Set word-wrap width (0 disables word wrapping). Influences output formatting, particularly for tables.") + docsCmd.Flags().UintP("width", "w", 0, "Set word-wrap width (0 disables word wrapping). Influences output formatting, particularly for tables.") } From 4dea0a3b8f110c625c88d7aadce54d9f01a43813 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Mon, 4 Nov 2024 11:43:18 -0500 Subject: [PATCH 05/10] feat: add max-width and docs parameters to atmos.yaml schema --- cmd/docs.go | 9 ++++----- pkg/schema/schema.go | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index a3f117bf0..51642c792 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -40,12 +40,12 @@ var docsCmd = &cobra.Command{ } // Detect terminal width if width flag is not set - width, _ := cmd.Flags().GetUint("width") - if !cmd.Flags().Changed("width") { + width := cliConfig.Settings.Docs.MaxWidth + if width == 0 { if term.IsTerminal(int(os.Stdout.Fd())) { w, _, err := term.GetSize(int(os.Stdout.Fd())) if err == nil { - width = uint(w) + width = int(w) } if width > 120 { @@ -89,7 +89,7 @@ var docsCmd = &cobra.Command{ glamour.WithWordWrap(int(width)), ) if err != nil { - u.LogErrorAndExit(schema.CliConfiguration{}, err) + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("failed to initialize markdown renderer: %w", err)) } componentDocs, err := r.Render(string(readmeContent)) @@ -122,5 +122,4 @@ var docsCmd = &cobra.Command{ func init() { RootCmd.AddCommand(docsCmd) - docsCmd.Flags().UintP("width", "w", 0, "Set word-wrap width (0 disables word wrapping). Influences output formatting, particularly for tables.") } diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 89e90b061..dbf46d082 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -29,6 +29,11 @@ type CliConfiguration struct { type CliSettings struct { ListMergeStrategy string `yaml:"list_merge_strategy" json:"list_merge_strategy" mapstructure:"list_merge_strategy"` + Docs Docs `yaml:"docs,omitempty" json:"docs,omitempty" mapstructure:"docs"` +} + +type Docs struct { + MaxWidth int `yaml:"max-width" json:"max_width" mapstructure:"max-width"` } type Templates struct { From b8326f34db3f890478a4ece2fa7fcd00c7c2a2f8 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Mon, 4 Nov 2024 12:54:54 -0500 Subject: [PATCH 06/10] feat: update variable names to adhere to usage --- cmd/docs.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 51642c792..4a10dcdfb 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -39,21 +39,21 @@ var docsCmd = &cobra.Command{ u.LogErrorAndExit(schema.CliConfiguration{}, err) } - // Detect terminal width if width flag is not set - width := cliConfig.Settings.Docs.MaxWidth - if width == 0 { + // Detect terminal width if not specified in `atmos.yaml` + maxWidth := cliConfig.Settings.Docs.MaxWidth + if maxWidth == 0 { if term.IsTerminal(int(os.Stdout.Fd())) { w, _, err := term.GetSize(int(os.Stdout.Fd())) if err == nil { - width = int(w) + maxWidth = int(w) } - if width > 120 { - width = 120 + if maxWidth > 120 { + maxWidth = 120 } - if width == 0 { - width = 80 + if maxWidth == 0 { + maxWidth = 80 } } } @@ -86,7 +86,7 @@ var docsCmd = &cobra.Command{ glamour.WithColorProfile(lipgloss.ColorProfile()), glamour.WithAutoStyle(), glamour.WithPreservedNewLines(), - glamour.WithWordWrap(int(width)), + glamour.WithWordWrap(int(maxWidth)), ) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("failed to initialize markdown renderer: %w", err)) From a9a2e415975450d6ea2d342a9ac9ee5224c0e415 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Mon, 4 Nov 2024 17:46:02 -0500 Subject: [PATCH 07/10] feat: add additional logic for maxwidth --- cmd/docs.go | 30 +++++++++++++++--------------- go.mod | 1 - 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 4a10dcdfb..554c4b542 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -40,24 +40,24 @@ var docsCmd = &cobra.Command{ } // Detect terminal width if not specified in `atmos.yaml` + // The default screen width is 120 characters, but uses maxWidth if set and greater than zero maxWidth := cliConfig.Settings.Docs.MaxWidth - if maxWidth == 0 { - if term.IsTerminal(int(os.Stdout.Fd())) { - w, _, err := term.GetSize(int(os.Stdout.Fd())) - if err == nil { - maxWidth = int(w) - } - - if maxWidth > 120 { - maxWidth = 120 - } - - if maxWidth == 0 { - maxWidth = 80 - } + defaultWidth := 120 + screenWidth := defaultWidth + + if term.IsTerminal(int(os.Stdout.Fd())) { + w, _, err := term.GetSize(int(os.Stdout.Fd())) + if err == nil && w > 0 { + screenWidth = w } } + if maxWidth > 0 { + screenWidth = min(maxWidth, screenWidth) + } else { + screenWidth = defaultWidth + } + // Construct the full path to the Terraform component by combining the Atmos base path, Terraform base path, and component name componentPath := path.Join(cliConfig.BasePath, cliConfig.Components.Terraform.BasePath, info.Component) componentPathExists, err := u.IsDirectory(componentPath) @@ -86,7 +86,7 @@ var docsCmd = &cobra.Command{ glamour.WithColorProfile(lipgloss.ColorProfile()), glamour.WithAutoStyle(), glamour.WithPreservedNewLines(), - glamour.WithWordWrap(int(maxWidth)), + glamour.WithWordWrap(screenWidth), ) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("failed to initialize markdown renderer: %w", err)) diff --git a/go.mod b/go.mod index 0fa648c07..f8fcd4c42 100644 --- a/go.mod +++ b/go.mod @@ -243,7 +243,6 @@ require ( golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect - golang.org/x/term v0.25.0 // indirect golang.org/x/text v0.19.0 // indirect golang.org/x/time v0.7.0 // indirect golang.org/x/tools v0.22.0 // indirect From 1e6d6a9fa78903ac2b32f0ce4074751761e97097 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Mon, 4 Nov 2024 17:57:23 -0500 Subject: [PATCH 08/10] refactor variable name --- cmd/docs.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 554c4b542..4245bdd67 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -46,9 +46,9 @@ var docsCmd = &cobra.Command{ screenWidth := defaultWidth if term.IsTerminal(int(os.Stdout.Fd())) { - w, _, err := term.GetSize(int(os.Stdout.Fd())) - if err == nil && w > 0 { - screenWidth = w + termWidth, _, err := term.GetSize(int(os.Stdout.Fd())) + if err == nil && termWidth > 0 { + screenWidth = termWidth } } From 6e184f25e134680c97a6810b588c66387a19f616 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Thu, 7 Nov 2024 11:02:04 -0600 Subject: [PATCH 09/10] fix: word wrap to terminal size and add better error message --- cmd/docs.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 4245bdd67..23d877a13 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -45,6 +45,7 @@ var docsCmd = &cobra.Command{ defaultWidth := 120 screenWidth := defaultWidth + // Detect terminal width and use it by default if available if term.IsTerminal(int(os.Stdout.Fd())) { termWidth, _, err := term.GetSize(int(os.Stdout.Fd())) if err == nil && termWidth > 0 { @@ -54,8 +55,6 @@ var docsCmd = &cobra.Command{ if maxWidth > 0 { screenWidth = min(maxWidth, screenWidth) - } else { - screenWidth = defaultWidth } // Construct the full path to the Terraform component by combining the Atmos base path, Terraform base path, and component name @@ -73,7 +72,7 @@ var docsCmd = &cobra.Command{ if os.IsNotExist(err) { u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("No README found for component: %s", info.Component)) } else { - u.LogErrorAndExit(schema.CliConfiguration{}, err) + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("Component %s not found", info.Component)) } } From be6e4ce81f3e0fd6424d77b7d26c3701ee680259 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Mon, 11 Nov 2024 08:53:13 -0500 Subject: [PATCH 10/10] fix: adjusted rendering based on terminal size by subtracting 2 characters which appears to be a subtle padding effect at the terminal boundaries --- cmd/docs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/docs.go b/cmd/docs.go index 23d877a13..547cd018a 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -49,7 +49,8 @@ var docsCmd = &cobra.Command{ if term.IsTerminal(int(os.Stdout.Fd())) { termWidth, _, err := term.GetSize(int(os.Stdout.Fd())) if err == nil && termWidth > 0 { - screenWidth = termWidth + // Adjusted for subtle padding effect at the terminal boundaries + screenWidth = termWidth - 2 } }