"github.com/gdamore/tcell"
)
+// 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.
type View struct {
cursor Cursor
topline int
s tcell.Screen
}
-func newView(buf *Buffer, s tcell.Screen) *View {
+// NewView returns a new view with fullscreen width and height
+func NewView(buf *Buffer, s tcell.Screen) *View {
w, h := s.Size()
- return newViewWidthHeight(buf, s, w, h)
+ return NewViewWidthHeight(buf, s, w, h-1)
}
-func newViewWidthHeight(buf *Buffer, s tcell.Screen, w, h int) *View {
+// NewViewWidthHeight returns a new view with the specified width and height
+func NewViewWidthHeight(buf *Buffer, s tcell.Screen, w, h int) *View {
v := new(View)
v.buf = buf
v.s = s
v.topline = 0
- v.height = h - 2
+ v.height = h - 1
v.width = w
v.cursor = Cursor{
x: 0,
return v
}
-func (v *View) scrollUp(n int) {
+// 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
}
}
-func (v *View) scrollDown(n int) {
+// 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
}
}
-// Returns an int describing how the screen needs to be redrawn
+// HandleEvent handles an event passed by the main loop
+// It returns an int describing how the screen needs to be redrawn
// 0: Screen does not need to be redrawn
// 1: Only the cursor/statusline needs to be redrawn
// 2: Everything needs to be redrawn
-func (v *View) handleEvent(event tcell.Event) int {
+func (v *View) HandleEvent(event tcell.Event) int {
var ret int
switch e := event.(type) {
case *tcell.EventKey:
switch e.Key() {
case tcell.KeyUp:
- v.cursor.up()
+ v.cursor.Up()
ret = 1
case tcell.KeyDown:
- v.cursor.down()
+ v.cursor.Down()
ret = 1
case tcell.KeyLeft:
- v.cursor.left()
+ v.cursor.Left()
ret = 1
case tcell.KeyRight:
- v.cursor.right()
+ v.cursor.Right()
ret = 1
case tcell.KeyEnter:
- v.buf.insert(v.cursor.loc, "\n")
- v.cursor.right()
+ v.buf.Insert(v.cursor.loc, "\n")
+ v.cursor.Right()
ret = 2
case tcell.KeySpace:
- v.buf.insert(v.cursor.loc, " ")
- v.cursor.right()
+ v.buf.Insert(v.cursor.loc, " ")
+ v.cursor.Right()
ret = 2
case tcell.KeyBackspace2:
- if v.cursor.hasSelection() {
- v.cursor.deleteSelected()
- v.cursor.resetSelection()
+ if v.cursor.HasSelection() {
+ v.cursor.DeleteSelected()
+ v.cursor.ResetSelection()
ret = 2
} else if v.cursor.loc > 0 {
- v.cursor.left()
- v.buf.remove(v.cursor.loc, v.cursor.loc+1)
+ v.cursor.Left()
+ v.buf.Remove(v.cursor.loc, v.cursor.loc+1)
ret = 2
}
case tcell.KeyTab:
- v.buf.insert(v.cursor.loc, "\t")
- v.cursor.right()
+ v.buf.Insert(v.cursor.loc, "\t")
+ v.cursor.Right()
ret = 2
case tcell.KeyCtrlS:
- err := v.buf.save()
+ err := v.buf.Save()
if err != nil {
// Error!
}
// Need to redraw the status line
ret = 1
case tcell.KeyRune:
- if v.cursor.hasSelection() {
- v.cursor.deleteSelected()
- v.cursor.resetSelection()
+ if v.cursor.HasSelection() {
+ v.cursor.DeleteSelected()
+ v.cursor.ResetSelection()
}
- v.buf.insert(v.cursor.loc, string(e.Rune()))
- v.cursor.right()
+ v.buf.Insert(v.cursor.loc, string(e.Rune()))
+ v.cursor.Right()
ret = 2
}
case *tcell.EventMouse:
switch button {
case tcell.Button1:
if y-v.topline > v.height-1 {
- v.scrollDown(1)
+ v.ScrollDown(1)
y = v.height + v.topline - 1
}
if y > len(v.buf.lines) {
y = len(v.buf.lines) - 2
}
- x = v.cursor.getCharPosInLine(y, x)
- if x > count(v.buf.lines[y]) {
- x = count(v.buf.lines[y])
+ x = v.cursor.GetCharPosInLine(y, x)
+ if x > Count(v.buf.lines[y]) {
+ x = Count(v.buf.lines[y])
}
- d := v.cursor.distance(x, y)
+ d := v.cursor.Distance(x, y)
v.cursor.loc += d
v.cursor.x = x
v.cursor.y = y
v.mouseReleased = true
return 0
case tcell.WheelUp:
- v.scrollUp(2)
+ v.ScrollUp(2)
return 2
case tcell.WheelDown:
- v.scrollDown(2)
+ v.ScrollDown(2)
return 2
}
}
return ret
}
-func (v *View) display() {
- charNum := v.cursor.loc + v.cursor.distance(0, v.topline)
+// Display renders the view to the screen
+func (v *View) Display() {
+ charNum := v.cursor.loc + v.cursor.Distance(0, v.topline)
for lineN := 0; lineN < v.height; lineN++ {
if lineN+v.topline >= len(v.buf.lines) {
break
}
- // line := strings.Replace(v.buf.lines[lineN+v.topline], "\t", emptyString(tabSize), -1)
line := v.buf.lines[lineN+v.topline]
tabchars := 0
for colN, ch := range line {
st := tcell.StyleDefault
- if v.cursor.hasSelection() &&
+ if v.cursor.HasSelection() &&
(charNum >= v.cursor.selectionStart && charNum <= v.cursor.selectionEnd ||
charNum <= v.cursor.selectionStart && charNum >= v.cursor.selectionEnd) {
st = st.Reverse(true)