micro/view.go

226 lines
4.7 KiB
Go
Raw Normal View History

2016-03-17 21:27:57 +00:00
package main
import (
"github.com/gdamore/tcell"
)
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 {
cursor Cursor
topline int
2016-03-17 22:20:07 +00:00
height int
width int
buf *Buffer
sl Statusline
2016-03-17 21:27:57 +00:00
mouseReleased bool
s tcell.Screen
}
2016-03-19 00:40:00 +00:00
// NewView returns a new view with fullscreen width and height
func NewView(buf *Buffer, s tcell.Screen) *View {
2016-03-17 22:20:07 +00:00
w, h := s.Size()
2016-03-19 00:40:00 +00:00
return NewViewWidthHeight(buf, s, w, h-1)
2016-03-17 22:20:07 +00:00
}
2016-03-19 00:40:00 +00:00
// NewViewWidthHeight returns a new view with the specified width and height
func NewViewWidthHeight(buf *Buffer, s tcell.Screen, w, h int) *View {
2016-03-17 21:27:57 +00:00
v := new(View)
v.buf = buf
v.s = s
v.topline = 0
2016-03-19 00:40:00 +00:00
v.height = h - 1
2016-03-17 22:20:07 +00:00
v.width = w
2016-03-17 21:27:57 +00:00
v.cursor = Cursor{
x: 0,
y: 0,
loc: 0,
v: 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-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++
}
}
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) {
case *tcell.EventKey:
switch e.Key() {
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 00:40:00 +00:00
v.buf.Insert(v.cursor.loc, "\n")
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 00:40:00 +00:00
v.buf.Insert(v.cursor.loc, " ")
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.DeleteSelected()
v.cursor.ResetSelection()
2016-03-19 00:01:05 +00:00
ret = 2
} else if v.cursor.loc > 0 {
2016-03-19 00:40:00 +00:00
v.cursor.Left()
v.buf.Remove(v.cursor.loc, v.cursor.loc+1)
2016-03-17 21:27:57 +00:00
ret = 2
}
case tcell.KeyTab:
2016-03-19 00:40:00 +00:00
v.buf.Insert(v.cursor.loc, "\t")
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-19 00:40:00 +00:00
err := v.buf.Save()
2016-03-17 22:20:07 +00:00
if err != nil {
// Error!
}
// Need to redraw the status line
ret = 1
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.DeleteSelected()
v.cursor.ResetSelection()
2016-03-19 00:01:05 +00:00
}
2016-03-19 00:40:00 +00:00
v.buf.Insert(v.cursor.loc, string(e.Rune()))
v.cursor.Right()
2016-03-17 21:27:57 +00:00
ret = 2
}
case *tcell.EventMouse:
x, y := e.Position()
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
}
if y > len(v.buf.lines) {
y = len(v.buf.lines) - 2
2016-03-17 21:27:57 +00:00
}
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() {
charNum := v.cursor.loc + v.cursor.Distance(0, v.topline)
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 00:01:05 +00:00
tabchars := 0
2016-03-17 21:27:57 +00:00
for colN, ch := range line {
st := tcell.StyleDefault
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) {
2016-03-17 21:27:57 +00:00
st = st.Reverse(true)
}
if ch == '\t' {
2016-03-19 00:01:05 +00:00
v.s.SetContent(colN+tabchars, lineN, ' ', nil, st)
for i := 0; i < tabSize-1; i++ {
tabchars++
2016-03-19 00:01:05 +00:00
v.s.SetContent(colN+tabchars, lineN, ' ', nil, st)
}
} else {
v.s.SetContent(colN+tabchars, lineN, ch, nil, st)
}
2016-03-17 21:27:57 +00:00
charNum++
}
charNum++
}
}