Debian Codegraph @

internal/display/termwindow.go

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