]> git.lizzy.rs Git - micro.git/blob - internal/display/termwindow.go
Autoclose plugin support
[micro.git] / internal / display / termwindow.go
1 package display
2
3 import (
4         "unicode/utf8"
5
6         "github.com/zyedidia/micro/internal/buffer"
7         "github.com/zyedidia/micro/internal/config"
8         "github.com/zyedidia/micro/internal/screen"
9         "github.com/zyedidia/micro/internal/shell"
10         "github.com/zyedidia/tcell"
11         "github.com/zyedidia/terminal"
12 )
13
14 type TermWindow struct {
15         *View
16         *shell.Terminal
17
18         active bool
19 }
20
21 func NewTermWindow(x, y, w, h int, term *shell.Terminal) *TermWindow {
22         tw := new(TermWindow)
23         tw.View = new(View)
24         tw.Terminal = term
25         tw.X, tw.Y = x, y
26         tw.Resize(w, h)
27         return tw
28 }
29
30 // Resize informs the terminal of a resize event
31 func (w *TermWindow) Resize(width, height int) {
32         if config.GetGlobalOption("statusline").(bool) {
33                 height--
34         }
35         w.Term.Resize(width, height)
36         w.Width, w.Height = width, height
37 }
38
39 func (w *TermWindow) SetActive(b bool) {
40         w.active = b
41 }
42
43 func (w *TermWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc {
44         return vloc
45 }
46
47 func (w *TermWindow) Clear() {
48         for y := 0; y < w.Height; y++ {
49                 for x := 0; x < w.Width; x++ {
50                         screen.Screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
51                 }
52         }
53 }
54
55 func (w *TermWindow) Relocate() bool { return true }
56 func (w *TermWindow) GetView() *View {
57         return w.View
58 }
59 func (w *TermWindow) SetView(v *View) {
60         w.View = v
61 }
62
63 // Display displays this terminal in a view
64 func (w *TermWindow) Display() {
65         w.State.Lock()
66         defer w.State.Unlock()
67
68         var l buffer.Loc
69         for y := 0; y < w.Height; y++ {
70                 for x := 0; x < w.Width; x++ {
71                         l.X, l.Y = x, y
72                         c, f, b := w.State.Cell(x, y)
73
74                         fg, bg := int(f), int(b)
75                         if f == terminal.DefaultFG {
76                                 fg = int(tcell.ColorDefault)
77                         }
78                         if b == terminal.DefaultBG {
79                                 bg = int(tcell.ColorDefault)
80                         }
81                         st := tcell.StyleDefault.Foreground(config.GetColor256(int(fg))).Background(config.GetColor256(int(bg)))
82
83                         if l.LessThan(w.Selection[1]) && l.GreaterEqual(w.Selection[0]) || l.LessThan(w.Selection[0]) && l.GreaterEqual(w.Selection[1]) {
84                                 st = st.Reverse(true)
85                         }
86
87                         screen.Screen.SetContent(w.X+x, w.Y+y, c, nil, st)
88                 }
89         }
90         if config.GetGlobalOption("statusline").(bool) {
91                 statusLineStyle := config.DefStyle.Reverse(true)
92                 if style, ok := config.Colorscheme["statusline"]; ok {
93                         statusLineStyle = style
94                 }
95
96                 text := []byte(w.Name())
97                 textLen := utf8.RuneCount(text)
98                 for x := 0; x < w.Width; x++ {
99                         if x < textLen {
100                                 r, size := utf8.DecodeRune(text)
101                                 text = text[size:]
102                                 screen.Screen.SetContent(w.X+x, w.Y+w.Height, r, nil, statusLineStyle)
103                         } else {
104                                 screen.Screen.SetContent(w.X+x, w.Y+w.Height, ' ', nil, statusLineStyle)
105                         }
106                 }
107         }
108         if w.State.CursorVisible() && w.active {
109                 curx, cury := w.State.Cursor()
110                 screen.Screen.ShowCursor(curx+w.X, cury+w.Y)
111         }
112 }