it now displays books from the DB!
This commit is contained in:
107
ui/ui.go
107
ui/ui.go
@@ -1,6 +1,7 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.yetaga.in/alazyreader/library/book"
|
||||
@@ -230,10 +231,15 @@ type List struct {
|
||||
x, y int
|
||||
h, w int
|
||||
selected int
|
||||
listItems []string
|
||||
listItems []ListKeyValue
|
||||
}
|
||||
|
||||
func NewList(listItems []string, initialSelected int) *List {
|
||||
type ListKeyValue struct {
|
||||
Key int
|
||||
Value string
|
||||
}
|
||||
|
||||
func NewList(listItems []ListKeyValue, initialSelected int) *List {
|
||||
return &List{
|
||||
listItems: listItems,
|
||||
selected: initialSelected,
|
||||
@@ -246,11 +252,11 @@ func (l *List) SetSize(x, y, h, w int) {
|
||||
|
||||
func (l *List) Draw(s tcell.Screen) {
|
||||
for i := range l.listItems {
|
||||
for j, r := range l.listItems[i] {
|
||||
for j, r := range l.listItems[i].Value {
|
||||
s.SetContent(l.x+j, l.y+i, r, nil, tcell.StyleDefault)
|
||||
}
|
||||
if i == l.selected {
|
||||
s.SetContent(l.x+len(l.listItems[i])+1, l.y+i, '<', nil, tcell.StyleDefault)
|
||||
s.SetContent(l.x+len(l.listItems[i].Value)+1, l.y+i, '<', nil, tcell.StyleDefault)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -259,24 +265,33 @@ func (l *List) Selected() int {
|
||||
return l.selected
|
||||
}
|
||||
|
||||
func (l *List) SelectedID() int {
|
||||
return l.listItems[l.selected].Key
|
||||
}
|
||||
|
||||
func (l *List) SetSelected(i int) {
|
||||
l.selected = i
|
||||
}
|
||||
|
||||
func (l *List) ListMembers() []string {
|
||||
func (l *List) ListMembers() []ListKeyValue {
|
||||
return l.listItems
|
||||
}
|
||||
|
||||
// A List is a scrollable, pageable list with a selector token.
|
||||
// BookDetails displays an editable list of book details
|
||||
type BookDetails struct {
|
||||
x, y int
|
||||
h, w int
|
||||
selected int
|
||||
book book.Book
|
||||
x, y int
|
||||
h, w int
|
||||
book *book.Book
|
||||
}
|
||||
|
||||
func NewBookDetails() *BookDetails {
|
||||
return &BookDetails{}
|
||||
func NewBookDetails(b *book.Book) *BookDetails {
|
||||
return &BookDetails{
|
||||
book: b,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BookDetails) SetBook(b *book.Book) {
|
||||
l.book = b
|
||||
}
|
||||
|
||||
func (l *BookDetails) SetSize(x, y, h, w int) {
|
||||
@@ -284,12 +299,36 @@ func (l *BookDetails) SetSize(x, y, h, w int) {
|
||||
}
|
||||
|
||||
func (l *BookDetails) Draw(s tcell.Screen) {
|
||||
items := []string{"title", "authors", "isbn-10", "isbn-13"}
|
||||
if l.book == nil {
|
||||
return
|
||||
}
|
||||
items := []struct {
|
||||
label string
|
||||
value string
|
||||
}{
|
||||
{"Title", l.book.Title},
|
||||
{"Authors", strings.Join(l.book.Authors, ", ")},
|
||||
{"Sort Author", l.book.SortAuthor},
|
||||
{"ISBN-10", l.book.ISBN10},
|
||||
{"ISBN-13", l.book.ISBN13},
|
||||
{"Format", l.book.Format},
|
||||
{"Genre", l.book.Genre},
|
||||
{"Publisher", l.book.Publisher},
|
||||
{"Series", l.book.Series},
|
||||
{"Volume", l.book.Volume},
|
||||
{"Year", l.book.Year},
|
||||
{"Signed", strconv.FormatBool(l.book.Signed)},
|
||||
{"On Loan", l.book.OnLoan},
|
||||
{"Cover URL", l.book.CoverURL},
|
||||
{"Notes", l.book.Notes},
|
||||
{"Description", l.book.Description},
|
||||
}
|
||||
for i := range items {
|
||||
for j, r := range items[i] {
|
||||
s.SetContent(l.x+j, l.y+i, r, nil, tcell.StyleDefault)
|
||||
if i < l.h-2 {
|
||||
kv := NewKeyValue(items[i].label, ": ", items[i].value)
|
||||
kv.SetSize(l.x, l.y+i, 0, 0)
|
||||
kv.Draw(s)
|
||||
}
|
||||
s.SetContent(l.x+len(items[i]), l.y+i, ':', nil, tcell.StyleDefault)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,3 +361,39 @@ func (p *PaddedText) Draw(s tcell.Screen) {
|
||||
}
|
||||
s.SetContent(t, p.y, ' ', nil, tcell.StyleDefault)
|
||||
}
|
||||
|
||||
type KeyValue struct {
|
||||
x, y int
|
||||
h, w int
|
||||
key string
|
||||
value string
|
||||
separator string
|
||||
}
|
||||
|
||||
func NewKeyValue(key, separator, value string) *KeyValue {
|
||||
return &KeyValue{
|
||||
key: key,
|
||||
separator: separator,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *KeyValue) SetSize(x, y, _, _ int) {
|
||||
p.x, p.y, p.h, p.w = x, y, 1, len(p.key)+len(p.separator)+len(p.value)
|
||||
}
|
||||
|
||||
func (p *KeyValue) Draw(s tcell.Screen) {
|
||||
for j, r := range p.key {
|
||||
s.SetContent(p.x+j, p.y, r, nil, tcell.StyleDefault)
|
||||
}
|
||||
for j, r := range p.separator {
|
||||
s.SetContent(p.x+len(p.key)+j, p.y, r, nil, tcell.StyleDefault)
|
||||
}
|
||||
for j, r := range p.value {
|
||||
s.SetContent(p.x+len(p.key)+len(p.separator)+j, p.y, r, nil, tcell.StyleDefault)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *KeyValue) GetValue() string {
|
||||
return p.value
|
||||
}
|
||||
|
Reference in New Issue
Block a user