Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]<ui>: finish normal use #2

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions cmd/samples/table/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/o98k-ok/voice/internal/ui"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))

type model struct {
table table.Model
}

func (m model) Init() tea.Cmd { return nil }

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
if m.table.Focused() {
m.table.Blur()
} else {
m.table.Focus()
}
case "q", "ctrl+c":
return m, tea.Quit
case "enter":
return m, tea.Quit
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}

type TableModel struct {
program *tea.Program
}

func NewTable(values [][]string) *TableModel {
columns := []table.Column{
{Title: "ID", Width: 4},
{Title: "标题", Width: 50},
{Title: "简介", Width: 100},
{Title: "时长", Width: 8},
{Title: "BVID", Width: 12},
}

var rows []table.Row
for _, r := range values {
rows = append(rows, table.Row{r[0], r[1], r[2], r[3], r[4]})
}

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(7),
)

s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)

return &TableModel{
program: tea.NewProgram(model{t}),
}
}
func (t *TableModel) Run() string {
res, err := t.program.Run()
if err != nil {
return ""
}

v, ok := res.(model)
if !ok {
return ""
}
// get id
return v.table.SelectedRow()[0]
}

func main() {
values := [][]string{
{"1", "2", "3", "4", "5"},
}
ui.NewTable(values).Run()
}
73 changes: 35 additions & 38 deletions cmd/terminal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"strconv"

"github.com/duke-git/lancet/v2/netutil"
"github.com/o98k-ok/voice/internal/bilibili"
Expand All @@ -29,51 +30,47 @@ func main() {
}
player.Run()

// search page
input := ui.NewInputModelProcess()
keyword := input.Run()
for {

fetcher := bilibili.NewBlibliFetcher(netutil.NewHttpClient())
musics, err := fetcher.Search(keyword, 1, 10)
if err != nil {
return
}
// search page
input := ui.NewInputModelProcess()
keyword := input.Run()

var pack [][]string
for _, m := range musics {
pack = append(pack, []string{m.Name, m.Desc, "3m30s", m.URL})
}
bvid := ui.NewTable(pack).Run()
fetcher := bilibili.NewBlibliFetcher(netutil.NewHttpClient())
musics, err := fetcher.Search(keyword, 1, 10)
if err != nil {
return
}

mconvertor := convertor.NewAfconvertConvertor("./data")
var pack [][]string
for i, m := range musics {
pack = append(pack, []string{strconv.Itoa(i), m.Name, m.Desc, m.Duration, m.URL})
}

for i, u := range fetcher.GetAudioURL(bvid) {
go func(bvID string, url string, idx int) {
namein := fmt.Sprintf("%s/%s_%d.mp4", ROOT, bvID, idx)
nameout := fmt.Sprintf("%s/%s_%d.wav", ROOT, bvID, idx)
fin, _ := os.Create(namein)
fout, _ := os.Create(nameout)
fetcher.Download(url, fin)
fin.Close()
orderID := ui.NewTable(pack).Run()
id, _ := strconv.Atoi(orderID)

fin, _ = os.Open(namein)
mconvertor := convertor.NewAfconvertConvertor("./data")
for i, u := range fetcher.GetAudioURL(musics[id].URL) {
go func(bvID string, url string, idx int) {
namein := fmt.Sprintf("%s/%s_%d.mp4", ROOT, bvID, idx)
nameout := fmt.Sprintf("%s/%s_%d.wav", ROOT, bvID, idx)
fin, _ := os.Create(namein)
fout, _ := os.Create(nameout)
fetcher.Download(url, fin)
fin.Close()

mconvertor.ConvertM4AToWav(fin, fout)
fin.Close()
fout.Close()
os.Remove(namein)
fin, _ = os.Open(namein)

err = player.AddInQueue(&music.Music{
Name: bvID,
Desc: bvID,
LocalPath: path.Join(nameout),
})
}(bvid, u, i)
}
mconvertor.ConvertM4AToWav(fin, fout)
fin.Close()
fout.Close()
os.Remove(namein)

for {
fmt.Print("Press [ENTER] to next. ")
fmt.Scanln()
player.Next()
musics[id].LocalPath = path.Join(nameout)
musics[id].URL = url
err = player.DryPlay(musics[id])
}(musics[id].URL, u, i)
}
}
}
16 changes: 12 additions & 4 deletions internal/bilibili/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"io"
"net/http"
"net/url"
"strings"

