Fixed a few loading bugs

This commit is contained in:
Leon Mika 2018-09-01 12:02:01 +10:00
parent ae833d5db8
commit 3a7c4d764b
7 changed files with 42 additions and 16 deletions

View file

@ -1,7 +1,7 @@
package main package main
import ( import (
"bitbucket.org/lmika/ted-v2/ui" "github.com/lmika/ted/ui"
"fmt" "fmt"
) )

View file

@ -1,7 +1,7 @@
package main package main
import ( import (
"bitbucket.org/lmika/ted-v2/ui" "github.com/lmika/ted/ui"
) )
type Mode int type Mode int
@ -38,8 +38,8 @@ func NewFrame(uiManager *ui.Ui) *Frame {
} }
frame.grid = ui.NewGrid(nil) frame.grid = ui.NewGrid(nil)
frame.messageView = &ui.TextView{"Hello"} frame.messageView = &ui.TextView{""}
frame.statusBar = &ui.StatusBar{"Test", "Status"} frame.statusBar = &ui.StatusBar{"Test", ""}
frame.textEntrySwitch = &ui.ProxyLayout{frame.messageView} frame.textEntrySwitch = &ui.ProxyLayout{frame.messageView}
frame.textEntry = &ui.TextEntry{} frame.textEntry = &ui.TextEntry{}
@ -71,6 +71,8 @@ func (frame *Frame) Grid() *ui.Grid {
func (frame *Frame) enterMode(mode Mode) { func (frame *Frame) enterMode(mode Mode) {
switch mode { switch mode {
case GridMode: case GridMode:
frame.statusBar.Left = frame.Session.Source.String()
frame.uiManager.SetFocusedComponent(frame) frame.uiManager.SetFocusedComponent(frame)
case EntryMode: case EntryMode:
frame.textEntrySwitch.Component = frame.textEntry frame.textEntrySwitch.Component = frame.textEntry

13
main.go
View file

@ -1,10 +1,19 @@
package main package main
import ( import (
"bitbucket.org/lmika/ted-v2/ui" "github.com/lmika/ted/ui"
"flag"
"fmt"
"os"
) )
func main() { func main() {
flag.Parse()
if flag.NArg() == 0 {
fmt.Fprintln(os.Stderr, "usage: ted FILENAME")
os.Exit(1)
}
uiManager, err := ui.NewUI() uiManager, err := ui.NewUI()
if err != nil { if err != nil {
panic(err) panic(err)
@ -12,7 +21,7 @@ func main() {
defer uiManager.Close() defer uiManager.Close()
frame := NewFrame(uiManager) frame := NewFrame(uiManager)
session := NewSession(uiManager, frame, CsvFileModelSource{"test.csv"}) session := NewSession(uiManager, frame, CsvFileModelSource{flag.Arg(0)})
session.LoadFromSource() session.LoadFromSource()
uiManager.SetRootComponent(frame.RootComponent()) uiManager.SetRootComponent(frame.RootComponent())

View file

@ -36,6 +36,11 @@ func (s CsvFileModelSource) String() string {
// Read the model from the given source // Read the model from the given source
func (s CsvFileModelSource) Read() (Model, error) { 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) f, err := os.Open(s.Filename)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -1,6 +1,6 @@
package main 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 session is responsible for managing the UI and the model and handling
// the interaction between the two and the user. // 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 { func NewSession(uiManager *ui.Ui, frame *Frame, source ModelSource) *Session {
session := &Session{ session := &Session{
Model: nil, Model: NewSingleCellStdModel(),
Source: source, Source: source,
Frame: frame, Frame: frame,
Commands: NewCommandMapping(), 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 // 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() newModel, err := session.Source.Read()
if err != nil { if err != nil {
return err session.Frame.Message(err.Error())
return
} }
session.Model = newModel session.Model = newModel
return nil
} }
// Input from the frame // Input from the frame

View file

@ -11,6 +11,15 @@ type StdModel struct {
dirty bool dirty bool
} }
//
func NewSingleCellStdModel() *StdModel {
sm := new(StdModel)
sm.appendStr([]string { "" })
sm.dirty = false
return sm
}
/** /**
* The dimensions of the model (height, width). * The dimensions of the model (height, width).
*/ */
@ -73,15 +82,16 @@ func (sm *StdModel) appendStr(row []string) {
sm.Resize(len(sm.Cells), len(row)) sm.Resize(len(sm.Cells), len(row))
cols = len(sm.Cells[0]) cols = len(sm.Cells[0])
} }
cells := sm.strSliceToCell(row, cols) cells := sm.strSliceToCell(row, cols)
sm.Cells = append(sm.Cells, cells) sm.Cells = append(sm.Cells, cells)
} }
func (sm *StdModel) strSliceToCell(row []string, targetRowLen int) []Cell { func (sm *StdModel) strSliceToCell(row []string, targetRowLen int) []Cell {
cs := make([]Cell, targetRowLen) cs := make([]Cell, targetRowLen)
for i, c := range row { for i := 0; i < targetRowLen; i++ {
if i < targetRowLen { if i < len(row) {
cs[i].Value = c cs[i].Value = row[i]
} }
} }
return cs return cs

View file

@ -179,7 +179,7 @@ func (grid *Grid) getCellData(cellX, cellY int) (text string, fg, bg Attribute)
return grid.model.CellValue(modelCellX, modelCellY), 0, 0 return grid.model.CellValue(modelCellX, modelCellY), 0, 0
} }
} else { } 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) { if (modelCellY >= 0) && (modelCellY < modelMaxY) {
cellHeight = grid.model.RowHeight(modelCellY) cellHeight = grid.model.RowHeight(modelCellY)
} else { } else {
cellHeight = 2 cellHeight = 1
} }
if (cellX == 0) && (cellY == 0) { if (cellX == 0) && (cellY == 0) {