From 3a7c4d764be8097cff2b07e2aec78c457fdc8ec1 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sat, 1 Sep 2018 12:02:01 +1000 Subject: [PATCH] Fixed a few loading bugs --- commandmap.go | 2 +- frame.go | 8 +++++--- main.go | 13 +++++++++++-- modelsource.go | 5 +++++ session.go | 10 +++++----- stdmodel.go | 16 +++++++++++++--- ui/grid.go | 4 ++-- 7 files changed, 42 insertions(+), 16 deletions(-) diff --git a/commandmap.go b/commandmap.go index 24cd5fd..c1d36b1 100644 --- a/commandmap.go +++ b/commandmap.go @@ -1,7 +1,7 @@ package main import ( - "bitbucket.org/lmika/ted-v2/ui" + "github.com/lmika/ted/ui" "fmt" ) diff --git a/frame.go b/frame.go index 9d795e6..6e7e53e 100644 --- a/frame.go +++ b/frame.go @@ -1,7 +1,7 @@ package main import ( - "bitbucket.org/lmika/ted-v2/ui" + "github.com/lmika/ted/ui" ) type Mode int @@ -38,8 +38,8 @@ func NewFrame(uiManager *ui.Ui) *Frame { } frame.grid = ui.NewGrid(nil) - frame.messageView = &ui.TextView{"Hello"} - frame.statusBar = &ui.StatusBar{"Test", "Status"} + frame.messageView = &ui.TextView{""} + frame.statusBar = &ui.StatusBar{"Test", ""} frame.textEntrySwitch = &ui.ProxyLayout{frame.messageView} frame.textEntry = &ui.TextEntry{} @@ -71,6 +71,8 @@ func (frame *Frame) Grid() *ui.Grid { func (frame *Frame) enterMode(mode Mode) { switch mode { case GridMode: + frame.statusBar.Left = frame.Session.Source.String() + frame.uiManager.SetFocusedComponent(frame) case EntryMode: frame.textEntrySwitch.Component = frame.textEntry diff --git a/main.go b/main.go index be00528..6aca219 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,19 @@ package main import ( - "bitbucket.org/lmika/ted-v2/ui" + "github.com/lmika/ted/ui" + "flag" + "fmt" + "os" ) func main() { + flag.Parse() + if flag.NArg() == 0 { + fmt.Fprintln(os.Stderr, "usage: ted FILENAME") + os.Exit(1) + } + uiManager, err := ui.NewUI() if err != nil { panic(err) @@ -12,7 +21,7 @@ func main() { defer uiManager.Close() frame := NewFrame(uiManager) - session := NewSession(uiManager, frame, CsvFileModelSource{"test.csv"}) + session := NewSession(uiManager, frame, CsvFileModelSource{flag.Arg(0)}) session.LoadFromSource() uiManager.SetRootComponent(frame.RootComponent()) diff --git a/modelsource.go b/modelsource.go index b512d34..9b5c4fe 100644 --- a/modelsource.go +++ b/modelsource.go @@ -36,6 +36,11 @@ func (s CsvFileModelSource) String() string { // Read the model from the given source func (s CsvFileModelSource) Read() (Model, error) { + // Check if the file exists. If not, return an empty model + if _, err := os.Stat(s.Filename); os.IsNotExist(err) { + return NewSingleCellStdModel(), nil + } + f, err := os.Open(s.Filename) if err != nil { return nil, err diff --git a/session.go b/session.go index 5a11e30..bf8e7d0 100644 --- a/session.go +++ b/session.go @@ -1,6 +1,6 @@ package main -import "bitbucket.org/lmika/ted-v2/ui" +import "github.com/lmika/ted/ui" // The session is responsible for managing the UI and the model and handling // the interaction between the two and the user. @@ -14,7 +14,7 @@ type Session struct { func NewSession(uiManager *ui.Ui, frame *Frame, source ModelSource) *Session { session := &Session{ - Model: nil, + Model: NewSingleCellStdModel(), Source: source, Frame: frame, Commands: NewCommandMapping(), @@ -33,14 +33,14 @@ func NewSession(uiManager *ui.Ui, frame *Frame, source ModelSource) *Session { } // LoadFromSource loads the model from the source, replacing the existing model -func (session *Session) LoadFromSource() error { +func (session *Session) LoadFromSource() { newModel, err := session.Source.Read() if err != nil { - return err + session.Frame.Message(err.Error()) + return } session.Model = newModel - return nil } // Input from the frame diff --git a/stdmodel.go b/stdmodel.go index e5d87fd..306e224 100644 --- a/stdmodel.go +++ b/stdmodel.go @@ -11,6 +11,15 @@ type StdModel struct { dirty bool } +// +func NewSingleCellStdModel() *StdModel { + sm := new(StdModel) + sm.appendStr([]string { "" }) + sm.dirty = false + + return sm +} + /** * The dimensions of the model (height, width). */ @@ -73,15 +82,16 @@ func (sm *StdModel) appendStr(row []string) { sm.Resize(len(sm.Cells), len(row)) cols = len(sm.Cells[0]) } + cells := sm.strSliceToCell(row, cols) sm.Cells = append(sm.Cells, cells) } func (sm *StdModel) strSliceToCell(row []string, targetRowLen int) []Cell { cs := make([]Cell, targetRowLen) - for i, c := range row { - if i < targetRowLen { - cs[i].Value = c + for i := 0; i < targetRowLen; i++ { + if i < len(row) { + cs[i].Value = row[i] } } return cs diff --git a/ui/grid.go b/ui/grid.go index 4ff0c8a..8ce62e5 100644 --- a/ui/grid.go +++ b/ui/grid.go @@ -179,7 +179,7 @@ func (grid *Grid) getCellData(cellX, cellY int) (text string, fg, bg Attribute) return grid.model.CellValue(modelCellX, modelCellY), 0, 0 } } else { - return "~", ColorBlue | AttrBold, 0 + return "~", ColorBlue, 0 } } } @@ -203,7 +203,7 @@ func (grid *Grid) getCellDimensions(cellX, cellY int) (width, height int) { if (modelCellY >= 0) && (modelCellY < modelMaxY) { cellHeight = grid.model.RowHeight(modelCellY) } else { - cellHeight = 2 + cellHeight = 1 } if (cellX == 0) && (cellY == 0) {