Skip to content

Commit

Permalink
feat(filepicker): added an option to disable plain text filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadBnei committed Mar 18, 2024
1 parent 43e9379 commit bc8c895
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ require (
github.com/tmc/langchaingo v0.1.5
go.uber.org/goleak v1.3.0
golang.org/x/sync v0.6.0
golang.org/x/tools v0.18.0
gopkg.in/yaml.v3 v3.0.1
moul.io/banner v1.0.1
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,6 @@ golang.org/x/tools v0.0.0-20190624180213-70d37148ca0c/go.mod h1:/rFqwRUd4F7ZHNgw
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.153.0 h1:N1AwGhielyKFaUqH07/ZSIQR3uNPcV7NVw0vj+j4iR4=
Expand Down
3 changes: 3 additions & 0 deletions ui/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ func (m *chatModel) Resize() {
w, h := AppStyle.GetFrameSize()
style.TitleStyle.MaxWidth(m.size.Width - w)

m.textarea.SetWidth(m.size.Width)

m.viewport.Width = m.size.Width - w
m.viewport.Height = m.size.Height - lipgloss.Height(m.GetTitleView()) - m.textarea.Height() - lipgloss.Height(m.help.View(m.keys)) - h


m.mdRenderer, _ = glamour.NewTermRenderer(glamour.WithAutoStyle(), glamour.WithWordWrap(m.viewport.Width-2))
}
15 changes: 10 additions & 5 deletions ui/file/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package file
import "github.com/charmbracelet/bubbles/key"

type keyMap struct {
submit key.Binding
changeCwd key.Binding
changeFocus key.Binding
toggleHidden key.Binding
addDir key.Binding
submit key.Binding
changeCwd key.Binding
changeFocus key.Binding
toggleHidden key.Binding
addDir key.Binding
toggleTextFilter key.Binding
}

func (k keyMap) ShortHelp() []key.Binding {
Expand Down Expand Up @@ -42,5 +43,9 @@ func newKeyMap() *keyMap {
key.WithKeys("ctrl+a"),
key.WithHelp("ctrl+a", "add dir"),
),
toggleTextFilter: key.NewBinding(
key.WithKeys("ctrl+f"),
key.WithHelp("ctrl+f", "toggle text filter"),
),
}
}
37 changes: 20 additions & 17 deletions ui/file/pickFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package file

import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -11,8 +12,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/samber/lo"
"golang.org/x/tools/godoc/util"
"golang.org/x/tools/godoc/vfs"

"github.com/MohammadBnei/go-ai-cli/service"
"github.com/MohammadBnei/go-ai-cli/ui/event"
Expand All @@ -21,14 +20,15 @@ import (
)

type PickFileModel struct {
filepicker Model
multiMode bool
keys *keyMap
help help.Model
title string
width int
selectedList *list.Model
fileFocus bool
filepicker Model
multiMode bool
keys *keyMap
help help.Model
title string
width int
selectedList *list.Model
fileFocus bool
textFiltering bool
}

// NewFilesPicker New creates a new instance of the UI.
Expand All @@ -47,12 +47,13 @@ func NewFilesPicker(allowedTypes []string) PickFileModel {
})

return PickFileModel{
filepicker: fp,
keys: newKeyMap(),
help: help.New(),
title: "File Picker",
selectedList: fileList,
fileFocus: true,
filepicker: fp,
keys: newKeyMap(),
help: help.New(),
title: "File Picker",
selectedList: fileList,
fileFocus: true,
textFiltering: true,
}
}

Expand Down Expand Up @@ -102,6 +103,8 @@ func (m PickFileModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.toggleHidden):
m.filepicker.ShowHidden = !m.filepicker.ShowHidden
return m, m.filepicker.readDir(m.filepicker.CurrentDirectory, m.filepicker.ShowHidden)
case key.Matches(msg, m.keys.toggleTextFilter):
m.textFiltering = !m.textFiltering
case key.Matches(msg, m.keys.addDir):
var addFileSequence []tea.Cmd
if err := filepath.Walk(m.filepicker.CurrentDirectory, func(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -139,7 +142,7 @@ func (m PickFileModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if err != nil {
return m, event.Error(err)
}
if !util.IsTextFile(vfs.OS("/"), msg.file) {
if !strings.Contains(http.DetectContentType(f), "text/plain") && m.textFiltering {
return m, event.Error(fmt.Errorf("file %s is not a text file", msg.file))
}
tokens, err := service.CountTokens(string(f))
Expand Down

0 comments on commit bc8c895

Please sign in to comment.