"github.com/duke-git/lancet/v2/convertor"
"github.com/duke-git/lancet/v2/netutil"
"github.com/duke-git/lancet/v2/strutil"
"github.com/o98k-ok/voice/internal/music"
"github.com/o98k-ok/voice/internal/pkg"
)
Expand Down Expand Up @@ -115,9 +117,14 @@ func (bf *BilibiliFetcher) Search(keyword string, page, pageSize int) ([]*music.
musics := make([]*music.Music, 0, len(result.Data.Result))
for _, item := range result.Data.Result {
musics = append(musics, &music.Music{
Name: item.Title,
Desc: item.Description,
URL: item.Bvid,
Name: func() string {
extra := fmt.Sprintf("<em class=\"keyword\">%s</em>", keyword)
str := strings.ReplaceAll(item.Title, extra, keyword)
return strutil.RemoveNonPrintable(str)
}(),
Desc: strutil.RemoveNonPrintable(item.Description),
URL: item.Bvid,
Duration: strings.ReplaceAll(item.Duration, ":", "m") + "s",
})
}
return musics, nil
Expand Down Expand Up @@ -222,7 +229,8 @@ func (bf *BilibiliFetcher) GetAudioURL(bvid string) []string {
for _, u := range play.Data.Dash.Audio {
urls = append(urls, u.BaseURL)
}
return urls
// 同一个bvid下面的音频基本重复,具体情况后面再看
return urls[:1]
}

func (bf *BilibiliFetcher) Download(url string, writer io.Writer) error {
Expand Down
1 change: 1 addition & 0 deletions internal/music/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Music struct {
URL string
LocalPath string
SampleRate beep.SampleRate
Duration string

NextTrigger func()
PauseTrigger func()
Expand Down
20 changes: 20 additions & 0 deletions internal/player/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type VoicePlayer struct {
SampleRate int64
PlayList *list.List
PlayingQueue *StreamerQueue

CurrentElem *list.Element
}

func NewVoicePlayer(sampleRate int64) *VoicePlayer {
Expand Down Expand Up @@ -89,6 +91,7 @@ func (vp *VoicePlayer) Run() error {
if elem != nil {
vp.PlayingQueue.Add(elem)
}
// try cache
case 1:
elem := vp.PlayingQueue.Current().Next()
if elem == nil {
Expand Down Expand Up @@ -148,6 +151,23 @@ func (vp *VoicePlayer) AddInQueue(song *music.Music) error {
return nil
}

func (vp *VoicePlayer) DryPlay(song *music.Music) error {
p := vp.PlayingQueue.Current()
if p == nil {
vp.AddInQueue(song)
return nil
}

vp.PlayList.InsertAfter(song, p.Next())

vp.Next()
// BUGS

time.Sleep(time.Millisecond * 300)
vp.Next()
return nil
}

// func (vp *VoicePlayer) Play() error {
// if vp.Playing == nil {
// if vp.PlayList.Size() == 0 {
Expand Down
16 changes: 9 additions & 7 deletions internal/ui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ type TableModel struct {

func NewTable(values [][]string) *TableModel {
columns := []table.Column{
{Title: "标题", Width: 40},
{Title: "ID", Width: 4},
{Title: "标题", Width: 50},
{Title: "简介", Width: 100},
{Title: "时长", Width: 100},
{Title: "BVID", Width: 10},
{Title: "时长", Width: 8},
{Title: "BVID", Width: 12},
}

var rows []table.Row
for _, r := range values {
rows = append(rows, table.Row{r[0], r[1], r[2], r[3]})
rows = append(rows, table.Row{r[0], r[1], r[2], r[3], r[4]})
}

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(7),
table.WithHeight(10),
)

s := table.DefaultStyles()
Expand All @@ -50,6 +51,7 @@ func NewTable(values [][]string) *TableModel {
Bold(false)
t.SetStyles(s)

// BUGS 全屏展示不好看
return &TableModel{
program: tea.NewProgram(tableModel{t}),
}
Expand All @@ -65,8 +67,8 @@ func (t *TableModel) Run() string {
if !ok {
return ""
}
// get bvid
return v.table.SelectedRow()[3]
// get id
return v.table.SelectedRow()[0]
}

func (m tableModel) Init() tea.Cmd { return nil }
Expand Down