@@ -69,7 +69,7 @@ func HighlightCode(code string, lexerName string, style string) (string, error)
69
69
}
70
70
71
71
// 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 ) {
73
73
if ! term .IsTerminal (int (os .Stdout .Fd ())) {
74
74
return code , nil
75
75
}
@@ -81,25 +81,31 @@ func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (str
81
81
// Get terminal width
82
82
config .Settings .Terminal .MaxWidth = templates .GetTerminalWidth ()
83
83
84
- // Determine lexer based on content format
84
+ // Determine lexer based on format flag or content format
85
85
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 ])
91
89
} 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"
100
96
} 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
+ }
103
109
}
104
110
}
105
111
@@ -141,13 +147,19 @@ func HighlightCodeWithConfig(code string, config schema.AtmosConfiguration) (str
141
147
type HighlightWriter struct {
142
148
config schema.AtmosConfiguration
143
149
writer io.Writer
150
+ format string
144
151
}
145
152
146
153
// 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
+ }
148
159
return & HighlightWriter {
149
160
config : config ,
150
161
writer : w ,
162
+ format : f ,
151
163
}
152
164
}
153
165
@@ -157,7 +169,7 @@ func NewHighlightWriter(w io.Writer, config schema.AtmosConfiguration) *Highligh
157
169
// This maintains compatibility with the io.Writer interface contract while still
158
170
// providing syntax highlighting functionality.
159
171
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 )
161
173
if err != nil {
162
174
return 0 , err
163
175
}
0 commit comments