mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-29 22:27:13 +09:00
Add statusline
This commit is contained in:
50
view.go
50
view.go
@@ -8,25 +8,31 @@ import (
|
||||
type View struct {
|
||||
cursor Cursor
|
||||
topline int
|
||||
linesN int
|
||||
colsN int
|
||||
height int
|
||||
width int
|
||||
|
||||
buf *Buffer
|
||||
sl Statusline
|
||||
|
||||
buf *Buffer
|
||||
mouseReleased bool
|
||||
|
||||
s tcell.Screen
|
||||
}
|
||||
|
||||
func newViewFromBuffer(buf *Buffer, s tcell.Screen) *View {
|
||||
func newView(buf *Buffer, s tcell.Screen) *View {
|
||||
w, h := s.Size()
|
||||
return newViewWidthHeight(buf, s, w, h)
|
||||
}
|
||||
|
||||
func newViewWidthHeight(buf *Buffer, s tcell.Screen, w, h int) *View {
|
||||
v := new(View)
|
||||
|
||||
v.buf = buf
|
||||
v.s = s
|
||||
w, h := s.Size()
|
||||
|
||||
v.topline = 0
|
||||
v.linesN = h
|
||||
v.colsN = w
|
||||
v.height = h - 2
|
||||
v.width = w
|
||||
v.cursor = Cursor{
|
||||
x: 0,
|
||||
y: 0,
|
||||
@@ -34,12 +40,16 @@ func newViewFromBuffer(buf *Buffer, s tcell.Screen) *View {
|
||||
v: v,
|
||||
}
|
||||
|
||||
v.sl = Statusline{
|
||||
v: v,
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// Returns an int describing how the screen needs to be redrawn
|
||||
// 0: Screen does not need to be redrawn
|
||||
// 1: Only the cursor needs 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 {
|
||||
var ret int
|
||||
@@ -62,6 +72,10 @@ func (v *View) handleEvent(event tcell.Event) int {
|
||||
v.buf.insert(v.cursor.loc, "\n")
|
||||
v.cursor.right()
|
||||
ret = 2
|
||||
case tcell.KeySpace:
|
||||
v.buf.insert(v.cursor.loc, " ")
|
||||
v.cursor.right()
|
||||
ret = 2
|
||||
case tcell.KeyBackspace2:
|
||||
if v.cursor.loc > 0 {
|
||||
v.cursor.left()
|
||||
@@ -72,6 +86,13 @@ func (v *View) handleEvent(event tcell.Event) int {
|
||||
v.buf.insert(v.cursor.loc, "\t")
|
||||
v.cursor.right()
|
||||
ret = 2
|
||||
case tcell.KeyCtrlS:
|
||||
err := v.buf.save()
|
||||
if err != nil {
|
||||
// Error!
|
||||
}
|
||||
// Need to redraw the status line
|
||||
ret = 1
|
||||
case tcell.KeyRune:
|
||||
v.buf.insert(v.cursor.loc, string(e.Rune()))
|
||||
v.cursor.right()
|
||||
@@ -88,8 +109,8 @@ func (v *View) handleEvent(event tcell.Event) int {
|
||||
|
||||
switch button {
|
||||
case tcell.Button1:
|
||||
if y-v.topline > v.linesN-1 {
|
||||
y = v.linesN + v.topline - 1
|
||||
if y-v.topline > v.height-1 {
|
||||
y = v.height + v.topline - 1
|
||||
}
|
||||
if y > len(v.buf.lines) {
|
||||
y = len(v.buf.lines) - 1
|
||||
@@ -120,7 +141,7 @@ func (v *View) handleEvent(event tcell.Event) int {
|
||||
return 0
|
||||
}
|
||||
case tcell.WheelDown:
|
||||
if v.topline < len(v.buf.lines)-v.linesN {
|
||||
if v.topline < len(v.buf.lines)-v.height {
|
||||
v.topline++
|
||||
return 2
|
||||
} else {
|
||||
@@ -134,8 +155,8 @@ func (v *View) handleEvent(event tcell.Event) int {
|
||||
v.topline = cy
|
||||
ret = 2
|
||||
}
|
||||
if cy > v.topline+v.linesN-1 {
|
||||
v.topline = cy - v.linesN + 1
|
||||
if cy > v.topline+v.height-1 {
|
||||
v.topline = cy - v.height + 1
|
||||
ret = 2
|
||||
}
|
||||
|
||||
@@ -143,9 +164,8 @@ func (v *View) handleEvent(event tcell.Event) int {
|
||||
}
|
||||
|
||||
func (v *View) display() {
|
||||
|
||||
var charNum int
|
||||
for lineN := 0; lineN < v.linesN; lineN++ {
|
||||
for lineN := 0; lineN < v.height; lineN++ {
|
||||
if lineN+v.topline >= len(v.buf.lines) {
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user