mirror of
https://github.com/zyedidia/micro.git
synced 2025-06-19 07:15:34 -04:00
Add statusline
This commit is contained in:
parent
1781766e21
commit
b3b7e8414d
@ -42,6 +42,7 @@ func (b *Buffer) save() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) saveAs(filename string) error {
|
func (b *Buffer) saveAs(filename string) error {
|
||||||
|
b.savedText = b.text
|
||||||
err := ioutil.WriteFile(filename, []byte(b.text), 0644)
|
err := ioutil.WriteFile(filename, []byte(b.text), 0644)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -95,6 +95,10 @@ func (c *Cursor) getCharPos(lineNum, visualPos int) int {
|
|||||||
return visualPos - (tabSize-1)*numTabs
|
return visualPos - (tabSize-1)*numTabs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cursor) getVisualX() int {
|
||||||
|
return c.x + numOccurences(c.v.buf.lines[c.y][:c.x], '\t')*(tabSize-1)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Cursor) distance(x, y int) int {
|
func (c *Cursor) distance(x, y int) int {
|
||||||
// Same line
|
// Same line
|
||||||
if y == c.y {
|
if y == c.y {
|
||||||
@ -138,7 +142,7 @@ func (c *Cursor) distance(x, y int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cursor) display() {
|
func (c *Cursor) display() {
|
||||||
if c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.linesN-1 {
|
if c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1 {
|
||||||
c.v.s.HideCursor()
|
c.v.s.HideCursor()
|
||||||
} else {
|
} else {
|
||||||
voffset := numOccurences(c.v.buf.lines[c.y][:c.x], '\t') * (tabSize - 1)
|
voffset := numOccurences(c.v.buf.lines[c.y][:c.x], '\t') * (tabSize - 1)
|
||||||
|
22
micro.go
22
micro.go
@ -19,12 +19,14 @@ func main() {
|
|||||||
|
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
filename = os.Args[1]
|
filename = os.Args[1]
|
||||||
var err error
|
if _, err := os.Stat(filename); err == nil {
|
||||||
input, err = ioutil.ReadFile(filename)
|
var err error
|
||||||
|
input, err = ioutil.ReadFile(filename)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if !isatty.IsTerminal(os.Stdin.Fd()) {
|
} else if !isatty.IsTerminal(os.Stdin.Fd()) {
|
||||||
bytes, err := ioutil.ReadAll(os.Stdin)
|
bytes, err := ioutil.ReadAll(os.Stdin)
|
||||||
@ -44,9 +46,15 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, "%v\n", e)
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defStyle := tcell.StyleDefault.
|
||||||
|
Background(tcell.ColorDefault).
|
||||||
|
Foreground(tcell.ColorDefault)
|
||||||
|
|
||||||
|
s.SetStyle(defStyle)
|
||||||
s.EnableMouse()
|
s.EnableMouse()
|
||||||
|
|
||||||
v := newViewFromBuffer(newBuffer(string(input), filename), s)
|
v := newView(newBuffer(string(input), filename), s)
|
||||||
|
|
||||||
// Initially everything needs to be drawn
|
// Initially everything needs to be drawn
|
||||||
redraw := 2
|
redraw := 2
|
||||||
@ -55,9 +63,11 @@ func main() {
|
|||||||
s.Clear()
|
s.Clear()
|
||||||
v.display()
|
v.display()
|
||||||
v.cursor.display()
|
v.cursor.display()
|
||||||
|
v.sl.display()
|
||||||
s.Show()
|
s.Show()
|
||||||
} else if redraw == 1 {
|
} else if redraw == 1 {
|
||||||
v.cursor.display()
|
v.cursor.display()
|
||||||
|
v.sl.display()
|
||||||
s.Show()
|
s.Show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
statusline.go
Normal file
33
statusline.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Statusline struct {
|
||||||
|
v *View
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sl *Statusline) display() {
|
||||||
|
y := sl.v.height
|
||||||
|
|
||||||
|
file := sl.v.buf.name
|
||||||
|
if file == "" {
|
||||||
|
file = "Untitled"
|
||||||
|
}
|
||||||
|
if sl.v.buf.text != sl.v.buf.savedText {
|
||||||
|
file += " +"
|
||||||
|
}
|
||||||
|
file += " (" + strconv.Itoa(sl.v.cursor.y+1) + "," + strconv.Itoa(sl.v.cursor.getVisualX()+1) + ")"
|
||||||
|
|
||||||
|
statusLineStyle := tcell.StyleDefault.Background(tcell.ColorNavy).Foreground(tcell.ColorBlack)
|
||||||
|
|
||||||
|
for x := 0; x < sl.v.width; x++ {
|
||||||
|
if x < count(file) {
|
||||||
|
sl.v.s.SetContent(x, y, []rune(file)[x], nil, statusLineStyle)
|
||||||
|
} else {
|
||||||
|
sl.v.s.SetContent(x, y, ' ', nil, statusLineStyle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
view.go
50
view.go
@ -8,25 +8,31 @@ import (
|
|||||||
type View struct {
|
type View struct {
|
||||||
cursor Cursor
|
cursor Cursor
|
||||||
topline int
|
topline int
|
||||||
linesN int
|
height int
|
||||||
colsN int
|
width int
|
||||||
|
|
||||||
|
buf *Buffer
|
||||||
|
sl Statusline
|
||||||
|
|
||||||
buf *Buffer
|
|
||||||
mouseReleased bool
|
mouseReleased bool
|
||||||
|
|
||||||
s tcell.Screen
|
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 := new(View)
|
||||||
|
|
||||||
v.buf = buf
|
v.buf = buf
|
||||||
v.s = s
|
v.s = s
|
||||||
w, h := s.Size()
|
|
||||||
|
|
||||||
v.topline = 0
|
v.topline = 0
|
||||||
v.linesN = h
|
v.height = h - 2
|
||||||
v.colsN = w
|
v.width = w
|
||||||
v.cursor = Cursor{
|
v.cursor = Cursor{
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
@ -34,12 +40,16 @@ func newViewFromBuffer(buf *Buffer, s tcell.Screen) *View {
|
|||||||
v: v,
|
v: v,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v.sl = Statusline{
|
||||||
|
v: v,
|
||||||
|
}
|
||||||
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an int describing how the screen needs to be redrawn
|
// Returns an int describing how the screen needs to be redrawn
|
||||||
// 0: Screen does not need 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
|
// 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
|
var ret int
|
||||||
@ -62,6 +72,10 @@ func (v *View) handleEvent(event tcell.Event) int {
|
|||||||
v.buf.insert(v.cursor.loc, "\n")
|
v.buf.insert(v.cursor.loc, "\n")
|
||||||
v.cursor.right()
|
v.cursor.right()
|
||||||
ret = 2
|
ret = 2
|
||||||
|
case tcell.KeySpace:
|
||||||
|
v.buf.insert(v.cursor.loc, " ")
|
||||||
|
v.cursor.right()
|
||||||
|
ret = 2
|
||||||
case tcell.KeyBackspace2:
|
case tcell.KeyBackspace2:
|
||||||
if v.cursor.loc > 0 {
|
if v.cursor.loc > 0 {
|
||||||
v.cursor.left()
|
v.cursor.left()
|
||||||
@ -72,6 +86,13 @@ func (v *View) handleEvent(event tcell.Event) int {
|
|||||||
v.buf.insert(v.cursor.loc, "\t")
|
v.buf.insert(v.cursor.loc, "\t")
|
||||||
v.cursor.right()
|
v.cursor.right()
|
||||||
ret = 2
|
ret = 2
|
||||||
|
case tcell.KeyCtrlS:
|
||||||
|
err := v.buf.save()
|
||||||
|
if err != nil {
|
||||||
|
// Error!
|
||||||
|
}
|
||||||
|
// Need to redraw the status line
|
||||||
|
ret = 1
|
||||||
case tcell.KeyRune:
|
case tcell.KeyRune:
|
||||||
v.buf.insert(v.cursor.loc, string(e.Rune()))
|
v.buf.insert(v.cursor.loc, string(e.Rune()))
|
||||||
v.cursor.right()
|
v.cursor.right()
|
||||||
@ -88,8 +109,8 @@ func (v *View) handleEvent(event tcell.Event) int {
|
|||||||
|
|
||||||
switch button {
|
switch button {
|
||||||
case tcell.Button1:
|
case tcell.Button1:
|
||||||
if y-v.topline > v.linesN-1 {
|
if y-v.topline > v.height-1 {
|
||||||
y = v.linesN + v.topline - 1
|
y = v.height + v.topline - 1
|
||||||
}
|
}
|
||||||
if y > len(v.buf.lines) {
|
if y > len(v.buf.lines) {
|
||||||
y = len(v.buf.lines) - 1
|
y = len(v.buf.lines) - 1
|
||||||
@ -120,7 +141,7 @@ func (v *View) handleEvent(event tcell.Event) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
case tcell.WheelDown:
|
case tcell.WheelDown:
|
||||||
if v.topline < len(v.buf.lines)-v.linesN {
|
if v.topline < len(v.buf.lines)-v.height {
|
||||||
v.topline++
|
v.topline++
|
||||||
return 2
|
return 2
|
||||||
} else {
|
} else {
|
||||||
@ -134,8 +155,8 @@ func (v *View) handleEvent(event tcell.Event) int {
|
|||||||
v.topline = cy
|
v.topline = cy
|
||||||
ret = 2
|
ret = 2
|
||||||
}
|
}
|
||||||
if cy > v.topline+v.linesN-1 {
|
if cy > v.topline+v.height-1 {
|
||||||
v.topline = cy - v.linesN + 1
|
v.topline = cy - v.height + 1
|
||||||
ret = 2
|
ret = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,9 +164,8 @@ func (v *View) handleEvent(event tcell.Event) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *View) display() {
|
func (v *View) display() {
|
||||||
|
|
||||||
var charNum int
|
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) {
|
if lineN+v.topline >= len(v.buf.lines) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user