Debian Codegraph @

internal/buffer/message.go

raw
1 package buffer
2
3 import (
4 "github.com/micro-editor/tcell/v2"
5 "github.com/zyedidia/micro/v2/internal/config"
6 )
7
8 type MsgType int
9
10 const (
11 MTInfo = iota
12 MTWarning
13 MTError
14 )
15
16 // Message represents the information for a gutter message
17 type Message struct {
18 // The Msg iteslf
19 Msg string
20 // Start and End locations for the message
21 Start, End Loc
22 // The Kind stores the message type
23 Kind MsgType
24 // The Owner of the message
25 Owner string
26 }
27
28 // NewMessage creates a new gutter message
29 func NewMessage(owner string, msg string, start, end Loc, kind MsgType) *Message {
30 return &Message{
31 Msg: msg,
32 Start: start,
33 End: end,
34 Kind: kind,
35 Owner: owner,
36 }
37 }
38
39 // NewMessageAtLine creates a new gutter message at a given line
40 func NewMessageAtLine(owner string, msg string, line int, kind MsgType) *Message {
41 start := Loc{-1, line - 1}
42 end := start
43 return NewMessage(owner, msg, start, end, kind)
44 }
45
46 func (m *Message) Style() tcell.Style {
47 switch m.Kind {
48 case MTInfo:
49 if style, ok := config.Colorscheme["gutter-info"]; ok {
50 return style
51 }
52 case MTWarning:
53 if style, ok := config.Colorscheme["gutter-warning"]; ok {
54 return style
55 }
56 case MTError:
57 if style, ok := config.Colorscheme["gutter-error"]; ok {
58 return style
59 }
60 }
61 return config.DefStyle
62 }
63
64 func (b *Buffer) AddMessage(m *Message) {
65 b.Messages = append(b.Messages, m)
66 }
67
68 func (b *Buffer) removeMsg(i int) {
69 copy(b.Messages[i:], b.Messages[i+1:])
70 b.Messages[len(b.Messages)-1] = nil
71 b.Messages = b.Messages[:len(b.Messages)-1]
72 }
73
74 func (b *Buffer) ClearMessages(owner string) {
75 for i := len(b.Messages) - 1; i >= 0; i-- {
76 if b.Messages[i].Owner == owner {
77 b.removeMsg(i)
78 }
79 }
80 }
81
82 func (b *Buffer) ClearAllMessages() {
83 b.Messages = make([]*Message, 0)
84 }
85
86 type Messager interface {
87 Message(msg ...any)
88 }
89
90 var prompt Messager
91
92 func SetMessager(m Messager) {
93 prompt = m
94 }