on main help sign in
Fix filepicker manual input (#146)
9345830Nicholas Hamilton
internal/tui/components/dialog/filepicker.go+27−30
@@ -127,6 +127,9 @@ func (f *filepickerCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
127 f.cursor = 0
127 f.cursor = 0
128 f.getCurrentFileBelowCursor()
128 f.getCurrentFileBelowCursor()
129 case tea.KeyMsg:
129 case tea.KeyMsg:
130+ if f.cwd.Focused() {
131+ f.cwd, cmd = f.cwd.Update(msg)
132+ }
130 switch {
133 switch {
131 case key.Matches(msg, filePickerKeyMap.InsertCWD):
134 case key.Matches(msg, filePickerKeyMap.InsertCWD):
132 f.cwd.Focus()
135 f.cwd.Focus()
@@ -165,7 +168,6 @@ func (f *filepickerCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
165 isPathDir = f.dirs[f.cursor].IsDir()
168 isPathDir = f.dirs[f.cursor].IsDir()
166 }
169 }
167 if isPathDir {
170 if isPathDir {
168− path := filepath.Join(f.cwdDetails.directory, "/", f.dirs[f.cursor].Name())
169 newWorkingDir := DirNode{parent: f.cwdDetails, directory: path}
171 newWorkingDir := DirNode{parent: f.cwdDetails, directory: path}
170 f.cwdDetails.child = &newWorkingDir
172 f.cwdDetails.child = &newWorkingDir
171 f.cwdDetails = f.cwdDetails.child
173 f.cwdDetails = f.cwdDetails.child
@@ -216,9 +218,6 @@ func (f *filepickerCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
216 f.getCurrentFileBelowCursor()
218 f.getCurrentFileBelowCursor()
217 }
219 }
218 }
220 }
219− if f.cwd.Focused() {
220− f.cwd, cmd = f.cwd.Update(msg)
221− }
222 return f, cmd
221 return f, cmd
223}
222}
224
223
@@ -228,37 +227,35 @@ func (f *filepickerCmp) addAttachmentToMessage() (tea.Model, tea.Cmd) {
228 logging.ErrorPersist(fmt.Sprintf("Model %s doesn't support attachments", modeInfo.Name))
227 logging.ErrorPersist(fmt.Sprintf("Model %s doesn't support attachments", modeInfo.Name))
229 return f, nil
228 return f, nil
230 }
229 }
231− if isExtSupported(f.dirs[f.cursor].Name()) {
232− f.selectedFile = f.dirs[f.cursor].Name()
233− selectedFilePath := filepath.Join(f.cwdDetails.directory, "/", f.selectedFile)
234− isFileLarge, err := image.ValidateFileSize(selectedFilePath, maxAttachmentSize)
235− if err != nil {
236− logging.ErrorPersist("unable to read the image")
237− return f, nil
238− }
239− if isFileLarge {
240− logging.ErrorPersist("file too large, max 5MB")
241− return f, nil
242− }
243
230
244− content, err := os.ReadFile(selectedFilePath)
231+ selectedFilePath := f.selectedFile
245− if err != nil {
232+ if !isExtSupported(selectedFilePath) {
246− logging.ErrorPersist("Unable read selected file")
233+ logging.ErrorPersist("Unsupported file")
247− return f, nil
234+ return f, nil
248− }
235+ }
249
236
250− mimeBufferSize := min(512, len(content))
237+ isFileLarge, err := image.ValidateFileSize(selectedFilePath, maxAttachmentSize)
251− mimeType := http.DetectContentType(content[:mimeBufferSize])
238+ if err != nil {
252− fileName := f.selectedFile
239+ logging.ErrorPersist("unable to read the image")
253− attachment := message.Attachment{FilePath: selectedFilePath, FileName: fileName, MimeType: mimeType, Content: content}
240+ return f, nil
254− f.selectedFile = ""
255− return f, util.CmdHandler(AttachmentAddedMsg{attachment})
256 }
241 }
257− if !isExtSupported(f.selectedFile) {
242+ if isFileLarge {
258− logging.ErrorPersist("Unsupported file")
243+ logging.ErrorPersist("file too large, max 5MB")
259 return f, nil
244 return f, nil
260 }
245 }
246+
247+ content, err := os.ReadFile(selectedFilePath)
248+ if err != nil {
249+ logging.ErrorPersist("Unable read selected file")
261− return f, nil
250+ return f, nil
251+ }
252+
253+ mimeBufferSize := min(512, len(content))
254+ mimeType := http.DetectContentType(content[:mimeBufferSize])
255+ fileName := filepath.Base(selectedFilePath)
256+ attachment := message.Attachment{FilePath: selectedFilePath, FileName: fileName, MimeType: mimeType, Content: content}
257+ f.selectedFile = ""
258+ return f, util.CmdHandler(AttachmentAddedMsg{attachment})
262}
259}
263
260
264func (f *filepickerCmp) View() string {
261func (f *filepickerCmp) View() string {