Skip to content

Commit e581338

Browse files
committed
flag detection
1 parent f09b2d7 commit e581338

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

pkg/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ var (
5858
ListMergeStrategy: "replace",
5959
Terminal: schema.Terminal{
6060
MaxWidth: templates.GetTerminalWidth(),
61-
Pager: true,
61+
UsePager: true,
6262
Colors: true,
6363
Unicode: true,
6464
SyntaxHighlighting: schema.SyntaxHighlighting{
6565
Enabled: true,
6666
Formatter: "terminal",
6767
Style: "dracula",
68-
Pager: true,
68+
UsePager: true,
6969
Options: schema.HighlightOptions{
7070
LineNumbers: true,
7171
Wrap: false,

pkg/utils/highlight_utils.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func HighlightCode(code string, lexerName string, style string) (string, error)
6969
}
7070

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

84-
// Determine lexer based on content format
84+
// Determine lexer based on format flag or content format
8585
var lexerName string
86-
trimmed := strings.TrimSpace(code)
87-
88-
// Try to parse as JSON first
89-
if json.Valid([]byte(trimmed)) {
90-
lexerName = "json"
86+
if len(format) > 0 && format[0] != "" {
87+
// Use format flag if provided
88+
lexerName = strings.ToLower(format[0])
9189
} else {
92-
// Check for common YAML indicators
93-
// 1. Contains key-value pairs with colons
94-
// 2. Does not start with a curly brace (which could indicate malformed JSON)
95-
// 3. Contains indentation or list markers
96-
if (strings.Contains(trimmed, ":") && !strings.HasPrefix(trimmed, "{")) ||
97-
strings.Contains(trimmed, "\n ") ||
98-
strings.Contains(trimmed, "\n- ") {
99-
lexerName = "yaml"
90+
// This is just a fallback
91+
trimmed := strings.TrimSpace(code)
92+
93+
// Try to parse as JSON first
94+
if json.Valid([]byte(trimmed)) {
95+
lexerName = "json"
10096
} else {
101-
// Fallback to plaintext if format is unclear
102-
lexerName = "plaintext"
97+
// Check for common YAML indicators
98+
// 1. Contains key-value pairs with colons
99+
// 2. Does not start with a curly brace (which could indicate malformed JSON)
100+
// 3. Contains indentation or list markers
101+
if (strings.Contains(trimmed, ":") && !strings.HasPrefix(trimmed, "{")) ||
102+
strings.Contains(trimmed, "\n ") ||
103+
strings.Contains(trimmed, "\n- ") {
104+
lexerName = "yaml"
105+
} else {
106+
// Fallback to plaintext if format is unclear
107+
lexerName = "plaintext"
108+
}
103109
}
104110
}
105111

@@ -141,13 +147,19 @@ func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (str
141147
type HighlightWriter struct {
142148
config schema.AtmosConfiguration
143149
writer io.Writer
150+
format string
144151
}
145152

146153
// NewHighlightWriter creates a new HighlightWriter
147-
func NewHighlightWriter(w io.Writer, config schema.AtmosConfiguration) *HighlightWriter {
154+
func NewHighlightWriter(w io.Writer, config schema.AtmosConfiguration, format ...string) *HighlightWriter {
155+
var f string
156+
if len(format) > 0 {
157+
f = format[0]
158+
}
148159
return &HighlightWriter{
149160
config: config,
150161
writer: w,
162+
format: f,
151163
}
152164
}
153165

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

0 commit comments

Comments
 (0)