Skip to content

Commit

Permalink
Merge pull request #11 from shalldie/feat-gitee
Browse files Browse the repository at this point in the history
Feat gitee
  • Loading branch information
shalldie committed Feb 22, 2024
2 parents 0a0db6e + 6a4b72c commit e1dfcad
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ output
__debug_bin
# debug env
*.env
.vscode
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ English | [中文](./README.zh-CN.md)

<img src="https://user-images.githubusercontent.com/9987486/229472271-62a5d923-f7b7-416c-913e-c842ecc2de4d.png" width="320" />

### Cloud Notebook in terminal, based on Github Gist. 🦋
### Cloud Notebook in terminal, based on Gist. 🦋

`终端中运行的云笔记本,基于 Github Gist 构建。`

Expand All @@ -22,7 +22,7 @@ English | [中文](./README.zh-CN.md)
[![Build Status](https://img.shields.io/github/actions/workflow/status/shalldie/tnote/ci.yml?logo=github&style=flat-square)](https://github.com/shalldie/tnote/actions)
[![License](https://img.shields.io/github/license/shalldie/tnote?logo=github&style=flat-square)](https://github.com/shalldie/tnote)

<img src="https://github.com/shalldie/tnote/assets/9987486/81a942ad-c00f-45ae-8e2f-3a29b4496bee" width="900">
<img src="https://github.com/shalldie/tnote/assets/9987486/57902934-4a58-482a-adff-fafba92dbae9" width="900">

</div>

Expand All @@ -32,7 +32,7 @@ English | [中文](./README.zh-CN.md)

- [x] Application 🎯
- [x] Shortcut keys, mouse operations
- [x] Cloud storage persistence
- [x] Cloud storage persistence, support Github/Gitee
- [x] i18n
- [x] Files
- [x] Add, delete, check, and modify
Expand All @@ -46,17 +46,20 @@ English | [中文](./README.zh-CN.md)

## Prepare & Config

The application is built on GitHub Gist and requires [applying for an access token](https://github.com/settings/tokens/new), and then add it to the environment variable `TNOTE_GIST_TOKEN`
The application is built on gist, support Github/Gitee.

- [Applying for Github access token](https://github.com/settings/tokens/new), and then add it to the environment variable `TNOTE_GIST_TOKEN`
- [Applying for Gitee access token](https://github.com/settings/tokens/new), and then add it to the environment variable `TNOTE_GIST_TOKEN`

```bash
# ~/.bashrc
# ~/.bashrc, github
export TNOTE_GIST_TOKEN="<your_access_token>"
```

| Environment Variable | Default | Description |
| :-------------------- | :-----: | :---------------------------------------------------- |
| `TNOTE_GIST_TOKEN` | | `access token` applied for |
| `TNOTE_LANG` / `LANG` | `en_US` | Language preferred, optional values: `en_US``zh_CN` |
| Environment Variable | Default | Description |
| :-------------------------------------------- | :-----: | :---------------------------------------------------- |
| `TNOTE_GIST_TOKEN` / `TNOTE_GIST_TOKEN_GITEE` | | `access token` applied for |
| `TNOTE_LANG` / `LANG` | `en_US` | Language preferred, optional values: `en_US``zh_CN` |

## Installation

Expand Down
21 changes: 12 additions & 9 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<img src="https://user-images.githubusercontent.com/9987486/229472271-62a5d923-f7b7-416c-913e-c842ecc2de4d.png" width="320" />

### Cloud Notebook in terminal, based on Github Gist. 🦋
### Cloud Notebook in terminal, based on Gist. 🦋

`终端中运行的云笔记本,基于 Github Gist 构建。`

Expand All @@ -22,7 +22,7 @@
[![Build Status](https://img.shields.io/github/actions/workflow/status/shalldie/tnote/ci.yml?logo=github&style=flat-square)](https://github.com/shalldie/tnote/actions)
[![License](https://img.shields.io/github/license/shalldie/tnote?logo=github&style=flat-square)](https://github.com/shalldie/tnote)

<img src="https://github.com/shalldie/tnote/assets/9987486/81a942ad-c00f-45ae-8e2f-3a29b4496bee" width="900">
<img src="https://github.com/shalldie/tnote/assets/9987486/57902934-4a58-482a-adff-fafba92dbae9" width="900">

</div>

Expand All @@ -32,7 +32,7 @@

- [x] 应用 🎯
- [x] 快捷键、鼠标操作
- [x] 云端存储持久化
- [x] 云端存储持久化,支持 Github/Gitee
- [x] 国际化
- [x] 文件
- [x] 增删查改
Expand All @@ -46,17 +46,20 @@

## 准备&配置

应用基于 github gist 构建,需要去 [申请 access token](https://github.com/settings/tokens/new),然后添加到环境变量 `TNOTE_GIST_TOKEN`
应用基于 gist 构建,支持 Github/Gitee 平台。

- [申请 Github access token](https://github.com/settings/tokens/new),添加到环境变量 `TNOTE_GIST_TOKEN`
- [申请 Gitee access token](https://gitee.com/profile/personal_access_tokens/new),添加到环境变量 `TNOTE_GIST_TOKEN_GITEE`

```bash
# ~/.bashrc
# ~/.bashrc, github
export TNOTE_GIST_TOKEN="<your_access_token>"
```

| 环境变量 | 默认值 | 描述 |
| :-------------------- | :-----: | :----------------------------------- |
| `TNOTE_GIST_TOKEN` | | 申请到的 access token |
| `TNOTE_LANG` / `LANG` | `en_US` | 使用的语言,可选值:`en_US``zh_CN` |
| 环境变量 | 默认值 | 描述 |
| :-------------------------------------------- | :-----: | :----------------------------------- |
| `TNOTE_GIST_TOKEN` / `TNOTE_GIST_TOKEN_GITEE` | | 申请到的 access token |
| `TNOTE_LANG` / `LANG` | `en_US` | 使用的语言,可选值:`en_US``zh_CN` |

## 安装&运行

Expand Down
31 changes: 14 additions & 17 deletions internal/app/app.go
Original file line number Diff line number Diff line change
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 @@ -236,7 +237,7 @@ func (m AppModel) View() string {

}

func Run(token string) {
func Run() {

zone.NewGlobal()

Expand All @@ -247,31 +248,27 @@ func Run(token string) {
)

go func() {
// utils.Log("init...")

store.SendImpl = func(cmd any) {
app.Send(cmd)
}

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(token)

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
2 changes: 1 addition & 1 deletion internal/app/file_list/file_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m FileListModel) Update(msg tea.Msg) (FileListModel, tea.Cmd) {
case "r":
file := store.State.GetFile()
if file != nil {
go m.renameFile(file.FileName)
go m.renameFile(file)
}
return m, nil

Expand Down
8 changes: 6 additions & 2 deletions internal/app/file_list/rename_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/shalldie/tnote/internal/i18n"
)

func (m *FileListModel) renameFile(filename string) {
func (m *FileListModel) renameFile(file *gist.GistFile) {
filename := file.FileName
store.Send(dialog.DialogPayload{
Mode: dialog.ModePrompt,
Title: i18n.Get(i18nTpl, "rename_title"),
Expand All @@ -29,7 +30,10 @@ func (m *FileListModel) renameFile(filename string) {
Message: i18n.Get(i18nTpl, "rename_renaming"),
})

store.Gist.UpdateFile(filename, &gist.UpdateGistPayload{Filename: newname})
store.Gist.UpdateFile(filename, &gist.UpdateGistPayload{
Filename: newname,
Content: file.Content, // gitee 需要这个
})

store.Send(store.StatusPayload{
Loading: false,
Expand Down
32 changes: 29 additions & 3 deletions internal/app/pkgs/dialog/model.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e1dfcad

Please sign in to comment.