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