micro/internal/display/termwindow.go

118 lines
2.7 KiB
Go
Raw Normal View History

2019-01-11 05:26:58 +03:00
package display
import (
2019-01-14 02:28:24 +03:00
"unicode/utf8"
2020-05-04 17:16:15 +03:00
"github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/screen"
"github.com/zyedidia/micro/v2/internal/shell"
2020-05-20 23:43:12 +03:00
"github.com/zyedidia/micro/v2/internal/util"
2020-01-01 04:15:45 +03:00
"github.com/zyedidia/tcell"
2019-01-11 05:26:58 +03:00
"github.com/zyedidia/terminal"
)
type TermWindow struct {
*View
*shell.Terminal
active bool
}
func NewTermWindow(x, y, w, h int, term *shell.Terminal) *TermWindow {
tw := new(TermWindow)
2019-01-11 22:49:22 +03:00
tw.View = new(View)
2019-01-11 05:26:58 +03:00
tw.Terminal = term
tw.X, tw.Y = x, y
2019-01-15 06:16:44 +03:00
tw.Resize(w, h)
2019-01-11 05:26:58 +03:00
return tw
}
// Resize informs the terminal of a resize event
func (w *TermWindow) Resize(width, height int) {
2019-01-15 06:16:44 +03:00
if config.GetGlobalOption("statusline").(bool) {
height--
}
2019-01-11 05:26:58 +03:00
w.Term.Resize(width, height)
2019-01-15 06:16:44 +03:00
w.Width, w.Height = width, height
2019-01-11 05:26:58 +03:00
}
func (w *TermWindow) SetActive(b bool) {
w.active = b
}
2019-08-04 09:53:33 +03:00
func (w *TermWindow) IsActive() bool {
return w.active
}
2019-12-25 00:01:08 +03:00
func (w *TermWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc {
2019-01-11 05:26:58 +03:00
return vloc
}
func (w *TermWindow) Clear() {
for y := 0; y < w.Height; y++ {
for x := 0; x < w.Width; x++ {
2020-01-02 06:40:51 +03:00
screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
2019-01-11 05:26:58 +03:00
}
}
}
func (w *TermWindow) Relocate() bool { return true }
func (w *TermWindow) GetView() *View {
return w.View
}
func (w *TermWindow) SetView(v *View) {
w.View = v
}
// Display displays this terminal in a view
func (w *TermWindow) Display() {
w.State.Lock()
defer w.State.Unlock()
var l buffer.Loc
for y := 0; y < w.Height; y++ {
for x := 0; x < w.Width; x++ {
l.X, l.Y = x, y
c, f, b := w.State.Cell(x, y)
fg, bg := int(f), int(b)
if f == terminal.DefaultFG {
fg = int(tcell.ColorDefault)
}
if b == terminal.DefaultBG {
bg = int(tcell.ColorDefault)
}
st := tcell.StyleDefault.Foreground(config.GetColor256(int(fg))).Background(config.GetColor256(int(bg)))
if l.LessThan(w.Selection[1]) && l.GreaterEqual(w.Selection[0]) || l.LessThan(w.Selection[0]) && l.GreaterEqual(w.Selection[1]) {
st = st.Reverse(true)
}
2020-01-02 06:40:51 +03:00
screen.SetContent(w.X+x, w.Y+y, c, nil, st)
2019-01-11 05:26:58 +03:00
}
}
2019-01-14 02:28:24 +03:00
if config.GetGlobalOption("statusline").(bool) {
statusLineStyle := config.DefStyle.Reverse(true)
if style, ok := config.Colorscheme["statusline"]; ok {
statusLineStyle = style
}
text := []byte(w.Name())
textLen := utf8.RuneCount(text)
for x := 0; x < w.Width; x++ {
if x < textLen {
2020-05-20 23:43:12 +03:00
r, combc, size := util.DecodeCharacter(text)
2019-01-14 02:28:24 +03:00
text = text[size:]
2020-05-20 23:43:12 +03:00
screen.SetContent(w.X+x, w.Y+w.Height, r, combc, statusLineStyle)
2019-01-14 02:28:24 +03:00
} else {
2020-01-02 06:40:51 +03:00
screen.SetContent(w.X+x, w.Y+w.Height, ' ', nil, statusLineStyle)
2019-01-14 02:28:24 +03:00
}
}
}
2019-01-11 05:26:58 +03:00
if w.State.CursorVisible() && w.active {
curx, cury := w.State.Cursor()
2020-01-02 05:29:18 +03:00
screen.ShowCursor(curx+w.X, cury+w.Y)
2019-01-11 05:26:58 +03:00
}
}