From b43c5ed3e7489da6fe2cbe7225403f3e36ef99e4 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Tue, 1 Jan 2019 22:36:12 -0500 Subject: [PATCH] Reorganize info bar --- cmd/micro/action/actions.go | 21 ++++++++-------- cmd/micro/{ => action}/editpane.go | 25 ++++++++++++++++---- cmd/micro/action/{handler.go => events.go} | 0 cmd/micro/action/globals.go | 7 ++++++ cmd/micro/display/infowindow.go | 6 ++--- cmd/micro/info/history.go | 4 ++-- cmd/micro/info/{infobar.go => infobuffer.go} | 24 +++++++------------ cmd/micro/micro.go | 14 ++++------- 8 files changed, 57 insertions(+), 44 deletions(-) rename cmd/micro/{ => action}/editpane.go (51%) rename cmd/micro/action/{handler.go => events.go} (100%) create mode 100644 cmd/micro/action/globals.go rename cmd/micro/info/{infobar.go => infobuffer.go} (82%) diff --git a/cmd/micro/action/actions.go b/cmd/micro/action/actions.go index 0422fed1..2921d4bf 100644 --- a/cmd/micro/action/actions.go +++ b/cmd/micro/action/actions.go @@ -7,7 +7,6 @@ import ( "github.com/zyedidia/clipboard" "github.com/zyedidia/micro/cmd/micro/buffer" - "github.com/zyedidia/micro/cmd/micro/info" "github.com/zyedidia/micro/cmd/micro/screen" "github.com/zyedidia/micro/cmd/micro/util" "github.com/zyedidia/tcell" @@ -299,7 +298,7 @@ func (h *BufHandler) InsertSpace() bool { // InsertNewline inserts a newline plus possible some whitespace if autoindent is on func (h *BufHandler) InsertNewline() bool { if h.Buf.Type == buffer.BTInfo { - info.MainBar.DonePrompt(false) + InfoBar.DonePrompt(false) return false } @@ -557,7 +556,7 @@ func (h *BufHandler) CutLine() bool { h.lastCutTime = time.Now() h.Cursor.DeleteSelection() h.Cursor.ResetSelection() - info.MainBar.Message("Cut line") + InfoBar.Message("Cut line") return true } @@ -568,7 +567,7 @@ func (h *BufHandler) Cut() bool { h.Cursor.DeleteSelection() h.Cursor.ResetSelection() h.freshClip = true - info.MainBar.Message("Cut selection") + InfoBar.Message("Cut selection") return true } else { @@ -586,7 +585,7 @@ func (h *BufHandler) DuplicateLine() bool { // h.Cursor.Right() } - info.MainBar.Message("Duplicated line") + InfoBar.Message("Duplicated line") return true } @@ -598,7 +597,7 @@ func (h *BufHandler) DeleteLine() bool { } h.Cursor.DeleteSelection() h.Cursor.ResetSelection() - info.MainBar.Message("Deleted line") + InfoBar.Message("Deleted line") return true } @@ -643,12 +642,12 @@ func (h *BufHandler) SelectAll() bool { func (h *BufHandler) OpenFile() bool { cb := func(resp string, canceled bool) { if !canceled { - info.MainBar.Message("Opening", resp) + InfoBar.Message("Opening", resp) } else { - info.MainBar.Error("Canceled") + InfoBar.Error("Canceled") } } - info.MainBar.Prompt("Open file: ", cb) + InfoBar.Prompt("Open file: ", cb) return false } @@ -717,10 +716,10 @@ func (h *BufHandler) HalfPageDown() bool { func (h *BufHandler) ToggleRuler() bool { if !h.Buf.Settings["ruler"].(bool) { h.Buf.Settings["ruler"] = true - info.MainBar.Message("Enabled ruler") + InfoBar.Message("Enabled ruler") } else { h.Buf.Settings["ruler"] = false - info.MainBar.Message("Disabled ruler") + InfoBar.Message("Disabled ruler") } return false } diff --git a/cmd/micro/editpane.go b/cmd/micro/action/editpane.go similarity index 51% rename from cmd/micro/editpane.go rename to cmd/micro/action/editpane.go index a551a53e..60eab596 100644 --- a/cmd/micro/editpane.go +++ b/cmd/micro/action/editpane.go @@ -1,14 +1,20 @@ -package main +package action import ( - "github.com/zyedidia/micro/cmd/micro/action" "github.com/zyedidia/micro/cmd/micro/buffer" "github.com/zyedidia/micro/cmd/micro/display" + "github.com/zyedidia/micro/cmd/micro/info" ) type EditPane struct { display.Window - action.Handler + Handler +} + +type InfoPane struct { + display.Window + Handler + *info.InfoBuf } func NewBufEditPane(x, y, width, height int, b *buffer.Buffer) *EditPane { @@ -16,7 +22,18 @@ func NewBufEditPane(x, y, width, height int, b *buffer.Buffer) *EditPane { // TODO: can probably replace editpane with bufhandler entirely w := display.NewBufWindow(x, y, width, height, b) e.Window = w - e.Handler = action.NewBufHandler(b, w) + e.Handler = NewBufHandler(b, w) + + return e +} + +func NewInfoBar() *InfoPane { + e := new(InfoPane) + ib := info.NewBuffer() + w := display.NewInfoWindow(ib) + e.Window = w + e.Handler = NewBufHandler(ib.Buffer, w) + e.InfoBuf = ib return e } diff --git a/cmd/micro/action/handler.go b/cmd/micro/action/events.go similarity index 100% rename from cmd/micro/action/handler.go rename to cmd/micro/action/events.go diff --git a/cmd/micro/action/globals.go b/cmd/micro/action/globals.go new file mode 100644 index 00000000..edc4f476 --- /dev/null +++ b/cmd/micro/action/globals.go @@ -0,0 +1,7 @@ +package action + +var InfoBar *InfoPane + +func InitGlobals() { + InfoBar = NewInfoBar() +} diff --git a/cmd/micro/display/infowindow.go b/cmd/micro/display/infowindow.go index a03fdc9e..d8f2fffe 100644 --- a/cmd/micro/display/infowindow.go +++ b/cmd/micro/display/infowindow.go @@ -13,7 +13,7 @@ import ( ) type InfoWindow struct { - *info.Bar + *info.InfoBuf *View defStyle tcell.Style @@ -23,9 +23,9 @@ type InfoWindow struct { y int } -func NewInfoWindow(b *info.Bar) *InfoWindow { +func NewInfoWindow(b *info.InfoBuf) *InfoWindow { iw := new(InfoWindow) - iw.Bar = b + iw.InfoBuf = b iw.View = new(View) iw.defStyle = config.DefStyle diff --git a/cmd/micro/info/history.go b/cmd/micro/info/history.go index 663fd594..aaa006db 100644 --- a/cmd/micro/info/history.go +++ b/cmd/micro/info/history.go @@ -10,7 +10,7 @@ import ( // LoadHistory attempts to load user history from configDir/buffers/history // into the history map // The savehistory option must be on -func (i *Bar) LoadHistory() { +func (i *InfoBuf) LoadHistory() { if config.GetGlobalOption("savehistory").(bool) { file, err := os.Open(config.ConfigDir + "/buffers/history") defer file.Close() @@ -37,7 +37,7 @@ func (i *Bar) LoadHistory() { // SaveHistory saves the user's command history to configDir/buffers/history // only if the savehistory option is on -func (i *Bar) SaveHistory() { +func (i *InfoBuf) SaveHistory() { if config.GetGlobalOption("savehistory").(bool) { // Don't save history past 100 for k, v := range i.History { diff --git a/cmd/micro/info/infobar.go b/cmd/micro/info/infobuffer.go similarity index 82% rename from cmd/micro/info/infobar.go rename to cmd/micro/info/infobuffer.go index fb46ea6a..c7360711 100644 --- a/cmd/micro/info/infobar.go +++ b/cmd/micro/info/infobuffer.go @@ -7,15 +7,9 @@ import ( "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. +// The InfoBuf 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 { +type InfoBuf struct { *buffer.Buffer HasPrompt bool @@ -35,8 +29,8 @@ type Bar struct { PromptCallback func(resp string, canceled bool) } -func NewBar() *Bar { - ib := new(Bar) +func NewBuffer() *InfoBuf { + ib := new(InfoBuf) ib.History = make(map[string][]string) ib.Buffer = buffer.NewBufferFromString("", "infobar", buffer.BTInfo) @@ -45,7 +39,7 @@ func NewBar() *Bar { } // Message sends a message to the user -func (i *Bar) Message(msg ...interface{}) { +func (i *InfoBuf) 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 { @@ -57,7 +51,7 @@ func (i *Bar) Message(msg ...interface{}) { } // Error sends an error message to the user -func (i *Bar) Error(msg ...interface{}) { +func (i *InfoBuf) 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 { @@ -68,7 +62,7 @@ func (i *Bar) Error(msg ...interface{}) { // TODO: add to log? } -func (i *Bar) Prompt(msg string, callback func(string, bool)) { +func (i *InfoBuf) Prompt(msg string, callback func(string, bool)) { // If we get another prompt mid-prompt we cancel the one getting overwritten if i.HasPrompt { i.DonePrompt(true) @@ -80,7 +74,7 @@ func (i *Bar) Prompt(msg string, callback func(string, bool)) { i.PromptCallback = callback } -func (i *Bar) DonePrompt(canceled bool) { +func (i *InfoBuf) DonePrompt(canceled bool) { i.HasPrompt = false if canceled { i.PromptCallback("", true) @@ -91,7 +85,7 @@ func (i *Bar) DonePrompt(canceled bool) { } // Reset resets the messenger's cursor, message and response -func (i *Bar) Reset() { +func (i *InfoBuf) Reset() { i.Msg = "" i.HasPrompt, i.HasMessage, i.HasError = false, false, false } diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 27d1ed29..37c8218d 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -12,8 +12,6 @@ import ( "github.com/zyedidia/micro/cmd/micro/action" "github.com/zyedidia/micro/cmd/micro/buffer" "github.com/zyedidia/micro/cmd/micro/config" - "github.com/zyedidia/micro/cmd/micro/display" - "github.com/zyedidia/micro/cmd/micro/info" "github.com/zyedidia/micro/cmd/micro/screen" "github.com/zyedidia/micro/cmd/micro/util" "github.com/zyedidia/tcell" @@ -193,11 +191,9 @@ func main() { b := LoadInput()[0] width, height := screen.Screen.Size() - ep := NewBufEditPane(0, 0, width, height-1, b) + ep := action.NewBufEditPane(0, 0, width, height-1, b) - info.InitInfoBar() - infowindow := display.NewInfoWindow(info.MainBar) - infobar := action.NewBufHandler(info.MainBar.Buffer, infowindow) + action.InitGlobals() // Here is the event loop which runs in a separate thread go func() { @@ -214,7 +210,7 @@ func main() { screen.Screen.Fill(' ', config.DefStyle) screen.Screen.HideCursor() ep.Display() - infowindow.Display() + action.InfoBar.Display() screen.Screen.Show() var event tcell.Event @@ -225,8 +221,8 @@ func main() { } if event != nil { - if info.MainBar.HasPrompt { - infobar.HandleEvent(event) + if action.InfoBar.HasPrompt { + action.InfoBar.HandleEvent(event) } else { ep.HandleEvent(event) }