file.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package tools
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/kujtimiihoxha/termai/internal/config"
  6. )
  7. // File record to track when files were read/written
  8. type fileRecord struct {
  9. path string
  10. readTime time.Time
  11. writeTime time.Time
  12. }
  13. var (
  14. fileRecords = make(map[string]fileRecord)
  15. fileRecordMutex sync.RWMutex
  16. )
  17. func removeWorkingDirectoryPrefix(path string) string {
  18. wd := config.WorkingDirectory()
  19. if len(path) > len(wd) && path[:len(wd)] == wd {
  20. return path[len(wd)+1:]
  21. }
  22. return path
  23. }
  24. func recordFileRead(path string) {
  25. fileRecordMutex.Lock()
  26. defer fileRecordMutex.Unlock()
  27. record, exists := fileRecords[path]
  28. if !exists {
  29. record = fileRecord{path: path}
  30. }
  31. record.readTime = time.Now()
  32. fileRecords[path] = record
  33. }
  34. func getLastReadTime(path string) time.Time {
  35. fileRecordMutex.RLock()
  36. defer fileRecordMutex.RUnlock()
  37. record, exists := fileRecords[path]
  38. if !exists {
  39. return time.Time{}
  40. }
  41. return record.readTime
  42. }
  43. func recordFileWrite(path string) {
  44. fileRecordMutex.Lock()
  45. defer fileRecordMutex.Unlock()
  46. record, exists := fileRecords[path]
  47. if !exists {
  48. record = fileRecord{path: path}
  49. }
  50. record.writeTime = time.Now()
  51. fileRecords[path] = record
  52. }