history.go 2.2 KB

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