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