github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/edit/dump_buf.go (about)

     1  package edit
     2  
     3  import (
     4  	"fmt"
     5  	"html"
     6  	"strings"
     7  
     8  	"github.com/u-root/u-root/cmds/core/elvish/eval"
     9  )
    10  
    11  func (ed *editor) dumpBuf(fm *eval.Frame) {
    12  	out := fm.OutputFile()
    13  	buf := ed.writer.CurrentBuffer()
    14  	for _, line := range buf.Lines {
    15  		style := ""
    16  		openedSpan := false
    17  		for _, c := range line {
    18  			if c.Style != style {
    19  				if openedSpan {
    20  					fmt.Fprint(out, "</span>")
    21  				}
    22  				var classes []string
    23  				for _, c := range strings.Split(c.Style, ";") {
    24  					classes = append(classes, "sgr-"+c)
    25  				}
    26  				fmt.Fprintf(out,
    27  					`<span class="%s">`, strings.Join(classes, " "))
    28  				style = c.Style
    29  				openedSpan = true
    30  			}
    31  			fmt.Fprintf(out, "%s", html.EscapeString(c.Text))
    32  		}
    33  		if openedSpan {
    34  			fmt.Fprint(out, "</span>")
    35  		}
    36  		fmt.Fprint(out, "\n")
    37  	}
    38  }