Skip to content

Commit

Permalink
Show items by tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrsalah committed Jul 21, 2021
1 parent 6b4dab4 commit 5ee5986
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 164 deletions.
31 changes: 0 additions & 31 deletions cmd/pkt/config.go

This file was deleted.

45 changes: 8 additions & 37 deletions cmd/pkt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/tarrsalah/pkt"
"github.com/tarrsalah/pkt/store/bolt"
"github.com/tarrsalah/pkt/internal/bolt"
"github.com/tarrsalah/pkt/internal/config"
"github.com/tarrsalah/pkt/internal/ui"
)

var (
Expand Down Expand Up @@ -68,13 +68,10 @@ run %s for usage.
}

func show() {
boltPath := getBoltPath()
configPath := getConfigPath()

db := bolt.NewDB(boltPath)
db := bolt.NewDB()
defer db.Close()

auth := loadAuth(configPath)
auth := config.GetAuth()
client := pkt.NewClient(auth)

oldItems := db.Get()
Expand All @@ -91,44 +88,18 @@ func show() {
db.Put(newItems)

items := db.Get()
draw(items)
app := ui.NewWindow(items)
app.Run()
}

func auth() {
configPath := getConfigPath()
r := bufio.NewReader(os.Stdin)
fmt.Print("pkt: enter your consumer key: ")
key, _ := r.ReadString('\n')

client := pkt.NewClient(nil)
auth := client.Authenticate(strings.TrimSpace(key))
saveAuth(auth, configPath)
config.PutAuth(auth)
log.Println("authorized!")

}

func loadAuth(configPath string) *pkt.Auth {
auth := &pkt.Auth{}
configFile, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatal(err)
}

err = json.Unmarshal(configFile, auth)
if err != nil {
log.Fatal(err)
}

return auth
}

func saveAuth(auth *pkt.Auth, configPath string) {
configFile, err := json.MarshalIndent(auth, " ", " ")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(configPath, configFile, 0644)
if err != nil {
log.Fatal(err)
}
}
76 changes: 0 additions & 76 deletions cmd/pkt/ui.go

This file was deleted.

7 changes: 5 additions & 2 deletions store/bolt/bolt.go → internal/bolt/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"sort"

"github.com/tarrsalah/pkt"
"github.com/tarrsalah/pkt/internal/config"
"go.etcd.io/bbolt"
"path/filepath"
)

var bucketName = []byte("pkt")
Expand All @@ -15,7 +17,8 @@ type DB struct {
db *bbolt.DB
}

func NewDB(path string) DB {
func NewDB() DB {
path := filepath.Join(config.Dir(), "pkt.bolt")
db, err := bbolt.Open(path, 0666, nil)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -73,7 +76,7 @@ func (b DB) Put(items []pkt.Item) {
if err != nil {
return err
}
b.Put([]byte(item.Id), v)
b.Put([]byte(item.ID), v)
}

return nil
Expand Down
61 changes: 61 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package config

import (
"log"
"os"
"os/user"
"path/filepath"

"encoding/json"
"github.com/tarrsalah/pkt"
"io/ioutil"
)

// GetAuth returns the saved pocket credentials
func GetAuth() *pkt.Auth {
auth := &pkt.Auth{}
configFile, err := ioutil.ReadFile(Path())
if err != nil {
log.Fatal(err)
}

err = json.Unmarshal(configFile, auth)
if err != nil {
log.Fatal(err)
}

return auth
}

// PutAuth saves the pocket credentials into a file
func PutAuth(auth *pkt.Auth) {
content, err := json.MarshalIndent(auth, " ", " ")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(Path(), content, 0644)
if err != nil {
log.Fatal(err)
}
}

// Dir returns the path of the confi directory
func Dir() string {
user, err := user.Current()
if err != nil {
log.Fatal(err)
}

configDir := filepath.Join(user.HomeDir, ".config", "pkt")
err = os.MkdirAll(configDir, 0777)
if err != nil {
log.Fatal(err)
}

return configDir
}

// Path returns the Path of the config file
func Path() string {
return filepath.Join(Dir(), "config.json")
}
53 changes: 53 additions & 0 deletions internal/ui/items_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ui

import (
"fmt"
"github.com/rivo/tview"
"github.com/tarrsalah/pkt"
)

type itemsTable struct {
*tview.Table
items pkt.Items
handleSelect func(int, int)
}

func newItemsTable() *itemsTable {
t := &itemsTable{
Table: tview.NewTable(),
}

t.SetBorder(true)
t.SetSelectable(true, false)

return t
}

func (t *itemsTable) Render() {
t.Clear()
for i, item := range t.items {
tags := fmt.Sprintf("[yellow] %s", item.Tags())
title := fmt.Sprintf("%d. %s [green](%s)",
i+1, item.Title(),
item.Host())

t.SetCell(i, 0, tview.NewTableCell(title).
SetMaxWidth(1).
SetExpansion(3))

t.SetCell(i, 1, tview.NewTableCell(tags).
SetMaxWidth(1).
SetExpansion(2))
}

t.SetSelectedFunc(t.handleSelect)
t.SetTitle(t.title())
}

func (t *itemsTable) title() string {
count := len(t.items)
if count > 0 {
return fmt.Sprintf("Pocket items (%d)", count)
}
return "Pocket items"
}
52 changes: 52 additions & 0 deletions internal/ui/tags_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ui

import (
"fmt"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/tarrsalah/pkt"
)

type tagsTable struct {
*tview.Table
tags pkt.Tags
selectedTags map[int]struct{}
handleSelect func(int, int)
}

func newTagsTable() *tagsTable {
t := &tagsTable{
Table: tview.NewTable(),
}

t.SetBorder(true)
t.SetSelectable(true, false)

return t
}

func (t *tagsTable) Render() {
t.Clear()
for i, tag := range t.tags {
cell := tview.NewTableCell(tag.Label)

if _, ok := t.selectedTags[i]; ok {
cell.SetTextColor(tcell.ColorYellow)
cell.SetAttributes(tcell.AttrUnderline | tcell.AttrBold)
}
t.Table.SetCell(i, 0, cell)

}
t.SetSelectedFunc(t.handleSelect)
t.SetTitle(t.title())
}

func (t *tagsTable) title() string {
l := len(t.tags)
if l > 0 {
return fmt.Sprintf("Tags (%d)", len(t.tags))
}

return "Tags"
}
Loading

0 comments on commit 5ee5986

Please sign in to comment.