edit: attr -> style

This commit is contained in:
Qi Xiao 2016-01-31 02:36:26 +01:00
parent f15bbaeab5
commit 981955abee
5 changed files with 64 additions and 64 deletions

View File

@ -36,8 +36,8 @@ func complVariable(n parse.Node, ed *Editor) []*candidate {
for variable := range ed.evaler.Global() {
if strings.HasPrefix(variable, head) {
cands = append(cands, &candidate{
source: styled{variable[len(head):], attrForType[Variable]},
menu: styled{"$" + variable, attrForType[Variable]}})
source: styled{variable[len(head):], styleForType[Variable]},
menu: styled{"$" + variable, styleForType[Variable]}})
}
}
return cands
@ -119,8 +119,8 @@ func complArg(cn *parse.Compound, head string, ed *Editor) []*candidate {
return complArgInner(head, false, ed, false)
}
// TODO: all of fileNames, determineAttr and the final directory check do
// stat on files.
// TODO: all of fileNames, getStyle and the final directory check do stat on
// files.
func complArgInner(head string, indir bool, ed *Editor, formHead bool) []*candidate {
var dir, file, indirSlash string
if indir {
@ -153,7 +153,7 @@ func complArgInner(head string, indir bool, ed *Editor, formHead bool) []*candid
}
cand := &candidate{
source: styled{indirSlash + s[len(file):], ""},
menu: styled{s, defaultLsColor.determineAttr(full)},
menu: styled{s, defaultLsColor.getStyle(full)},
}
cands = append(cands, cand)
}

View File

