More work on porting the diff kitten

This commit is contained in:
Kovid Goyal
2023-03-17 16:31:58 +05:30
parent bf1f0c00f4
commit 293c0ab845
4 changed files with 330 additions and 0 deletions

View File

@@ -4,13 +4,64 @@ package diff
import (
"fmt"
"kitty/tools/utils"
"os"
"path/filepath"
"strings"
)
var _ = fmt.Print
var path_name_map, remote_dirs map[string]string
var mimetypes_cache, data_cache *utils.LRUCache[string, string]
var lines_cache *utils.LRUCache[string, []string]
func init_caches() {
mimetypes_cache = utils.NewLRUCache[string, string](4096)
data_cache = utils.NewLRUCache[string, string](4096)
lines_cache = utils.NewLRUCache[string, []string](4096)
}
func mimetype_for_path(path string) string {
return mimetypes_cache.MustGetOrCreate(path, func(path string) string {
mt := utils.GuessMimeTypeWithFileSystemAccess(path)
if mt == "" {
mt = "application/octet-stream"
}
if utils.KnownTextualMimes[mt] {
if _, a, found := strings.Cut(mt, "/"); found {
mt = "text/" + a
}
}
return mt
})
}
func data_for_path(path string) (string, error) {
return data_cache.GetOrCreate(path, func(path string) (string, error) {
ans, err := os.ReadFile(path)
return utils.UnsafeBytesToString(ans), err
})
}
func sanitize(x string) string {
x = strings.ReplaceAll(x, "\r\n", "⏎\n")
return utils.SanitizeControlCodes(x, "░")
}
func lines_for_path(path string) ([]string, error) {
return lines_cache.GetOrCreate(path, func(path string) ([]string, error) {
ans, err := data_for_path(path)
if err != nil {
return nil, err
}
ans = sanitize(strings.ReplaceAll(ans, "\t", conf.Replace_tab_by))
lines := make([]string, 0, 256)
splitlines_like_git(ans, false, func(line string) { lines = append(lines, line) })
return lines, nil
})
}
type Collection struct {
}