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
import (
"bitbucket.org/lmika/ted-v2/ui"
"github.com/lmika/ted/ui"
"fmt"
)

View file

@ -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

13
main.go
View file

@ -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())

View file

@ -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

View file

@ -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

View file

@ -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

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
}
} 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) {