mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-14 23:47:09 +08:00
vendor: update buildkit and dockerd
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
1
vendor/github.com/jaguilar/vt100/.travis.yml
generated
vendored
Normal file
1
vendor/github.com/jaguilar/vt100/.travis.yml
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
language: go
|
22
vendor/github.com/jaguilar/vt100/LICENSE
generated
vendored
Normal file
22
vendor/github.com/jaguilar/vt100/LICENSE
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 James Aguilar
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
54
vendor/github.com/jaguilar/vt100/README.md
generated
vendored
Normal file
54
vendor/github.com/jaguilar/vt100/README.md
generated
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
#VT100
|
||||
|
||||
[](https://travis-ci.org/jaguilar/vt100)
|
||||
|
||||
[](https://godoc.org/github.com/jaguilar/vt100)
|
||||
|
||||
This is a vt100 screen reader. It seems to do a pretty
|
||||
decent job of parsing the nethack input stream, which
|
||||
is all I want it for anyway.
|
||||
|
||||
Here is a screenshot of the HTML-formatted screen data:
|
||||
|
||||

|
||||
|
||||
The features we currently support:
|
||||
|
||||
* Cursor movement
|
||||
* Erasing
|
||||
* Many of the text properties -- underline, inverse, blink, etc.
|
||||
* Sixteen colors
|
||||
* Cursor saving and unsaving
|
||||
* UTF-8
|
||||
|
||||
Not currently supported (and no plans to support):
|
||||
|
||||
* Scrolling
|
||||
* Prompts
|
||||
* Other cooked mode features
|
||||
|
||||
The API is not stable! This is a v0 package.
|
||||
|
||||
## Demo
|
||||
|
||||
Try running the demo! Install nethack:
|
||||
|
||||
sudo apt-get install nethack
|
||||
|
||||
Get this code:
|
||||
|
||||
go get github.com/jaguilar/vt100
|
||||
cd $GOPATH/src/githib.com/jaguilar/vt100
|
||||
|
||||
Run this code:
|
||||
|
||||
go run demo/demo.go -port=8080 2>/tmp/error.txt
|
||||
|
||||
Play some nethack and check out the resulting VT100 terminal status:
|
||||
|
||||
# From another terminal . . .
|
||||
xdg-open http://localhost:8080/debug/vt100
|
||||
|
||||
The demo probably assumes Linux (it uses pty-related syscalls). I'll happily
|
||||
accept pull requests that replicate the pty-spawning functions on OSX and
|
||||
Windows.
|
288
vendor/github.com/jaguilar/vt100/command.go
generated
vendored
Normal file
288
vendor/github.com/jaguilar/vt100/command.go
generated
vendored
Normal file
@ -0,0 +1,288 @@
|
||||
package vt100
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UnsupportedError indicates that we parsed an operation that this
|
||||
// terminal does not implement. Such errors indicate that the client
|
||||
// program asked us to perform an action that we don't know how to.
|
||||
// It MAY be safe to continue trying to do additional operations.
|
||||
// This is a distinct category of errors from things we do know how
|
||||
// to do, but are badly encoded, or errors from the underlying io.RuneScanner
|
||||
// that we're reading commands from.
|
||||
type UnsupportedError struct {
|
||||
error
|
||||
}
|
||||
|
||||
var (
|
||||
supportErrors = expvar.NewMap("vt100-unsupported-operations")
|
||||
)
|
||||
|
||||
func supportError(e error) error {
|
||||
supportErrors.Add(e.Error(), 1)
|
||||
return UnsupportedError{e}
|
||||
}
|
||||
|
||||
// Command is a type of object that the terminal can process to perform
|
||||
// an update.
|
||||
type Command interface {
|
||||
display(v *VT100) error
|
||||
}
|
||||
|
||||
// runeCommand is a simple command that just writes a rune
|
||||
// to the current cell and advances the cursor.
|
||||
type runeCommand rune
|
||||
|
||||
func (r runeCommand) display(v *VT100) error {
|
||||
v.put(rune(r))
|
||||
return nil
|
||||
}
|
||||
|
||||
// escapeCommand is a control sequence command. It includes a variety
|
||||
// of control and escape sequences that move and modify the cursor
|
||||
// or the terminal.
|
||||
type escapeCommand struct {
|
||||
cmd rune
|
||||
args string
|
||||
}
|
||||
|
||||
func (c escapeCommand) String() string {
|
||||
return fmt.Sprintf("[%q %U](%v)", c.cmd, c.cmd, c.args)
|
||||
}
|
||||
|
||||
type intHandler func(*VT100, []int) error
|
||||
|
||||
var (
|
||||
// intHandlers are handlers for which all arguments are numbers.
|
||||
// This is most of them -- all the ones that we process. Eventually,
|
||||
// we may add handlers that support non-int args. Those handlers
|
||||
// will instead receive []string, and they'll have to choose on their
|
||||
// own how they might be parsed.
|
||||
intHandlers = map[rune]intHandler{
|
||||
's': save,
|
||||
'7': save,
|
||||
'u': unsave,
|
||||
'8': unsave,
|
||||
'A': relativeMove(-1, 0),
|
||||
'B': relativeMove(1, 0),
|
||||
'C': relativeMove(0, 1),
|
||||
'D': relativeMove(0, -1),
|
||||
'K': eraseColumns,
|
||||
'J': eraseLines,
|
||||
'H': home,
|
||||
'f': home,
|
||||
'm': updateAttributes,
|
||||
}
|
||||
)
|
||||
|
||||
func save(v *VT100, _ []int) error {
|
||||
v.save()
|
||||
return nil
|
||||
}
|
||||
|
||||
func unsave(v *VT100, _ []int) error {
|
||||
v.unsave()
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
codeColors = []color.RGBA{
|
||||
Black,
|
||||
Red,
|
||||
Green,
|
||||
Yellow,
|
||||
Blue,
|
||||
Magenta,
|
||||
Cyan,
|
||||
White,
|
||||
{}, // Not used.
|
||||
DefaultColor,
|
||||
}
|
||||
)
|
||||
|
||||
// A command to update the attributes of the cursor based on the arg list.
|
||||
func updateAttributes(v *VT100, args []int) error {
|
||||
f := &v.Cursor.F
|
||||
|
||||
var unsupported []int
|
||||
for _, x := range args {
|
||||
switch x {
|
||||
case 0:
|
||||
*f = Format{}
|
||||
case 1:
|
||||
f.Intensity = Bright
|
||||
case 2:
|
||||
f.Intensity = Dim
|
||||
case 22:
|
||||
f.Intensity = Normal
|
||||
case 4:
|
||||
f.Underscore = true
|
||||
case 24:
|
||||
f.Underscore = false
|
||||
case 5, 6:
|
||||
f.Blink = true // We don't distinguish between blink speeds.
|
||||
case 25:
|
||||
f.Blink = false
|
||||
case 7:
|
||||
f.Inverse = true
|
||||
case 27:
|
||||
f.Inverse = false
|
||||
case 8:
|
||||
f.Conceal = true
|
||||
case 28:
|
||||
f.Conceal = false
|
||||
case 30, 31, 32, 33, 34, 35, 36, 37, 39:
|
||||
f.Fg = codeColors[x-30]
|
||||
case 40, 41, 42, 43, 44, 45, 46, 47, 49:
|
||||
f.Bg = codeColors[x-40]
|
||||
// 38 and 48 not supported. Maybe someday.
|
||||
default:
|
||||
unsupported = append(unsupported, x)
|
||||
}
|
||||
}
|
||||
|
||||
if unsupported != nil {
|
||||
return supportError(fmt.Errorf("unknown attributes: %v", unsupported))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func relativeMove(y, x int) func(*VT100, []int) error {
|
||||
return func(v *VT100, args []int) error {
|
||||
c := 1
|
||||
if len(args) >= 1 {
|
||||
c = args[0]
|
||||
}
|
||||
// home is 1-indexed, because that's what the terminal sends us. We want to
|
||||
// reuse its sanitization scheme, so we'll just modify our args by that amount.
|
||||
return home(v, []int{v.Cursor.Y + y*c + 1, v.Cursor.X + x*c + 1})
|
||||
}
|
||||
}
|
||||
|
||||
func eraseColumns(v *VT100, args []int) error {
|
||||
d := eraseForward
|
||||
if len(args) > 0 {
|
||||
d = eraseDirection(args[0])
|
||||
}
|
||||
if d > eraseAll {
|
||||
return fmt.Errorf("unknown erase direction: %d", d)
|
||||
}
|
||||
v.eraseColumns(d)
|
||||
return nil
|
||||
}
|
||||
|
||||
func eraseLines(v *VT100, args []int) error {
|
||||
d := eraseForward
|
||||
if len(args) > 0 {
|
||||
d = eraseDirection(args[0])
|
||||
}
|
||||
if d > eraseAll {
|
||||
return fmt.Errorf("unknown erase direction: %d", d)
|
||||
}
|
||||
v.eraseLines(d)
|
||||
return nil
|
||||
}
|
||||
|
||||
func sanitize(v *VT100, y, x int) (int, int, error) {
|
||||
var err error
|
||||
if y < 0 || y >= v.Height || x < 0 || x >= v.Width {
|
||||
err = fmt.Errorf("out of bounds (%d, %d)", y, x)
|
||||
} else {
|
||||
return y, x, nil
|
||||
}
|
||||
|
||||
if y < 0 {
|
||||
y = 0
|
||||
}
|
||||
if y >= v.Height {
|
||||
y = v.Height - 1
|
||||
}
|
||||
if x < 0 {
|
||||
x = 0
|
||||
}
|
||||
if x >= v.Width {
|
||||
x = v.Width - 1
|
||||
}
|
||||
return y, x, err
|
||||
}
|
||||
|
||||
func home(v *VT100, args []int) error {
|
||||
var y, x int
|
||||
if len(args) >= 2 {
|
||||
y, x = args[0]-1, args[1]-1 // home args are 1-indexed.
|
||||
}
|
||||
y, x, err := sanitize(v, y, x) // Clamp y and x to the bounds of the terminal.
|
||||
v.home(y, x) // Try to do something like what the client asked.
|
||||
return err
|
||||
}
|
||||
|
||||
func (c escapeCommand) display(v *VT100) error {
|
||||
f, ok := intHandlers[c.cmd]
|
||||
if !ok {
|
||||
return supportError(c.err(errors.New("unsupported command")))
|
||||
}
|
||||
|
||||
args, err := c.argInts()
|
||||
if err != nil {
|
||||
return c.err(fmt.Errorf("while parsing int args: %v", err))
|
||||
}
|
||||
|
||||
return f(v, args)
|
||||
}
|
||||
|
||||
// err enhances e with information about the current escape command
|
||||
func (c escapeCommand) err(e error) error {
|
||||
return fmt.Errorf("%s: %s", c, e)
|
||||
}
|
||||
|
||||
var csArgsRe = regexp.MustCompile("^([^0-9]*)(.*)$")
|
||||
|
||||
// argInts parses c.args as a slice of at least arity ints. If the number
|
||||
// of ; separated arguments is less than arity, the remaining elements of
|
||||
// the result will be zero. errors only on integer parsing failure.
|
||||
func (c escapeCommand) argInts() ([]int, error) {
|
||||
if len(c.args) == 0 {
|
||||
return make([]int, 0), nil
|
||||
}
|
||||
args := strings.Split(c.args, ";")
|
||||
out := make([]int, len(args))
|
||||
for i, s := range args {
|
||||
x, err := strconv.ParseInt(s, 10, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[i] = int(x)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type controlCommand rune
|
||||
|
||||
const (
|
||||
backspace controlCommand = '\b'
|
||||
_horizontalTab = '\t'
|
||||
linefeed = '\n'
|
||||
_verticalTab = '\v'
|
||||
_formfeed = '\f'
|
||||
carriageReturn = '\r'
|
||||
)
|
||||
|
||||
func (c controlCommand) display(v *VT100) error {
|
||||
switch c {
|
||||
case backspace:
|
||||
v.backspace()
|
||||
case linefeed:
|
||||
v.Cursor.Y++
|
||||
v.Cursor.X = 0
|
||||
case carriageReturn:
|
||||
v.Cursor.X = 0
|
||||
}
|
||||
return nil
|
||||
}
|
5
vendor/github.com/jaguilar/vt100/go.mod
generated
vendored
Normal file
5
vendor/github.com/jaguilar/vt100/go.mod
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
module github.com/jaguilar/vt100
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/stretchr/testify v1.3.0
|
7
vendor/github.com/jaguilar/vt100/go.sum
generated
vendored
Normal file
7
vendor/github.com/jaguilar/vt100/go.sum
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
97
vendor/github.com/jaguilar/vt100/scanner.go
generated
vendored
Normal file
97
vendor/github.com/jaguilar/vt100/scanner.go
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
package vt100
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Decode decodes one ANSI terminal command from s.
|
||||
//
|
||||
// s should be connected to a client program that expects an
|
||||
// ANSI terminal on the other end. It will push bytes to us that we are meant
|
||||
// to intepret as terminal control codes, or text to place onto the terminal.
|
||||
//
|
||||
// This Command alone does not actually update the terminal. You need to pass
|
||||
// it to VT100.Process().
|
||||
//
|
||||
// You should not share s with any other reader, because it could leave
|
||||
// the stream in an invalid state.
|
||||
func Decode(s io.RuneScanner) (Command, error) {
|
||||
r, size, err := s.ReadRune()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r == unicode.ReplacementChar && size == 1 {
|
||||
return nil, fmt.Errorf("non-utf8 data from reader")
|
||||
}
|
||||
|
||||
if r == escape || r == monogramCsi { // At beginning of escape sequence.
|
||||
s.UnreadRune()
|
||||
return scanEscapeCommand(s)
|
||||
}
|
||||
|
||||
if unicode.IsControl(r) {
|
||||
return controlCommand(r), nil
|
||||
}
|
||||
|
||||
return runeCommand(r), nil
|
||||
}
|
||||
|
||||
const (
|
||||
// There are two ways to begin an escape sequence. One is to put the escape byte.
|
||||
// The other is to put the single-rune control sequence indicator, which is equivalent
|
||||
// to putting "\u001b[".
|
||||
escape = '\u001b'
|
||||
monogramCsi = '\u009b'
|
||||
)
|
||||
|
||||
var (
|
||||
csEnd = &unicode.RangeTable{R16: []unicode.Range16{{Lo: 64, Hi: 126, Stride: 1}}}
|
||||
)
|
||||
|
||||
// scanEscapeCommand scans to the end of the current escape sequence. The scanner
|
||||
// must be positioned at an escape rune (esc or the unicode CSI).
|
||||
func scanEscapeCommand(s io.RuneScanner) (Command, error) {
|
||||
csi := false
|
||||
esc, _, err := s.ReadRune()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if esc != escape && esc != monogramCsi {
|
||||
return nil, fmt.Errorf("invalid content")
|
||||
}
|
||||
if esc == monogramCsi {
|
||||
csi = true
|
||||
}
|
||||
|
||||
var args bytes.Buffer
|
||||
quote := false
|
||||
for i := 0; ; i++ {
|
||||
r, _, err := s.ReadRune()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if i == 0 && r == '[' {
|
||||
csi = true
|
||||
continue
|
||||
}
|
||||
|
||||
if !csi {
|
||||
return escapeCommand{r, ""}, nil
|
||||
} else if quote == false && unicode.Is(csEnd, r) {
|
||||
return escapeCommand{r, args.String()}, nil
|
||||
}
|
||||
|
||||
if r == '"' {
|
||||
quote = !quote
|
||||
}
|
||||
|
||||
// Otherwise, we're still in the args, and this rune is one of those args.
|
||||
if _, err := args.WriteRune(r); err != nil {
|
||||
panic(err) // WriteRune cannot return an error from bytes.Buffer.
|
||||
}
|
||||
}
|
||||
}
|
435
vendor/github.com/jaguilar/vt100/vt100.go
generated
vendored
Normal file
435
vendor/github.com/jaguilar/vt100/vt100.go
generated
vendored
Normal file
@ -0,0 +1,435 @@
|
||||
// package vt100 implements a quick-and-dirty programmable ANSI terminal emulator.
|
||||
//
|
||||
// You could, for example, use it to run a program like nethack that expects
|
||||
// a terminal as a subprocess. It tracks the position of the cursor,
|
||||
// colors, and various other aspects of the terminal's state, and
|
||||
// allows you to inspect them.
|
||||
//
|
||||
// We do very much mean the dirty part. It's not that we think it might have
|
||||
// bugs. It's that we're SURE it does. Currently, we only handle raw mode, with no
|
||||
// cooked mode features like scrolling. We also misinterpret some of the control
|
||||
// codes, which may or may not matter for your purpose.
|
||||
package vt100
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Intensity int
|
||||
|
||||
const (
|
||||
Normal Intensity = 0
|
||||
Bright = 1
|
||||
Dim = 2
|
||||
// TODO(jaguilar): Should this be in a subpackage, since the names are pretty collide-y?
|
||||
)
|
||||
|
||||
var (
|
||||
// Technically RGBAs are supposed to be premultiplied. But CSS doesn't expect them
|
||||
// that way, so we won't do it in this file.
|
||||
DefaultColor = color.RGBA{0, 0, 0, 0}
|
||||
// Our black has 255 alpha, so it will compare negatively with DefaultColor.
|
||||
Black = color.RGBA{0, 0, 0, 255}
|
||||
Red = color.RGBA{255, 0, 0, 255}
|
||||
Green = color.RGBA{0, 255, 0, 255}
|
||||
Yellow = color.RGBA{255, 255, 0, 255}
|
||||
Blue = color.RGBA{0, 0, 255, 255}
|
||||
Magenta = color.RGBA{255, 0, 255, 255}
|
||||
Cyan = color.RGBA{0, 255, 255, 255}
|
||||
White = color.RGBA{255, 255, 255, 255}
|
||||
)
|
||||
|
||||
func (i Intensity) alpha() uint8 {
|
||||
switch i {
|
||||
case Bright:
|
||||
return 255
|
||||
case Normal:
|
||||
return 170
|
||||
case Dim:
|
||||
return 85
|
||||
default:
|
||||
return 170
|
||||
}
|
||||
}
|
||||
|
||||
// Format represents the display format of text on a terminal.
|
||||
type Format struct {
|
||||
// Fg is the foreground color.
|
||||
Fg color.RGBA
|
||||
// Bg is the background color.
|
||||
Bg color.RGBA
|
||||
// Intensity is the text intensity (bright, normal, dim).
|
||||
Intensity Intensity
|
||||
// Various text properties.
|
||||
Underscore, Conceal, Negative, Blink, Inverse bool
|
||||
}
|
||||
|
||||
func toCss(c color.RGBA) string {
|
||||
return fmt.Sprintf("rgba(%d, %d, %d, %f)", c.R, c.G, c.B, float32(c.A)/255)
|
||||
}
|
||||
|
||||
func (f Format) css() string {
|
||||
parts := make([]string, 0)
|
||||
fg, bg := f.Fg, f.Bg
|
||||
if f.Inverse {
|
||||
bg, fg = fg, bg
|
||||
}
|
||||
|
||||
if f.Intensity != Normal {
|
||||
// Intensity only applies to the text -- i.e., the foreground.
|
||||
fg.A = f.Intensity.alpha()
|
||||
}
|
||||
|
||||
if fg != DefaultColor {
|
||||
parts = append(parts, "color:"+toCss(fg))
|
||||
}
|
||||
if bg != DefaultColor {
|
||||
parts = append(parts, "background-color:"+toCss(bg))
|
||||
}
|
||||
if f.Underscore {
|
||||
parts = append(parts, "text-decoration:underline")
|
||||
}
|
||||
if f.Conceal {
|
||||
parts = append(parts, "display:none")
|
||||
}
|
||||
if f.Blink {
|
||||
parts = append(parts, "text-decoration:blink")
|
||||
}
|
||||
|
||||
// We're not in performance sensitive code. Although this sort
|
||||
// isn't strictly necessary, it gives us the nice property that
|
||||
// the style of a particular set of attributes will always be
|
||||
// generated the same way. As a result, we can use the html
|
||||
// output in tests.
|
||||
sort.StringSlice(parts).Sort()
|
||||
|
||||
return strings.Join(parts, ";")
|
||||
}
|
||||
|
||||
// Cursor represents both the position and text type of the cursor.
|
||||
type Cursor struct {
|
||||
// Y and X are the coordinates.
|
||||
Y, X int
|
||||
|
||||
// F is the format that will be displayed.
|
||||
F Format
|
||||
}
|
||||
|
||||
// VT100 represents a simplified, raw VT100 terminal.
|
||||
type VT100 struct {
|
||||
// Height and Width are the dimensions of the terminal.
|
||||
Height, Width int
|
||||
|
||||
// Content is the text in the terminal.
|
||||
Content [][]rune
|
||||
|
||||
// Format is the display properties of each cell.
|
||||
Format [][]Format
|
||||
|
||||
// Cursor is the current state of the cursor.
|
||||
Cursor Cursor
|
||||
|
||||
// savedCursor is the state of the cursor last time save() was called.
|
||||
savedCursor Cursor
|
||||
|
||||
unparsed []byte
|
||||
}
|
||||
|
||||
// NewVT100 creates a new VT100 object with the specified dimensions. y and x
|
||||
// must both be greater than zero.
|
||||
//
|
||||
// Each cell is set to contain a ' ' rune, and all formats are left as the
|
||||
// default.
|
||||
func NewVT100(y, x int) *VT100 {
|
||||
if y == 0 || x == 0 {
|
||||
panic(fmt.Errorf("invalid dim (%d, %d)", y, x))
|
||||
}
|
||||
|
||||
v := &VT100{
|
||||
Height: y,
|
||||
Width: x,
|
||||
Content: make([][]rune, y),
|
||||
Format: make([][]Format, y),
|
||||
}
|
||||
|
||||
for row := 0; row < y; row++ {
|
||||
v.Content[row] = make([]rune, x)
|
||||
v.Format[row] = make([]Format, x)
|
||||
|
||||
for col := 0; col < x; col++ {
|
||||
v.clear(row, col)
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *VT100) UsedHeight() int {
|
||||
count := 0
|
||||
for _, l := range v.Content {
|
||||
for _, r := range l {
|
||||
if r != ' ' {
|
||||
count++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func (v *VT100) Resize(y, x int) {
|
||||
if y > v.Height {
|
||||
n := y - v.Height
|
||||
for row := 0; row < n; row++ {
|
||||
v.Content = append(v.Content, make([]rune, v.Width))
|
||||
v.Format = append(v.Format, make([]Format, v.Width))
|
||||
for col := 0; col < v.Width; col++ {
|
||||
v.clear(v.Height+row, col)
|
||||
}
|
||||
}
|
||||
v.Height = y
|
||||
} else if y < v.Height {
|
||||
v.Content = v.Content[:y]
|
||||
v.Height = y
|
||||
}
|
||||
|
||||
if x > v.Width {
|
||||
for i := range v.Content {
|
||||
row := make([]rune, x)
|
||||
copy(row, v.Content[i])
|
||||
v.Content[i] = row
|
||||
format := make([]Format, x)
|
||||
copy(format, v.Format[i])
|
||||
v.Format[i] = format
|
||||
for j := v.Width; j < x; j++ {
|
||||
v.clear(i, j)
|
||||
}
|
||||
}
|
||||
v.Width = x
|
||||
} else if x < v.Width {
|
||||
for i := range v.Content {
|
||||
v.Content[i] = v.Content[i][:x]
|
||||
v.Format[i] = v.Format[i][:x]
|
||||
}
|
||||
v.Width = x
|
||||
}
|
||||
}
|
||||
|
||||
func (v *VT100) Write(dt []byte) (int, error) {
|
||||
n := len(dt)
|
||||
if len(v.unparsed) > 0 {
|
||||
dt = append(v.unparsed, dt...) // this almost never happens
|
||||
v.unparsed = nil
|
||||
}
|
||||
buf := bytes.NewBuffer(dt)
|
||||
for {
|
||||
if buf.Len() == 0 {
|
||||
return n, nil
|
||||
}
|
||||
cmd, err := Decode(buf)
|
||||
if err != nil {
|
||||
if l := buf.Len(); l > 0 && l < 12 { // on small leftover handle unparsed, otherwise skip
|
||||
v.unparsed = buf.Bytes()
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
v.Process(cmd) // ignore error
|
||||
}
|
||||
}
|
||||
|
||||
// Process handles a single ANSI terminal command, updating the terminal
|
||||
// appropriately.
|
||||
//
|
||||
// One special kind of error that this can return is an UnsupportedError. It's
|
||||
// probably best to check for these and skip, because they are likely recoverable.
|
||||
// Support errors are exported as expvars, so it is possibly not necessary to log
|
||||
// them. If you want to check what's failed, start a debug http server and examine
|
||||
// the vt100-unsupported-commands field in /debug/vars.
|
||||
func (v *VT100) Process(c Command) error {
|
||||
return c.display(v)
|
||||
}
|
||||
|
||||
// HTML renders v as an HTML fragment. One idea for how to use this is to debug
|
||||
// the current state of the screen reader.
|
||||
func (v *VT100) HTML() string {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(`<pre style="color:white;background-color:black;">`)
|
||||
|
||||
// Iterate each row. When the css changes, close the previous span, and open
|
||||
// a new one. No need to close a span when the css is empty, we won't have
|
||||
// opened one in the past.
|
||||
var lastFormat Format
|
||||
for y, row := range v.Content {
|
||||
for x, r := range row {
|
||||
f := v.Format[y][x]
|
||||
if f != lastFormat {
|
||||
if lastFormat != (Format{}) {
|
||||
buf.WriteString("</span>")
|
||||
}
|
||||
if f != (Format{}) {
|
||||
buf.WriteString(`<span style="` + f.css() + `">`)
|
||||
}
|
||||
lastFormat = f
|
||||
}
|
||||
if s := maybeEscapeRune(r); s != "" {
|
||||
buf.WriteString(s)
|
||||
} else {
|
||||
buf.WriteRune(r)
|
||||
}
|
||||
}
|
||||
buf.WriteRune('\n')
|
||||
}
|
||||
buf.WriteString("</pre>")
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// maybeEscapeRune potentially escapes a rune for display in an html document.
|
||||
// It only escapes the things that html.EscapeString does, but it works without allocating
|
||||
// a string to hold r. Returns an empty string if there is no need to escape.
|
||||
func maybeEscapeRune(r rune) string {
|
||||
switch r {
|
||||
case '&':
|
||||
return "&"
|
||||
case '\'':
|
||||
return "'"
|
||||
case '<':
|
||||
return "<"
|
||||
case '>':
|
||||
return ">"
|
||||
case '"':
|
||||
return """
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// put puts r onto the current cursor's position, then advances the cursor.
|
||||
func (v *VT100) put(r rune) {
|
||||
v.scrollIfNeeded()
|
||||
v.Content[v.Cursor.Y][v.Cursor.X] = r
|
||||
v.Format[v.Cursor.Y][v.Cursor.X] = v.Cursor.F
|
||||
v.advance()
|
||||
}
|
||||
|
||||
// advance advances the cursor, wrapping to the next line if need be.
|
||||
func (v *VT100) advance() {
|
||||
v.Cursor.X++
|
||||
if v.Cursor.X >= v.Width {
|
||||
v.Cursor.X = 0
|
||||
v.Cursor.Y++
|
||||
}
|
||||
// if v.Cursor.Y >= v.Height {
|
||||
// // TODO(jaguilar): if we implement scroll, this should probably scroll.
|
||||
// // v.Cursor.Y = 0
|
||||
// v.scroll()
|
||||
// }
|
||||
}
|
||||
|
||||
func (v *VT100) scrollIfNeeded() {
|
||||
if v.Cursor.Y >= v.Height {
|
||||
first := v.Content[0]
|
||||
copy(v.Content, v.Content[1:])
|
||||
for i := range first {
|
||||
first[i] = ' '
|
||||
}
|
||||
v.Content[v.Height-1] = first
|
||||
v.Cursor.Y = v.Height - 1
|
||||
}
|
||||
}
|
||||
|
||||
// home moves the cursor to the coordinates y x. If y x are out of bounds, v.Err
|
||||
// is set.
|
||||
func (v *VT100) home(y, x int) {
|
||||
v.Cursor.Y, v.Cursor.X = y, x
|
||||
}
|
||||
|
||||
// eraseDirection is the logical direction in which an erase command happens,
|
||||
// from the cursor. For both erase commands, forward is 0, backward is 1,
|
||||
// and everything is 2.
|
||||
type eraseDirection int
|
||||
|
||||
const (
|
||||
// From the cursor to the end, inclusive.
|
||||
eraseForward eraseDirection = iota
|
||||
|
||||
// From the beginning to the cursor, inclusive.
|
||||
eraseBack
|
||||
|
||||
// Everything.
|
||||
eraseAll
|
||||
)
|
||||
|
||||
// eraseColumns erases columns from the current line.
|
||||
func (v *VT100) eraseColumns(d eraseDirection) {
|
||||
y, x := v.Cursor.Y, v.Cursor.X // Aliases for simplicity.
|
||||
switch d {
|
||||
case eraseBack:
|
||||
v.eraseRegion(y, 0, y, x)
|
||||
case eraseForward:
|
||||
v.eraseRegion(y, x, y, v.Width-1)
|
||||
case eraseAll:
|
||||
v.eraseRegion(y, 0, y, v.Width-1)
|
||||
}
|
||||
}
|
||||
|
||||
// eraseLines erases lines from the current terminal. Note that
|
||||
// no matter what is selected, the entire current line is erased.
|
||||
func (v *VT100) eraseLines(d eraseDirection) {
|
||||
y := v.Cursor.Y // Alias for simplicity.
|
||||
switch d {
|
||||
case eraseBack:
|
||||
v.eraseRegion(0, 0, y, v.Width-1)
|
||||
case eraseForward:
|
||||
v.eraseRegion(y, 0, v.Height-1, v.Width-1)
|
||||
case eraseAll:
|
||||
v.eraseRegion(0, 0, v.Height-1, v.Width-1)
|
||||
}
|
||||
}
|
||||
|
||||
func (v *VT100) eraseRegion(y1, x1, y2, x2 int) {
|
||||
// Do not sanitize or bounds-check these coordinates, since they come from the
|
||||
// programmer (me). We should panic if any of them are out of bounds.
|
||||
if y1 > y2 {
|
||||
y1, y2 = y2, y1
|
||||
}
|
||||
if x1 > x2 {
|
||||
x1, x2 = x2, x1
|
||||
}
|
||||
|
||||
for y := y1; y <= y2; y++ {
|
||||
for x := x1; x <= x2; x++ {
|
||||
v.clear(y, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *VT100) clear(y, x int) {
|
||||
if y >= len(v.Content) || x >= len(v.Content[0]) {
|
||||
return
|
||||
}
|
||||
v.Content[y][x] = ' '
|
||||
v.Format[y][x] = Format{}
|
||||
}
|
||||
|
||||
func (v *VT100) backspace() {
|
||||
v.Cursor.X--
|
||||
if v.Cursor.X < 0 {
|
||||
if v.Cursor.Y == 0 {
|
||||
v.Cursor.X = 0
|
||||
} else {
|
||||
v.Cursor.Y--
|
||||
v.Cursor.X = v.Width - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *VT100) save() {
|
||||
v.savedCursor = v.Cursor
|
||||
}
|
||||
|
||||
func (v *VT100) unsave() {
|
||||
v.Cursor = v.savedCursor
|
||||
}
|
Reference in New Issue
Block a user