/
output.go
319 lines (290 loc) · 10 KB
/
output.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2021-2023 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package core
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/liamg/clinch/terminal"
"github.com/mattn/go-isatty"
"github.com/pterm/pterm"
log "github.com/sirupsen/logrus"
)
// Info Two newlines at the start are important to lay the output nicely in CLI.
var Info = fmt.Sprintf(`
%s (%s)
https://jb.gg/qodana-cli
Documentation – https://jb.gg/qodana-docs
Contact us at qodana-support@jetbrains.com
Bug Tracker: https://jb.gg/qodana-issue
Community forum: https://jb.gg/qodana-forum
`, "Qodana CLI", Version)
var PricingUrl = "https://www.jetbrains.com/qodana/buy/"
// IsInteractive returns true if the current execution environment is interactive (useful for colors/animations toggle).
func IsInteractive() bool {
return !IsContainer() && os.Getenv("NONINTERACTIVE") == "" && (isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()))
}
// DisableColor disables colors in the output.
func DisableColor() {
pterm.DisableColor()
}
// styles and different declarations intended to be used only inside this file
var (
noLineWidth = 7
QodanaSpinner = pterm.DefaultSpinner
spinnerSequence = []string{"| ", "/ ", "- ", "\\ "}
primaryStyle = pterm.NewStyle() // primaryStyle is a primary text style.
primaryBoldStyle = pterm.NewStyle(pterm.Bold) // primaryBoldStyle is a primary bold text style.
errorStyle = pterm.NewStyle(pterm.FgRed) // errorStyle is an error style.
warningStyle = pterm.NewStyle(pterm.FgYellow) // warningStyle is a warning style.
miscStyle = pterm.NewStyle(pterm.FgGray) // miscStyle is a log style.
tableSep = miscStyle.Sprint("─")
tableSepUp = miscStyle.Sprint("┬")
tableSepMid = miscStyle.Sprint("│")
tableSepDown = miscStyle.Sprint("┴")
tableUp = strings.Repeat(tableSep, noLineWidth) + tableSepUp
tableDown = strings.Repeat(tableSep, noLineWidth) + tableSepDown
qodanaInteractiveSelect = pterm.InteractiveSelectPrinter{
TextStyle: primaryStyle,
DefaultText: "Please select the product to use",
Options: []string{},
OptionStyle: primaryStyle,
DefaultOption: "",
MaxHeight: 5,
Selector: ">",
SelectorStyle: primaryStyle,
}
DefaultPromptText = "Do you want to continue?"
qodanaInteractiveConfirm = pterm.InteractiveConfirmPrinter{
DefaultValue: true,
DefaultText: DefaultPromptText,
TextStyle: primaryStyle,
ConfirmText: "Yes",
ConfirmStyle: primaryStyle,
RejectText: "No",
RejectStyle: primaryStyle,
SuffixStyle: primaryStyle,
}
)
// primary prints a message in the primary style.
func primary(text string, a ...interface{}) string {
text = fmt.Sprintf(text, a...)
return primaryStyle.Sprint(text)
}
// PrimaryBold prints a message in the primary bold style.
func PrimaryBold(text string, a ...interface{}) string {
text = fmt.Sprintf(text, a...)
return primaryBoldStyle.Sprint(text)
}
// EmptyMessage is a message that is used when there is no message to show.
func EmptyMessage() {
pterm.Println()
}
// SuccessMessage prints a success message with the icon.
func SuccessMessage(message string, a ...interface{}) {
message = fmt.Sprintf(message, a...)
icon := pterm.Green("✓ ")
pterm.Println(icon, primary(message))
}
// WarningMessage prints a warning message with the icon.
func WarningMessage(message string, a ...interface{}) {
message = fmt.Sprintf(message, a...)
icon := warningStyle.Sprint("\n! ")
pterm.Println(icon, primary(message))
}
// ErrorMessage prints an error message with the icon.
func ErrorMessage(message string, a ...interface{}) {
message = fmt.Sprintf(message, a...)
icon := errorStyle.Sprint("✗ ")
pterm.Println(icon, errorStyle.Sprint(message))
}
// printLinterLog prints the linter logs with color, when needed.
func printLinterLog(line string) {
if strings.Contains(line, " / /") ||
strings.Contains(line, "_ _") ||
strings.Contains(line, "\\/__") ||
strings.Contains(line, "\\ \\") {
primaryStyle.Println(line)
} else {
miscStyle.Println(line)
}
}
// printProcess prints the message for processing phase. TODO: Add ETA based on previous runs
func printProcess(f func(spinner *pterm.SpinnerPrinter), start string, finished string) {
if err := spin(f, start); err != nil {
log.Fatal("\nProblem occurred:", err.Error())
}
if finished != "" {
SuccessMessage("Finished %s", finished)
}
}
// spin creates spinner and runs the given function. Also, spin is a spider in Dutch.
func spin(fun func(spinner *pterm.SpinnerPrinter), message string) error {
spinner, _ := startQodanaSpinner(message)
if spinner == nil {
fmt.Println(primary(message + "..."))
}
fun(spinner)
if spinner != nil {
spinner.Success()
}
return nil
}
// startQodanaSpinner starts a new spinner with the given message.
func startQodanaSpinner(message string) (*pterm.SpinnerPrinter, error) {
if IsInteractive() {
QodanaSpinner.Sequence = spinnerSequence
QodanaSpinner.MessageStyle = primaryStyle
return QodanaSpinner.WithStyle(pterm.NewStyle(pterm.FgGray)).WithRemoveWhenDone(true).Start(message + "...")
}
return nil, nil
}
// updateText updates the text of the spinner.
func updateText(spinner *pterm.SpinnerPrinter, message string) {
if spinner != nil {
spinner.UpdateText(message + "...")
}
}
// PrintFile prints the given file content with lines like printProblem.
func PrintFile(file string) {
printHeader("", "", file)
content, err := os.ReadFile(file)
if err != nil {
log.Fatalf("failed to read file %s: %s", file, err)
}
printLines(string(content), 1, 0, true)
}
// printProblem prints problem with source code or without it.
func printProblem(ruleId string, level string, message string, path string, line int, column int, contextLine int, context string) {
printHeader(level, ruleId, "")
printPath(path, line, column)
printLines(context, contextLine, line, false)
fmt.Print(message + "\n")
}
// getTerminalWidth returns the width of the terminal.
func getTerminalWidth() int {
width, _ := terminal.Size()
if width <= 0 {
width = 80
}
return width
}
// printHeader prints the header of the problem/file.
func printHeader(level string, ruleId string, file string) {
width := getTerminalWidth()
fmt.Printf("%s %s\n", PrimaryBold(strings.ToUpper(level)), primary(ruleId))
fmt.Println(strings.Repeat(tableSep, width))
if file != "" {
fmt.Printf("%5s %s %s\n", "", tableSepMid, PrimaryBold(file))
fmt.Println(strings.Repeat(tableSep, width))
}
}
// printPath prints the path of the problem.
func printPath(path string, line int, column int) {
if path != "" && line > 0 && column > 0 {
fmt.Printf(" %s:%d:%d\n", path, line, column)
fmt.Printf("%s%s\n", tableUp, strings.Repeat(tableSep, getTerminalWidth()-noLineWidth-1))
} else {
fmt.Println(strings.Repeat(tableSep, getTerminalWidth()))
}
}
// printLines prints the lines of the problem.
func printLines(content string, contextLine int, line int, skipHighlight bool) {
lines := strings.Split(content, "\n")
lineCount := len(lines)
if content[len(content)-1] == '\n' {
lineCount -= 1 // Remove the last empty line if content ends with a newline
}
for i := 0; i < lineCount; i++ {
var printLine string
currentLine := contextLine + i
if skipHighlight {
printLine = lines[i]
} else if currentLine == line {
printLine = errorStyle.Sprint(lines[i]) + " ←"
} else {
printLine = warningStyle.Sprint(lines[i])
}
lineNumber := miscStyle.Sprintf("%5d", currentLine)
fmt.Printf("%s %s %s\n", lineNumber, tableSepMid, printLine)
}
fmt.Printf("%s%s\n", tableDown, strings.Repeat(tableSep, getTerminalWidth()-noLineWidth-1))
}
// PrintContributorsTable prints the contributors table and helpful messages.
func PrintContributorsTable(contributors []contributor, days int, dirs int) {
count := len(contributors)
contributorsTableData := pterm.TableData{
[]string{
PrimaryBold("Username"),
PrimaryBold("Email"),
PrimaryBold("Commits"),
},
}
for _, p := range contributors {
contributorsTableData = append(contributorsTableData, []string{
p.Author.Username,
p.Author.Email,
strconv.Itoa(p.Count),
})
}
table := pterm.DefaultTable.WithData(contributorsTableData)
table.HeaderRowSeparator = ""
table.Separator = " "
table.Boxed = true
err := table.Render()
if err != nil {
return
}
EmptyMessage()
SuccessMessage(
"There are %s active contributor(s)* for the last %s days in the provided %s project(s).",
PrimaryBold(strconv.Itoa(count)),
PrimaryBold(strconv.Itoa(days)),
PrimaryBold(strconv.Itoa(dirs)),
)
fmt.Print(getPlanMessage("Community", 0, count))
fmt.Print(getPlanMessage("Ultimate", 6, count))
fmt.Print(getPlanMessage("Ultimate Plus*", 9, count))
EmptyMessage()
fmt.Printf(
`* Run %s or visit %s for more information.
Note: Qodana will always be free for verified open source projects.`,
PrimaryBold("qodana contributors -h"),
PricingUrl,
)
EmptyMessage()
}
// getPlanMessage returns a message with the cost of the plan.
func getPlanMessage(plan string, cost int, contributors int) string {
var costMessage string
if cost == 0 {
costMessage = fmt.Sprintf(" %s = %d * $0 – Qodana is completely free for %s plan\n",
PrimaryBold("$0"),
contributors,
PrimaryBold(plan),
)
} else {
costMessage = fmt.Sprintf(
" %s = %d * $%d – approximate cost/month for %s plan\n",
PrimaryBold(fmt.Sprintf("$%d", cost*contributors)),
contributors,
cost,
PrimaryBold(plan),
)
}
return costMessage
}