Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package terminal |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "io" |
| 20 | "os" |
| 21 | "syscall" |
| 22 | "unsafe" |
| 23 | ) |
| 24 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 25 | func isSmartTerminal(w io.Writer) bool { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 26 | if f, ok := w.(*os.File); ok { |
Mikhail Naganov | 9d42425 | 2019-07-02 09:51:42 -0700 | [diff] [blame] | 27 | if term, ok := os.LookupEnv("TERM"); ok && term == "dumb" { |
| 28 | return false |
| 29 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 30 | var termios syscall.Termios |
| 31 | _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, f.Fd(), |
| 32 | ioctlGetTermios, uintptr(unsafe.Pointer(&termios)), |
| 33 | 0, 0, 0) |
| 34 | return err == 0 |
Colin Cross | dde49cb | 2019-06-08 21:59:42 -0700 | [diff] [blame] | 35 | } else if _, ok := w.(*fakeSmartTerminal); ok { |
| 36 | return true |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 37 | } |
| 38 | return false |
| 39 | } |
| 40 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 41 | func termSize(w io.Writer) (width int, height int, ok bool) { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 42 | if f, ok := w.(*os.File); ok { |
| 43 | var winsize struct { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 44 | wsRow, wsColumn uint16 |
| 45 | wsXpixel, wsYpixel uint16 |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 46 | } |
| 47 | _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, f.Fd(), |
| 48 | syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&winsize)), |
| 49 | 0, 0, 0) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 50 | return int(winsize.wsColumn), int(winsize.wsRow), err == 0 |
Colin Cross | dde49cb | 2019-06-08 21:59:42 -0700 | [diff] [blame] | 51 | } else if f, ok := w.(*fakeSmartTerminal); ok { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 52 | return f.termWidth, f.termHeight, true |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 53 | } |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 54 | return 0, 0, false |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // stripAnsiEscapes strips ANSI control codes from a byte array in place. |
| 58 | func stripAnsiEscapes(input []byte) []byte { |
| 59 | // read represents the remaining part of input that needs to be processed. |
| 60 | read := input |
| 61 | // write represents where we should be writing in input. |
| 62 | // It will share the same backing store as input so that we make our modifications |
| 63 | // in place. |
| 64 | write := input |
| 65 | |
| 66 | // advance will copy count bytes from read to write and advance those slices |
| 67 | advance := func(write, read []byte, count int) ([]byte, []byte) { |
| 68 | copy(write, read[:count]) |
| 69 | return write[count:], read[count:] |
| 70 | } |
| 71 | |
| 72 | for { |
| 73 | // Find the next escape sequence |
| 74 | i := bytes.IndexByte(read, 0x1b) |
| 75 | // If it isn't found, or if there isn't room for <ESC>[, finish |
| 76 | if i == -1 || i+1 >= len(read) { |
| 77 | copy(write, read) |
| 78 | break |
| 79 | } |
| 80 | |
| 81 | // Not a CSI code, continue searching |
| 82 | if read[i+1] != '[' { |
| 83 | write, read = advance(write, read, i+1) |
| 84 | continue |
| 85 | } |
| 86 | |
| 87 | // Found a CSI code, advance up to the <ESC> |
| 88 | write, read = advance(write, read, i) |
| 89 | |
| 90 | // Find the end of the CSI code |
| 91 | i = bytes.IndexFunc(read, func(r rune) bool { |
| 92 | return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') |
| 93 | }) |
| 94 | if i == -1 { |
| 95 | // We didn't find the end of the code, just remove the rest |
| 96 | i = len(read) - 1 |
| 97 | } |
| 98 | |
| 99 | // Strip off the end marker too |
| 100 | i = i + 1 |
| 101 | |
| 102 | // Skip the reader forward and reduce final length by that amount |
| 103 | read = read[i:] |
| 104 | input = input[:len(input)-i] |
| 105 | } |
| 106 | |
| 107 | return input |
| 108 | } |
Colin Cross | dde49cb | 2019-06-08 21:59:42 -0700 | [diff] [blame] | 109 | |
| 110 | type fakeSmartTerminal struct { |
| 111 | bytes.Buffer |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 112 | termWidth, termHeight int |
Colin Cross | dde49cb | 2019-06-08 21:59:42 -0700 | [diff] [blame] | 113 | } |