]> git.lizzy.rs Git - micro.git/blob - internal/display/termwindow.go
Merge pull request #1287 from didactic-drunk/ruby_syntax
[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) IsActive() bool {
44         return w.active
45 }
46
47 func (w *TermWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc {
48         return vloc
49 }
50
51 func (w *TermWindow) Clear() {
52         for y := 0; y < w.Height; y++ {
53                 for x := 0; x < w.Width; x++ {
54                         screen.Screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
55                 }
56         }
57 }
58
59 func (w *TermWindow) Relocate() bool { return true }
60 func (w *TermWindow) GetView() *View {
61         return w.View
62 }
63 func (w *TermWindow) SetView(v *View) {
64         w.View = v
65 }
66
67 // Display displays this terminal in a view
68 func (w *TermWindow) Display() {
69         w.State.Lock()
70         defer w.State.Unlock()
71
72         var l buffer.Loc
73         for y := 0; y < w.Height; y++ {
74                 for x := 0; x < w.Width; x++ {
75                         l.X, l.Y = x, y
76                         c, f, b := w.State.Cell(x, y)
77
78                         fg, bg := int(f), int(b)
79                         if f == terminal.DefaultFG {
80                                 fg = int(tcell.ColorDefault)
81                         }
82                         if b == terminal.DefaultBG {
83                                 bg = int(tcell.ColorDefault)
84                         }
85                         st := tcell.StyleDefault.Foreground(config.GetColor256(int(fg))).Background(config.GetColor256(int(bg)))
86
87                         if l.LessThan(w.Selection[1]) && l.GreaterEqual(w.Selection[0]) || l.LessThan(w.Selection[0]) && l.GreaterEqual(w.Selection[1]) {
88                                 st = st.Reverse(true)
89                         }
90
91                         screen.Screen.SetContent(w.X+x, w.Y+y, c, nil, st)
92                 }
93         }
94         if config.GetGlobalOption("statusline").(bool) {
95                 statusLineStyle := config.DefStyle.Reverse(true)
96                 if style, ok := config.Colorscheme["statusline"]; ok {
97                         statusLineStyle = style
98                 }
99
100                 text := []byte(w.Name())
101                 textLen := utf8.RuneCount(text)
102                 for x := 0; x < w.Width; x++ {
103                         if x < textLen {
104                                 r, size := utf8.DecodeRune(text)
105                                 text = text[size:]
106                                 screen.Screen.SetContent(w.X+x, w.Y+w.Height, r, nil, statusLineStyle)
107                         } else {
108                                 screen.Screen.SetContent(w.X+x, w.Y+w.Height, ' ', nil, statusLineStyle)
109                         }
110                 }
111         }
112         if w.State.CursorVisible() && w.active {
113                 curx, cury := w.State.Cursor()
114                 screen.Screen.ShowCursor(curx+w.X, cury+w.Y)
115         }
116 }