]> git.lizzy.rs Git - micro.git/blob - cmd/micro/statusline.go
67d81c77c3c6a70988c2245a6364d71a3d60ef3c
[micro.git] / cmd / micro / statusline.go
1 package main
2
3 import (
4         "strconv"
5 )
6
7 // Statusline represents the information line at the bottom
8 // of each view
9 // It gives information such as filename, whether the file has been
10 // modified, filetype, cursor location
11 type Statusline struct {
12         view *View
13 }
14
15 // Display draws the statusline to the screen
16 func (sline *Statusline) Display() {
17         // We'll draw the line at the lowest line in the view
18         y := sline.view.height + sline.view.y
19
20         file := sline.view.Buf.Name
21         if file == "" {
22                 file = sline.view.Buf.Path
23         }
24
25         // If the buffer is dirty (has been modified) write a little '+'
26         if sline.view.Buf.IsModified {
27                 file += " +"
28         }
29
30         // Add one to cursor.x and cursor.y because (0,0) is the top left,
31         // but users will be used to (1,1) (first line,first column)
32         // We use GetVisualX() here because otherwise we get the column number in runes
33         // so a '\t' is only 1, when it should be tabSize
34         columnNum := strconv.Itoa(sline.view.Cursor.GetVisualX() + 1)
35         lineNum := strconv.Itoa(sline.view.Cursor.Y + 1)
36
37         file += " (" + lineNum + "," + columnNum + ")"
38
39         // Add the filetype
40         file += " " + sline.view.Buf.FileType()
41
42         rightText := helpBinding + " for help "
43         if sline.view.Type == vtHelp {
44                 rightText = helpBinding + " to close help "
45         }
46
47         statusLineStyle := defStyle.Reverse(true)
48         if style, ok := colorscheme["statusline"]; ok {
49                 statusLineStyle = style
50         }
51
52         // Maybe there is a unicode filename?
53         fileRunes := []rune(file)
54         viewX := sline.view.x
55         if viewX != 0 {
56                 screen.SetContent(viewX, y, ' ', nil, statusLineStyle)
57                 viewX++
58         }
59         for x := 0; x < sline.view.width; x++ {
60                 if x < len(fileRunes) {
61                         screen.SetContent(viewX+x, y, fileRunes[x], nil, statusLineStyle)
62                 } else if x >= sline.view.width-len(rightText) && x < len(rightText)+sline.view.width-len(rightText) {
63                         screen.SetContent(viewX+x, y, []rune(rightText)[x-sline.view.width+len(rightText)], nil, statusLineStyle)
64                 } else {
65                         screen.SetContent(viewX+x, y, ' ', nil, statusLineStyle)
66                 }
67         }
68 }