]> git.lizzy.rs Git - micro.git/blob - cmd/micro/statusline.go
Merge pull request #782 from i-amdroid/master
[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         if messenger.hasPrompt && !GetGlobalOption("infobar").(bool) {
18                 return
19         }
20
21         // We'll draw the line at the lowest line in the view
22         y := sline.view.Height + sline.view.y
23
24         file := sline.view.Buf.GetName()
25
26         // If the buffer is dirty (has been modified) write a little '+'
27         if sline.view.Buf.Modified() {
28                 file += " +"
29         }
30
31         // Add one to cursor.x and cursor.y because (0,0) is the top left,
32         // but users will be used to (1,1) (first line,first column)
33         // We use GetVisualX() here because otherwise we get the column number in runes
34         // so a '\t' is only 1, when it should be tabSize
35         columnNum := strconv.Itoa(sline.view.Cursor.GetVisualX() + 1)
36         lineNum := strconv.Itoa(sline.view.Cursor.Y + 1)
37
38         file += " (" + lineNum + "," + columnNum + ")"
39
40         // Add the filetype
41         file += " " + sline.view.Buf.FileType()
42
43         file += " " + sline.view.Buf.Settings["fileformat"].(string)
44
45         rightText := ""
46         if len(kmenuBinding) > 0 {
47                 if globalSettings["keymenu"].(bool) {
48                         rightText += kmenuBinding + ": hide bindings"
49                 } else {
50                         rightText += kmenuBinding + ": show bindings"
51                 }
52         }
53         if len(helpBinding) > 0 {
54                 if len(kmenuBinding) > 0 {
55                         rightText += ", "
56                 }
57                 if sline.view.Type == vtHelp {
58                         rightText += helpBinding + ": close help"
59                 } else {
60                         rightText += helpBinding + ": open help"
61                 }
62         }
63         rightText += " "
64
65         statusLineStyle := defStyle.Reverse(true)
66         if style, ok := colorscheme["statusline"]; ok {
67                 statusLineStyle = style
68         }
69
70         // Maybe there is a unicode filename?
71         fileRunes := []rune(file)
72         viewX := sline.view.x
73         if viewX != 0 {
74                 screen.SetContent(viewX, y, ' ', nil, statusLineStyle)
75                 viewX++
76         }
77         for x := 0; x < sline.view.Width; x++ {
78                 if x < len(fileRunes) {
79                         screen.SetContent(viewX+x, y, fileRunes[x], nil, statusLineStyle)
80                 } else if x >= sline.view.Width-len(rightText) && x < len(rightText)+sline.view.Width-len(rightText) {
81                         screen.SetContent(viewX+x, y, []rune(rightText)[x-sline.view.Width+len(rightText)], nil, statusLineStyle)
82                 } else {
83                         screen.SetContent(viewX+x, y, ' ', nil, statusLineStyle)
84                 }
85         }
86 }