util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package terminal
  15. import (
  16. "bytes"
  17. "io"
  18. "os"
  19. "syscall"
  20. "unsafe"
  21. )
  22. func isSmartTerminal(w io.Writer) bool {
  23. if f, ok := w.(*os.File); ok {
  24. if term, ok := os.LookupEnv("TERM"); ok && term == "dumb" {
  25. return false
  26. }
  27. var termios syscall.Termios
  28. _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, f.Fd(),
  29. ioctlGetTermios, uintptr(unsafe.Pointer(&termios)),
  30. 0, 0, 0)
  31. return err == 0
  32. } else if _, ok := w.(*fakeSmartTerminal); ok {
  33. return true
  34. }
  35. return false
  36. }
  37. func termSize(w io.Writer) (width int, height int, ok bool) {
  38. if f, ok := w.(*os.File); ok {
  39. var winsize struct {
  40. wsRow, wsColumn uint16
  41. wsXpixel, wsYpixel uint16
  42. }
  43. _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, f.Fd(),
  44. syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&winsize)),
  45. 0, 0, 0)
  46. return int(winsize.wsColumn), int(winsize.wsRow), err == 0
  47. } else if f, ok := w.(*fakeSmartTerminal); ok {
  48. return f.termWidth, f.termHeight, true
  49. }
  50. return 0, 0, false
  51. }
  52. // stripAnsiEscapes strips ANSI control codes from a byte array in place.
  53. func stripAnsiEscapes(input []byte) []byte {
  54. // read represents the remaining part of input that needs to be processed.
  55. read := input
  56. // write represents where we should be writing in input.
  57. // It will share the same backing store as input so that we make our modifications
  58. // in place.
  59. write := input
  60. // advance will copy count bytes from read to write and advance those slices
  61. advance := func(write, read []byte, count int) ([]byte, []byte) {
  62. copy(write, read[:count])
  63. return write[count:], read[count:]
  64. }
  65. for {
  66. // Find the next escape sequence
  67. i := bytes.IndexByte(read, 0x1b)
  68. // If it isn't found, or if there isn't room for <ESC>[, finish
  69. if i == -1 || i+1 >= len(read) {
  70. copy(write, read)
  71. break
  72. }
  73. // Not a CSI code, continue searching
  74. if read[i+1] != '[' {
  75. write, read = advance(write, read, i+1)
  76. continue
  77. }
  78. // Found a CSI code, advance up to the <ESC>
  79. write, read = advance(write, read, i)
  80. // Find the end of the CSI code
  81. i = bytes.IndexFunc(read, func(r rune) bool {
  82. return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
  83. })
  84. if i == -1 {
  85. // We didn't find the end of the code, just remove the rest
  86. i = len(read) - 1
  87. }
  88. // Strip off the end marker too
  89. i = i + 1
  90. // Skip the reader forward and reduce final length by that amount
  91. read = read[i:]
  92. input = input[:len(input)-i]
  93. }
  94. return input
  95. }
  96. type fakeSmartTerminal struct {
  97. bytes.Buffer
  98. termWidth, termHeight int
  99. }