You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB

  1. package readline
  2. type SegmentCompleter interface {
  3. // a
  4. // |- a1
  5. // |--- a11
  6. // |- a2
  7. // b
  8. // input:
  9. // DoTree([], 0) [a, b]
  10. // DoTree([a], 1) [a]
  11. // DoTree([a, ], 0) [a1, a2]
  12. // DoTree([a, a], 1) [a1, a2]
  13. // DoTree([a, a1], 2) [a1]
  14. // DoTree([a, a1, ], 0) [a11]
  15. // DoTree([a, a1, a], 1) [a11]
  16. DoSegment([][]rune, int) [][]rune
  17. }
  18. type dumpSegmentCompleter struct {
  19. f func([][]rune, int) [][]rune
  20. }
  21. func (d *dumpSegmentCompleter) DoSegment(segment [][]rune, n int) [][]rune {
  22. return d.f(segment, n)
  23. }
  24. func SegmentFunc(f func([][]rune, int) [][]rune) AutoCompleter {
  25. return &SegmentComplete{&dumpSegmentCompleter{f}}
  26. }
  27. func SegmentAutoComplete(completer SegmentCompleter) *SegmentComplete {
  28. return &SegmentComplete{
  29. SegmentCompleter: completer,
  30. }
  31. }
  32. type SegmentComplete struct {
  33. SegmentCompleter
  34. }
  35. func RetSegment(segments [][]rune, cands [][]rune, idx int) ([][]rune, int) {
  36. ret := make([][]rune, 0, len(cands))
  37. lastSegment := segments[len(segments)-1]
  38. for _, cand := range cands {
  39. if !runes.HasPrefix(cand, lastSegment) {
  40. continue
  41. }
  42. ret = append(ret, cand[len(lastSegment):])
  43. }
  44. return ret, idx
  45. }
  46. func SplitSegment(line []rune, pos int) ([][]rune, int) {
  47. segs := [][]rune{}
  48. lastIdx := -1
  49. line = line[:pos]
  50. pos = 0
  51. for idx, l := range line {
  52. if l == ' ' {
  53. pos = 0
  54. segs = append(segs, line[lastIdx+1:idx])
  55. lastIdx = idx
  56. } else {
  57. pos++
  58. }
  59. }
  60. segs = append(segs, line[lastIdx+1:])
  61. return segs, pos
  62. }
  63. func (c *SegmentComplete) Do(line []rune, pos int) (newLine [][]rune, offset int) {
  64. segment, idx := SplitSegment(line, pos)
  65. cands := c.DoSegment(segment, idx)
  66. newLine, offset = RetSegment(segment, cands, idx)
  67. for idx := range newLine {
  68. newLine[idx] = append(newLine[idx], ' ')
  69. }
  70. return newLine, offset
  71. }