Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-he committed Nov 8, 2023
1 parent cee9dd6 commit fa75e0a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 70 deletions.
5 changes: 4 additions & 1 deletion internal/control/cli/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,10 @@ func NewController(
return
}

nameStringEditor := controller.data.TaskEditor.GetEditor("name")
nameStringEditor, ok := controller.data.TaskEditor.(*editor.E).GetEditor("name").(*editor.StringEditor)
if !ok {
log.Fatal().Msgf("did not get a string editor for 'name' but a %T", controller.data.TaskEditor.(*editor.E).GetEditor("name"))
}
if nameStringEditor == nil {
log.Warn().Msgf("task editor for '%s' has no view 'name'", currentTask.Name)
return
Expand Down
60 changes: 26 additions & 34 deletions internal/control/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,43 @@ import (
"github.com/rs/zerolog/log"

"github.com/ja-he/dayplan/internal/input"
"github.com/ja-he/dayplan/internal/ui"
)

// View is the inspect interface of an editor.
type View interface {
GetViews() []StringEditorView // TODO: there must be a way to get other editor thingys
GetView(string) StringEditorView // TODO: there must be a way to get other editor thingys
}

// Editor is the control and inspect interface of an editor.
// Editor...
type Editor interface {
View
GetName() string

// // View returns a view of the editor.
// //
// // The type of view is specific to the type of editor it is. Callers will
// // have to type-check and act based on the view's type. For example,
// View() any

// Commit the state of the editor.
Commit()

GetEditor(string) StringEditor // TODO: there must be a way to get other editor thingys
SwitchToNextField()
SwitchToPrevField()
// Abort the editors actions, discarding state.
Abort()

// GetPane returns a pane that represents this editor.
GetPane() ui.Pane
}

// E implements Editor
type E struct {
fields []*stringEditor
fields []Editor
activeFieldIndex int
}

// TODO:
// anywhere where StringEditorView is used, it should be replaced by a more
// general thing which I have not yet come up with.

// GetViews...
func (e *E) GetViews() []StringEditorView {
fields := make([]StringEditorView, len(e.fields))
for i, f := range e.fields {
fields[i] = f
}
return fields
}

// GetView returns a view by its name.
func (e *E) GetView(name string) StringEditorView {
for _, f := range e.fields {
if f.Name == name {
return f
}
}
return nil
}

// GetEditor returns an editor by its name.
func (e *E) GetEditor(name string) StringEditor {
func (e *E) GetEditor(name string) Editor {
for _, f := range e.fields {
if f.Name == name {
if f.GetName() == name {
return f
}
}
Expand Down Expand Up @@ -130,7 +117,7 @@ func ConstructEditor[T any](obj *T, extraSpec map[string]any) (Editor, error) {
switch field.Type.Kind() {
case reflect.String:
f := structValue.Field(i)
e.fields = append(e.fields, &stringEditor{
e.fields = append(e.fields, &StringEditor{
Name: editspec.Name,
Content: f.String(),
CursorPos: 0,
Expand Down Expand Up @@ -159,3 +146,8 @@ type dpedit struct {
Ignore bool
Subedit bool
}

func (e *E) GetName() string { panic("TODO") }
func (e *E) Commit() { panic("TODO") }
func (e *E) Abort() { panic("TODO") }
func (e *E) GetPane() ui.Pane { panic("TODO") }
95 changes: 60 additions & 35 deletions internal/control/editor/string_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import (
"strconv"

"github.com/ja-he/dayplan/internal/input"
"github.com/ja-he/dayplan/internal/ui"
)

// A StringEditor is a control and inspect interface for editing a string.
type StringEditor interface {
StringEditorView
StringEditorControl
Commit()
}

// StringEditorView allows inspection of a string editor.
type StringEditorView interface {

Expand Down Expand Up @@ -51,24 +45,36 @@ type StringEditorControl interface {
AddRune(newRune rune)
}

type stringEditor struct {
// StringEditor ...
type StringEditor struct {
Name string

Content string
CursorPos int
Mode input.TextEditMode

originalContent string

CommitFn func(string)
}

func (e stringEditor) GetName() string { return e.Name }
func (e stringEditor) GetContent() string { return e.Content }
func (e stringEditor) GetCursorPos() int { return e.CursorPos }
func (e stringEditor) GetMode() input.TextEditMode { return e.Mode }
// GetName returns the name of the editor.
func (e StringEditor) GetName() string { return e.Name }

// GetContent returns the current (edited) contents.
func (e StringEditor) GetContent() string { return e.Content }

func (e *stringEditor) SetMode(m input.TextEditMode) { e.Mode = m }
// GetCursorPos returns the current cursor position in the string, 0 being
func (e StringEditor) GetCursorPos() int { return e.CursorPos }

func (e *stringEditor) DeleteRune() {
// GetMode returns the current mode of the editor.
func (e StringEditor) GetMode() input.TextEditMode { return e.Mode }

// SetMode sets the current mode of the editor.
func (e *StringEditor) SetMode(m input.TextEditMode) { e.Mode = m }

// DeleteRune deletes the rune at the cursor position.
func (e *StringEditor) DeleteRune() {
tmpStr := []rune(e.Content)
if e.CursorPos < len(tmpStr) {
preCursor := tmpStr[:e.CursorPos]
Expand All @@ -78,7 +84,8 @@ func (e *stringEditor) DeleteRune() {
}
}

func (e *stringEditor) BackspaceRune() {
// BackspaceRune deletes the rune before the cursor position.
func (e *StringEditor) BackspaceRune() {
if e.CursorPos > 0 {
tmpStr := []rune(e.Content)
preCursor := tmpStr[:e.CursorPos-1]
Expand All @@ -89,55 +96,58 @@ func (e *stringEditor) BackspaceRune() {
}
}

func (e *stringEditor) BackspaceToBeginning() {
// BackspaceToBeginning deletes all runes before the cursor position.
func (e *StringEditor) BackspaceToBeginning() {
nameAfterCursor := []rune(e.Content)[e.CursorPos:]
e.Content = string(nameAfterCursor)
e.CursorPos = 0
}

func (e *stringEditor) DeleteToEnd() {
// DeleteToEnd deletes all runes after the cursor position.
func (e *StringEditor) DeleteToEnd() {
nameBeforeCursor := []rune(e.Content)[:e.CursorPos]
e.Content = string(nameBeforeCursor)
}

func (e *stringEditor) Clear() {
// Clear deletes all runes in the editor.
func (e *StringEditor) Clear() {
e.Content = ""
e.CursorPos = 0
}

func (e *stringEditor) MoveCursorToBeginning() {
// MoveCursorToBeginning moves the cursor to the beginning of the string.
func (e *StringEditor) MoveCursorToBeginning() {
e.CursorPos = 0
}

func (e *stringEditor) MoveCursorToEnd() {
// MoveCursorToEnd moves the cursor to the end of the string.
func (e *StringEditor) MoveCursorToEnd() {
e.CursorPos = len([]rune(e.Content)) - 1
}

func (e *stringEditor) MoveCursorPastEnd() {
// MoveCursorPastEnd moves the cursor past the end of the string.
func (e *StringEditor) MoveCursorPastEnd() {
e.CursorPos = len([]rune(e.Content))
}

func (e *stringEditor) MoveCursorLeft() {
// MoveCursorLeft moves the cursor one rune to the left.
func (e *StringEditor) MoveCursorLeft() {
if e.CursorPos > 0 {
e.CursorPos--
}
}

func (e *stringEditor) MoveCursorRight() {
// MoveCursorRight moves the cursor one rune to the right.
func (e *StringEditor) MoveCursorRight() {
nameLen := len([]rune(e.Content))
if e.CursorPos+1 < nameLen {
e.CursorPos++
}
}

func (e *stringEditor) MoveCursorRightA() {
nameLen := len([]rune(e.Content))
if e.CursorPos < nameLen {
e.CursorPos++
}
}

func (e *stringEditor) MoveCursorNextWordBeginning() {
// MoveCursorNextWordBeginning moves the cursor one rune to the right, or to
// the end of the string if already at the end.
func (e *StringEditor) MoveCursorNextWordBeginning() {
if len([]rune(e.Content)) == 0 {
e.CursorPos = 0
return
Expand All @@ -159,7 +169,9 @@ func (e *stringEditor) MoveCursorNextWordBeginning() {
}
}

func (e *stringEditor) MoveCursorPrevWordBeginning() {
// MoveCursorPrevWordBeginning moves the cursor one rune to the left, or to the
// beginning of the string if already at the beginning.
func (e *StringEditor) MoveCursorPrevWordBeginning() {
nameBeforeCursor := []rune(e.Content)[:e.CursorPos]
if len(nameBeforeCursor) == 0 {
return
Expand All @@ -174,7 +186,8 @@ func (e *stringEditor) MoveCursorPrevWordBeginning() {
e.CursorPos = i
}

func (e *stringEditor) MoveCursorNextWordEnd() {
// MoveCursorNextWordEnd moves the cursor to the end of the next word.
func (e *StringEditor) MoveCursorNextWordEnd() {
nameAfterCursor := []rune(e.Content)[e.CursorPos:]
if len(nameAfterCursor) == 0 {
return
Expand All @@ -195,7 +208,8 @@ func (e *stringEditor) MoveCursorNextWordEnd() {
}
}

func (e *stringEditor) AddRune(newRune rune) {
// AddRune adds a rune at the cursor position.
func (e *StringEditor) AddRune(newRune rune) {
if strconv.IsPrint(newRune) {
tmpName := []rune(e.Content)
cursorPos := e.CursorPos
Expand All @@ -210,6 +224,17 @@ func (e *stringEditor) AddRune(newRune rune) {
}
}

func (e *stringEditor) Commit() {
// Commit commits the current contents of the editor.
func (e *StringEditor) Commit() {
e.CommitFn(e.Content)
}

// Abort aborts the current editing session, reverting to the original
func (e *StringEditor) Abort() {
e.Content = e.originalContent
e.CursorPos = 0
e.Mode = input.TextEditModeNormal
}

// GetPane returns a UI pane representing the editor.
func (p *StringEditor) GetPane() ui.Pane { panic("TODO") }

0 comments on commit fa75e0a

Please sign in to comment.