Skip to content

Commit

Permalink
提供平台选择功能 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalldie committed Feb 20, 2024
1 parent 797b8c1 commit 9814f12
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 41 deletions.
28 changes: 13 additions & 15 deletions internal/app/app.go
Expand Up @@ -7,18 +7,19 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
"github.com/shalldie/gog/gs"
"github.com/shalldie/tnote/internal/app/file_list"
"github.com/shalldie/tnote/internal/app/file_panel"
"github.com/shalldie/tnote/internal/app/pkgs/dialog"
"github.com/shalldie/tnote/internal/app/pkgs/model"
"github.com/shalldie/tnote/internal/app/status_bar"
"github.com/shalldie/tnote/internal/app/store"
"github.com/shalldie/tnote/internal/conf"
"github.com/shalldie/tnote/internal/utils"
)

var (
app *tea.Program
// gt *gist.Gist
)

type AppModel struct {
Expand Down Expand Up @@ -255,23 +256,20 @@ func Run() {

app.SetWindowTitle(fmt.Sprintf("tnote - %v", conf.VERSION))

store.Send(store.StatusPayload{
Loading: true,
Message: "loading...",
pfList := gs.Filter([]string{
utils.Ternary(conf.HasGithub(), conf.PF_GITHUB, ""),
utils.Ternary(conf.HasGitee(), conf.PF_GITEE, ""),
}, func(pf string, index int) bool {
return len(pf) > 0
})

store.Setup()

store.Send(store.StatusPayload{Loading: false})
store.Send(store.CMD_REFRESH_FILES(""))
store.Send(store.CMD_UPDATE_FILE(""))
if len(pfList) == 1 {
conf.PF_CURRENT = pfList[0]
store.Setup()
} else {
store.Send(store.CMD_SHOW_PLATFORM(true))
}

// time.Sleep(time.Second * 3)
// app.Send(dialog.DialogPayload{
// Message: "hello world",
// Mode: 1,
// PromptValue: "这个是默认值",
// })
}()

if _, err := app.Run(); err != nil {
Expand Down
32 changes: 29 additions & 3 deletions internal/app/pkgs/dialog/model.go
Expand Up @@ -3,10 +3,12 @@ package dialog
import (
"strings"

"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
"github.com/shalldie/gog/gs"
"github.com/shalldie/tnote/internal/app/pkgs/model"
"github.com/shalldie/tnote/internal/app/store"
"github.com/shalldie/tnote/internal/i18n"
Expand All @@ -22,6 +24,7 @@ type DialogModel struct {

// components
TextInput textinput.Model
Select list.Model
}

func (m *DialogModel) Focus() {
Expand All @@ -34,6 +37,10 @@ func (m *DialogModel) Blur() {
store.State.DialogMode = false
}

func (m *DialogModel) isSelect() bool {
return len(m.Payload.SelectList) > 0
}

func (m *DialogModel) isPrompt() bool {
return m.Payload.Mode == ModePrompt
}
Expand Down Expand Up @@ -74,6 +81,8 @@ func (m *DialogModel) Show(payload *DialogPayload) {
if m.isPrompt() {
m.TextInput.Focus()
}
// select
m.updateSelect()
}

func (m *DialogModel) Close() {
Expand All @@ -85,7 +94,13 @@ func (m *DialogModel) Close() {
func (m *DialogModel) FnOK() {
ok := true
if m.Payload.FnOK != nil {
ok = m.Payload.FnOK(strings.TrimSpace(m.TextInput.Value()))
result := strings.TrimSpace(m.TextInput.Value())
if m.isSelect() {
if item, ok := m.Select.SelectedItem().(selectItem); ok {
result = string(item)
}
}
ok = m.Payload.FnOK(result)
}
if ok {
m.Close()
Expand All @@ -104,6 +119,9 @@ func (m DialogModel) propagate(msg tea.Msg) (DialogModel, tea.Cmd) {
m.TextInput, cmd = m.TextInput.Update(msg)
cmds = append(cmds, cmd)

m.Select, cmd = m.Select.Update(msg)
cmds = append(cmds, cmd)

return m, tea.Batch(cmds...)
}

Expand Down Expand Up @@ -177,13 +195,20 @@ func (m DialogModel) View() string {
// message
message := lipgloss.NewStyle().Width(diaWidth).Align(lipgloss.Left).Render(m.Payload.Message)

// select
sl := utils.Ternary(m.isSelect(), m.Select.View(), "")

// prompt
prompt := zone.Mark(m.ID+"textarea", lipgloss.NewStyle().MarginTop(1).Render(m.TextInput.View()))

ui = lipgloss.JoinVertical(lipgloss.Top,
stacks := gs.Filter([]string{
message,
sl,
utils.Ternary(m.Payload.Mode == ModePrompt, prompt, ""),
)
}, func(str string, index int) bool {
return len(str) > 0
})
ui = lipgloss.JoinVertical(lipgloss.Top, stacks...)

// btn
btnCancel := zone.Mark(m.ID+"btn-cancel", utils.Ternary(m.TabIndex == 1, activeButtonStyle, buttonStyle).Render(i18n.Get(i18nTpl, "cancel")))
Expand Down Expand Up @@ -220,5 +245,6 @@ func New() DialogModel {
return DialogModel{
BoxModel: box,
TextInput: input,
Select: createSelect(),
}
}
1 change: 1 addition & 0 deletions internal/app/pkgs/dialog/payload.go
Expand Up @@ -11,6 +11,7 @@ type DialogPayload struct {
Title string
Message string
PromptValue string
SelectList []string
FnOK func(args ...string) bool
Width int // 宽度,默认 42
}
68 changes: 68 additions & 0 deletions internal/app/pkgs/dialog/select.go
@@ -0,0 +1,68 @@
package dialog

import (
"fmt"
"io"
"strings"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/shalldie/gog/gs"
)

// https://github.com/charmbracelet/bubbletea/blob/master/examples/list-simple/main.go

var (
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
)

type selectItem string

func (i selectItem) FilterValue() string { return "" }

type itemDelegate struct{}

func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(selectItem)
if !ok {
return
}

str := fmt.Sprintf("%d. %s", index+1, i)

fn := itemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render("> " + strings.Join(s, " "))
}
}

fmt.Fprint(w, fn(str))
}

func createSelect() list.Model {
l := list.New([]list.Item{}, itemDelegate{}, 0, 0)
l.SetShowTitle(false)
l.SetFilteringEnabled(false)
l.SetShowPagination(false)
l.SetShowHelp(false)
l.SetShowStatusBar(false)
return l
}

func (m *DialogModel) updateSelect() {
if len(m.Payload.SelectList) <= 0 {
return
}
items := gs.Map(m.Payload.SelectList, func(pf string, index int) list.Item {
return selectItem(pf)
})

m.Select.SetItems(items)
m.Select.SetHeight(len(items) + 3)
}
43 changes: 43 additions & 0 deletions internal/app/status_bar/platform.go
@@ -0,0 +1,43 @@
package status_bar

import (
"github.com/shalldie/tnote/internal/app/pkgs/dialog"
"github.com/shalldie/tnote/internal/app/store"
"github.com/shalldie/tnote/internal/conf"
)

// zone
var PLATFORM_ID = "STATUSBAR_SHOW_PLATFORM"

// 展示 「平台」弹框
func (m *StatusBarModel) showPlatform() {

store.Send(dialog.DialogPayload{
Mode: dialog.ModeAlert,
Title: "平台",
Message: "选择要使用的平台\n",
SelectList: []string{conf.PF_GITHUB, conf.PF_GITEE},
Width: 50,
FnOK: func(args ...string) bool {
if args[0] == conf.PF_GITHUB && !conf.HasGithub() {
go store.Send(store.StatusPayload{
Message: "",
Duration: 3,
})
return false
}

if args[0] == conf.PF_GITEE && !conf.HasGitee() {
go store.Send(store.StatusPayload{
Message: "",
Duration: 3,
})
return false
}

conf.PF_CURRENT = args[0]
go store.Setup()
return true
},
})
}
21 changes: 20 additions & 1 deletion internal/app/status_bar/status_bar.go
Expand Up @@ -9,6 +9,7 @@ import (
zone "github.com/lrstanley/bubblezone"
"github.com/shalldie/tnote/internal/app/pkgs/model"
"github.com/shalldie/tnote/internal/app/store"
"github.com/shalldie/tnote/internal/conf"
"github.com/shalldie/tnote/internal/i18n"
)

Expand Down Expand Up @@ -42,16 +43,29 @@ func (m StatusBarModel) Update(msg tea.Msg) (StatusBarModel, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {

case "f10":
go store.Send(store.CMD_SHOW_PLATFORM(true))
return m, nil

case "f12":
go m.showAbout()
return m, nil

}

case store.CMD_SHOW_PLATFORM:
if msg {
go m.showPlatform()
}
return m, nil

case tea.MouseMsg:
if msg.Button != tea.MouseButtonLeft || store.State.InputFocus {
return m, nil
}
if zone.Get(PLATFORM_ID).InBounds(msg) {
go store.Send(store.CMD_SHOW_PLATFORM(true))
}
if zone.Get(ABOUT_ID).InBounds(msg) {
go m.showAbout()
}
Expand Down Expand Up @@ -101,6 +115,10 @@ func (m StatusBarModel) View() string {
// helpStyle := baseStyle.Copy().Background(lipgloss.Color("#A550DF"))
// helpCol := helpStyle.Render("🛎️ Help - F12")

// platform
pfStyle := baseStyle.Copy().Background(lipgloss.Color("#A550DF"))
pfCol := zone.Mark(PLATFORM_ID, pfStyle.Render(conf.PF_CURRENT+" - F10"))

// version
versionStyle := baseStyle.Copy().Background(lipgloss.Color("#6124DF"))
versionCol := zone.Mark(ABOUT_ID, versionStyle.Render(i18n.Get(i18nTpl, "about")))
Expand All @@ -111,13 +129,14 @@ func (m StatusBarModel) View() string {
// Foreground(lipgloss.Color("#FFFDF5")).
// Background(lipgloss.Color("#6124DF")).
// Width(m.Width - w(statusCol) - w(versionCol) - w(helpCol)).
Width(m.Width - w(statusCol) - w(versionCol)).
Width(m.Width - w(statusCol) - w(pfCol) - w(versionCol)).
Render(store.State.Status.Message)

return lipgloss.JoinHorizontal(lipgloss.Top,
statusCol,
spaceCol,
// helpCol,
pfCol,
versionCol,
)

Expand Down
3 changes: 3 additions & 0 deletions internal/app/store/commands.go
Expand Up @@ -13,3 +13,6 @@ type CMD_SELECT_FILE string

// 触发编辑模式
type CMD_INVOKE_EDIT bool

// 展示平台选择
type CMD_SHOW_PLATFORM bool
12 changes: 11 additions & 1 deletion internal/app/store/state.go
Expand Up @@ -41,7 +41,17 @@ func (s *storeState) SetFile(file *gist.GistFile) {
}

func Setup() {
Gist = gist.NewGist().Setup()
if Gist == nil {
Gist = gist.NewGist()
}
Send(StatusPayload{
Loading: true,
Message: "loading...",
})
Gist.Setup()
Send(StatusPayload{Loading: false})
Send(CMD_REFRESH_FILES(""))
Send(CMD_UPDATE_FILE(""))
}

var State = &storeState{
Expand Down
9 changes: 3 additions & 6 deletions internal/conf/conf.go
Expand Up @@ -26,13 +26,10 @@ func init() {
TNOTE_GIST_TOKEN = os.Getenv("TNOTE_GIST_TOKEN")
TNOTE_GIST_TOKEN_GITEE = os.Getenv("TNOTE_GIST_TOKEN_GITEE")

if TNOTE_GIST_TOKEN == "" && TNOTE_GIST_TOKEN_GITEE == "" {
fmt.Println("Can't find any $TNOTE_GIST_TOKEN in $PATH")
// 啥都没配置
if !HasGithub() && !HasGitee() {
fmt.Println("Can't find any token in $PATH")
os.Exit(1)
}

if TNOTE_GIST_TOKEN_GITEE != "" {
ENV_CURRENT = ENV_GITEE
}

}
15 changes: 0 additions & 15 deletions internal/conf/env.go

This file was deleted.

0 comments on commit 9814f12

Please sign in to comment.