diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-10-17 13:00:44 +1100 |
---|---|---|
committer | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2021-10-17 04:17:59 +0000 |
commit | 345c90ac05bffd6e4163be9dabc0386cd8918057 (patch) | |
tree | ed29b41dfe6354adba1f81c11d676519ebdf2daa /pkg/gui | |
parent | 7564e506b525e0831a1fb88dfc70e9d27564d8a1 (diff) |
fix editor
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/commit_message_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/confirmation_panel.go | 31 | ||||
-rw-r--r-- | pkg/gui/credentials_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/editors.go | 87 | ||||
-rw-r--r-- | pkg/gui/files_panel.go | 7 | ||||
-rw-r--r-- | pkg/gui/searching.go | 4 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 14 |
7 files changed, 69 insertions, 80 deletions
diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index 4c131db20..fb1303a55 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -10,7 +10,7 @@ import ( ) func (gui *Gui) handleCommitConfirm() error { - message := gui.trimmedContent(gui.Views.CommitMessage) + message := strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent()) if message == "" { return gui.createErrorPanel(gui.Tr.CommitWithoutMessageErr) } @@ -48,7 +48,7 @@ func (gui *Gui) handleCommitMessageFocused() error { } func (gui *Gui) getBufferLength(view *gocui.View) string { - return " " + strconv.Itoa(strings.Count(view.Buffer(), "")-1) + " " + return " " + strconv.Itoa(strings.Count(view.TextArea.GetContent(), "")-1) + " " } // RenderCommitLength is a function. diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index edcde8821..17838c1f6 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -1,9 +1,5 @@ // lots of this has been directly ported from one of the example files, will brush up later -// Copyright 2014 The gocui Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - package gui import ( @@ -169,7 +165,7 @@ func (gui *Gui) getConfirmationPanelDimensions(wrap bool, prompt string) (int, i height/2 + panelHeight/2 } -func (gui *Gui) prepareConfirmationPanel(title, prompt string, hasLoader bool, findSuggestionsFunc func(string) []*types.Suggestion) error { +func (gui *Gui) prepareConfirmationPanel(title, prompt string, hasLoader bool, findSuggestionsFunc func(string) []*types.Suggestion, editable bool) error { x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(true, prompt) // calling SetView on an existing view returns the same view, so I'm not bothering // to reassign to gui.Views.Confirmation @@ -182,7 +178,8 @@ func (gui *Gui) prepareConfirmationPanel(title, prompt string, hasLoader bool, f gui.g.StartTicking() } gui.Views.Confirmation.Title = title - gui.Views.Confirmation.Wrap = true + // for now we do not support wrapping in our editor + gui.Views.Confirmation.Wrap = !editable gui.Views.Confirmation.FgColor = theme.GocuiDefaultTextColor gui.findSuggestions = findSuggestionsFunc @@ -209,19 +206,27 @@ func (gui *Gui) createPopupPanel(opts createPopupPanelOpts) error { // remove any previous keybindings gui.clearConfirmationViewKeyBindings() - err := gui.prepareConfirmationPanel(opts.title, opts.prompt, opts.hasLoader, opts.findSuggestionsFunc) + err := gui.prepareConfirmationPanel( + opts.title, + opts.prompt, + opts.hasLoader, + opts.findSuggestionsFunc, + opts.editable, + ) if err != nil { return err } - gui.Views.Confirmation.Editable = opts.editable - gui.Views.Confirmation.Editor = gocui.EditorFunc(gui.defaultEditor) + confirmationView := gui.Views.Confirmation + confirmationView.Editable = opts.editable + confirmationView.Editor = gocui.EditorFunc(gui.defaultEditor) if opts.editable { - if err := gui.Views.Confirmation.SetEditorContent(opts.prompt); err != nil { - return err - } + textArea := confirmationView.TextArea + textArea.Clear() + textArea.TypeString(opts.prompt) + confirmationView.RenderTextArea() } else { - if err := gui.renderStringSync(gui.Views.Confirmation, opts.prompt); err != nil { + if err := gui.renderStringSync(confirmationView, opts.prompt); err != nil { return err } } diff --git a/pkg/gui/credentials_panel.go b/pkg/gui/credentials_panel.go index ac162ae02..cb814243f 100644 --- a/pkg/gui/credentials_panel.go +++ b/pkg/gui/credentials_panel.go @@ -41,7 +41,7 @@ func (gui *Gui) promptUserForCredential(passOrUname string) string { func (gui *Gui) handleSubmitCredential() error { credentialsView := gui.Views.Credentials - message := gui.trimmedContent(credentialsView) + message := strings.TrimSpace(credentialsView.TextArea.GetContent()) gui.credentials <- message gui.clearEditorView(credentialsView) if err := gui.returnFromContext(); err != nil { diff --git a/pkg/gui/editors.go b/pkg/gui/editors.go index 7d3054cd0..6cc6409cb 100644 --- a/pkg/gui/editors.go +++ b/pkg/gui/editors.go @@ -6,88 +6,77 @@ import ( "github.com/jesseduffield/gocui" ) -// we've just copy+pasted the editor from gocui to here so that we can also re- -// render the commit message length on each keypress -func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool { +func (gui *Gui) handleEditorKeypress(textArea *gocui.TextArea, key gocui.Key, ch rune, mod gocui.Modifier, allowMultiline bool) bool { newlineKey, ok := gui.getKey(gui.Config.GetUserConfig().Keybinding.Universal.AppendNewline).(gocui.Key) if !ok { newlineKey = gocui.KeyAltEnter } - matched := true switch { case key == gocui.KeyBackspace || key == gocui.KeyBackspace2: - v.EditDelete(true) + textArea.BackSpaceChar() case key == gocui.KeyCtrlD || key == gocui.KeyDelete: - v.EditDelete(false) + textArea.DeleteChar() case key == gocui.KeyArrowDown: - v.MoveCursor(0, 1, false) + textArea.MoveCursorDown() case key == gocui.KeyArrowUp: - v.MoveCursor(0, -1, false) + textArea.MoveCursorUp() case key == gocui.KeyArrowLeft: - v.MoveCursor(-1, 0, false) + textArea.MoveCursorLeft() case key == gocui.KeyArrowRight: - v.MoveCursor(1, 0, false) + textArea.MoveCursorRight() case key == newlineKey: - v.EditNewLine() + if allowMultiline { + textArea.TypeRune('\n') + } else { + return false + } case key == gocui.KeySpace: - v.EditWrite(' ') + textArea.TypeRune(' ') case key == gocui.KeyInsert: - v.Overwrite = !v.Overwrite + textArea.ToggleOverwrite() case key == gocui.KeyCtrlU: - v.EditDeleteToStartOfLine() + textArea.DeleteToStartOfLine() case key == gocui.KeyCtrlA: - v.EditGotoToStartOfLine() + textArea.GoToStartOfLine() case key == gocui.KeyCtrlE: - v.EditGotoToEndOfLine() + textArea.GoToEndOfLine() // TODO: see if we need all three of these conditions: maybe the final one is sufficient case ch != 0 && mod == 0 && unicode.IsPrint(ch): - v.EditWrite(ch) + textArea.TypeRune(ch) default: - matched = false + return false } + return true +} + +// we've just copy+pasted the editor from gocui to here so that we can also re- +// render the commit message length on each keypress +func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool { + matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, true) + + // This function is called again on refresh as part of the general resize popup call, + // but we need to call it here so that when we go to render the text area it's not + // considered out of bounds to add a newline, meaning we can avoid unnecessary scrolling. + err := gui.resizePopupPanel(v, v.TextArea.GetContent()) + if err != nil { + gui.Log.Error(err) + } + v.RenderTextArea() gui.RenderCommitLength() return matched } func (gui *Gui) defaultEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool { - matched := true - switch { - case key == gocui.KeyBackspace || key == gocui.KeyBackspace2: - v.EditDelete(true) - case key == gocui.KeyCtrlD || key == gocui.KeyDelete: - v.EditDelete(false) - case key == gocui.KeyArrowDown: - v.MoveCursor(0, 1, false) - case key == gocui.KeyArrowUp: - v.MoveCursor(0, -1, false) - case key == gocui.KeyArrowLeft: - v.MoveCursor(-1, 0, false) - case key == gocui.KeyArrowRight: - v.MoveCursor(1, 0, false) - case key == gocui.KeySpace: - v.EditWrite(' ') - case key == gocui.KeyInsert: - v.Overwrite = !v.Overwrite - case key == gocui.KeyCtrlU: - v.EditDeleteToStartOfLine() - case key == gocui.KeyCtrlA: - v.EditGotoToStartOfLine() - case key == gocui.KeyCtrlE: - v.EditGotoToEndOfLine() + matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false) - // TODO: see if we need all three of these conditions: maybe the final one is sufficient - case ch != 0 && mod == 0 && unicode.IsPrint(ch): - v.EditWrite(ch) - default: - matched = false - } + v.RenderTextArea() if gui.findSuggestions != nil { - input := v.Buffer() + input := v.TextArea.GetContent() suggestions := gui.findSuggestions(input) gui.setSuggestions(suggestions) } diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 28554a428..05b966e5d 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -347,9 +347,10 @@ func (gui *Gui) handleWIPCommitPress() error { return gui.createErrorPanel(gui.Tr.SkipHookPrefixNotConfigured) } - if err := gui.Views.CommitMessage.SetEditorContent(skipHookPrefix); err != nil { - return err - } + textArea := gui.Views.CommitMessage.TextArea + textArea.Clear() + textArea.TypeString(skipHookPrefix) + gui.Views.CommitMessage.RenderTextArea() return gui.handleCommitPress() } diff --git a/pkg/gui/searching.go b/pkg/gui/searching.go index 6832aab71..9d7c1ec84 100644 --- a/pkg/gui/searching.go +++ b/pkg/gui/searching.go @@ -15,7 +15,7 @@ func (gui *Gui) handleOpenSearch(viewName string) error { gui.State.Searching.isSearching = true gui.State.Searching.view = view - gui.renderString(gui.Views.Search, "") + gui.Views.Search.ClearTextArea() if err := gui.pushContext(gui.State.Contexts.Search); err != nil { return err @@ -25,7 +25,7 @@ func (gui *Gui) handleOpenSearch(viewName string) error { } func (gui *Gui) handleSearch() error { - gui.State.Searching.searchString = gui.Views.Search.Buffer() + gui.State.Searching.searchString = gui.Views.Search.TextArea.GetContent() if err := gui.returnFromContext(); err != nil { return err } diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 429f47a11..50b661a12 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -244,10 +244,6 @@ func (gui *Gui) renderOptionsMap(optionsMap map[string]string) { gui.renderString(gui.Views.Options, gui.optionsMapToString(optionsMap)) } -func (gui *Gui) trimmedContent(v *gocui.View) string { - return strings.TrimSpace(v.Buffer()) -} - func (gui *Gui) currentViewName() string { currentView := gui.g.CurrentView() if currentView == nil { @@ -262,15 +258,14 @@ func (gui *Gui) resizeCurrentPopupPanel() error { return nil } if gui.isPopupPanel(v.Name()) { - return gui.resizePopupPanel(v) + return gui.resizePopupPanel(v, v.Buffer()) } return nil } -func (gui *Gui) resizePopupPanel(v *gocui.View) error { +func (gui *Gui) resizePopupPanel(v *gocui.View, content string) error { // If the confirmation panel is already displayed, just resize the width, // otherwise continue - content := v.Buffer() x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(v.Wrap, content) vx0, vy0, vx1, vy1 := v.Dimensions() if vx0 == x0 && vy0 == y0 && vx1 == x1 && vy1 == y1 { @@ -346,9 +341,8 @@ func (gui *Gui) secondaryViewFocused() bool { } func (gui *Gui) clearEditorView(v *gocui.View) { - v.Clear() - _ = v.SetCursor(0, 0) - _ = v.SetOrigin(0, 0) + v.TextArea.Clear() + v.RenderTextArea() } func (gui *Gui) onViewTabClick(viewName string, tabIndex int) error { |