From 1250929be3fdf19d61c6121895dc39b3a2d33653 Mon Sep 17 00:00:00 2001 From: RoseSecurity <72598486+RoseSecurity@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:04:52 -0500 Subject: [PATCH] feat: additional `atmos docs` parameters for specifying width, using auto-styling and color profile, and preserving new lines (#757) * 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 * feat: dynamically detect terminal width * feat: add additional comment on parameter option * feat: move width out of globals * feat: add max-width and docs parameters to atmos.yaml schema * feat: update variable names to adhere to usage * feat: add additional logic for maxwidth * refactor variable name * fix: word wrap to terminal size and add better error message * 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 | 41 ++++++++++++++++++++++++++++++++++------- pkg/schema/schema.go | 5 +++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index ba014f546..547cd018a 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -8,7 +8,9 @@ import ( "runtime" "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" @@ -37,18 +39,33 @@ var docsCmd = &cobra.Command{ u.LogErrorAndExit(schema.CliConfiguration{}, err) } + // 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 + 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 { + // Adjusted for subtle padding effect at the terminal boundaries + screenWidth = termWidth - 2 + } + } + + if maxWidth > 0 { + screenWidth = min(maxWidth, screenWidth) + } + // 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") @@ -56,7 +73,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)) } } @@ -65,7 +82,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(screenWidth), + ) + if err != nil { + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("failed to initialize markdown renderer: %w", err)) + } + + componentDocs, err := r.Render(string(readmeContent)) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 41b677970..d9fefef3f 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 {