Skip to content

Commit

Permalink
lexer fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerebrovinny committed Jan 7, 2025
1 parent 209938d commit 4406a5b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
3 changes: 1 addition & 2 deletions atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,9 @@ settings:
timestamps: false # Show timestamps in logs
colors: true # Enable colored output
unicode: true # Use unicode characters

syntax_highlighting:
enabled: true
lexer: yaml # Default lexer for the content
formatter: terminal # Output formatter (e.g., terminal, html)
style: dracula # Highlighting style
pager: true # Enable pager
Expand Down
6 changes: 3 additions & 3 deletions internal/tui/templates/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func SetCustomUsageFunc(cmd *cobra.Command) error {
return nil
}

// getTerminalWidth returns the width of the terminal, defaulting to 80 if it cannot be determined
func getTerminalWidth() int {
// GetTerminalWidth returns the width of the terminal, defaulting to 80 if it cannot be determined
func GetTerminalWidth() int {
defaultWidth := 80
screenWidth := defaultWidth

Expand All @@ -151,7 +151,7 @@ func getTerminalWidth() int {
// WrappedFlagUsages formats the flag usage string to fit within the terminal width
func WrappedFlagUsages(f *pflag.FlagSet) string {
var builder strings.Builder
width := getTerminalWidth()
width := GetTerminalWidth()
printer, err := NewHelpFlagPrinter(&builder, uint(width), f)
if err != nil {
// If we can't create the printer, return empty string
Expand Down
11 changes: 6 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/viper"

"github.com/cloudposse/atmos/internal/tui/templates"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/version"
Expand Down Expand Up @@ -54,20 +55,20 @@ var (
},
},
Settings: schema.AtmosSettings{
ListMergeStrategy: "replace",
Terminal: schema.Terminal{
MaxWidth: 180,
MaxWidth: templates.GetTerminalWidth(),
Pager: true,
Colors: true,
Unicode: true,
SyntaxHighlighting: schema.SyntaxHighlighting{
Enabled: true,
Lexer: "yaml",
Formatter: "terminal256",
Style: "monokai",
Formatter: "terminal",
Style: "dracula",
Pager: true,
Options: schema.HighlightOptions{
LineNumbers: true,
Wrap: true,
Wrap: false,
},
},
},
Expand Down
23 changes: 17 additions & 6 deletions pkg/utils/highlight_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"io"
"os"
"strings"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/quick"
"github.com/alecthomas/chroma/styles"
"github.com/cloudposse/atmos/internal/tui/templates"
"github.com/cloudposse/atmos/pkg/schema"
"golang.org/x/term"
)
Expand All @@ -18,11 +20,11 @@ import (
func DefaultHighlightSettings() *schema.SyntaxHighlighting {
return &schema.SyntaxHighlighting{
Enabled: true,
Lexer: "yaml",
Formatter: "terminal",
Style: "dracula",
Pager: true,
Options: schema.HighlightOptions{
LineNumbers: false,
LineNumbers: true,
Wrap: false,
},
}
Expand All @@ -39,9 +41,6 @@ func GetHighlightSettings(config schema.AtmosConfiguration) *schema.SyntaxHighli
if !settings.Enabled {
settings.Enabled = defaults.Enabled
}
if settings.Lexer == "" {
settings.Lexer = defaults.Lexer
}
if settings.Formatter == "" {
settings.Formatter = defaults.Formatter
}
Expand Down Expand Up @@ -76,8 +75,20 @@ func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (str
if !settings.Enabled {
return code, nil
}

// Get terminal width
config.Settings.Terminal.MaxWidth = templates.GetTerminalWidth()

// Determine lexer based on content format
var lexerName string
if strings.HasPrefix(strings.TrimSpace(code), "{") {
lexerName = "json"
} else {
lexerName = "yaml"
}

// Get lexer
lexer := lexers.Get(settings.Lexer)
lexer := lexers.Get(lexerName)
if lexer == nil {
lexer = lexers.Fallback
}
Expand Down

0 comments on commit 4406a5b

Please sign in to comment.