parser_actions.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package ansiterm
  2. func (ap *AnsiParser) collectParam() error {
  3. currChar := ap.context.currentChar
  4. ap.logf("collectParam %#x", currChar)
  5. ap.context.paramBuffer = append(ap.context.paramBuffer, currChar)
  6. return nil
  7. }
  8. func (ap *AnsiParser) collectInter() error {
  9. currChar := ap.context.currentChar
  10. ap.logf("collectInter %#x", currChar)
  11. ap.context.paramBuffer = append(ap.context.interBuffer, currChar)
  12. return nil
  13. }
  14. func (ap *AnsiParser) escDispatch() error {
  15. cmd, _ := parseCmd(*ap.context)
  16. intermeds := ap.context.interBuffer
  17. ap.logf("escDispatch currentChar: %#x", ap.context.currentChar)
  18. ap.logf("escDispatch: %v(%v)", cmd, intermeds)
  19. switch cmd {
  20. case "D": // IND
  21. return ap.eventHandler.IND()
  22. case "E": // NEL, equivalent to CRLF
  23. err := ap.eventHandler.Execute(ANSI_CARRIAGE_RETURN)
  24. if err == nil {
  25. err = ap.eventHandler.Execute(ANSI_LINE_FEED)
  26. }
  27. return err
  28. case "M": // RI
  29. return ap.eventHandler.RI()
  30. }
  31. return nil
  32. }
  33. func (ap *AnsiParser) csiDispatch() error {
  34. cmd, _ := parseCmd(*ap.context)
  35. params, _ := parseParams(ap.context.paramBuffer)
  36. ap.logf("Parsed params: %v with length: %d", params, len(params))
  37. ap.logf("csiDispatch: %v(%v)", cmd, params)
  38. switch cmd {
  39. case "@":
  40. return ap.eventHandler.ICH(getInt(params, 1))
  41. case "A":
  42. return ap.eventHandler.CUU(getInt(params, 1))
  43. case "B":
  44. return ap.eventHandler.CUD(getInt(params, 1))
  45. case "C":
  46. return ap.eventHandler.CUF(getInt(params, 1))
  47. case "D":
  48. return ap.eventHandler.CUB(getInt(params, 1))
  49. case "E":
  50. return ap.eventHandler.CNL(getInt(params, 1))
  51. case "F":
  52. return ap.eventHandler.CPL(getInt(params, 1))
  53. case "G":
  54. return ap.eventHandler.CHA(getInt(params, 1))
  55. case "H":
  56. ints := getInts(params, 2, 1)
  57. x, y := ints[0], ints[1]
  58. return ap.eventHandler.CUP(x, y)
  59. case "J":
  60. param := getEraseParam(params)
  61. return ap.eventHandler.ED(param)
  62. case "K":
  63. param := getEraseParam(params)
  64. return ap.eventHandler.EL(param)
  65. case "L":
  66. return ap.eventHandler.IL(getInt(params, 1))
  67. case "M":
  68. return ap.eventHandler.DL(getInt(params, 1))
  69. case "P":
  70. return ap.eventHandler.DCH(getInt(params, 1))
  71. case "S":
  72. return ap.eventHandler.SU(getInt(params, 1))
  73. case "T":
  74. return ap.eventHandler.SD(getInt(params, 1))
  75. case "c":
  76. return ap.eventHandler.DA(params)
  77. case "d":
  78. return ap.eventHandler.VPA(getInt(params, 1))
  79. case "f":
  80. ints := getInts(params, 2, 1)
  81. x, y := ints[0], ints[1]
  82. return ap.eventHandler.HVP(x, y)
  83. case "h":
  84. return ap.hDispatch(params)
  85. case "l":
  86. return ap.lDispatch(params)
  87. case "m":
  88. return ap.eventHandler.SGR(getInts(params, 1, 0))
  89. case "r":
  90. ints := getInts(params, 2, 1)
  91. top, bottom := ints[0], ints[1]
  92. return ap.eventHandler.DECSTBM(top, bottom)
  93. default:
  94. ap.logf("ERROR: Unsupported CSI command: '%s', with full context: %v", cmd, ap.context)
  95. return nil
  96. }
  97. }
  98. func (ap *AnsiParser) print() error {
  99. return ap.eventHandler.Print(ap.context.currentChar)
  100. }
  101. func (ap *AnsiParser) clear() error {
  102. ap.context = &ansiContext{}
  103. return nil
  104. }
  105. func (ap *AnsiParser) execute() error {
  106. return ap.eventHandler.Execute(ap.context.currentChar)
  107. }