file.go

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