M card.go => card.go +6 -7
@@ 26,7 26,6 @@ type Card struct {
FeedImage image.Image
Article *readability.Article
Media *media.Media
- Redraw func()
}
type Cards []*Card
@@ 50,12 49,12 @@ func (card *Card) saveImage() func(image.Image) {
TODO check if this is needed
for _, vi := range onScreenCards {
if vi == card {
- card.Redraw()
+ card.photon.cb.Redraw()
break
}
}
*/
- card.Redraw()
+ card.photon.cb.Redraw()
}
}
@@ 82,11 81,11 @@ func (card *Card) OpenArticle(articleChanged func(*readability.Article)) {
card.photon.OpenedArticle.Image,
func(img image.Image) {
card.photon.OpenedArticle.TopImage = img
- card.Redraw()
+ card.photon.cb.Redraw()
},
)
}
- card.Redraw()
+ card.photon.cb.Redraw()
}
func (card *Card) GetMedia() (*media.Media, error) {
@@ 108,11 107,11 @@ func (card *Card) RunMedia() {
return
}
events.Emit(&events.RunMediaStart{Link: card.Item.Link})
- card.Redraw()
+ card.photon.cb.Redraw()
go func() {
defer func() {
events.Emit(&events.RunMediaEnd{Link: card.Item.Link})
- card.Redraw()
+ card.photon.cb.Redraw()
}()
if _, err := card.GetMedia(); err != nil {
log.Println("ERROR: extracting media link:", err)
M keybindings/keybindings.go => keybindings/keybindings.go +5 -8
@@ 76,23 76,20 @@ func parseStates(s string) (KeyEvents, error) {
}
type Registry struct {
- currentLayout CurrentLayout
- reg map[states.StateEnum]map[string]Callback
+ currentLayout states.Func
+ reg map[states.Enum]map[string]Callback
currentState KeyEvents
repeat int
}
-//Function type that returns the layout that is currently used
-type CurrentLayout func() states.StateEnum
-
-func New(cl CurrentLayout) *Registry {
+func New(cl states.Func) *Registry {
return &Registry{
- reg: make(map[states.StateEnum]map[string]Callback),
+ reg: make(map[states.Enum]map[string]Callback),
currentLayout: cl,
}
}
-func (kbr *Registry) Add(layout states.StateEnum, keyString string, callback Callback) {
+func (kbr *Registry) Add(layout states.Enum, keyString string, callback Callback) {
if _, ok := kbr.reg[layout]; !ok {
kbr.reg[layout] = make(map[string]Callback)
}
M keybindings/keybindings_lua.go => keybindings/keybindings_lua.go +2 -2
@@ 1,8 1,8 @@
package keybindings
import (
+ "git.sr.ht/~ghost08/libphoton/states"
lua "github.com/yuin/gopher-lua"
- "git.sr.ht/~ghost08/photon/internal/states"
)
func Loader(kb *Registry) lua.LGFunction {
@@ 19,7 19,7 @@ func Loader(kb *Registry) lua.LGFunction {
func keybindingsAdd(kb *Registry) lua.LGFunction {
return func(L *lua.LState) int {
- state := states.StateEnum(L.ToNumber(1))
+ state := states.Enum(L.ToNumber(1))
keyString := L.ToString(2)
fn := L.ToFunction(3)
kb.Add(state, keyString, func() error {
M libphoton.go => libphoton.go +24 -9
@@ 3,6 3,7 @@ package libphoton
import (
"bytes"
"fmt"
+ "image"
"log"
"net/http"
"os"
@@ 14,6 15,7 @@ import (
"git.sr.ht/~ghost08/libphoton/inputs"
"git.sr.ht/~ghost08/libphoton/keybindings"
"git.sr.ht/~ghost08/libphoton/media"
+ "git.sr.ht/~ghost08/libphoton/states"
"github.com/mmcdole/gofeed"
)
@@ 24,17 26,30 @@ type Photon struct {
httpClient *http.Client
KeyBindings *keybindings.Registry
downloadPath string
- redraw func()
+ cb Callbacks
- cards Cards
- visibleCards Cards
- SelectedCard *Card
- searchQuery string
- OpenedArticle *Article
+ cards Cards
+ visibleCards Cards
+ SelectedCard *Card
+ SelectedCardPos image.Point
+ searchQuery string
+ OpenedArticle *Article
}
-func New(redraw func(), state states.Func, paths []string, options ...Option) (*Photon, error) {
- p := &Photon{}
+type Callbacks interface {
+ Redraw()
+ State() states.Enum
+ SelectedCardMoveDown()
+ SelectedCardMoveUp()
+ SelectedCardMoveRight()
+ SelectedCardMoveLeft()
+}
+
+func New(cb Callbacks, paths []string, options ...Option) (*Photon, error) {
+ p := &Photon{
+ KeyBindings: keybindings.New(cb.State),
+ cb: cb,
+ }
feedInputs, err := p.loadFeeds(paths)
if err != nil {
return nil, err
@@ 186,9 201,9 @@ func (p *Photon) RefreshFeed() {
newCards := make(Cards, len(f.Items))
for i, item := range f.Items {
newCards[i] = &Card{
+ photon: p,
Item: item,
Feed: f,
- Redraw: p.redraw,
}
}
p.cards = append(p.cards, newCards...)
M plugins.go => plugins.go +23 -22
@@ 11,6 11,7 @@ import (
"git.sr.ht/~ghost08/libphoton/inputs"
"git.sr.ht/~ghost08/libphoton/keybindings"
"git.sr.ht/~ghost08/libphoton/media"
+ "git.sr.ht/~ghost08/libphoton/states"
lua "github.com/yuin/gopher-lua"
)
@@ 60,60 61,60 @@ func (p *Photon) initLuaState() {
luaState = lua.NewState()
media.Loader(luaState)
cardsLoader(luaState)
- luaState.PreloadModule("photon", photonLoader)
+ luaState.PreloadModule("photon", p.photonLoader)
luaState.PreloadModule("photon.events", events.Loader)
luaState.PreloadModule("photon.feedInputs", inputs.Loader(&p.feedInputs))
- luaState.PreloadModule("photon.keybindings", keybindings.Loader(p.keyBindings))
+ luaState.PreloadModule("photon.keybindings", keybindings.Loader(p.KeyBindings))
}
-func photonLoader(L *lua.LState) int {
+func (p *Photon) photonLoader(L *lua.LState) int {
var exports = map[string]lua.LGFunction{
- "state": photonState,
+ "state": p.photonState,
}
mod := L.SetFuncs(L.NewTable(), exports)
- registerTypeSelectedCard(L)
+ p.registerTypeSelectedCard(L)
L.SetField(mod, "Normal", lua.LNumber(states.Normal))
L.SetField(mod, "Article", lua.LNumber(states.Article))
L.SetField(mod, "Search", lua.LNumber(states.Search))
- L.SetField(mod, "cards", newCards(&cards, L))
- L.SetField(mod, "visibleCards", newCards(&visibleCards, L))
- L.SetField(mod, "selectedCard", newSelectedCard(L))
+ L.SetField(mod, "cards", newCards(&p.cards, L))
+ L.SetField(mod, "visibleCards", newCards(&p.visibleCards, L))
+ L.SetField(mod, "selectedCard", p.newSelectedCard(L))
L.Push(mod)
return 1
}
-func photonState(L *lua.LState) int {
- L.Push(lua.LNumber(State()))
+func (p *Photon) photonState(L *lua.LState) int {
+ L.Push(lua.LNumber(p.cb.State()))
return 1
}
const luaSelectedCardTypeName = "photon.selectedCardType"
-func registerTypeSelectedCard(L *lua.LState) {
+func (p *Photon) registerTypeSelectedCard(L *lua.LState) {
var selectedCardMethods = map[string]lua.LGFunction{
- "moveRight": func(L *lua.LState) int { selectedCard.MoveRight(); return 0 },
- "moveLeft": func(L *lua.LState) int { selectedCard.MoveLeft(); return 0 },
- "moveUp": func(L *lua.LState) int { selectedCard.MoveUp(); return 0 },
- "moveDown": func(L *lua.LState) int { selectedCard.MoveDown(); return 0 },
+ "moveRight": func(L *lua.LState) int { p.cb.SelectedCardMoveRight(); return 0 },
+ "moveLeft": func(L *lua.LState) int { p.cb.SelectedCardMoveLeft(); return 0 },
+ "moveUp": func(L *lua.LState) int { p.cb.SelectedCardMoveUp(); return 0 },
+ "moveDown": func(L *lua.LState) int { p.cb.SelectedCardMoveDown(); return 0 },
"posX": func(L *lua.LState) int {
if L.GetTop() == 2 {
- selectedCard.Pos.X = L.CheckInt(2)
+ p.SelectedCardPos.X = L.CheckInt(2)
return 0
}
- L.Push(lua.LNumber(selectedCard.Pos.X))
+ L.Push(lua.LNumber(p.SelectedCardPos.X))
return 1
},
"posY": func(L *lua.LState) int {
if L.GetTop() == 2 {
- selectedCard.Pos.Y = L.CheckInt(2)
+ p.SelectedCardPos.Y = L.CheckInt(2)
return 0
}
- L.Push(lua.LNumber(selectedCard.Pos.X))
+ L.Push(lua.LNumber(p.SelectedCardPos.X))
return 1
},
"card": func(L *lua.LState) int {
- L.Push(newCard(selectedCard.Card, L))
+ L.Push(newCard(p.SelectedCard, L))
return 1
},
}
@@ 122,9 123,9 @@ func registerTypeSelectedCard(L *lua.LState) {
L.SetField(mt, "__index", newClass)
}
-func newSelectedCard(L *lua.LState) *lua.LUserData {
+func (p *Photon) newSelectedCard(L *lua.LState) *lua.LUserData {
ud := L.NewUserData()
- ud.Value = selectedCard
+ ud.Value = p.SelectedCard
L.SetMetatable(ud, L.GetTypeMetatable(luaSelectedCardTypeName))
return ud
}
M states/states.go => states/states.go +4 -2
@@ 1,9 1,11 @@
package states
-type StateEnum int
+type Enum int
const (
- Normal StateEnum = iota
+ Normal Enum = iota
Article
Search
)
+
+type Func func() Enum