@ -62,8 +62,8 @@ var featureForName = map[string]fileFeature{
}
type lsColor struct {
attrForFeature map[fileFeature]string
attrForExt map[string]string
styleForFeature map[fileFeature]string
styleForExt map[string]string
}
const defaultLsColorString = `rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:`
@ -81,13 +81,13 @@ func parseLsColor(s string) *lsColor {
}
key, value := words[0], words[1]
if strings.HasPrefix(key, "*.") {
lc.attrForExt[key[2:]] = value
lc.styleForExt[key[2:]] = value
} else {
feature, ok := featureForName[key]
if !ok {
continue
}
lc.attrForFeature[feature] = value
lc.styleForFeature[feature] = value
}
}
return lc
@ -173,18 +173,18 @@ func determineFeature(fname string, mh bool) (fileFeature, error) {
return featureRegular, nil
}
func (lc *lsColor) determineAttr(fname string) string {
mh := strings.Trim(lc.attrForFeature[featureMultiHardLink], "0") != ""
func (lc *lsColor) getStyle(fname string) string {
mh := strings.Trim(lc.styleForFeature[featureMultiHardLink], "0") != ""
// TODO Handle error from determineFeature
feature, _ := determineFeature(fname, mh)
if feature == featureRegular {
if ext := path.Ext(fname); ext != "" {
if attr, ok := lc.attrForExt[ext]; ok {
return attr
if style, ok := lc.styleForExt[ext]; ok {
return style
}
}
}
return lc.attrForFeature[feature]
return lc.styleForFeature[feature]
}
func init() {

View File

@ -14,13 +14,13 @@ var (
type navColumn struct {
names []string
attrs []string
styles []string
selected int
err error
}
func newNavColumn(names, attrs []string) *navColumn {
nc := &navColumn{names, attrs, 0, nil}
func newNavColumn(names, styles []string) *navColumn {
nc := &navColumn{names, styles, 0, nil}
nc.resetSelected()
return nc
}
@ -59,7 +59,7 @@ func newNavigation() *navigation {
return n
}
func readdirnames(dir string) (names, attrs []string, err error) {
func readdirnames(dir string) (names, styles []string, err error) {
f, err := os.Open(dir)
if err != nil {
return nil, nil, err
@ -69,11 +69,11 @@ func readdirnames(dir string) (names, attrs []string, err error) {
return nil, nil, err
}
sort.Strings(names)
attrs = make([]string, len(names))
styles = make([]string, len(names))
for i, name := range names {
attrs[i] = defaultLsColor.determineAttr(path.Join(dir, name))
styles[i] = defaultLsColor.getStyle(path.Join(dir, name))
}
return names, attrs, nil
return names, styles, nil
}
func (n *navigation) maintainSelected(name string) {
@ -86,12 +86,12 @@ func (n *navigation) maintainSelected(name string) {
func (n *navigation) refreshCurrent() {
selectedName := n.current.selectedName()
names, attrs, err := readdirnames(".")
names, styles, err := readdirnames(".")
if err != nil {
n.current = newErrNavColumn(err)
return
}
n.current = newNavColumn(names, attrs)
n.current = newNavColumn(names, styles)
if selectedName != "" {
// Maintain n.current.selected. The same file, if still present, is
// selected. Otherwise a file near it is selected.
@ -110,12 +110,12 @@ func (n *navigation) refreshParent() {
if wd == "/" {
n.parent = newNavColumn(nil, nil)
} else {
names, attrs, err := readdirnames("..")
names, styles, err := readdirnames("..")
if err != nil {
n.parent = newErrNavColumn(err)
return
}
n.parent = newNavColumn(names, attrs)
n.parent = newNavColumn(names, styles)
cwd, err := os.Stat(".")
if err != nil {
@ -142,12 +142,12 @@ func (n *navigation) refreshDirPreview() {
return
}
if fi.Mode().IsDir() {
names, attrs, err := readdirnames(name)
names, styles, err := readdirnames(name)
if err != nil {
n.dirPreview = newErrNavColumn(err)
return
}
n.dirPreview = newNavColumn(names, attrs)
n.dirPreview = newNavColumn(names, styles)
} else {
// TODO(xiaq): Support regular file preview in navigation mode
n.dirPreview = nil

View File

@ -2,17 +2,17 @@ package edit
// Styles for UI.
var (
attrForPrompt = ""
attrForRprompt = "7"
attrForCompleted = ";4"
attrForMode = "1;3;35"
attrForTip = ""
attrForCurrentCompletion = ";7"
attrForCompletedHistory = "4"
attrForSelectedFile = ";7"
styleForPrompt = ""
styleForRPrompt = "7"
styleForCompleted = ";4"
styleForMode = "1;3;35"
styleForTip = ""
styleForCurrentCompletion = ";7"
styleForCompletedHistory = "4"
styleForSelectedFile = ";7"
)
var attrForType = map[TokenType]string{
var styleForType = map[TokenType]string{
ParserError: "31;3",
Bareword: "",
SingleQuoted: "33",

View File

@ -23,7 +23,7 @@ const (
type cell struct {
rune
width byte
attr string
style string
}
// pos is the position within a buffer.
@ -116,7 +116,7 @@ func (b *buffer) extendHorizontal(b2 *buffer, w, m int) {
}
// write appends a single rune to a buffer.
func (b *buffer) write(r rune, attr string) {
func (b *buffer) write(r rune, style string) {
if r == '\n' {
b.newline()
return
@ -125,7 +125,7 @@ func (b *buffer) write(r rune, attr string) {
return
}
wd := WcWidth(r)
c := cell{r, byte(wd), attr}
c := cell{r, byte(wd), style}
if b.col+wd > b.width {
b.newline()
@ -138,14 +138,14 @@ func (b *buffer) write(r rune, attr string) {
}
}
func (b *buffer) writes(s string, attr string) {
func (b *buffer) writes(s string, style string) {
for _, r := range s {
b.write(r, attr)
b.write(r, style)
}
}
func (b *buffer) writePadding(w int, attr string) {
b.writes(strings.Repeat(" ", w), attr)
func (b *buffer) writePadding(w int, style string) {
b.writes(strings.Repeat(" ", w), style)
}
func (b *buffer) line() int {
@ -225,7 +225,7 @@ func (w *writer) commitBuffer(buf *buffer) error {
}
bytesBuf.WriteString("\r")
attr := ""
style := ""
for i, line := range buf.cells {
if i > 0 {
bytesBuf.WriteString("\n")
@ -241,9 +241,9 @@ func (w *writer) commitBuffer(buf *buffer) error {
// Move to the first differing column and erase the rest of line
fmt.Fprintf(bytesBuf, "\033[%dG\033[K", j+1)
for _, c := range line[j:] {
if c.width > 0 && c.attr != attr {
fmt.Fprintf(bytesBuf, "\033[m\033[%sm", c.attr)
attr = c.attr
if c.width > 0 && c.style != style {
fmt.Fprintf(bytesBuf, "\033[m\033[%sm", c.style)
style = c.style
}
bytesBuf.WriteString(string(c.rune))
}
@ -252,7 +252,7 @@ func (w *writer) commitBuffer(buf *buffer) error {
if len(w.oldBuf.cells) > len(buf.cells) || fullRefresh {
bytesBuf.WriteString("\n\033[J\033[A")
}
if attr != "" {
if style != "" {
bytesBuf.WriteString("\033[m")
}
cursor := buf.cursor()
@ -311,17 +311,17 @@ func renderNavColumn(nc *navColumn, w, h int) *buffer {
b.newline()
}
text := nc.names[i]
attr := nc.attrs[i]
style := nc.styles[i]
if i == nc.selected {
attr += attrForSelectedFile
style += styleForSelectedFile
}
if w >= navigationListingMinWidthForPadding {
padding := navigationListingColPadding
b.writePadding(padding, attr)
b.writes(ForceWcWidth(text, w-2), attr)
b.writePadding(padding, attr)
b.writePadding(padding, style)
b.writes(ForceWcWidth(text, w-2), style)
b.writePadding(padding, style)
} else {
b.writes(ForceWcWidth(text, w), attr)
b.writes(ForceWcWidth(text, w), style)
}
}
return b
@ -339,7 +339,7 @@ func (w *writer) refresh(es *editorState) error {
b.newlineWhenFull = true
b.writes(es.prompt, attrForPrompt)
b.writes(es.prompt, styleForPrompt)
if b.line() == 0 && b.col*2 < b.width {
b.indent = b.col
@ -356,7 +356,7 @@ func (w *writer) refresh(es *editorState) error {
if hasComp {
// Put the current completion candidate.
candSource := comp.candidates[comp.current].source
b.writes(candSource.text, candSource.style+attrForCompleted)
b.writes(candSource.text, candSource.style+styleForCompleted)
}
b.dot = b.cursor()
}
@ -365,7 +365,7 @@ func (w *writer) refresh(es *editorState) error {
tokens:
for _, token := range es.tokens {
for _, r := range token.Text {
b.write(r, attrForType[token.Type]+token.MoreStyle)
b.write(r, styleForType[token.Type]+token.MoreStyle)
i += utf8.RuneLen(r)
nowAt(i)
@ -379,7 +379,7 @@ tokens:
// Put the rest of current history, position the cursor at the
// end of the line, and finish writing
h := es.history
b.writes(h.line[len(h.prefix):], attrForCompletedHistory)
b.writes(h.line[len(h.prefix):], styleForCompletedHistory)
b.dot = b.cursor()
}
@ -388,7 +388,7 @@ tokens:
if padding >= 1 {
b.newlineWhenFull = false
b.writePadding(padding, "")
b.writes(es.rprompt, attrForRprompt)
b.writes(es.rprompt, styleForRPrompt)
}
// bufMode
@ -406,7 +406,7 @@ tokens:
case modeHistory:
text = fmt.Sprintf("HISTORY #%d", es.history.current)
}
b.writes(TrimWcWidth(" "+text+" ", width), attrForMode)
b.writes(TrimWcWidth(" "+text+" ", width), styleForMode)
}
// bufTips
@ -414,7 +414,7 @@ tokens:
if len(es.tips) > 0 {
b := newBuffer(width)
bufTips = b
b.writes(TrimWcWidth(strings.Join(es.tips, ", "), width), attrForTip)
b.writes(TrimWcWidth(strings.Join(es.tips, ", "), width), styleForTip)
}
hListing := 0
@ -475,12 +475,12 @@ tokens:
if k >= len(cands) {
continue
}
attr := cands[k].menu.style
style := cands[k].menu.style
if k == comp.current {
attr += attrForCurrentCompletion
style += styleForCurrentCompletion
}
text := cands[k].menu.text
b.writes(ForceWcWidth(text, colWidth), attr)
b.writes(ForceWcWidth(text, colWidth), style)
b.writePadding(margin, "")
}
}