terminal.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package pio
  2. import (
  3. "io"
  4. "os/exec"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "github.com/kataras/pio/terminal"
  9. )
  10. func isTerminal(output io.Writer) bool {
  11. isTerminal := !IsNop(output) || terminal.IsTerminal(output)
  12. // if it's not a terminal and the os is not a windows one,
  13. // then return whatever already found.
  14. if !isTerminal || runtime.GOOS != "windows" {
  15. return isTerminal
  16. }
  17. // check specific for windows operating system
  18. // versions, after windows 10 microsoft
  19. // gave suppprt for 256-color console.
  20. cmd := exec.Command("cmd", "ver")
  21. b, err := cmd.Output()
  22. if err != nil {
  23. return false
  24. }
  25. /*
  26. Microsoft Windows [Version 10.0.15063]
  27. (c) 2017 Microsoft Corporation. All rights reserved.
  28. */
  29. lines := string(b)
  30. if lines == "" {
  31. return false
  32. }
  33. start := strings.IndexByte(lines, '[')
  34. end := strings.IndexByte(lines, ']')
  35. winLine := lines[start+1 : end]
  36. if len(winLine) < 10 {
  37. return false
  38. }
  39. // Version 10.0.15063
  40. versionsLine := winLine[strings.IndexByte(winLine, ' ')+1:]
  41. // 10.0.15063
  42. versionSems := strings.Split(versionsLine, ".")
  43. // 10
  44. // 0
  45. // 15063
  46. if len(versionSems) < 3 {
  47. return false
  48. }
  49. // ok, we need to check if it's windows version 10
  50. if versionSems[0] != "10" {
  51. return false
  52. }
  53. buildNumber, err := strconv.Atoi(versionSems[2])
  54. // and the build number is equal or greater than 10586
  55. if err != nil {
  56. return false
  57. }
  58. return buildNumber >= 10586
  59. }