Commit 8295f672 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Do up the editor a bit

parent 13870b14
Loading
Loading
Loading
Loading
+50 −64
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@
Package editor provides a simple editor.

This is implemented with [github.com/gdamore/tcell] and
[github.com/rivo/tview].  It would be good to make this work more like EDT.
[github.com/rivo/tview].  The keybindings are loosely inspired by EDT
but adapted for modern terminals.

In addition, it might be an idea to offer a set of editors that work
like EDT, nano, vi and emacs and allow users to set a preference.
@@ -32,13 +33,33 @@ func Editor(placeholder, title, message string) (string, error) {
		ContrastSecondaryTextColor:  tcell.ColorDefault,
	}
	tview.Styles = theme

	// Use single-line borders everywhere (tview defaults to double
	// for focused widgets).
	tview.Borders.HorizontalFocus = tview.Borders.Horizontal
	tview.Borders.VerticalFocus = tview.Borders.Vertical
	tview.Borders.TopLeftFocus = tview.Borders.TopLeft
	tview.Borders.TopRightFocus = tview.Borders.TopRight
	tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
	tview.Borders.BottomRightFocus = tview.Borders.BottomRight

	app := tview.NewApplication()

	textArea := tview.NewTextArea().
		SetPlaceholder(placeholder)
	textArea.SetTitle(title)

	// Remap Ctrl-C to copy (by converting it to Ctrl-Q which tview's
	// TextArea handles as the copy-to-clipboard action).
	textArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyCtrlC {
			return tcell.NewEventKey(tcell.KeyCtrlQ, 0, tcell.ModNone)
		}
		return event
	})

	helpInfo := tview.NewTextView().
		SetText(" Press F1 for help, press Ctrl-C to exit")
		SetText(" ^Q Quit  F1 Help")
	position := tview.NewTextView().
		SetTextAlign(tview.AlignRight)
	pages := tview.NewPages()
@@ -47,9 +68,9 @@ func Editor(placeholder, title, message string) (string, error) {
	updateInfos := func() {
		fromRow, fromColumn, toRow, toColumn := textArea.GetCursor()
		if fromRow == toRow && fromColumn == toColumn {
			position.SetText(fmt.Sprintf("Row: %d, Column: %d ", fromRow, fromColumn))
			position.SetText(fmt.Sprintf("Line: %d  Col: %d ", fromRow+1, fromColumn+1))
		} else {
			position.SetText(fmt.Sprintf("From Row: %d, Column: %d - To Row: %d, To Column: %d ", fromRow, fromColumn, toRow, toColumn))
			position.SetText(fmt.Sprintf("%d:%d - %d:%d ", fromRow+1, fromColumn+1, toRow+1, toColumn+1))
		}
	}

@@ -62,55 +83,26 @@ func Editor(placeholder, title, message string) (string, error) {
		AddItem(helpInfo, 1, 0, 1, 1, 0, 0, false).
		AddItem(position, 1, 1, 1, 1, 0, 0, false)

	help1 := tview.NewTextView().
		SetText(`Navigation

Left arrow: Move left.
Right arrow: Move right.
Down arrow: Move down.
Up arrow: Move up.
Ctrl-A, Home: Move to the beginning of the current line.
Ctrl-E, End: Move to the end of the current line.
Ctrl-F, page down: Move down by one page.
Ctrl-B, page up: Move up by one page.
Alt-Up arrow: Scroll the page up.
Alt-Down arrow: Scroll the page down.
Alt-Left arrow: Scroll the page to the left.
Alt-Right arrow:  Scroll the page to the right.
Alt-B, Ctrl-Left arrow: Move back by one word.
Alt-F, Ctrl-Right arrow: Move forward by one word.

Press Enter for more help, press Escape to return.`)
	help2 := tview.NewTextView().
		SetText(`Editing

Type to enter text.
Ctrl-H, Backspace: Delete the left character.
Ctrl-D, Delete: Delete the right character.
Ctrl-K: Delete until the end of the line.
Ctrl-W: Delete the rest of the word.
Ctrl-U: Delete the current line.

Press Enter for more help, press Escape to return.`)
	help3 := tview.NewTextView().
		SetText(`Selecting Text

Move while holding Shift.
Double-click to select a word.

Clipboard

Ctrl-Q: Copy.
Ctrl-X: Cut.
Ctrl-V: Paste.

	helpText := tview.NewTextView().
		SetText(`Navigation                Editing
──────────                ───────
Arrows     Move cursor    Backspace  Delete left
Home       Start of line  Delete     Delete right
End        End of line    ^K         Delete to EOL
PgUp/PgDn  Page up/down  ^W         Delete word
^Left/^Right  Word jump   ^U         Delete line

Selection                 Clipboard
─────────                 ─────────
Shift+Arrows  Select      ^C  Copy
Double-click  Select word ^X  Cut
                          ^V  Paste
Undo
────
^Z  Undo    ^Y  Redo

Ctrl-Z: Undo.
Ctrl-Y: Redo.

Press Enter for more help, press Escape to return.`)
	help := tview.NewFrame(help1).
Press Escape to return.`)
	help := tview.NewFrame(helpText).
		SetBorders(1, 1, 0, 0, 2, 2)
	help.SetBorder(true).
		SetTitle("Help").
@@ -118,28 +110,22 @@ Press Enter for more help, press Escape to return.`)
			if event.Key() == tcell.KeyEscape {
				pages.SwitchToPage("main")
				return nil
			} else if event.Key() == tcell.KeyEnter {
				switch {
				case help.GetPrimitive() == help1:
					help.SetPrimitive(help2)
				case help.GetPrimitive() == help2:
					help.SetPrimitive(help3)
				case help.GetPrimitive() == help3:
					help.SetPrimitive(help1)
				}
				return nil
			}
			return event
		})

	pages.AddAndSwitchToPage("main", mainView, true).
		AddPage("help", tview.NewGrid().
			SetColumns(0, 64, 0).
			SetRows(0, 22, 0).
			SetColumns(0, 52, 0).
			SetRows(0, 24, 0).
			AddItem(help, 1, 1, 1, 1, 0, 0, true), true, false)

	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyF1 {
		switch event.Key() {
		case tcell.KeyCtrlQ:
			app.Stop()
			return nil
		case tcell.KeyF1:
			pages.ShowPage("help")
			return nil
		}