2015-01-06 11:34:06 +00:00
|
|
|
package main
|
|
|
|
|
|
2020-06-16 00:05:29 +00:00
|
|
|
import (
|
|
|
|
|
"github.com/lmika/ted/ui"
|
|
|
|
|
"regexp"
|
|
|
|
|
)
|
2015-01-06 11:34:06 +00:00
|
|
|
|
|
|
|
|
// The session is responsible for managing the UI and the model and handling
|
|
|
|
|
// the interaction between the two and the user.
|
|
|
|
|
type Session struct {
|
2020-06-16 04:23:17 +00:00
|
|
|
model Model
|
|
|
|
|
Source ModelSource
|
|
|
|
|
Frame *Frame
|
|
|
|
|
Commands *CommandMapping
|
|
|
|
|
UIManager *ui.Ui
|
|
|
|
|
modelController *ModelViewCtrl
|
2020-06-16 00:05:29 +00:00
|
|
|
|
|
|
|
|
LastSearch *regexp.Regexp
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-01 01:27:34 +00:00
|
|
|
func NewSession(uiManager *ui.Ui, frame *Frame, source ModelSource) *Session {
|
2020-06-16 04:23:17 +00:00
|
|
|
model := NewSingleCellStdModel()
|
2017-08-25 23:48:22 +00:00
|
|
|
session := &Session{
|
2020-06-16 04:23:17 +00:00
|
|
|
model: model,
|
|
|
|
|
Source: source,
|
|
|
|
|
Frame: frame,
|
|
|
|
|
Commands: NewCommandMapping(),
|
|
|
|
|
UIManager: uiManager,
|
|
|
|
|
modelController: NewGridViewModel(model),
|
2017-08-25 23:48:22 +00:00
|
|
|
}
|
2015-01-06 11:34:06 +00:00
|
|
|
|
2020-06-16 04:23:17 +00:00
|
|
|
frame.SetModel(&SessionGridModel{session.modelController})
|
2015-03-24 02:12:40 +00:00
|
|
|
|
2017-08-25 23:48:22 +00:00
|
|
|
session.Commands.RegisterViewCommands()
|
|
|
|
|
session.Commands.RegisterViewKeyBindings()
|
2015-01-06 11:34:06 +00:00
|
|
|
|
2017-08-25 23:48:22 +00:00
|
|
|
// Also assign this session with the frame
|
|
|
|
|
frame.Session = session
|
2015-01-06 11:34:06 +00:00
|
|
|
|
2017-08-25 23:48:22 +00:00
|
|
|
return session
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-01 01:27:34 +00:00
|
|
|
// LoadFromSource loads the model from the source, replacing the existing model
|
2018-09-01 02:02:01 +00:00
|
|
|
func (session *Session) LoadFromSource() {
|
2018-09-01 01:27:34 +00:00
|
|
|
newModel, err := session.Source.Read()
|
|
|
|
|
if err != nil {
|
2018-09-01 02:02:01 +00:00
|
|
|
session.Frame.Message(err.Error())
|
|
|
|
|
return
|
2018-09-01 01:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-16 04:23:17 +00:00
|
|
|
session.model = newModel
|
|
|
|
|
session.modelController.SetModel(newModel)
|
2018-09-01 01:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 11:34:06 +00:00
|
|
|
// Input from the frame
|
|
|
|
|
func (session *Session) KeyPressed(key rune, mod int) {
|
2017-08-25 23:48:22 +00:00
|
|
|
// Add the mod key modifier
|
|
|
|
|
if mod&ui.ModKeyAlt != 0 {
|
|
|
|
|
key |= ModAlt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := session.Commands.KeyMapping(key)
|
|
|
|
|
if cmd != nil {
|
|
|
|
|
err := cmd.Do(&CommandContext{session})
|
|
|
|
|
if err != nil {
|
|
|
|
|
session.Frame.ShowMessage(err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The command context used by the session
|
2017-08-25 23:48:22 +00:00
|
|
|
type CommandContext struct {
|
|
|
|
|
session *Session
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-16 04:23:17 +00:00
|
|
|
func (scc *CommandContext) ModelVC() *ModelViewCtrl {
|
|
|
|
|
return scc.session.modelController
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 23:48:22 +00:00
|
|
|
func (scc *CommandContext) Session() *Session {
|
|
|
|
|
return scc.session
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-25 23:48:22 +00:00
|
|
|
func (scc *CommandContext) Frame() *Frame {
|
|
|
|
|
return scc.session.Frame
|
|
|
|
|
}
|
2015-03-24 02:12:40 +00:00
|
|
|
|
2018-09-01 01:27:34 +00:00
|
|
|
// Error displays an error if err is not nil
|
2020-06-16 00:05:29 +00:00
|
|
|
func (scc *CommandContext) Error(err error) {
|
|
|
|
|
scc.Frame().Error(err)
|
2018-09-01 01:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-24 02:12:40 +00:00
|
|
|
// Session grid model
|
|
|
|
|
type SessionGridModel struct {
|
2020-06-16 04:23:17 +00:00
|
|
|
GridViewModel *ModelViewCtrl
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the size of the grid model (width x height)
|
|
|
|
|
func (sgm *SessionGridModel) Dimensions() (int, int) {
|
2020-06-16 04:23:17 +00:00
|
|
|
rs, cs := sgm.GridViewModel.Model().Dimensions()
|
2017-08-25 23:48:22 +00:00
|
|
|
return cs, rs
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the size of the particular column. If the size is 0, this indicates that the column is hidden.
|
2020-06-16 04:23:17 +00:00
|
|
|
func (sgm *SessionGridModel) ColWidth(col int) int {
|
|
|
|
|
return sgm.GridViewModel.ColAttrs(col).Size
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the size of the particular row. If the size is 0, this indicates that the row is hidden.
|
2020-06-16 04:23:17 +00:00
|
|
|
func (sgm *SessionGridModel) RowHeight(row int) int {
|
|
|
|
|
return sgm.GridViewModel.RowAttrs(row).Size
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the value of the cell a position X, Y
|
|
|
|
|
func (sgm *SessionGridModel) CellValue(x int, y int) string {
|
2020-06-16 04:23:17 +00:00
|
|
|
return sgm.GridViewModel.Model().CellValue(y, x)
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
2020-06-16 04:23:17 +00:00
|
|
|
|
|
|
|
|
func (sgm *SessionGridModel) CellAttributes(x int, y int) (fg, bg ui.Attribute) {
|
|
|
|
|
rowAttrs := sgm.GridViewModel.RowAttrs(y)
|
|
|
|
|
colAttrs := sgm.GridViewModel.ColAttrs(y)
|
|
|
|
|
|
|
|
|
|
if rowAttrs.Marker != MarkerNone {
|
|
|
|
|
return markerAttributes[rowAttrs.Marker], 0
|
|
|
|
|
} else if colAttrs.Marker != MarkerNone {
|
|
|
|
|
return markerAttributes[colAttrs.Marker], 0
|
|
|
|
|
}
|
|
|
|
|
return 0,0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var markerAttributes = map[Marker]ui.Attribute {
|
|
|
|
|
MarkerRed: ui.ColorRed,
|
|
|
|
|
MarkerGreen: ui.ColorGreen,
|
|
|
|
|
MarkerBlue: ui.ColorBlue,
|
|
|
|
|
}
|