Skip to content

Commit

Permalink
initial viewing of content / ansi img
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Sep 9, 2021
1 parent f485db3 commit 29dae86
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
code.rocketnine.space/tslocum/cbind v0.1.5
code.rocketnine.space/tslocum/cview v1.5.7
github.com/eliukblau/pixterm v1.3.1
github.com/gdamore/tcell/v2 v2.4.1-0.20210828201608-73703f7ed490
github.com/ipfs/go-ipfs-api v0.2.0
github.com/spf13/cobra v1.2.1
Expand All @@ -14,6 +15,7 @@ require (
require (
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand Down Expand Up @@ -43,6 +45,7 @@ require (
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/eliukblau/pixterm v1.3.1 h1:XeouQViH+lmzCa7sMUoK2cd7qlgHYGLIjwRKaOdJbKA=
github.com/eliukblau/pixterm v1.3.1/go.mod h1:on5ueknFt+ZFVvIVVzQ7/JXwPjv5fJd8Q1Ybh7XixfU=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -347,6 +351,7 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand All @@ -361,6 +366,9 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 h1:gQ6GUSD102fPgli+Yb4cR/cGaHF7tNBt+GYoRCpGC7s=
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
12 changes: 12 additions & 0 deletions ipfs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ipfs
import (
"context"
"fmt"
"io/ioutil"

api "github.com/ipfs/go-ipfs-api"
"gopkg.in/yaml.v2"
Expand All @@ -21,6 +22,17 @@ func NewClient(url string) *Client {
return c
}

func (c *Client) ReadFile(path string, entry *api.MfsLsEntry) ([]byte, error) {
if entry.Type == api.TDirectory {
return []byte("directory"), nil
}
r, err := c.sh.FilesRead(context.Background(), path)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}

func (c *Client) GetDag(ref string) (out map[string]interface{}, err error) {
out = make(map[string]interface{})
err = c.sh.DagGet(ref, &out)
Expand Down
2 changes: 1 addition & 1 deletion ui/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func NewRepoTree(app *App) *RepoTree {
m.SetPadding(1, 1, 1, 1)
m.SetTitle("repo")
m.SetBackgroundColor(tcell.ColorDefault)
m.SetSelectedTextColor(tcell.ColorTeal)

rootNode := cview.NewTreeNode("/")
m.SetRoot(rootNode)
Expand Down Expand Up @@ -112,6 +111,7 @@ func (r *RepoTree) handleSelect(ev *tcell.EventKey) *tcell.EventKey {

r.app.info.SetItem(e.path, e.entry)
r.app.dag.SetItem(e.entry)
r.app.content.SetItem(e.path, e.entry)
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type App struct {
repo *RepoTree
info *FileInfo
dag *DagInfo
content *Content
panels *cview.Panels
focusManager *cview.FocusManager
}
Expand All @@ -28,6 +29,7 @@ func (app *App) initViews() {
app.repo = NewRepoTree(app)
app.info = NewFileInfo(app)
app.dag = NewDagInfo(app)
app.content = NewContentView(app)

panels := cview.NewPanels()
app.panels = panels
Expand All @@ -36,6 +38,7 @@ func (app *App) initViews() {
mid.SetBackgroundColor(tcell.ColorDefault)
mid.SetDirection(cview.FlexRow)
// mid.AddItem(app.panels, 0, 4, true)
mid.AddItem(app.content, 0, 4, false)
mid.AddItem(app.info, 0, 4, false)
mid.AddItem(app.dag, 0, 4, false)

Expand Down
95 changes: 95 additions & 0 deletions ui/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ui

import (
"bytes"
"image/color"
"io"
"net/http"

"code.rocketnine.space/tslocum/cview"
"github.com/eliukblau/pixterm/pkg/ansimage"
"github.com/gdamore/tcell/v2"
api "github.com/ipfs/go-ipfs-api"
)

type Content struct {
*cview.TextView

app *App
entry *api.MfsLsEntry
}

func NewContentView(app *App) *Content {
m := &Content{
TextView: cview.NewTextView(),
app: app,
}
m.SetBorder(true)
m.SetPadding(1, 1, 1, 1)
m.SetTitle("content")
m.SetBackgroundColor(tcell.ColorDefault)

return m
}

func (c *Content) SetItem(path string, entry *api.MfsLsEntry) {

data, err := c.app.client.ReadFile(path, entry)
if err != nil {
panic(err)
}
contentType, err := getFileContentType(data)
if err != nil {
c.SetText(err.Error())
}

switch contentType {
case "image/png":
c.SetDynamicColors(true)
_, _, w, h := c.GetInnerRect()
r := bytes.NewReader(data)
img := translateImage(r, w, h)
c.SetText(img)
default:
c.SetText(contentType)
}
}

func translateImage(reader io.Reader, x, y int) string {
img, err := buildImage(reader, x, y)
if err != nil {
return ""
}
ansi := img.Render()
return cview.TranslateANSI(ansi)

}

func buildImage(reader io.Reader, x, y int) (*ansimage.ANSImage, error) {
pix, err := ansimage.NewScaledFromReader(reader, y, x, color.Transparent, ansimage.ScaleModeFill, ansimage.NoDithering)
// pix, err := ansimage.NewScaledFromURL(url, y, x, color.Transparent, ansimage.ScaleModeResize, ansimage.NoDithering)
if err != nil {
return nil, err
}
return pix, nil

}

func getFileContentType(data []byte) (string, error) {

s := bytes.NewBuffer(data)

// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)

_, err := s.Read(buffer)
if err != nil {
return "", err
}

// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)

return contentType, nil
}

0 comments on commit 29dae86

Please sign in to comment.