Add infobar

This commit is contained in:
Zachary Yedidia
2018-12-31 22:07:01 -05:00
parent 367a7bbb5f
commit f79d45aafb
11 changed files with 309 additions and 135 deletions

18
cmd/micro/info/gutter.go Normal file
View File

@@ -0,0 +1,18 @@
package info
// A GutterMessage is a message displayed on the side of the editor
type GutterMessage struct {
lineNum int
msg string
kind int
}
// These are the different types of messages
const (
// GutterInfo represents a simple info message
GutterInfo = iota
// GutterWarning represents a compiler warning
GutterWarning
// GutterError represents a compiler error
GutterError
)

61
cmd/micro/info/history.go Normal file
View File

@@ -0,0 +1,61 @@
package info
import (
"encoding/gob"
"os"
"github.com/zyedidia/micro/cmd/micro/config"
)
// LoadHistory attempts to load user history from configDir/buffers/history
// into the history map
// The savehistory option must be on
func (i *Bar) LoadHistory() {
if config.GetGlobalOption("savehistory").(bool) {
file, err := os.Open(config.ConfigDir + "/buffers/history")
defer file.Close()
var decodedMap map[string][]string
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(&decodedMap)
if err != nil {
i.Error("Error loading history:", err)
return
}
}
if decodedMap != nil {
i.History = decodedMap
} else {
i.History = make(map[string][]string)
}
} else {
i.History = make(map[string][]string)
}
}
// SaveHistory saves the user's command history to configDir/buffers/history
// only if the savehistory option is on
func (i *Bar) SaveHistory() {
if config.GetGlobalOption("savehistory").(bool) {
// Don't save history past 100
for k, v := range i.History {
if len(v) > 100 {
i.History[k] = v[len(i.History[k])-100:]
}
}
file, err := os.Create(config.ConfigDir + "/buffers/history")
defer file.Close()
if err == nil {
encoder := gob.NewEncoder(file)
err = encoder.Encode(i.History)
if err != nil {
i.Error("Error saving history:", err)
return
}
}
}
}

66
cmd/micro/info/infobar.go Normal file
View File

@@ -0,0 +1,66 @@
package info
import (
"fmt"
"github.com/zyedidia/micro/cmd/micro/buffer"
)
var MainBar *Bar
func InitInfoBar() {
MainBar = NewBar()
}
// The Bar displays messages and other info at the bottom of the screen.
// It is respresented as a buffer and a message with a style.
type Bar struct {
*buffer.Buffer
HasPrompt bool
HasMessage bool
HasError bool
Msg string
// This map stores the history for all the different kinds of uses Prompt has
// It's a map of history type -> history array
History map[string][]string
HistoryNum int
// Is the current message a message from the gutter
GutterMessage bool
}
func NewBar() *Bar {
ib := new(Bar)
ib.History = make(map[string][]string)
ib.Buffer = buffer.NewBufferFromString("", "infobar", buffer.BTScratch)
return ib
}
// Message sends a message to the user
func (i *Bar) Message(msg ...interface{}) {
// only display a new message if there isn't an active prompt
// this is to prevent overwriting an existing prompt to the user
if i.HasPrompt == false {
displayMessage := fmt.Sprint(msg...)
// if there is no active prompt then style and display the message as normal
i.Msg = displayMessage
i.HasMessage = true
}
}
// Error sends an error message to the user
func (i *Bar) Error(msg ...interface{}) {
// only display a new message if there isn't an active prompt
// this is to prevent overwriting an existing prompt to the user
if i.HasPrompt == false {
// if there is no active prompt then style and display the message as normal
i.Msg = fmt.Sprint(msg...)
i.HasError = true
}
// TODO: add to log?
}