micro/src/view.go

435 lines
9.5 KiB
Go
Raw Normal View History

2016-03-17 21:27:57 +00:00
package main
import (
"github.com/atotto/clipboard"
2016-03-23 20:37:49 +00:00
"github.com/gdamore/tcell"
2016-03-23 15:41:04 +00:00
"os"
2016-03-19 13:24:04 +00:00
"strconv"
2016-03-23 15:41:04 +00:00
"strings"
2016-03-17 21:27:57 +00:00
)
2016-03-19 00:40:00 +00:00
// The View struct stores information about a view into a buffer.
// It has a value for the cursor, and the window that the user sees
// the buffer from.
2016-03-17 21:27:57 +00:00
type View struct {
2016-03-21 21:43:53 +00:00
cursor Cursor
topline int
// Leftmost column. Used for horizontal scrolling
leftCol int
// Percentage of the terminal window that this view takes up
heightPercent float32
widthPercent float32
2016-03-19 13:24:04 +00:00
height int
width int
2016-03-21 21:43:53 +00:00
// How much to offset because of line numbers
2016-03-19 13:24:04 +00:00
lineNumOffset int
2016-03-17 22:20:07 +00:00
2016-03-19 22:16:10 +00:00
eh *EventHandler
2016-03-17 22:20:07 +00:00
buf *Buffer
sl Statusline
2016-03-17 21:27:57 +00:00
mouseReleased bool
// Syntax highlighting matches
matches map[int]tcell.Style
2016-03-23 15:41:04 +00:00
m *Messenger
2016-03-17 21:27:57 +00:00
s tcell.Screen
}
2016-03-19 00:40:00 +00:00
// NewView returns a new view with fullscreen width and height
2016-03-23 15:41:04 +00:00
func NewView(buf *Buffer, m *Messenger, s tcell.Screen) *View {
return NewViewWidthHeight(buf, m, s, 1, 1)
2016-03-17 22:20:07 +00:00
}
2016-03-21 21:43:53 +00:00
// NewViewWidthHeight returns a new view with the specified width and height percentages
2016-03-23 15:41:04 +00:00
func NewViewWidthHeight(buf *Buffer, m *Messenger, s tcell.Screen, w, h float32) *View {
2016-03-17 21:27:57 +00:00
v := new(View)
v.buf = buf
v.s = s
2016-03-23 15:41:04 +00:00
v.m = m
2016-03-17 21:27:57 +00:00
2016-03-21 21:43:53 +00:00
v.widthPercent = w
v.heightPercent = h
v.Resize(s.Size())
2016-03-17 21:27:57 +00:00
v.topline = 0
v.cursor = Cursor{
x: 0,
y: 0,
loc: 0,
v: v,
}
2016-03-19 22:16:10 +00:00
v.eh = NewEventHandler(v)
2016-03-17 22:20:07 +00:00
v.sl = Statusline{
v: v,
}
2016-03-17 21:27:57 +00:00
return v
}
2016-03-21 21:43:53 +00:00
// Resize recalculates the width and height of the view based on the width and height percentages
func (v *View) Resize(w, h int) {
h--
v.height = int(float32(h)*v.heightPercent) - 1
v.width = int(float32(w) * v.widthPercent)
}
2016-03-19 00:40:00 +00:00
// ScrollUp scrolls the view up n lines (if possible)
func (v *View) ScrollUp(n int) {
// Try to scroll by n but if it would overflow, scroll by 1
if v.topline-n >= 0 {
v.topline -= n
} else if v.topline > 0 {
v.topline--
}
}
2016-03-19 00:40:00 +00:00
// ScrollDown scrolls the view down n lines (if possible)
func (v *View) ScrollDown(n int) {
// Try to scroll by n but if it would overflow, scroll by 1
if v.topline+n <= len(v.buf.lines)-v.height {
v.topline += n
} else if v.topline < len(v.buf.lines)-v.height {
v.topline++
}
}
// PageUp scrolls the view up a page
func (v *View) PageUp() {
if v.topline > v.height {
v.ScrollUp(v.height)
} else {
v.topline = 0
}
}
// PageDown scrolls the view down a page
func (v *View) PageDown() {
if len(v.buf.lines)-(v.topline+v.height) > v.height {
v.ScrollDown(v.height)
} else {
v.topline = len(v.buf.lines) - v.height
}
}
// HalfPageUp scrolls the view up half a page
func (v *View) HalfPageUp() {
if v.topline > v.height/2 {
v.ScrollUp(v.height / 2)
} else {
v.topline = 0
}
}
// HalfPageDown scrolls the view down half a page
func (v *View) HalfPageDown() {
if len(v.buf.lines)-(v.topline+v.height) > v.height/2 {
v.ScrollDown(v.height / 2)
} else {
v.topline = len(v.buf.lines) - v.height
}
}
2016-03-19 00:40:00 +00:00
// HandleEvent handles an event passed by the main loop
// It returns an int describing how the screen needs to be redrawn
2016-03-17 21:27:57 +00:00
// 0: Screen does not need to be redrawn
2016-03-17 22:20:07 +00:00
// 1: Only the cursor/statusline needs to be redrawn
2016-03-17 21:27:57 +00:00
// 2: Everything needs to be redrawn
2016-03-19 00:40:00 +00:00
func (v *View) HandleEvent(event tcell.Event) int {
2016-03-17 21:27:57 +00:00
var ret int
switch e := event.(type) {
2016-03-21 21:43:53 +00:00
case *tcell.EventResize:
v.Resize(e.Size())
ret = 2
2016-03-17 21:27:57 +00:00
case *tcell.EventKey:
switch e.Key() {
2016-03-23 15:41:04 +00:00
case tcell.KeyCtrlQ:
if v.buf.IsDirty() {
quit, canceled := v.m.Prompt("You have unsaved changes. Quit anyway? ")
if !canceled {
if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
v.s.Fini()
os.Exit(0)
} else {
return 2
}
} else {
return 2
}
} else {
v.s.Fini()
os.Exit(0)
}
2016-03-17 21:27:57 +00:00
case tcell.KeyUp:
2016-03-19 00:40:00 +00:00
v.cursor.Up()
2016-03-17 21:27:57 +00:00
ret = 1
case tcell.KeyDown:
2016-03-19 00:40:00 +00:00
v.cursor.Down()
2016-03-17 21:27:57 +00:00
ret = 1
case tcell.KeyLeft:
2016-03-19 00:40:00 +00:00
v.cursor.Left()
2016-03-17 21:27:57 +00:00
ret = 1
case tcell.KeyRight:
2016-03-19 00:40:00 +00:00
v.cursor.Right()
2016-03-17 21:27:57 +00:00
ret = 1
case tcell.KeyEnter:
2016-03-19 22:16:10 +00:00
v.eh.Insert(v.cursor.loc, "\n")
2016-03-19 00:40:00 +00:00
v.cursor.Right()
2016-03-17 21:27:57 +00:00
ret = 2
2016-03-17 22:20:07 +00:00
case tcell.KeySpace:
2016-03-19 22:16:10 +00:00
v.eh.Insert(v.cursor.loc, " ")
2016-03-19 00:40:00 +00:00
v.cursor.Right()
2016-03-17 22:20:07 +00:00
ret = 2
2016-03-17 21:27:57 +00:00
case tcell.KeyBackspace2:
2016-03-19 00:40:00 +00:00
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
2016-03-19 00:40:00 +00:00
v.cursor.ResetSelection()
2016-03-19 00:01:05 +00:00
ret = 2
} else if v.cursor.loc > 0 {
2016-03-19 22:16:10 +00:00
// We have to do something a bit hacky here because we want to
// delete the line by first moving left and then deleting backwards
// but the undo redo would place the cursor in the wrong place
// So instead we move left, save the position, move back, delete
// and restore the position
2016-03-19 00:40:00 +00:00
v.cursor.Left()
2016-03-19 22:16:10 +00:00
cx, cy, cloc := v.cursor.x, v.cursor.y, v.cursor.loc
v.cursor.Right()
v.eh.Remove(v.cursor.loc-1, v.cursor.loc)
v.cursor.x, v.cursor.y, v.cursor.loc = cx, cy, cloc
2016-03-17 21:27:57 +00:00
ret = 2
}
case tcell.KeyTab:
2016-03-19 22:16:10 +00:00
v.eh.Insert(v.cursor.loc, "\t")
2016-03-19 00:40:00 +00:00
v.cursor.Right()
2016-03-17 21:27:57 +00:00
ret = 2
2016-03-17 22:20:07 +00:00
case tcell.KeyCtrlS:
2016-03-23 15:41:04 +00:00
if v.buf.path == "" {
filename, canceled := v.m.Prompt("Filename: ")
if !canceled {
v.buf.path = filename
v.buf.name = filename
} else {
return 2
}
}
2016-03-19 00:40:00 +00:00
err := v.buf.Save()
2016-03-17 22:20:07 +00:00
if err != nil {
2016-03-23 15:41:04 +00:00
v.m.Error(err.Error())
2016-03-17 22:20:07 +00:00
}
// Need to redraw the status line
ret = 1
2016-03-19 22:16:10 +00:00
case tcell.KeyCtrlZ:
v.eh.Undo()
ret = 2
case tcell.KeyCtrlY:
v.eh.Redo()
ret = 2
case tcell.KeyCtrlC:
if v.cursor.HasSelection() {
if !clipboard.Unsupported {
clipboard.WriteAll(v.cursor.GetSelection())
ret = 2
}
}
case tcell.KeyCtrlX:
if v.cursor.HasSelection() {
if !clipboard.Unsupported {
clipboard.WriteAll(v.cursor.GetSelection())
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
ret = 2
}
}
case tcell.KeyCtrlV:
if !clipboard.Unsupported {
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
v.cursor.ResetSelection()
}
clip, _ := clipboard.ReadAll()
v.eh.Insert(v.cursor.loc, clip)
// This is a bit weird... Not sure if there's a better way
for i := 0; i < Count(clip); i++ {
v.cursor.Right()
}
ret = 2
}
case tcell.KeyPgUp:
v.PageUp()
return 2
case tcell.KeyPgDn:
v.PageDown()
return 2
case tcell.KeyCtrlU:
v.HalfPageUp()
return 2
case tcell.KeyCtrlD:
v.HalfPageDown()
return 2
2016-03-17 21:27:57 +00:00
case tcell.KeyRune:
2016-03-19 00:40:00 +00:00
if v.cursor.HasSelection() {
v.cursor.DeleteSelection()
2016-03-19 00:40:00 +00:00
v.cursor.ResetSelection()
2016-03-19 00:01:05 +00:00
}
2016-03-19 22:16:10 +00:00
v.eh.Insert(v.cursor.loc, string(e.Rune()))
2016-03-19 00:40:00 +00:00
v.cursor.Right()
2016-03-17 21:27:57 +00:00
ret = 2
}
case *tcell.EventMouse:
x, y := e.Position()
2016-03-19 13:24:04 +00:00
x -= v.lineNumOffset
2016-03-17 21:27:57 +00:00
y += v.topline
// Position always seems to be off by one
x--
y--
button := e.Buttons()
switch button {
case tcell.Button1:
2016-03-17 22:20:07 +00:00
if y-v.topline > v.height-1 {
2016-03-19 00:40:00 +00:00
v.ScrollDown(1)
2016-03-17 22:20:07 +00:00
y = v.height + v.topline - 1
2016-03-17 21:27:57 +00:00
}
2016-03-22 22:17:11 +00:00
if y >= len(v.buf.lines) {
2016-03-21 21:02:41 +00:00
y = len(v.buf.lines) - 1
2016-03-17 21:27:57 +00:00
}
2016-03-19 13:24:04 +00:00
if x < 0 {
x = 0
}
2016-03-19 00:40:00 +00:00
x = v.cursor.GetCharPosInLine(y, x)
if x > Count(v.buf.lines[y]) {
x = Count(v.buf.lines[y])
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
d := v.cursor.Distance(x, y)
2016-03-17 21:27:57 +00:00
v.cursor.loc += d
v.cursor.x = x
v.cursor.y = y
if v.mouseReleased {
v.cursor.selectionStart = v.cursor.loc
2016-03-19 00:01:05 +00:00
v.cursor.selectionStartX = v.cursor.x
v.cursor.selectionStartY = v.cursor.y
2016-03-17 21:27:57 +00:00
}
v.cursor.selectionEnd = v.cursor.loc
v.mouseReleased = false
return 2
2016-03-17 21:27:57 +00:00
case tcell.ButtonNone:
v.mouseReleased = true
return 0
2016-03-17 21:27:57 +00:00
case tcell.WheelUp:
2016-03-19 00:40:00 +00:00
v.ScrollUp(2)
return 2
2016-03-17 21:27:57 +00:00
case tcell.WheelDown:
2016-03-19 00:40:00 +00:00
v.ScrollDown(2)
return 2
2016-03-17 21:27:57 +00:00
}
}
cy := v.cursor.y
if cy < v.topline {
v.topline = cy
ret = 2
}
2016-03-17 22:20:07 +00:00
if cy > v.topline+v.height-1 {
v.topline = cy - v.height + 1
2016-03-17 21:27:57 +00:00
ret = 2
}
return ret
}
2016-03-19 00:40:00 +00:00
// Display renders the view to the screen
func (v *View) Display() {
2016-03-19 13:24:04 +00:00
var x int
2016-03-19 00:40:00 +00:00
charNum := v.cursor.loc + v.cursor.Distance(0, v.topline)
2016-03-19 13:24:04 +00:00
// Convert the length of buffer to a string, and get the length of the string
// We are going to have to offset by that amount
maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
// + 1 for the little space after the line number
v.lineNumOffset = maxLineLength + 1
2016-03-20 19:24:40 +00:00
var highlightStyle tcell.Style
2016-03-20 15:28:41 +00:00
2016-03-17 22:20:07 +00:00
for lineN := 0; lineN < v.height; lineN++ {
2016-03-17 21:27:57 +00:00
if lineN+v.topline >= len(v.buf.lines) {
break
}
line := v.buf.lines[lineN+v.topline]
2016-03-19 13:24:04 +00:00
// Write the line number
lineNumStyle := tcell.StyleDefault
if _, ok := colorscheme["line-number"]; ok {
lineNumStyle = colorscheme["line-number"]
}
2016-03-19 13:24:04 +00:00
// Write the spaces before the line number if necessary
lineNum := strconv.Itoa(lineN + v.topline + 1)
for i := 0; i < maxLineLength-len(lineNum); i++ {
v.s.SetContent(x, lineN, ' ', nil, lineNumStyle)
x++
}
// Write the actual line number
for _, ch := range lineNum {
v.s.SetContent(x, lineN, ch, nil, lineNumStyle)
x++
}
// Write the extra space
v.s.SetContent(x, lineN, ' ', nil, lineNumStyle)
x++
// Write the line
2016-03-19 00:01:05 +00:00
tabchars := 0
2016-03-19 13:24:04 +00:00
for _, ch := range line {
2016-03-20 19:24:40 +00:00
var lineStyle tcell.Style
st, ok := v.matches[charNum]
2016-03-20 15:28:41 +00:00
if ok {
2016-03-20 19:24:40 +00:00
highlightStyle = st
} else {
highlightStyle = tcell.StyleDefault
2016-03-20 15:28:41 +00:00
}
2016-03-19 00:40:00 +00:00
if v.cursor.HasSelection() &&
2016-03-19 00:01:05 +00:00
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
lineStyle = tcell.StyleDefault.Reverse(true)
if _, ok := colorscheme["selection"]; ok {
lineStyle = colorscheme["selection"]
}
2016-03-20 19:24:40 +00:00
} else {
lineStyle = highlightStyle
2016-03-17 21:27:57 +00:00
}
if ch == '\t' {
2016-03-20 15:28:41 +00:00
v.s.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
for i := 0; i < tabSize-1; i++ {
tabchars++
2016-03-20 15:28:41 +00:00
v.s.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
}
} else {
2016-03-20 15:28:41 +00:00
v.s.SetContent(x+tabchars, lineN, ch, nil, lineStyle)
}
2016-03-17 21:27:57 +00:00
charNum++
2016-03-19 13:24:04 +00:00
x++
2016-03-17 21:27:57 +00:00
}
2016-03-19 13:24:04 +00:00
x = 0
st, ok := v.matches[charNum]
2016-03-20 15:28:41 +00:00
if ok {
2016-03-20 19:24:40 +00:00
highlightStyle = st
2016-03-20 15:28:41 +00:00
}
2016-03-17 21:27:57 +00:00
charNum++
}
}