forked from Azure/azure-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.go
More file actions
129 lines (106 loc) · 3.7 KB
/
colors.go
File metadata and controls
129 lines (106 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package output
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/azure/azure-dev/cli/azd/internal/terminal"
"github.com/charmbracelet/glamour"
"github.com/fatih/color"
"github.com/nathan-fiscaletti/consolesize-go"
)
// withLinkFormat creates string with hyperlink-looking color
func WithLinkFormat(link string, a ...interface{}) string {
return color.HiCyanString(link, a...)
}
// withHighLightFormat creates string with highlight-looking color
func WithHighLightFormat(text string, a ...interface{}) string {
return color.HiBlueString(text, a...)
}
func WithErrorFormat(text string, a ...interface{}) string {
return color.RedString(text, a...)
}
func WithWarningFormat(text string, a ...interface{}) string {
return color.YellowString(text, a...)
}
func WithSuccessFormat(text string, a ...interface{}) string {
return color.GreenString(text, a...)
}
func WithGrayFormat(text string, a ...interface{}) string {
return color.HiBlackString(text, a...)
}
func WithHintFormat(text string, a ...interface{}) string {
return color.MagentaString(text, a...)
}
func WithBold(text string, a ...interface{}) string {
format := color.New(color.FgHiWhite, color.Bold)
return format.Sprintf(text, a...)
}
func WithUnderline(text string, a ...interface{}) string {
format := color.New(color.Underline)
return format.Sprintf(text, a...)
}
// WithBackticks wraps text with the backtick (`) character.
func WithBackticks(s string) string {
return fmt.Sprintf("`%s`", s)
}
func AzdLabel() string {
return "[azd]"
}
func AzdAgentLabel() string {
return color.HiMagentaString(fmt.Sprintf("🤖 %s Agent", AzdLabel()))
}
// WithMarkdown converts markdown to terminal-friendly colorized output using glamour.
// This provides rich markdown rendering including bold, italic, code blocks, headers, etc.
func WithMarkdown(markdownText string) string {
// Get dynamic console width with fallback to 120
consoleWidth := getConsoleWidth()
// Create a custom glamour renderer with auto-style detection
r, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(consoleWidth), // Use dynamic console width
)
if err != nil {
// Fallback to returning original text if glamour fails
return markdownText
}
// Render the markdown
rendered, err := r.Render(markdownText)
if err != nil {
// Fallback to returning original text if rendering fails
return markdownText
}
// Trim trailing whitespace that glamour sometimes adds
return strings.TrimSpace(rendered)
}
// WithHyperlink wraps text with the colored hyperlink format escape sequence.
// When stdout is not a terminal (e.g., in CI/CD pipelines like GitHub Actions),
// it returns the plain URL without escape codes to avoid displaying raw ANSI sequences.
func WithHyperlink(url string, text string) string {
// Check if stdout is a terminal
if !terminal.IsTerminal(os.Stdout.Fd(), os.Stdin.Fd()) {
// Not a terminal - return plain URL without escape codes
return url
}
// Terminal - use hyperlink escape codes
return WithLinkFormat(fmt.Sprintf("\033]8;;%s\007%s\033]8;;\007", url, text))
}
// getConsoleWidth gets the console width with fallback logic.
// It uses the consolesize package to get the size and falls back to check the COLUMNS environment variable.
// Defaults to 120 if the console size cannot be determined.
func getConsoleWidth() int {
width, _ := consolesize.GetConsoleSize()
if width <= 0 {
// Default to 120 if console size cannot be determined
width = 120
consoleWidth := os.Getenv("COLUMNS")
if consoleWidth != "" {
if parsedWidth, err := strconv.Atoi(consoleWidth); err == nil {
width = parsedWidth
}
}
}
return width
}