Skip to content

Commit

Permalink
Added styles type, which translates human-readable values to ANSI codes
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli authored and xiaq committed Jan 11, 2017
1 parent dab13c5 commit 94e0275
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 127 deletions.
2 changes: 1 addition & 1 deletion edit/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (b *buffer) writes(s string, style string) {
}

func (b *buffer) writeStyled(s *styled) {
b.writes(s.text, s.style)
b.writes(s.text, s.styles.String())
}

func (b *buffer) writeStyleds(ss []*styled) {
Expand Down
2 changes: 1 addition & 1 deletion edit/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func callFnForPrompt(ed *Editor, fn eval.Fn) []*styled {
if s, ok := v.(*styled); ok {
ss = append(ss, s)
} else {
ss = append(ss, &styled{eval.ToString(v), ""})
ss = append(ss, &styled{eval.ToString(v), styles{}})
}
}
return ss
Expand Down
2 changes: 1 addition & 1 deletion edit/completers.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func complFilenameInner(head string, executableOnly bool) ([]*candidate, error)

cands = append(cands, &candidate{
text: full, suffix: suffix,
display: styled{name, lsColor.getStyle(full)},
display: styled{name, stylesFromString(lsColor.getStyle(full))},
})
}

Expand Down
18 changes: 9 additions & 9 deletions edit/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func (c *completion) ModeLine(width int) *buffer {
b.writes(" ", "")
// Write title
title := fmt.Sprintf("COMPLETING %s", c.completer)
b.writes(util.TrimWcwidth(title, width), styleForMode)
b.writes(util.TrimWcwidth(title, width), styleForMode.String())
// Write filter
if c.filtering {
b.writes(" ", "")
b.writes(c.filter, styleForFilter)
b.writes(c.filter, styleForFilter.String())
b.dot = b.cursor()
}
// Write horizontal scrollbar, using the remaining space
Expand Down Expand Up @@ -331,15 +331,15 @@ func (comp *completion) List(width, maxHeight int) *buffer {
}
if j >= len(cands) {
// Write padding to make the listing a rectangle.
col.writePadding(totalColWidth, styleForCompletion)
col.writePadding(totalColWidth, styleForCompletion.String())
} else {
col.writePadding(completionColMarginLeft, styleForCompletion)
style := joinStyle(styleForCompletion, cands[j].display.style)
col.writePadding(completionColMarginLeft, styleForCompletion.String())
s := joinStyles(styleForCompletion, cands[j].display.styles)
if j == comp.selected {
style = joinStyle(style, styleForSelectedCompletion)
s = append(s, styleForSelectedCompletion)
}
col.writes(util.ForceWcwidth(cands[j].display.text, colWidth), style)
col.writePadding(completionColMarginRight, styleForCompletion)
col.writes(util.ForceWcwidth(cands[j].display.text, colWidth), s.String())
col.writePadding(completionColMarginRight, styleForCompletion.String())
if !trimmed {
comp.lastShownInFull = j
}
Expand All @@ -360,7 +360,7 @@ func (comp *completion) List(width, maxHeight int) *buffer {
if i > 0 {
col.newline()
}
col.writePadding(remainedWidth, styleForCompletion)
col.writePadding(remainedWidth, styleForCompletion.String())
}
b.extendHorizontal(col, 0)
remainedWidth = 0
Expand Down
2 changes: 1 addition & 1 deletion edit/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (ed *Editor) refresh(fullRefresh bool, tips bool) error {
p := err.Begin
for i, token := range ed.tokens {
if token.Node.Begin() <= p && p < token.Node.End() {
ed.tokens[i].addStyle(styleForCompilerError)
ed.tokens[i].MoreStyle = joinStyles(ed.tokens[i].MoreStyle, styleForCompilerError)
break
}
}
Expand Down
20 changes: 10 additions & 10 deletions edit/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (l *listing) ModeLine(width int) *buffer {
title := l.provider.ModeTitle(l.selected)
// TODO keep it one line.
b := newBuffer(width)
b.writes(util.TrimWcwidth(title, width), styleForMode)
b.writes(util.TrimWcwidth(title, width), styleForMode.String())
b.writes(" ", "")
b.writes(l.filter, styleForFilter)
b.writes(l.filter, styleForFilter.String())
b.dot = b.cursor()
return b
}
Expand Down Expand Up @@ -78,13 +78,13 @@ func (l *listing) List(width, maxHeight int) *buffer {
getEntry := func(i int) []styled {
s := l.provider.Show(i, width)
lines := strings.Split(s.text, "\n")
style := s.style
st := s.styles
if i == l.selected {
style = joinStyle(style, styleForSelected)
st = append(st, styleForSelected.String())
}
styleds := make([]styled, len(lines))
for i, line := range lines {
styleds[i] = styled{line, style}
styleds[i] = styled{line, st}
}
return styleds
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (l *listing) List(width, maxHeight int) *buffer {
if p != lines.Front() {
b.newline()
}
b.writes(s.text, s.style)
b.writes(s.text, s.styles.String())
}

if scrollbar != nil {
Expand All @@ -148,9 +148,9 @@ func writeHorizontalScrollbar(b *buffer, n, low, high, width int) {
slow, shigh := findScrollInterval(n, low, high, width)
for i := 0; i < width; i++ {
if slow <= i && i < shigh {
b.write(' ', styleForScrollBarThumb)
b.write(' ', styleForScrollBarThumb.String())
} else {
b.write('━', styleForScrollBarArea)
b.write('━', styleForScrollBarArea.String())
}
}
}
Expand All @@ -164,9 +164,9 @@ func renderScrollbar(n, low, high, height int) *buffer {
b.newline()
}
if slow <= i && i < shigh {
b.write(' ', styleForScrollBarThumb)
b.write(' ', styleForScrollBarThumb.String())
} else {
b.write('│', styleForScrollBarArea)
b.write('│', styleForScrollBarArea.String())
}
}
return b
Expand Down
12 changes: 6 additions & 6 deletions edit/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func (n *navigation) ModeLine(width int) *buffer {
s += "(show hidden) "
}
b := newBuffer(width)
b.writes(util.TrimWcwidth(s, width), styleForMode)
b.writes(util.TrimWcwidth(s, width), styleForMode.String())
b.writes(" ", "")
b.writes(n.filter, styleForFilter)
b.writes(n.filter, styleForFilter.String())
b.dot = b.cursor()
return b
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func (n *navigation) loaddir(dir string) ([]styled, error) {
for _, info := range infos {
if n.showHidden || info.Name()[0] != '.' {
name := info.Name()
all = append(all, styled{name, lsColor.getStyle(path.Join(dir, name))})
all = append(all, styled{name, stylesFromString(lsColor.getStyle(path.Join(dir, name)))})
}
}
sortStyleds(all)
Expand Down Expand Up @@ -393,7 +393,7 @@ func newFilePreviewNavColumn(fname string) *navColumn {
lines := strings.Split(content, "\n")
styleds := make([]styled, len(lines))
for i, line := range lines {
styleds[i] = styled{strings.Replace(line, "\t", " ", -1), ""}
styleds[i] = styled{strings.Replace(line, "\t", " ", -1), styles{}}
}
return newNavColumn(styleds, func(int) bool { return false })
}
Expand All @@ -412,9 +412,9 @@ func (nc *navColumn) Len() int {
func (nc *navColumn) Show(i, w int) styled {
s := nc.candidates[i]
if w >= navigationListingMinWidthForPadding {
return styled{" " + util.ForceWcwidth(s.text, w-2), s.style}
return styled{" " + util.ForceWcwidth(s.text, w-2), s.styles}
}
return styled{util.ForceWcwidth(s.text, w), s.style}
return styled{util.ForceWcwidth(s.text, w), s.styles}
}

func (nc *navColumn) Filter(filter string) int {
Expand Down
4 changes: 2 additions & 2 deletions edit/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func defaultPrompts() (eval.FnValue, eval.FnValue) {
// Make default prompts.
prompt := func(ec *eval.EvalCtx, args []eval.Value, opts map[string]eval.Value) {
out := ec.OutputChan()
out <- &styled{util.Getwd() + "> ", ""}
out <- &styled{util.Getwd() + "> ", styles{}}
}

username := "???"
Expand All @@ -38,7 +38,7 @@ func defaultPrompts() (eval.FnValue, eval.FnValue) {
rpromptStr := username + "@" + hostname
rprompt := func(ec *eval.EvalCtx, args []eval.Value, opts map[string]eval.Value) {
out := ec.OutputChan()
out <- &styled{rpromptStr, "7"}
out <- &styled{rpromptStr, styles{"7"}}
}

return &eval.BuiltinFn{"default prompt", prompt}, &eval.BuiltinFn{"default rprompt", rprompt}
Expand Down
Loading

0 comments on commit 94e0275

Please sign in to comment.