Skip to content

Commit

Permalink
cli: use ui logger
Browse files Browse the repository at this point in the history
  • Loading branch information
akupila committed Apr 21, 2020
1 parent 2ab8251 commit 9a3b481
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 321 deletions.
289 changes: 162 additions & 127 deletions cli/app.go

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions cli/diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cli

import (
"bufio"
"fmt"
"strings"

"github.com/func/func/ui"
"github.com/hashicorp/hcl/v2"
"github.com/mitchellh/go-wordwrap"
)

type diagnostic struct {
*hcl.Diagnostic
File *hcl.File

ExtendLines int
}

func (d diagnostic) Render(f ui.Frame) string {
var out strings.Builder

out.WriteByte('\n')

switch d.Severity {
case hcl.DiagError:
out.WriteString(ui.Format("Error", ui.Red) + ": ")
case hcl.DiagWarning:
out.WriteString(ui.Format("Warning", ui.Yellow) + ": ")
}
out.WriteString(d.Summary)
out.WriteString("\n")

if d.Detail != "" {
padRight := 8 // So text doesn't go all the way to the edge; easier to read
out.WriteString(wordwrap.WrapString(d.Detail, uint(f.Width-padRight)))
out.WriteString("\n")
}
out.WriteString("\n")

if d.Subject != nil {
subjectRange := *d.Subject
contextRange := subjectRange
if d.Context != nil {
contextRange = hcl.RangeOver(contextRange, *d.Context)
}

if d.File != nil && d.File.Bytes != nil {
out.WriteString(ui.Format(d.Subject.Filename, ui.Dim, ui.Italic))
out.WriteString("\n\n")

src := d.File.Bytes
sc := hcl.NewRangeScanner(src, d.Subject.Filename, bufio.ScanLines)

for sc.Scan() {
lineRange := sc.Range()
if lineRange.Start.Line < d.Subject.Start.Line-d.ExtendLines {
// Too early, skip
continue
}
if lineRange.Start.Line > d.Subject.Start.Line+d.ExtendLines {
// Too late, skip
continue
}

linePrefix := fmt.Sprintf("%4d │ ", lineRange.Start.Line)
codeWidth := f.Width - ui.StringWidth(linePrefix)

if !lineRange.Overlaps(contextRange) {
// Not in context, print dim
out.WriteString(ui.Format(linePrefix, ui.Dim))
out.WriteString(wrapCode(string(sc.Bytes()), codeWidth))
out.WriteString("\n")
continue
}

beforeRange, highlightRange, afterRange := lineRange.PartitionAround(subjectRange)
out.WriteString(ui.Format(fmt.Sprintf("%4d ", lineRange.Start.Line), ui.Bold))
out.WriteString(ui.Format("│ ", ui.Dim))
if highlightRange.Empty() {
out.WriteString(wrapCode(string(sc.Bytes()), codeWidth))
} else {
before := beforeRange.SliceBytes(src)
highlighted := highlightRange.SliceBytes(src)
after := afterRange.SliceBytes(src)
out.WriteString(wrapCode(string(before), codeWidth))
out.WriteString(ui.Format(wrapCode(string(highlighted), codeWidth), ui.Bold, ui.Underline))
out.WriteString(wrapCode(string(after), codeWidth))
}
out.WriteByte('\n')
}
}
}

return out.String()
}

func wrapCode(code string, w int) string {
wrapped := ui.Wrap(code, w)
var out strings.Builder
for i, l := range strings.Split(wrapped, "\n") {
if i > 0 {
out.WriteString(ui.Format("\n ┊ ", ui.Dim))
}
out.WriteString(l)
}
return out.String()
}
27 changes: 27 additions & 0 deletions cli/indicator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cli

import (
"time"

"github.com/func/func/ui"
)

var lineIndicatorFrames = []string{
"╴ ",
"─ ",
"╶╴",
" ─",
" ╶",
" ╶",
" ─",
"╶╴",
"─ ",
"╴ ",
"╴ ",
}

func lineIndicator(_ ui.Frame) string {
frameDur := time.Second / 25
num := int(time.Now().UnixNano()/frameDur.Nanoseconds()) % len(lineIndicatorFrames)
return lineIndicatorFrames[num]
}
Loading

0 comments on commit 9a3b481

Please sign in to comment.