├── doc.go ├── go.mod ├── termios_bsd.go ├── termios_nonbsd.go ├── .gitignore ├── windows ├── doc.go ├── ansi_reader_test.go ├── console.go ├── ansi_writer.go └── ansi_reader.go ├── go.sum ├── ascii_test.go ├── termios_unix.go ├── termios_windows.go ├── README.md ├── ascii.go ├── .github ├── workflows │ └── test.yml └── SECURITY.md ├── term_unix.go ├── proxy.go ├── term.go ├── term_test.go ├── term_windows.go ├── proxy_test.go └── LICENSE /doc.go: -------------------------------------------------------------------------------- 1 | // Package term provides structures and helper functions to work with 2 | // terminal (state, sizes). 3 | package term 4 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/moby/term 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c 7 | github.com/creack/pty v1.1.18 8 | golang.org/x/sys v0.1.0 9 | ) 10 | -------------------------------------------------------------------------------- /termios_bsd.go: -------------------------------------------------------------------------------- 1 | //go:build darwin || freebsd || openbsd || netbsd 2 | // +build darwin freebsd openbsd netbsd 3 | 4 | package term 5 | 6 | import ( 7 | "golang.org/x/sys/unix" 8 | ) 9 | 10 | const ( 11 | getTermios = unix.TIOCGETA 12 | setTermios = unix.TIOCSETA 13 | ) 14 | -------------------------------------------------------------------------------- /termios_nonbsd.go: -------------------------------------------------------------------------------- 1 | //go:build !darwin && !freebsd && !netbsd && !openbsd && !windows 2 | // +build !darwin,!freebsd,!netbsd,!openbsd,!windows 3 | 4 | package term 5 | 6 | import ( 7 | "golang.org/x/sys/unix" 8 | ) 9 | 10 | const ( 11 | getTermios = unix.TCGETS 12 | setTermios = unix.TCSETS 13 | ) 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # if you want to ignore files created by your editor/tools, consider using a 2 | # global .gitignore or .git/info/exclude see https://help.github.com/articles/ignoring-files 3 | .* 4 | !.github 5 | !.gitignore 6 | profile.out 7 | # support running go modules in vendor mode for local development 8 | vendor/ 9 | -------------------------------------------------------------------------------- /windows/doc.go: -------------------------------------------------------------------------------- 1 | // These files implement ANSI-aware input and output streams for use by the Docker Windows client. 2 | // When asked for the set of standard streams (e.g., stdin, stdout, stderr), the code will create 3 | // and return pseudo-streams that convert ANSI sequences to / from Windows Console API calls. 4 | 5 | package windowsconsole 6 | -------------------------------------------------------------------------------- /windows/ansi_reader_test.go: -------------------------------------------------------------------------------- 1 | //go:build windows 2 | // +build windows 3 | 4 | package windowsconsole 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/Azure/go-ansiterm" 10 | "github.com/Azure/go-ansiterm/winterm" 11 | ) 12 | 13 | func TestKeyToString(t *testing.T) { 14 | ke := &winterm.KEY_EVENT_RECORD{ 15 | ControlKeyState: winterm.LEFT_ALT_PRESSED, 16 | UnicodeChar: 65, // capital A 17 | } 18 | 19 | const expected = ansiterm.KEY_ESC_N + "a" 20 | out := keyToString(ke, nil) 21 | if out != expected { 22 | t.Errorf("expected %s, got %s", expected, out) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg= 2 | github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= 3 | github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= 4 | github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= 5 | golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 6 | golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= 7 | golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 8 | -------------------------------------------------------------------------------- /ascii_test.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func TestToBytes(t *testing.T) { 9 | codes, err := ToBytes("ctrl-a,a") 10 | if err != nil { 11 | t.Error(err) 12 | } 13 | expected := []byte{1, 97} 14 | if !bytes.Equal(codes, expected) { 15 | t.Errorf("expected: %+v, got: %+v", expected, codes) 16 | } 17 | 18 | _, err = ToBytes("shift-z") 19 | if err == nil { 20 | t.Error("expected and error") 21 | } 22 | 23 | codes, err = ToBytes("ctrl-@,ctrl-[,~,ctrl-o") 24 | if err != nil { 25 | t.Error(err) 26 | } 27 | expected = []byte{0, 27, 126, 15} 28 | if !bytes.Equal(codes, expected) { 29 | t.Errorf("expected: %+v, got: %+v", expected, codes) 30 | } 31 | 32 | codes, err = ToBytes("DEL,+") 33 | if err != nil { 34 | t.Error(err) 35 | } 36 | expected = []byte{127, 43} 37 | if !bytes.Equal(codes, expected) { 38 | t.Errorf("expected: %+v, got: %+v", expected, codes) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /termios_unix.go: -------------------------------------------------------------------------------- 1 | //go:build !windows 2 | // +build !windows 3 | 4 | package term 5 | 6 | import ( 7 | "golang.org/x/sys/unix" 8 | ) 9 | 10 | // Termios is the Unix API for terminal I/O. 11 | // 12 | // Deprecated: use [unix.Termios]. 13 | type Termios = unix.Termios 14 | 15 | func makeRaw(fd uintptr) (*State, error) { 16 | termios, err := tcget(fd) 17 | if err != nil { 18 | return nil, err 19 | } 20 | 21 | oldState := State{termios: *termios} 22 | 23 | termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON 24 | termios.Oflag &^= unix.OPOST 25 | termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN 26 | termios.Cflag &^= unix.CSIZE | unix.PARENB 27 | termios.Cflag |= unix.CS8 28 | termios.Cc[unix.VMIN] = 1 29 | termios.Cc[unix.VTIME] = 0 30 | 31 | if err := tcset(fd, termios); err != nil { 32 | return nil, err 33 | } 34 | return &oldState, nil 35 | } 36 | -------------------------------------------------------------------------------- /termios_windows.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import "golang.org/x/sys/windows" 4 | 5 | func makeRaw(fd uintptr) (*State, error) { 6 | state, err := SaveState(fd) 7 | if err != nil { 8 | return nil, err 9 | } 10 | 11 | mode := state.mode 12 | 13 | // See 14 | // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx 15 | // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx 16 | 17 | // Disable these modes 18 | mode &^= windows.ENABLE_ECHO_INPUT 19 | mode &^= windows.ENABLE_LINE_INPUT 20 | mode &^= windows.ENABLE_MOUSE_INPUT 21 | mode &^= windows.ENABLE_WINDOW_INPUT 22 | mode &^= windows.ENABLE_PROCESSED_INPUT 23 | 24 | // Enable these modes 25 | mode |= windows.ENABLE_EXTENDED_FLAGS 26 | mode |= windows.ENABLE_INSERT_MODE 27 | mode |= windows.ENABLE_QUICK_EDIT_MODE 28 | if vtInputSupported { 29 | mode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT 30 | } 31 | 32 | err = windows.SetConsoleMode(windows.Handle(fd), mode) 33 | if err != nil { 34 | return nil, err 35 | } 36 | return state, nil 37 | } 38 | -------------------------------------------------------------------------------- /windows/console.go: -------------------------------------------------------------------------------- 1 | //go:build windows 2 | // +build windows 3 | 4 | package windowsconsole 5 | 6 | import ( 7 | "os" 8 | 9 | "golang.org/x/sys/windows" 10 | ) 11 | 12 | // GetHandleInfo returns file descriptor and bool indicating whether the file is a console. 13 | func GetHandleInfo(in interface{}) (uintptr, bool) { 14 | switch t := in.(type) { 15 | case *ansiReader: 16 | return t.Fd(), true 17 | case *ansiWriter: 18 | return t.Fd(), true 19 | } 20 | 21 | var inFd uintptr 22 | var isTerminal bool 23 | 24 | if file, ok := in.(*os.File); ok { 25 | inFd = file.Fd() 26 | isTerminal = isConsole(inFd) 27 | } 28 | return inFd, isTerminal 29 | } 30 | 31 | // IsConsole returns true if the given file descriptor is a Windows Console. 32 | // The code assumes that GetConsoleMode will return an error for file descriptors that are not a console. 33 | // 34 | // Deprecated: use [windows.GetConsoleMode] or [golang.org/x/term.IsTerminal]. 35 | func IsConsole(fd uintptr) bool { 36 | return isConsole(fd) 37 | } 38 | 39 | func isConsole(fd uintptr) bool { 40 | var mode uint32 41 | err := windows.GetConsoleMode(windows.Handle(fd), &mode) 42 | return err == nil 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # term - utilities for dealing with terminals 2 | 3 | ![Test](https://github.com/moby/term/workflows/Test/badge.svg) [![GoDoc](https://godoc.org/github.com/moby/term?status.svg)](https://godoc.org/github.com/moby/term) [![Go Report Card](https://goreportcard.com/badge/github.com/moby/term)](https://goreportcard.com/report/github.com/moby/term) 4 | 5 | term provides structures and helper functions to work with terminal (state, sizes). 6 | 7 | #### Using term 8 | 9 | ```go 10 | package main 11 | 12 | import ( 13 | "log" 14 | "os" 15 | 16 | "github.com/moby/term" 17 | ) 18 | 19 | func main() { 20 | fd := os.Stdin.Fd() 21 | if term.IsTerminal(fd) { 22 | ws, err := term.GetWinsize(fd) 23 | if err != nil { 24 | log.Fatalf("term.GetWinsize: %s", err) 25 | } 26 | log.Printf("%d:%d\n", ws.Height, ws.Width) 27 | } 28 | } 29 | ``` 30 | 31 | ## Contributing 32 | 33 | Want to hack on term? [Docker's contributions guidelines](https://github.com/docker/docker/blob/master/CONTRIBUTING.md) apply. 34 | 35 | ## Copyright and license 36 | Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons. 37 | -------------------------------------------------------------------------------- /ascii.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | // ASCII list the possible supported ASCII key sequence 9 | var ASCII = []string{ 10 | "ctrl-@", 11 | "ctrl-a", 12 | "ctrl-b", 13 | "ctrl-c", 14 | "ctrl-d", 15 | "ctrl-e", 16 | "ctrl-f", 17 | "ctrl-g", 18 | "ctrl-h", 19 | "ctrl-i", 20 | "ctrl-j", 21 | "ctrl-k", 22 | "ctrl-l", 23 | "ctrl-m", 24 | "ctrl-n", 25 | "ctrl-o", 26 | "ctrl-p", 27 | "ctrl-q", 28 | "ctrl-r", 29 | "ctrl-s", 30 | "ctrl-t", 31 | "ctrl-u", 32 | "ctrl-v", 33 | "ctrl-w", 34 | "ctrl-x", 35 | "ctrl-y", 36 | "ctrl-z", 37 | "ctrl-[", 38 | "ctrl-\\", 39 | "ctrl-]", 40 | "ctrl-^", 41 | "ctrl-_", 42 | } 43 | 44 | // ToBytes converts a string representing a suite of key-sequence to the corresponding ASCII code. 45 | func ToBytes(keys string) ([]byte, error) { 46 | codes := []byte{} 47 | next: 48 | for _, key := range strings.Split(keys, ",") { 49 | if len(key) != 1 { 50 | for code, ctrl := range ASCII { 51 | if ctrl == key { 52 | codes = append(codes, byte(code)) 53 | continue next 54 | } 55 | } 56 | if key == "DEL" { 57 | codes = append(codes, 127) 58 | } else { 59 | return nil, fmt.Errorf("Unknown character: '%s'", key) 60 | } 61 | } else { 62 | codes = append(codes, key[0]) 63 | } 64 | } 65 | return codes, nil 66 | } 67 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | # Default to 'contents: read', which grants actions to read commits. 4 | # 5 | # If any permission is set, any permission not included in the list is 6 | # implicitly set to "none". 7 | # 8 | # see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions 9 | permissions: 10 | contents: read 11 | 12 | on: [push, pull_request] 13 | 14 | concurrency: 15 | group: ${{ github.workflow }}-${{ github.ref }} 16 | cancel-in-progress: true 17 | 18 | jobs: 19 | test: 20 | strategy: 21 | matrix: 22 | go: ["1.18.x", "1.22.x", "1.23.x"] 23 | platform: [ubuntu-latest, windows-latest, macos-latest] 24 | runs-on: ${{ matrix.platform }} 25 | steps: 26 | - name: Install Go ${{ matrix.go }} 27 | uses: actions/setup-go@v5 28 | with: 29 | go-version: ${{ matrix.go }} 30 | - name: Checkout code 31 | uses: actions/checkout@v4 32 | - name: Test 33 | run: go test -v ./... 34 | lint: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - name: Checkout code 38 | uses: actions/checkout@v4 39 | - name: go mod tidy 40 | run: | 41 | go mod tidy 42 | git diff --exit-code 43 | - name: Lint 44 | run: | 45 | docker run --rm -v ./:/go/src/github.com/moby/term -w /go/src/github.com/moby/term \ 46 | golangci/golangci-lint:v1.62.2 golangci-lint run -v \ 47 | -E gofmt \ 48 | -E misspell \ 49 | -E revive 50 | -------------------------------------------------------------------------------- /windows/ansi_writer.go: -------------------------------------------------------------------------------- 1 | //go:build windows 2 | // +build windows 3 | 4 | package windowsconsole 5 | 6 | import ( 7 | "io" 8 | "os" 9 | 10 | ansiterm "github.com/Azure/go-ansiterm" 11 | "github.com/Azure/go-ansiterm/winterm" 12 | ) 13 | 14 | // ansiWriter wraps a standard output file (e.g., os.Stdout) providing ANSI sequence translation. 15 | type ansiWriter struct { 16 | file *os.File 17 | fd uintptr 18 | infoReset *winterm.CONSOLE_SCREEN_BUFFER_INFO 19 | command []byte 20 | escapeSequence []byte 21 | inAnsiSequence bool 22 | parser *ansiterm.AnsiParser 23 | } 24 | 25 | // NewAnsiWriter returns an io.Writer that provides VT100 terminal emulation on top of a 26 | // Windows console output handle. 27 | func NewAnsiWriter(nFile int) io.Writer { 28 | file, fd := winterm.GetStdFile(nFile) 29 | info, err := winterm.GetConsoleScreenBufferInfo(fd) 30 | if err != nil { 31 | return nil 32 | } 33 | 34 | parser := ansiterm.CreateParser("Ground", winterm.CreateWinEventHandler(fd, file)) 35 | 36 | return &ansiWriter{ 37 | file: file, 38 | fd: fd, 39 | infoReset: info, 40 | command: make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH), 41 | escapeSequence: []byte(ansiterm.KEY_ESC_CSI), 42 | parser: parser, 43 | } 44 | } 45 | 46 | func (aw *ansiWriter) Fd() uintptr { 47 | return aw.fd 48 | } 49 | 50 | // Write writes len(p) bytes from p to the underlying data stream. 51 | func (aw *ansiWriter) Write(p []byte) (total int, err error) { 52 | if len(p) == 0 { 53 | return 0, nil 54 | } 55 | 56 | return aw.parser.Parse(p) 57 | } 58 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | The maintainers of the Moby project take security seriously. If you discover 4 | a security issue, please bring it to their attention right away! 5 | 6 | ## Reporting a Vulnerability 7 | 8 | Please **DO NOT** file a public issue, instead send your report privately 9 | to [security@docker.com](mailto:security@docker.com). 10 | 11 | Reporter(s) can expect a response within 72 hours, acknowledging the issue was 12 | received. 13 | 14 | ## Review Process 15 | 16 | After receiving the report, an initial triage and technical analysis is 17 | performed to confirm the report and determine its scope. We may request 18 | additional information in this stage of the process. 19 | 20 | Once a reviewer has confirmed the relevance of the report, a draft security 21 | advisory will be created on GitHub. The draft advisory will be used to discuss 22 | the issue with maintainers, the reporter(s), and where applicable, other 23 | affected parties under embargo. 24 | 25 | If the vulnerability is accepted, a timeline for developing a patch, public 26 | disclosure, and patch release will be determined. If there is an embargo period 27 | on public disclosure before the patch release, the reporter(s) are expected to 28 | participate in the discussion of the timeline and abide by agreed upon dates 29 | for public disclosure. 30 | 31 | ## Accreditation 32 | 33 | Security reports are greatly appreciated and we will publicly thank you, 34 | although we will keep your name confidential if you request it. We also like to 35 | send gifts - if you're into swag, make sure to let us know. We do not currently 36 | offer a paid security bounty program at this time. 37 | 38 | ## Supported Versions 39 | 40 | This project does not provide long-term-supported releases. Only the current 41 | release is maintained. 42 | -------------------------------------------------------------------------------- /term_unix.go: -------------------------------------------------------------------------------- 1 | //go:build !windows 2 | // +build !windows 3 | 4 | package term 5 | 6 | import ( 7 | "errors" 8 | "io" 9 | "os" 10 | 11 | "golang.org/x/sys/unix" 12 | ) 13 | 14 | // ErrInvalidState is returned if the state of the terminal is invalid. 15 | // 16 | // Deprecated: ErrInvalidState is no longer used. 17 | var ErrInvalidState = errors.New("Invalid terminal state") 18 | 19 | // terminalState holds the platform-specific state / console mode for the terminal. 20 | type terminalState struct { 21 | termios unix.Termios 22 | } 23 | 24 | func stdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { 25 | return os.Stdin, os.Stdout, os.Stderr 26 | } 27 | 28 | func getFdInfo(in interface{}) (uintptr, bool) { 29 | var inFd uintptr 30 | var isTerminalIn bool 31 | if file, ok := in.(*os.File); ok { 32 | inFd = file.Fd() 33 | isTerminalIn = isTerminal(inFd) 34 | } 35 | return inFd, isTerminalIn 36 | } 37 | 38 | func getWinsize(fd uintptr) (*Winsize, error) { 39 | uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ) 40 | ws := &Winsize{Height: uws.Row, Width: uws.Col, x: uws.Xpixel, y: uws.Ypixel} 41 | return ws, err 42 | } 43 | 44 | func setWinsize(fd uintptr, ws *Winsize) error { 45 | return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, &unix.Winsize{ 46 | Row: ws.Height, 47 | Col: ws.Width, 48 | Xpixel: ws.x, 49 | Ypixel: ws.y, 50 | }) 51 | } 52 | 53 | func isTerminal(fd uintptr) bool { 54 | _, err := tcget(fd) 55 | return err == nil 56 | } 57 | 58 | func restoreTerminal(fd uintptr, state *State) error { 59 | if state == nil { 60 | return errors.New("invalid terminal state") 61 | } 62 | return tcset(fd, &state.termios) 63 | } 64 | 65 | func saveState(fd uintptr) (*State, error) { 66 | termios, err := tcget(fd) 67 | if err != nil { 68 | return nil, err 69 | } 70 | return &State{termios: *termios}, nil 71 | } 72 | 73 | func disableEcho(fd uintptr, state *State) error { 74 | newState := state.termios 75 | newState.Lflag &^= unix.ECHO 76 | 77 | return tcset(fd, &newState) 78 | } 79 | 80 | func setRawTerminal(fd uintptr) (*State, error) { 81 | return makeRaw(fd) 82 | } 83 | 84 | func setRawTerminalOutput(uintptr) (*State, error) { 85 | return nil, nil 86 | } 87 | 88 | func tcget(fd uintptr) (*unix.Termios, error) { 89 | p, err := unix.IoctlGetTermios(int(fd), getTermios) 90 | if err != nil { 91 | return nil, err 92 | } 93 | return p, nil 94 | } 95 | 96 | func tcset(fd uintptr, p *unix.Termios) error { 97 | return unix.IoctlSetTermios(int(fd), setTermios, p) 98 | } 99 | -------------------------------------------------------------------------------- /proxy.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | // EscapeError is special error which returned by a TTY proxy reader's Read() 8 | // method in case its detach escape sequence is read. 9 | type EscapeError struct{} 10 | 11 | func (EscapeError) Error() string { 12 | return "read escape sequence" 13 | } 14 | 15 | // escapeProxy is used only for attaches with a TTY. It is used to proxy 16 | // stdin keypresses from the underlying reader and look for the passed in 17 | // escape key sequence to signal a detach. 18 | type escapeProxy struct { 19 | escapeKeys []byte 20 | escapeKeyPos int 21 | r io.Reader 22 | buf []byte 23 | } 24 | 25 | // NewEscapeProxy returns a new TTY proxy reader which wraps the given reader 26 | // and detects when the specified escape keys are read, in which case the Read 27 | // method will return an error of type EscapeError. 28 | func NewEscapeProxy(r io.Reader, escapeKeys []byte) io.Reader { 29 | return &escapeProxy{ 30 | escapeKeys: escapeKeys, 31 | r: r, 32 | } 33 | } 34 | 35 | func (r *escapeProxy) Read(buf []byte) (n int, err error) { 36 | if len(r.escapeKeys) > 0 && r.escapeKeyPos == len(r.escapeKeys) { 37 | return 0, EscapeError{} 38 | } 39 | 40 | if len(r.buf) > 0 { 41 | n = copy(buf, r.buf) 42 | r.buf = r.buf[n:] 43 | } 44 | 45 | nr, err := r.r.Read(buf[n:]) 46 | n += nr 47 | if len(r.escapeKeys) == 0 { 48 | return n, err 49 | } 50 | 51 | for i := 0; i < n; i++ { 52 | if buf[i] == r.escapeKeys[r.escapeKeyPos] { 53 | r.escapeKeyPos++ 54 | 55 | // Check if the full escape sequence is matched. 56 | if r.escapeKeyPos == len(r.escapeKeys) { 57 | n = i + 1 - r.escapeKeyPos 58 | if n < 0 { 59 | n = 0 60 | } 61 | return n, EscapeError{} 62 | } 63 | continue 64 | } 65 | 66 | // If we need to prepend a partial escape sequence from the previous 67 | // read, make sure the new buffer size doesn't exceed len(buf). 68 | // Otherwise, preserve any extra data in a buffer for the next read. 69 | if i < r.escapeKeyPos { 70 | preserve := make([]byte, 0, r.escapeKeyPos+n) 71 | preserve = append(preserve, r.escapeKeys[:r.escapeKeyPos]...) 72 | preserve = append(preserve, buf[:n]...) 73 | n = copy(buf, preserve) 74 | i += r.escapeKeyPos 75 | r.buf = append(r.buf, preserve[n:]...) 76 | } 77 | r.escapeKeyPos = 0 78 | } 79 | 80 | // If we're in the middle of reading an escape sequence, make sure we don't 81 | // let the caller read it. If later on we find that this is not the escape 82 | // sequence, we'll prepend it back to buf. 83 | n -= r.escapeKeyPos 84 | if n < 0 { 85 | n = 0 86 | } 87 | return n, err 88 | } 89 | -------------------------------------------------------------------------------- /term.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import "io" 4 | 5 | // State holds the platform-specific state / console mode for the terminal. 6 | type State terminalState 7 | 8 | // Winsize represents the size of the terminal window. 9 | type Winsize struct { 10 | Height uint16 11 | Width uint16 12 | 13 | // Only used on Unix 14 | x uint16 15 | y uint16 16 | } 17 | 18 | // StdStreams returns the standard streams (stdin, stdout, stderr). 19 | // 20 | // On Windows, it attempts to turn on VT handling on all std handles if 21 | // supported, or falls back to terminal emulation. On Unix, this returns 22 | // the standard [os.Stdin], [os.Stdout] and [os.Stderr]. 23 | func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { 24 | return stdStreams() 25 | } 26 | 27 | // GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal. 28 | func GetFdInfo(in interface{}) (fd uintptr, isTerminal bool) { 29 | return getFdInfo(in) 30 | } 31 | 32 | // GetWinsize returns the window size based on the specified file descriptor. 33 | func GetWinsize(fd uintptr) (*Winsize, error) { 34 | return getWinsize(fd) 35 | } 36 | 37 | // SetWinsize tries to set the specified window size for the specified file 38 | // descriptor. It is only implemented on Unix, and returns an error on Windows. 39 | func SetWinsize(fd uintptr, ws *Winsize) error { 40 | return setWinsize(fd, ws) 41 | } 42 | 43 | // IsTerminal returns true if the given file descriptor is a terminal. 44 | func IsTerminal(fd uintptr) bool { 45 | return isTerminal(fd) 46 | } 47 | 48 | // RestoreTerminal restores the terminal connected to the given file descriptor 49 | // to a previous state. 50 | func RestoreTerminal(fd uintptr, state *State) error { 51 | return restoreTerminal(fd, state) 52 | } 53 | 54 | // SaveState saves the state of the terminal connected to the given file descriptor. 55 | func SaveState(fd uintptr) (*State, error) { 56 | return saveState(fd) 57 | } 58 | 59 | // DisableEcho applies the specified state to the terminal connected to the file 60 | // descriptor, with echo disabled. 61 | func DisableEcho(fd uintptr, state *State) error { 62 | return disableEcho(fd, state) 63 | } 64 | 65 | // SetRawTerminal puts the terminal connected to the given file descriptor into 66 | // raw mode and returns the previous state. On UNIX, this is the equivalent of 67 | // [MakeRaw], and puts both the input and output into raw mode. On Windows, it 68 | // only puts the input into raw mode. 69 | func SetRawTerminal(fd uintptr) (previousState *State, err error) { 70 | return setRawTerminal(fd) 71 | } 72 | 73 | // SetRawTerminalOutput puts the output of terminal connected to the given file 74 | // descriptor into raw mode. On UNIX, this does nothing and returns nil for the 75 | // state. On Windows, it disables LF -> CRLF translation. 76 | func SetRawTerminalOutput(fd uintptr) (previousState *State, err error) { 77 | return setRawTerminalOutput(fd) 78 | } 79 | 80 | // MakeRaw puts the terminal (Windows Console) connected to the 81 | // given file descriptor into raw mode and returns the previous state of 82 | // the terminal so that it can be restored. 83 | func MakeRaw(fd uintptr) (previousState *State, err error) { 84 | return makeRaw(fd) 85 | } 86 | -------------------------------------------------------------------------------- /term_test.go: -------------------------------------------------------------------------------- 1 | //go:build !windows 2 | // +build !windows 3 | 4 | package term 5 | 6 | import ( 7 | "os" 8 | "reflect" 9 | "testing" 10 | 11 | cpty "github.com/creack/pty" 12 | ) 13 | 14 | func newTTYForTest(t *testing.T) *os.File { 15 | t.Helper() 16 | pty, tty, err := cpty.Open() 17 | if err != nil { 18 | t.Fatalf("error creating pty: %v", err) 19 | } else { 20 | t.Cleanup(func() { 21 | _ = pty.Close() 22 | _ = tty.Close() 23 | }) 24 | } 25 | 26 | return pty 27 | } 28 | 29 | func newTempFile(t *testing.T) *os.File { 30 | t.Helper() 31 | tmpFile, err := os.CreateTemp(t.TempDir(), "temp") 32 | if err != nil { 33 | t.Fatalf("error creating tempfile: %v", err) 34 | } else { 35 | t.Cleanup(func() { _ = tmpFile.Close() }) 36 | } 37 | return tmpFile 38 | } 39 | 40 | func TestGetWinsize(t *testing.T) { 41 | tty := newTTYForTest(t) 42 | winSize, err := GetWinsize(tty.Fd()) 43 | if err != nil { 44 | t.Error(err) 45 | } 46 | if winSize == nil { 47 | t.Fatal("winSize is nil") 48 | } 49 | 50 | newSize := Winsize{Width: 200, Height: 200, x: winSize.x, y: winSize.y} 51 | err = SetWinsize(tty.Fd(), &newSize) 52 | if err != nil { 53 | t.Fatal(err) 54 | } 55 | winSize, err = GetWinsize(tty.Fd()) 56 | if err != nil { 57 | t.Fatal(err) 58 | } 59 | if !reflect.DeepEqual(*winSize, newSize) { 60 | t.Fatalf("expected: %+v, got: %+v", newSize, *winSize) 61 | } 62 | } 63 | 64 | func TestSetWinsize(t *testing.T) { 65 | tty := newTTYForTest(t) 66 | winSize, err := GetWinsize(tty.Fd()) 67 | if err != nil { 68 | t.Fatal(err) 69 | } 70 | if winSize == nil { 71 | t.Fatal("winSize is nil") 72 | } 73 | newSize := Winsize{Width: 200, Height: 200, x: winSize.x, y: winSize.y} 74 | err = SetWinsize(tty.Fd(), &newSize) 75 | if err != nil { 76 | t.Fatal(err) 77 | } 78 | winSize, err = GetWinsize(tty.Fd()) 79 | if err != nil { 80 | t.Fatal(err) 81 | } 82 | if !reflect.DeepEqual(*winSize, newSize) { 83 | t.Fatalf("expected: %+v, got: %+v", newSize, *winSize) 84 | } 85 | } 86 | 87 | func TestGetFdInfo(t *testing.T) { 88 | tty := newTTYForTest(t) 89 | inFd, isTerm := GetFdInfo(tty) 90 | if inFd != tty.Fd() { 91 | t.Errorf("expected: %d, got: %d", tty.Fd(), inFd) 92 | } 93 | if !isTerm { 94 | t.Error("expected file-descriptor to be a terminal") 95 | } 96 | tmpFile := newTempFile(t) 97 | inFd, isTerm = GetFdInfo(tmpFile) 98 | if inFd != tmpFile.Fd() { 99 | t.Errorf("expected: %d, got: %d", tty.Fd(), inFd) 100 | } 101 | if isTerm { 102 | t.Error("expected file-descriptor to not be a terminal") 103 | } 104 | } 105 | 106 | func TestIsTerminal(t *testing.T) { 107 | tty := newTTYForTest(t) 108 | isTerm := IsTerminal(tty.Fd()) 109 | if !isTerm { 110 | t.Error("expected file-descriptor to be a terminal") 111 | } 112 | tmpFile := newTempFile(t) 113 | isTerm = IsTerminal(tmpFile.Fd()) 114 | if isTerm { 115 | t.Error("expected file-descriptor to not be a terminal") 116 | } 117 | } 118 | 119 | func TestSaveState(t *testing.T) { 120 | tty := newTTYForTest(t) 121 | state, err := SaveState(tty.Fd()) 122 | if err != nil { 123 | t.Error(err) 124 | } 125 | if state == nil { 126 | t.Fatal("state is nil") 127 | } 128 | tty = newTTYForTest(t) 129 | err = RestoreTerminal(tty.Fd(), state) 130 | if err != nil { 131 | t.Fatal(err) 132 | } 133 | } 134 | 135 | func TestDisableEcho(t *testing.T) { 136 | tty := newTTYForTest(t) 137 | state, err := SetRawTerminal(tty.Fd()) 138 | defer func() { 139 | if err := RestoreTerminal(tty.Fd(), state); err != nil { 140 | t.Error(err) 141 | } 142 | }() 143 | if err != nil { 144 | t.Error(err) 145 | } 146 | if state == nil { 147 | t.Fatal("state is nil") 148 | } 149 | err = DisableEcho(tty.Fd(), state) 150 | if err != nil { 151 | t.Fatal(err) 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /term_windows.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | "os/signal" 8 | 9 | windowsconsole "github.com/moby/term/windows" 10 | "golang.org/x/sys/windows" 11 | ) 12 | 13 | // terminalState holds the platform-specific state / console mode for the terminal. 14 | type terminalState struct { 15 | mode uint32 16 | } 17 | 18 | // vtInputSupported is true if winterm.ENABLE_VIRTUAL_TERMINAL_INPUT is supported by the console 19 | var vtInputSupported bool 20 | 21 | func stdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { 22 | // Turn on VT handling on all std handles, if possible. This might 23 | // fail, in which case we will fall back to terminal emulation. 24 | var ( 25 | emulateStdin, emulateStdout, emulateStderr bool 26 | 27 | mode uint32 28 | ) 29 | 30 | fd := windows.Handle(os.Stdin.Fd()) 31 | if err := windows.GetConsoleMode(fd, &mode); err == nil { 32 | // Validate that winterm.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it. 33 | if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil { 34 | emulateStdin = true 35 | } else { 36 | vtInputSupported = true 37 | } 38 | // Unconditionally set the console mode back even on failure because SetConsoleMode 39 | // remembers invalid bits on input handles. 40 | _ = windows.SetConsoleMode(fd, mode) 41 | } 42 | 43 | fd = windows.Handle(os.Stdout.Fd()) 44 | if err := windows.GetConsoleMode(fd, &mode); err == nil { 45 | // Validate winterm.DISABLE_NEWLINE_AUTO_RETURN is supported, but do not set it. 46 | if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil { 47 | emulateStdout = true 48 | } else { 49 | _ = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) 50 | } 51 | } 52 | 53 | fd = windows.Handle(os.Stderr.Fd()) 54 | if err := windows.GetConsoleMode(fd, &mode); err == nil { 55 | // Validate winterm.DISABLE_NEWLINE_AUTO_RETURN is supported, but do not set it. 56 | if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil { 57 | emulateStderr = true 58 | } else { 59 | _ = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) 60 | } 61 | } 62 | 63 | if emulateStdin { 64 | h := uint32(windows.STD_INPUT_HANDLE) 65 | stdIn = windowsconsole.NewAnsiReader(int(h)) 66 | } else { 67 | stdIn = os.Stdin 68 | } 69 | 70 | if emulateStdout { 71 | h := uint32(windows.STD_OUTPUT_HANDLE) 72 | stdOut = windowsconsole.NewAnsiWriter(int(h)) 73 | } else { 74 | stdOut = os.Stdout 75 | } 76 | 77 | if emulateStderr { 78 | h := uint32(windows.STD_ERROR_HANDLE) 79 | stdErr = windowsconsole.NewAnsiWriter(int(h)) 80 | } else { 81 | stdErr = os.Stderr 82 | } 83 | 84 | return stdIn, stdOut, stdErr 85 | } 86 | 87 | func getFdInfo(in interface{}) (uintptr, bool) { 88 | return windowsconsole.GetHandleInfo(in) 89 | } 90 | 91 | func getWinsize(fd uintptr) (*Winsize, error) { 92 | var info windows.ConsoleScreenBufferInfo 93 | if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil { 94 | return nil, err 95 | } 96 | 97 | winsize := &Winsize{ 98 | Width: uint16(info.Window.Right - info.Window.Left + 1), 99 | Height: uint16(info.Window.Bottom - info.Window.Top + 1), 100 | } 101 | 102 | return winsize, nil 103 | } 104 | 105 | func setWinsize(fd uintptr, ws *Winsize) error { 106 | return fmt.Errorf("not implemented on Windows") 107 | } 108 | 109 | func isTerminal(fd uintptr) bool { 110 | var mode uint32 111 | err := windows.GetConsoleMode(windows.Handle(fd), &mode) 112 | return err == nil 113 | } 114 | 115 | func restoreTerminal(fd uintptr, state *State) error { 116 | return windows.SetConsoleMode(windows.Handle(fd), state.mode) 117 | } 118 | 119 | func saveState(fd uintptr) (*State, error) { 120 | var mode uint32 121 | 122 | if err := windows.GetConsoleMode(windows.Handle(fd), &mode); err != nil { 123 | return nil, err 124 | } 125 | 126 | return &State{mode: mode}, nil 127 | } 128 | 129 | func disableEcho(fd uintptr, state *State) error { 130 | // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx 131 | mode := state.mode 132 | mode &^= windows.ENABLE_ECHO_INPUT 133 | mode |= windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT 134 | err := windows.SetConsoleMode(windows.Handle(fd), mode) 135 | if err != nil { 136 | return err 137 | } 138 | 139 | // Register an interrupt handler to catch and restore prior state 140 | restoreAtInterrupt(fd, state) 141 | return nil 142 | } 143 | 144 | func setRawTerminal(fd uintptr) (*State, error) { 145 | oldState, err := MakeRaw(fd) 146 | if err != nil { 147 | return nil, err 148 | } 149 | 150 | // Register an interrupt handler to catch and restore prior state 151 | restoreAtInterrupt(fd, oldState) 152 | return oldState, err 153 | } 154 | 155 | func setRawTerminalOutput(fd uintptr) (*State, error) { 156 | oldState, err := saveState(fd) 157 | if err != nil { 158 | return nil, err 159 | } 160 | 161 | // Ignore failures, since winterm.DISABLE_NEWLINE_AUTO_RETURN might not be supported on this 162 | // version of Windows. 163 | _ = windows.SetConsoleMode(windows.Handle(fd), oldState.mode|windows.DISABLE_NEWLINE_AUTO_RETURN) 164 | return oldState, err 165 | } 166 | 167 | func restoreAtInterrupt(fd uintptr, state *State) { 168 | sigchan := make(chan os.Signal, 1) 169 | signal.Notify(sigchan, os.Interrupt) 170 | 171 | go func() { 172 | _ = <-sigchan 173 | _ = RestoreTerminal(fd, state) 174 | os.Exit(0) 175 | }() 176 | } 177 | -------------------------------------------------------------------------------- /windows/ansi_reader.go: -------------------------------------------------------------------------------- 1 | //go:build windows 2 | // +build windows 3 | 4 | package windowsconsole 5 | 6 | import ( 7 | "bytes" 8 | "errors" 9 | "fmt" 10 | "io" 11 | "os" 12 | "strings" 13 | "unsafe" 14 | 15 | ansiterm "github.com/Azure/go-ansiterm" 16 | "github.com/Azure/go-ansiterm/winterm" 17 | ) 18 | 19 | const ( 20 | escapeSequence = ansiterm.KEY_ESC_CSI 21 | ) 22 | 23 | // ansiReader wraps a standard input file (e.g., os.Stdin) providing ANSI sequence translation. 24 | type ansiReader struct { 25 | file *os.File 26 | fd uintptr 27 | buffer []byte 28 | cbBuffer int 29 | command []byte 30 | } 31 | 32 | // NewAnsiReader returns an io.ReadCloser that provides VT100 terminal emulation on top of a 33 | // Windows console input handle. 34 | func NewAnsiReader(nFile int) io.ReadCloser { 35 | file, fd := winterm.GetStdFile(nFile) 36 | return &ansiReader{ 37 | file: file, 38 | fd: fd, 39 | command: make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH), 40 | buffer: make([]byte, 0), 41 | } 42 | } 43 | 44 | // Close closes the wrapped file. 45 | func (ar *ansiReader) Close() (err error) { 46 | return ar.file.Close() 47 | } 48 | 49 | // Fd returns the file descriptor of the wrapped file. 50 | func (ar *ansiReader) Fd() uintptr { 51 | return ar.fd 52 | } 53 | 54 | // Read reads up to len(p) bytes of translated input events into p. 55 | func (ar *ansiReader) Read(p []byte) (int, error) { 56 | if len(p) == 0 { 57 | return 0, nil 58 | } 59 | 60 | // Previously read bytes exist, read as much as we can and return 61 | if len(ar.buffer) > 0 { 62 | originalLength := len(ar.buffer) 63 | copiedLength := copy(p, ar.buffer) 64 | 65 | if copiedLength == originalLength { 66 | ar.buffer = make([]byte, 0, len(p)) 67 | } else { 68 | ar.buffer = ar.buffer[copiedLength:] 69 | } 70 | 71 | return copiedLength, nil 72 | } 73 | 74 | // Read and translate key events 75 | events, err := readInputEvents(ar, len(p)) 76 | if err != nil { 77 | return 0, err 78 | } else if len(events) == 0 { 79 | return 0, nil 80 | } 81 | 82 | keyBytes := translateKeyEvents(events, []byte(escapeSequence)) 83 | 84 | // Save excess bytes and right-size keyBytes 85 | if len(keyBytes) > len(p) { 86 | ar.buffer = keyBytes[len(p):] 87 | keyBytes = keyBytes[:len(p)] 88 | } else if len(keyBytes) == 0 { 89 | return 0, nil 90 | } 91 | 92 | copiedLength := copy(p, keyBytes) 93 | if copiedLength != len(keyBytes) { 94 | return 0, errors.New("unexpected copy length encountered") 95 | } 96 | 97 | return copiedLength, nil 98 | } 99 | 100 | // readInputEvents polls until at least one event is available. 101 | func readInputEvents(ar *ansiReader, maxBytes int) ([]winterm.INPUT_RECORD, error) { 102 | // Determine the maximum number of records to retrieve 103 | // -- Cast around the type system to obtain the size of a single INPUT_RECORD. 104 | // unsafe.Sizeof requires an expression vs. a type-reference; the casting 105 | // tricks the type system into believing it has such an expression. 106 | recordSize := int(unsafe.Sizeof(*((*winterm.INPUT_RECORD)(unsafe.Pointer(&maxBytes))))) 107 | countRecords := maxBytes / recordSize 108 | if countRecords > ansiterm.MAX_INPUT_EVENTS { 109 | countRecords = ansiterm.MAX_INPUT_EVENTS 110 | } else if countRecords == 0 { 111 | countRecords = 1 112 | } 113 | 114 | // Wait for and read input events 115 | events := make([]winterm.INPUT_RECORD, countRecords) 116 | nEvents := uint32(0) 117 | eventsExist, err := winterm.WaitForSingleObject(ar.fd, winterm.WAIT_INFINITE) 118 | if err != nil { 119 | return nil, err 120 | } 121 | 122 | if eventsExist { 123 | err = winterm.ReadConsoleInput(ar.fd, events, &nEvents) 124 | if err != nil { 125 | return nil, err 126 | } 127 | } 128 | 129 | // Return a slice restricted to the number of returned records 130 | return events[:nEvents], nil 131 | } 132 | 133 | // KeyEvent Translation Helpers 134 | 135 | var arrowKeyMapPrefix = map[uint16]string{ 136 | winterm.VK_UP: "%s%sA", 137 | winterm.VK_DOWN: "%s%sB", 138 | winterm.VK_RIGHT: "%s%sC", 139 | winterm.VK_LEFT: "%s%sD", 140 | } 141 | 142 | var keyMapPrefix = map[uint16]string{ 143 | winterm.VK_UP: "\x1B[%sA", 144 | winterm.VK_DOWN: "\x1B[%sB", 145 | winterm.VK_RIGHT: "\x1B[%sC", 146 | winterm.VK_LEFT: "\x1B[%sD", 147 | winterm.VK_HOME: "\x1B[1%s~", // showkey shows ^[[1 148 | winterm.VK_END: "\x1B[4%s~", // showkey shows ^[[4 149 | winterm.VK_INSERT: "\x1B[2%s~", 150 | winterm.VK_DELETE: "\x1B[3%s~", 151 | winterm.VK_PRIOR: "\x1B[5%s~", 152 | winterm.VK_NEXT: "\x1B[6%s~", 153 | winterm.VK_F1: "", 154 | winterm.VK_F2: "", 155 | winterm.VK_F3: "\x1B[13%s~", 156 | winterm.VK_F4: "\x1B[14%s~", 157 | winterm.VK_F5: "\x1B[15%s~", 158 | winterm.VK_F6: "\x1B[17%s~", 159 | winterm.VK_F7: "\x1B[18%s~", 160 | winterm.VK_F8: "\x1B[19%s~", 161 | winterm.VK_F9: "\x1B[20%s~", 162 | winterm.VK_F10: "\x1B[21%s~", 163 | winterm.VK_F11: "\x1B[23%s~", 164 | winterm.VK_F12: "\x1B[24%s~", 165 | } 166 | 167 | // translateKeyEvents converts the input events into the appropriate ANSI string. 168 | func translateKeyEvents(events []winterm.INPUT_RECORD, escapeSequence []byte) []byte { 169 | var buffer bytes.Buffer 170 | for _, event := range events { 171 | if event.EventType == winterm.KEY_EVENT && event.KeyEvent.KeyDown != 0 { 172 | buffer.WriteString(keyToString(&event.KeyEvent, escapeSequence)) 173 | } 174 | } 175 | 176 | return buffer.Bytes() 177 | } 178 | 179 | // keyToString maps the given input event record to the corresponding string. 180 | func keyToString(keyEvent *winterm.KEY_EVENT_RECORD, escapeSequence []byte) string { 181 | if keyEvent.UnicodeChar == 0 { 182 | return formatVirtualKey(keyEvent.VirtualKeyCode, keyEvent.ControlKeyState, escapeSequence) 183 | } 184 | 185 | _, alt, control := getControlKeys(keyEvent.ControlKeyState) 186 | if control { 187 | // TODO(azlinux): Implement following control sequences 188 | // -D Signals the end of input from the keyboard; also exits current shell. 189 | // -H Deletes the first character to the left of the cursor. Also called the ERASE key. 190 | // -Q Restarts printing after it has been stopped with -s. 191 | // -S Suspends printing on the screen (does not stop the program). 192 | // -U Deletes all characters on the current line. Also called the KILL key. 193 | // -E Quits current command and creates a core 194 | } 195 | 196 | // +Key generates ESC N Key 197 | if !control && alt { 198 | return ansiterm.KEY_ESC_N + strings.ToLower(string(rune(keyEvent.UnicodeChar))) 199 | } 200 | 201 | return string(rune(keyEvent.UnicodeChar)) 202 | } 203 | 204 | // formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string. 205 | func formatVirtualKey(key uint16, controlState uint32, escapeSequence []byte) string { 206 | shift, alt, control := getControlKeys(controlState) 207 | modifier := getControlKeysModifier(shift, alt, control) 208 | 209 | if format, ok := arrowKeyMapPrefix[key]; ok { 210 | return fmt.Sprintf(format, escapeSequence, modifier) 211 | } 212 | 213 | if format, ok := keyMapPrefix[key]; ok { 214 | return fmt.Sprintf(format, modifier) 215 | } 216 | 217 | return "" 218 | } 219 | 220 | // getControlKeys extracts the shift, alt, and ctrl key states. 221 | func getControlKeys(controlState uint32) (shift, alt, control bool) { 222 | shift = 0 != (controlState & winterm.SHIFT_PRESSED) 223 | alt = 0 != (controlState & (winterm.LEFT_ALT_PRESSED | winterm.RIGHT_ALT_PRESSED)) 224 | control = 0 != (controlState & (winterm.LEFT_CTRL_PRESSED | winterm.RIGHT_CTRL_PRESSED)) 225 | return shift, alt, control 226 | } 227 | 228 | // getControlKeysModifier returns the ANSI modifier for the given combination of control keys. 229 | func getControlKeysModifier(shift, alt, control bool) string { 230 | if shift && alt && control { 231 | return ansiterm.KEY_CONTROL_PARAM_8 232 | } 233 | if alt && control { 234 | return ansiterm.KEY_CONTROL_PARAM_7 235 | } 236 | if shift && control { 237 | return ansiterm.KEY_CONTROL_PARAM_6 238 | } 239 | if control { 240 | return ansiterm.KEY_CONTROL_PARAM_5 241 | } 242 | if shift && alt { 243 | return ansiterm.KEY_CONTROL_PARAM_4 244 | } 245 | if alt { 246 | return ansiterm.KEY_CONTROL_PARAM_3 247 | } 248 | if shift { 249 | return ansiterm.KEY_CONTROL_PARAM_2 250 | } 251 | return "" 252 | } 253 | -------------------------------------------------------------------------------- /proxy_test.go: -------------------------------------------------------------------------------- 1 | package term 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func TestEscapeProxyRead(t *testing.T) { 9 | t.Run("no escape keys, keys [a]", func(t *testing.T) { 10 | escapeKeys, _ := ToBytes("") 11 | keys, _ := ToBytes("a") 12 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 13 | 14 | buf := make([]byte, len(keys)) 15 | nr, err := reader.Read(buf) 16 | if err != nil { 17 | t.Error(err) 18 | } 19 | if expected := len(keys); nr != expected { 20 | t.Errorf("expected: %d, got: %d", expected, nr) 21 | } 22 | if !bytes.Equal(buf, keys) { 23 | t.Errorf("expected: %+v, got: %+v", keys, buf) 24 | } 25 | }) 26 | 27 | t.Run("no escape keys, keys [a,b,c]", func(t *testing.T) { 28 | escapeKeys, _ := ToBytes("") 29 | keys, _ := ToBytes("a,b,c") 30 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 31 | 32 | buf := make([]byte, len(keys)) 33 | nr, err := reader.Read(buf) 34 | if err != nil { 35 | t.Error(err) 36 | } 37 | if expected := len(keys); nr != expected { 38 | t.Errorf("expected: %d, got: %d", expected, nr) 39 | } 40 | if !bytes.Equal(buf, keys) { 41 | t.Errorf("expected: %+v, got: %+v", keys, buf) 42 | } 43 | }) 44 | 45 | t.Run("no escape keys, no keys", func(t *testing.T) { 46 | escapeKeys, _ := ToBytes("") 47 | keys, _ := ToBytes("") 48 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 49 | 50 | buf := make([]byte, len(keys)) 51 | nr, err := reader.Read(buf) 52 | if err == nil { 53 | t.Error("expected an error when there are no keys are to read") 54 | } 55 | if expected := 0; len(keys) != expected { 56 | t.Errorf("expected: %d, got: %d", expected, len(keys)) 57 | } 58 | if expected := len(keys); nr != expected { 59 | t.Errorf("expected: %d, got: %d", expected, nr) 60 | } 61 | if expected := len(keys); len(buf) != expected { 62 | t.Errorf("expected: %d, got: %d", expected, len(buf)) 63 | } 64 | }) 65 | 66 | t.Run("DEL escape key, keys [a,b,c,+]", func(t *testing.T) { 67 | escapeKeys, _ := ToBytes("DEL") 68 | keys, _ := ToBytes("a,b,c,+") 69 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 70 | 71 | buf := make([]byte, len(keys)) 72 | nr, err := reader.Read(buf) 73 | if err != nil { 74 | t.Error(err) 75 | } 76 | if expected := len(keys); nr != expected { 77 | t.Errorf("expected: %d, got: %d", expected, nr) 78 | } 79 | if !bytes.Equal(buf, keys) { 80 | t.Errorf("expected: %+v, got: %+v", keys, buf) 81 | } 82 | }) 83 | 84 | t.Run("DEL escape key, no keys", func(t *testing.T) { 85 | escapeKeys, _ := ToBytes("DEL") 86 | keys, _ := ToBytes("") 87 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 88 | 89 | buf := make([]byte, len(keys)) 90 | nr, err := reader.Read(buf) 91 | if err == nil { 92 | t.Error("expected an error when there are no keys are to read") 93 | } 94 | if expected := 0; len(keys) != expected { 95 | t.Errorf("expected: %d, got: %d", expected, len(keys)) 96 | } 97 | if expected := len(keys); nr != expected { 98 | t.Errorf("expected: %d, got: %d", expected, nr) 99 | } 100 | if expected := len(keys); len(buf) != expected { 101 | t.Errorf("expected: %d, got: %d", expected, len(buf)) 102 | } 103 | }) 104 | 105 | t.Run("ctrl-x,ctrl-@ escape key, keys [DEL]", func(t *testing.T) { 106 | escapeKeys, _ := ToBytes("ctrl-x,ctrl-@") 107 | keys, _ := ToBytes("DEL") 108 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 109 | 110 | buf := make([]byte, len(keys)) 111 | nr, err := reader.Read(buf) 112 | if err != nil { 113 | t.Error(err) 114 | } 115 | if expected := 1; nr != expected { 116 | t.Errorf("expected: %d, got: %d", expected, nr) 117 | } 118 | if !bytes.Equal(buf, keys) { 119 | t.Errorf("expected: %+v, got: %+v", keys, buf) 120 | } 121 | }) 122 | 123 | t.Run("ctrl-c escape key, keys [ctrl-c]", func(t *testing.T) { 124 | escapeKeys, _ := ToBytes("ctrl-c") 125 | keys, _ := ToBytes("ctrl-c") 126 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 127 | 128 | buf := make([]byte, len(keys)) 129 | nr, err := reader.Read(buf) 130 | if expected := "read escape sequence"; err == nil || err.Error() != expected { 131 | t.Errorf("expected: %v, got: %v", expected, err) 132 | } 133 | if expected := 0; nr != expected { 134 | t.Errorf("expected: %d, got: %d", expected, nr) 135 | } 136 | if !bytes.Equal(buf, keys) { 137 | t.Errorf("expected: %+v, got: %+v", keys, buf) 138 | } 139 | }) 140 | 141 | t.Run("ctrl-c,ctrl-z escape key, keys [ctrl-c],[ctrl-z]", func(t *testing.T) { 142 | escapeKeys, _ := ToBytes("ctrl-c,ctrl-z") 143 | keys, _ := ToBytes("ctrl-c,ctrl-z") 144 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 145 | 146 | buf := make([]byte, 1) 147 | nr, err := reader.Read(buf) 148 | if err != nil { 149 | t.Error(err) 150 | } 151 | if expected := 0; nr != expected { 152 | t.Errorf("expected: %d, got: %d", expected, nr) 153 | } 154 | if !bytes.Equal(buf, keys[0:1]) { 155 | t.Errorf("expected: %+v, got: %+v", keys, buf) 156 | } 157 | 158 | nr, err = reader.Read(buf) 159 | if expected := "read escape sequence"; err == nil || err.Error() != expected { 160 | t.Errorf("expected: %v, got: %v", expected, err) 161 | } 162 | if expected := 0; nr != expected { 163 | t.Errorf("expected: %d, got: %d", expected, nr) 164 | } 165 | if !bytes.Equal(buf, keys[1:]) { 166 | t.Errorf("expected: %+v, got: %+v", keys, buf) 167 | } 168 | }) 169 | 170 | t.Run("ctrl-c,ctrl-z escape key, keys [ctrl-c,ctrl-z]", func(t *testing.T) { 171 | escapeKeys, _ := ToBytes("ctrl-c,ctrl-z") 172 | keys, _ := ToBytes("ctrl-c,ctrl-z") 173 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 174 | 175 | buf := make([]byte, 2) 176 | nr, err := reader.Read(buf) 177 | if expected := "read escape sequence"; err == nil || err.Error() != expected { 178 | t.Errorf("expected: %v, got: %v", expected, err) 179 | } 180 | if expected := 0; nr != expected { 181 | t.Errorf("expected: %d, got: %d", expected, nr) 182 | } 183 | if !bytes.Equal(buf, keys) { 184 | t.Errorf("expected: %+v, got: %+v", keys, buf) 185 | } 186 | }) 187 | 188 | t.Run("ctrl-c,ctrl-z escape key, keys [ctrl-c],[DEL,+]", func(t *testing.T) { 189 | escapeKeys, _ := ToBytes("ctrl-c,ctrl-z") 190 | keys, _ := ToBytes("ctrl-c,DEL,+") 191 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 192 | 193 | buf := make([]byte, 1) 194 | nr, err := reader.Read(buf) 195 | if err != nil { 196 | t.Error(err) 197 | } 198 | if expected := 0; nr != expected { 199 | t.Errorf("expected: %d, got: %d", expected, nr) 200 | } 201 | if !bytes.Equal(buf, keys[0:1]) { 202 | t.Errorf("expected: %+v, got: %+v", keys, buf) 203 | } 204 | 205 | buf = make([]byte, len(keys)) 206 | nr, err = reader.Read(buf) 207 | if err != nil { 208 | t.Error(err) 209 | } 210 | if expected := len(keys); nr != expected { 211 | t.Errorf("expected: %d, got: %d", expected, nr) 212 | } 213 | if !bytes.Equal(buf, keys) { 214 | t.Errorf("expected: %+v, got: %+v", keys, buf) 215 | } 216 | }) 217 | 218 | t.Run("ctrl-c,ctrl-z escape key, keys [ctrl-c],[DEL]", func(t *testing.T) { 219 | escapeKeys, _ := ToBytes("ctrl-c,ctrl-z") 220 | keys, _ := ToBytes("ctrl-c,DEL") 221 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 222 | 223 | buf := make([]byte, 1) 224 | nr, err := reader.Read(buf) 225 | if err != nil { 226 | t.Error(err) 227 | } 228 | if expected := 0; nr != expected { 229 | t.Errorf("expected: %d, got: %d", expected, nr) 230 | } 231 | if !bytes.Equal(buf, keys[0:1]) { 232 | t.Errorf("expected: %+v, got: %+v", keys, buf) 233 | } 234 | 235 | buf = make([]byte, len(keys)) 236 | nr, err = reader.Read(buf) 237 | if err != nil { 238 | t.Error(err) 239 | } 240 | if expected := len(keys); nr != expected { 241 | t.Errorf("expected: %d, got: %d", expected, nr) 242 | } 243 | if !bytes.Equal(buf, keys) { 244 | t.Errorf("expected: %+v, got: %+v", keys, buf) 245 | } 246 | }) 247 | 248 | t.Run("a,b,c,d escape key, keys [a,b],[c,d]", func(t *testing.T) { 249 | escapeKeys, _ := ToBytes("a,b,c,d") 250 | keys, _ := ToBytes("a,b,c,d") 251 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 252 | 253 | buf := make([]byte, 2) 254 | nr, err := reader.Read(buf) 255 | if err != nil { 256 | t.Error(err) 257 | } 258 | if expected := 0; nr != expected { 259 | t.Errorf("expected: %d, got: %d", expected, nr) 260 | } 261 | if !bytes.Equal(buf, keys[0:2]) { 262 | t.Errorf("expected: %+v, got: %+v", keys, buf) 263 | } 264 | 265 | buf = make([]byte, 2) 266 | nr, err = reader.Read(buf) 267 | if expected := "read escape sequence"; err == nil || err.Error() != expected { 268 | t.Errorf("expected: %v, got: %v", expected, err) 269 | } 270 | if expected := 0; nr != expected { 271 | t.Errorf("expected: %d, got: %d", expected, nr) 272 | } 273 | if !bytes.Equal(buf, keys[2:4]) { 274 | t.Errorf("expected: %+v, got: %+v", keys, buf) 275 | } 276 | }) 277 | 278 | t.Run("ctrl-p,ctrl-q escape key, keys [ctrl-p],[a],[ctrl-p,ctrl-q]", func(t *testing.T) { 279 | escapeKeys, _ := ToBytes("ctrl-p,ctrl-q") 280 | keys, _ := ToBytes("ctrl-p,a,ctrl-p,ctrl-q") 281 | reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) 282 | 283 | buf := make([]byte, 1) 284 | nr, err := reader.Read(buf) 285 | if err != nil { 286 | t.Error(err) 287 | } 288 | if expected := 0; nr != expected { 289 | t.Errorf("expected: %d, got: %d", expected, nr) 290 | } 291 | 292 | buf = make([]byte, 1) 293 | nr, err = reader.Read(buf) 294 | if err != nil { 295 | t.Error(err) 296 | } 297 | if expected := 1; nr != expected { 298 | t.Errorf("expected: %d, got: %d", expected, nr) 299 | } 300 | if !bytes.Equal(buf, keys[:1]) { 301 | t.Errorf("expected: %+v, got: %+v", keys, buf) 302 | } 303 | 304 | buf = make([]byte, 2) 305 | nr, err = reader.Read(buf) 306 | if err != nil { 307 | t.Error(err) 308 | } 309 | if expected := 1; nr != expected { 310 | t.Errorf("expected: %d, got: %d", expected, nr) 311 | } 312 | if !bytes.Equal(buf, keys[1:3]) { 313 | t.Errorf("expected: %+v, got: %+v", keys, buf) 314 | } 315 | 316 | buf = make([]byte, 2) 317 | nr, err = reader.Read(buf) 318 | if expected := "read escape sequence"; err == nil || err.Error() != expected { 319 | t.Errorf("expected: %v, got: %v", expected, err) 320 | } 321 | if expected := 0; nr != expected { 322 | t.Errorf("expected: %d, got: %d", expected, nr) 323 | } 324 | }) 325 | } 326 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | https://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | Copyright 2013-2018 Docker, Inc. 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | https://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | --------------------------------------------------------------------------------