Files
futriis/internal/repl/history.go
2026-04-08 21:43:35 +03:00

97 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Файл: internal/repl/history.go
// Назначение: Управление историей команд REPL
package repl
import (
"bufio"
"os"
"path/filepath"
)
// History управляет историей команд
type History struct {
entries []string
maxSize int
filePath string
}
// NewHistory создаёт новый объект истории
func NewHistory(maxSize int) *History {
homeDir, _ := os.UserHomeDir()
filePath := filepath.Join(homeDir, ".futriis_history")
return &History{
entries: make([]string, 0, maxSize),
maxSize: maxSize,
filePath: filePath,
}
}
// Add добавляет команду в историю
func (h *History) Add(cmd string) error {
// Не добавляем дубликаты подряд
if len(h.entries) > 0 && h.entries[len(h.entries)-1] == cmd {
return nil
}
h.entries = append(h.entries, cmd)
// Ограничиваем размер истории
if len(h.entries) > h.maxSize {
h.entries = h.entries[len(h.entries)-h.maxSize:]
}
return nil
}
// Load загружает историю из файла
func (h *History) Load() error {
file, err := os.Open(h.filePath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
cmd := scanner.Text()
if cmd != "" {
h.entries = append(h.entries, cmd)
}
}
// Ограничиваем размер
if len(h.entries) > h.maxSize {
h.entries = h.entries[len(h.entries)-h.maxSize:]
}
return scanner.Err()
}
// Save сохраняет историю в файл
func (h *History) Save() error {
file, err := os.Create(h.filePath)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for _, cmd := range h.entries {
if _, err := writer.WriteString(cmd + "\n"); err != nil {
return err
}
}
return writer.Flush()
}
// GetEntries возвращает все записи истории
func (h *History) GetEntries() []string {
return h.entries
}