repl_windows.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. // MA 02110-1301 USA
  17. package ethrepl
  18. import (
  19. "bufio"
  20. "fmt"
  21. "os"
  22. "strings"
  23. )
  24. func (self *JSRepl) read() {
  25. reader := bufio.NewReader(os.Stdin)
  26. for {
  27. fmt.Printf(self.prompt)
  28. str, _, err := reader.ReadLine()
  29. if err != nil {
  30. fmt.Println("Error reading input", err)
  31. } else {
  32. if string(str) == "exit" {
  33. self.Stop()
  34. break
  35. } else {
  36. self.parseInput(string(str))
  37. }
  38. }
  39. }
  40. }
  41. func addHistory(s string) {
  42. }
  43. func printColored(outputVal string) {
  44. for outputVal != "" {
  45. codePart := ""
  46. if strings.HasPrefix(outputVal, "\033[32m") {
  47. codePart = "\033[32m"
  48. changeColor(2)
  49. }
  50. if strings.HasPrefix(outputVal, "\033[1m\033[30m") {
  51. codePart = "\033[1m\033[30m"
  52. changeColor(8)
  53. }
  54. if strings.HasPrefix(outputVal, "\033[31m") {
  55. codePart = "\033[31m"
  56. changeColor(red)
  57. }
  58. if strings.HasPrefix(outputVal, "\033[35m") {
  59. codePart = "\033[35m"
  60. changeColor(5)
  61. }
  62. if strings.HasPrefix(outputVal, "\033[0m") {
  63. codePart = "\033[0m"
  64. resetColorful()
  65. }
  66. textPart := outputVal[len(codePart):len(outputVal)]
  67. index := strings.Index(textPart, "\033")
  68. if index == -1 {
  69. outputVal = ""
  70. } else {
  71. outputVal = textPart[index:len(textPart)]
  72. textPart = textPart[0:index]
  73. }
  74. fmt.Printf("%v", textPart)
  75. }
  76. }
  77. func (self *JSRepl) PrintValue(v interface{}) {
  78. method, _ := self.re.Vm.Get("prettyPrint")
  79. v, err := self.re.Vm.ToValue(v)
  80. if err == nil {
  81. val, err := method.Call(method, v)
  82. if err == nil {
  83. printColored(fmt.Sprintf("%v", val))
  84. }
  85. }
  86. }