history.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package readline
  2. import (
  3. "bufio"
  4. "errors"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/emirpasic/gods/lists/arraylist"
  10. )
  11. type History struct {
  12. Buf *arraylist.List
  13. Autosave bool
  14. Pos int
  15. Limit int
  16. Filename string
  17. Enabled bool
  18. }
  19. func NewHistory() (*History, error) {
  20. h := &History{
  21. Buf: arraylist.New(),
  22. Limit: 100, //resizeme
  23. Autosave: true,
  24. Enabled: true,
  25. }
  26. err := h.Init()
  27. if err != nil {
  28. return nil, err
  29. }
  30. return h, nil
  31. }
  32. func (h *History) Init() error {
  33. home, err := os.UserHomeDir()
  34. if err != nil {
  35. return err
  36. }
  37. path := filepath.Join(home, ".ollama", "history")
  38. h.Filename = path
  39. //todo check if the file exists
  40. f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0600)
  41. if err != nil {
  42. if errors.Is(err, os.ErrNotExist) {
  43. return nil
  44. }
  45. return err
  46. }
  47. defer f.Close()
  48. r := bufio.NewReader(f)
  49. for {
  50. line, err := r.ReadString('\n')
  51. if err != nil {
  52. if err == io.EOF {
  53. break
  54. }
  55. return err
  56. }
  57. line = strings.TrimSpace(line)
  58. if len(line) == 0 {
  59. continue
  60. }
  61. h.Add([]rune(line))
  62. }
  63. return nil
  64. }
  65. func (h *History) Add(l []rune) {
  66. h.Buf.Add(l)
  67. h.Compact()
  68. h.Pos = h.Size()
  69. if h.Autosave {
  70. h.Save()
  71. }
  72. }
  73. func (h *History) Compact() {
  74. s := h.Buf.Size()
  75. if s > h.Limit {
  76. for cnt := 0; cnt < s-h.Limit; cnt++ {
  77. h.Buf.Remove(0)
  78. }
  79. }
  80. }
  81. func (h *History) Clear() {
  82. h.Buf.Clear()
  83. }
  84. func (h *History) Prev() []rune {
  85. var line []rune
  86. if h.Pos > 0 {
  87. h.Pos -= 1
  88. }
  89. v, _ := h.Buf.Get(h.Pos)
  90. line, _ = v.([]rune)
  91. return line
  92. }
  93. func (h *History) Next() []rune {
  94. var line []rune
  95. if h.Pos < h.Buf.Size() {
  96. h.Pos += 1
  97. v, _ := h.Buf.Get(h.Pos)
  98. line, _ = v.([]rune)
  99. }
  100. return line
  101. }
  102. func (h *History) Size() int {
  103. return h.Buf.Size()
  104. }
  105. func (h *History) Save() error {
  106. if !h.Enabled {
  107. return nil
  108. }
  109. tmpFile := h.Filename + ".tmp"
  110. f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_APPEND, 0666)
  111. if err != nil {
  112. return err
  113. }
  114. defer f.Close()
  115. buf := bufio.NewWriter(f)
  116. for cnt := 0; cnt < h.Size(); cnt++ {
  117. v, _ := h.Buf.Get(cnt)
  118. line, _ := v.([]rune)
  119. buf.WriteString(string(line) + "\n")
  120. }
  121. buf.Flush()
  122. f.Close()
  123. if err = os.Rename(tmpFile, h.Filename); err != nil {
  124. return err
  125. }
  126. return nil
  127. }