diff --git a/adapters/main.go b/adapters/main.go
index 61254ad..231e99c 100644
--- a/adapters/main.go
+++ b/adapters/main.go
@@ -6,2 +5,0 @@ import (
- "os"
- "path/filepath"
@@ -17,6 +15,6 @@ type Adapter struct {
- Init func(width int, height int)
- Render func(state []element.State)
- Load func(state []element.State)
- events map[string][]func(element.Event)
- Library *library.Shelf
- FileSystem FileSystem
+ Init func(width int, height int)
+ Render func(state []element.State)
+ Load func(state []element.State)
+ events map[string][]func(element.Event)
+ Library *library.Shelf
+ FilePaths []string
@@ -41,22 +39,2 @@ func (a *Adapter) DispatchEvent(event element.Event) {
-type FileSystem struct {
- Paths []string
- ReadFile func(path string) ([]byte, error)
- WriteFile func(path string, data []byte)
-}
-
-func (fs *FileSystem) AddFile(path string) {
- fs.Paths = append(fs.Paths, path)
-}
-
-func (fs *FileSystem) AddDir(path string) error {
- // Walk through the directory and collect all file paths
- err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() { // Only add files, not directories
- fs.Paths = append(fs.Paths, filePath)
- }
- return nil
- })
- return err
+func (a *Adapter) AddFilePath(path string) {
+ a.FilePaths = append(a.FilePaths, path)
@@ -117 +95 @@ type fileWithDistance struct {
-func (fs *FileSystem) FindFile(query string) string {
+func (a *Adapter) FindFile(query string) string {
@@ -121,2 +99,2 @@ func (fs *FileSystem) FindFile(query string) string {
- for _, path := range fs.Paths {
- distance := Levenshtein(query, filepath.Base(path))
+ for _, path := range a.FilePaths {
+ distance := Levenshtein(query, path)
package adapter
import (
"grim/element"
"grim/library"
"os"
"path/filepath"
"sort"
)
// !TODO: For things like fonts the file system interaction works fine, but if you want to run grim on a pi pico it doesn't have the file system int like
// + computers have
// + Option 1: make a file system adapter where authors can control how data is read
// + Option 2: at build time fetch all files needed and bundle them
type Adapter struct {
Init func(width int, height int)
Render func(state []element.State)
Load func(state []element.State)
events map[string][]func(element.Event)
Library *library.Shelf
FileSystem FileSystem
}
func (a *Adapter) AddEventListener(name string, callback func(element.Event)) {
if a.events == nil {
a.events = map[string][]func(element.Event){}
}
a.events[name] = append(a.events[name], callback)
}
func (a *Adapter) DispatchEvent(event element.Event) {
if a.events != nil {
evts := a.events[event.Name]
for _, v := range evts {
v(event)
}
}
}
type FileSystem struct {
Paths []string
ReadFile func(path string) ([]byte, error)
WriteFile func(path string, data []byte)
}
func (fs *FileSystem) AddFile(path string) {
fs.Paths = append(fs.Paths, path)
}
func (fs *FileSystem) AddDir(path string) error {
// Walk through the directory and collect all file paths
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() { // Only add files, not directories
fs.Paths = append(fs.Paths, filePath)
}
return nil
})
return err
}
// Levenshtein Distance Function (no external packages)
func Levenshtein(a, b string) int {
lenA, lenB := len(a), len(b)
// Create a matrix to hold distances
matrix := make([][]int, lenA+1)
for i := range matrix {
matrix[i] = make([]int, lenB+1)
}
// Initialize the first row and first column
for i := 0; i <= lenA; i++ {
matrix[i][0] = i
}
for j := 0; j <= lenB; j++ {
matrix[0][j] = j
}
// Calculate the Levenshtein distance
for i := 1; i <= lenA; i++ {
for j := 1; j <= lenB; j++ {
cost := 0
if a[i-1] != b[j-1] {
cost = 1
}
matrix[i][j] = min(matrix[i-1][j]+1, matrix[i][j-1]+1, matrix[i-1][j-1]+cost)
}
}
return matrix[lenA][lenB]
}
// Helper function to return the minimum of three values
func min(a, b, c int) int {
if a < b {
if a < c {
return a
}
return c
}
if b < c {
return b
}
return c
}
// Struct to hold file path and its Levenshtein distance
type fileWithDistance struct {
path string
distance int
}
// Fuzzy search function that returns the closest matches, sorted by Levenshtein distance
func (fs *FileSystem) FindFile(query string) string {
var fileDistances []fileWithDistance
// Calculate the Levenshtein distance for each file path
for _, path := range fs.Paths {
distance := Levenshtein(query, filepath.Base(path))
fileDistances = append(fileDistances, fileWithDistance{path, distance})
}
// Sort the fileDistances slice by distance (ascending order)
sort.Slice(fileDistances, func(i, j int) bool {
return fileDistances[i].distance < fileDistances[j].distance
})
// Create a slice to hold only the sorted file paths
var sortedPaths []string
for _, file := range fileDistances {
sortedPaths = append(sortedPaths, file.path)
}
return sortedPaths[0]
}