Skip to content

Commit

Permalink
flag detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerebrovinny committed Jan 9, 2025
1 parent f09b2d7 commit e581338
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ var (
ListMergeStrategy: "replace",
Terminal: schema.Terminal{
MaxWidth: templates.GetTerminalWidth(),
Pager: true,
UsePager: true,
Colors: true,
Unicode: true,
SyntaxHighlighting: schema.SyntaxHighlighting{
Enabled: true,
Formatter: "terminal",
Style: "dracula",
Pager: true,
UsePager: true,
Options: schema.HighlightOptions{
LineNumbers: true,
Wrap: false,
Expand Down
50 changes: 31 additions & 19 deletions pkg/utils/highlight_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func HighlightCode(code string, lexerName string, style string) (string, error)
}

// HighlightCodeWithConfig highlights the given code using the provided configuration
func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (string, error) {
func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration, format ...string) (string, error) {
if !term.IsTerminal(int(os.Stdout.Fd())) {
return code, nil
}
Expand All @@ -81,25 +81,31 @@ func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (str
// Get terminal width
config.Settings.Terminal.MaxWidth = templates.GetTerminalWidth()

// Determine lexer based on content format
// Determine lexer based on format flag or content format
var lexerName string
trimmed := strings.TrimSpace(code)

// Try to parse as JSON first
if json.Valid([]byte(trimmed)) {
lexerName = "json"
if len(format) > 0 && format[0] != "" {
// Use format flag if provided
lexerName = strings.ToLower(format[0])
} else {
// Check for common YAML indicators
// 1. Contains key-value pairs with colons
// 2. Does not start with a curly brace (which could indicate malformed JSON)
// 3. Contains indentation or list markers
if (strings.Contains(trimmed, ":") && !strings.HasPrefix(trimmed, "{")) ||
strings.Contains(trimmed, "\n ") ||
strings.Contains(trimmed, "\n- ") {
lexerName = "yaml"
// This is just a fallback
trimmed := strings.TrimSpace(code)

// Try to parse as JSON first
if json.Valid([]byte(trimmed)) {
lexerName = "json"
} else {
// Fallback to plaintext if format is unclear
lexerName = "plaintext"
// Check for common YAML indicators
// 1. Contains key-value pairs with colons
// 2. Does not start with a curly brace (which could indicate malformed JSON)
// 3. Contains indentation or list markers
if (strings.Contains(trimmed, ":") && !strings.HasPrefix(trimmed, "{")) ||
strings.Contains(trimmed, "\n ") ||
strings.Contains(trimmed, "\n- ") {
lexerName = "yaml"
} else {
// Fallback to plaintext if format is unclear
lexerName = "plaintext"
}
}
}

Expand Down Expand Up @@ -141,13 +147,19 @@ func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (str
type HighlightWriter struct {
config schema.AtmosConfiguration
writer io.Writer
format string
}

// NewHighlightWriter creates a new HighlightWriter
func NewHighlightWriter(w io.Writer, config schema.AtmosConfiguration) *HighlightWriter {
func NewHighlightWriter(w io.Writer, config schema.AtmosConfiguration, format ...string) *HighlightWriter {
var f string
if len(format) > 0 {
f = format[0]
}
return &HighlightWriter{
config: config,
writer: w,
format: f,
}
}

Expand All @@ -157,7 +169,7 @@ func NewHighlightWriter(w io.Writer, config schema.AtmosConfiguration) *Highligh
// This maintains compatibility with the io.Writer interface contract while still
// providing syntax highlighting functionality.
func (h *HighlightWriter) Write(p []byte) (n int, err error) {
highlighted, err := HighlightCodeWithConfig(string(p), h.config)
highlighted, err := HighlightCodeWithConfig(string(p), h.config, h.format)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit e581338

Please sign in to comment.