Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Command Line Interface (CLI) https://github.com/siyuan-note/siyuan/issues/17674

Danield6b27de5e08d
diff --git a/kernel/cli/cmd/notebook.go b/kernel/cli/cmd/notebook.go
index d5c1010581..3e3a59a735 100644
--- a/kernel/cli/cmd/notebook.go
+++ b/kernel/cli/cmd/notebook.go
@@ -19,10 +19,15 @@ package cmd
import (
"encoding/json"
"fmt"
+ "math/rand"
"os"
+ "path/filepath"
+ "sync"
"text/tabwriter"
"time"
+ "github.com/88250/gulu"
+
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -192,6 +197,182 @@ func printNotebookTable(boxes []*model.Box) {
w.Flush()
}
+// notebookSetIconCmd 设置笔记本图标。
+// icon 取值格式:emoji hex 码点(如 "1f4ca")、emoji 字符、自定义图片路径或动态图标 URL。
+var notebookSetIconCmd = &cobra.Command{
+ Use: "set-icon --id <id> --icon <icon>",
+ Short: "Set a notebook icon",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ id, _ := cmd.Flags().GetString("id")
+ icon, _ := cmd.Flags().GetString("icon")
+ if id == "" {
+ return fmt.Errorf("--id is required")
+ }
+ if icon == "" {
+ return fmt.Errorf("--icon is required")
+ }
+
+ // 校验笔记本存在,避免对一个不存在的 id 静默写入图标。
+ exists := false
+ notebooks, err := model.ListNotebooks()
+ if err != nil {
+ return err
+ }
+ for _, nb := range notebooks {
+ if nb.ID == id {
+ exists = true
+ break
+ }
+ }
+ if !exists {
+ return fmt.Errorf("notebook not found: %s", id)
+ }
+
+ if dryRun {
+ fmt.Printf("[dry-run] Would set notebook %s icon to %s\n", id, icon)
+ return nil
+ }
+
+ // SetBoxIcon 内部对自定义图片名做 XSS 过滤。
+ model.SetBoxIcon(id, icon)
+ util.PushReloadFiletree()
+
+ switch outputFormat {
+ case "json":
+ data, _ := json.MarshalIndent(map[string]string{"id": id, "icon": icon}, "", " ")
+ fmt.Println(string(data))
+ default:
+ fmt.Printf("%s\t%s\n", id, icon)
+ }
+ return nil
+ },
+}
+
+// notebookRandomIconCmd 为笔记本随机换一个内置 emoji 图标。
+// 传 --id 仅换该笔记本;不传则对全部笔记本各随机换一个。
+var notebookRandomIconCmd = &cobra.Command{
+ Use: "random-icon [--id <id>]",
+ Short: "Randomly set notebook icon(s) from built-in emojis",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ id, _ := cmd.Flags().GetString("id")
+
+ notebooks, err := model.ListNotebooks()
+ if err != nil {
+ return err
+ }
+
+ // 目标范围:传 id 仅换该笔记本;不传则对全部笔记本各随机换一个。
+ targets := notebooks
+ if id != "" {
+ var found *model.Box
+ for _, nb := range notebooks {
+ if nb.ID == id {
+ found = nb
+ break
+ }
+ }
+ if found == nil {
+ return fmt.Errorf("notebook not found: %s", id)
+ }
+ targets = []*model.Box{found}
+ }
+ if len(targets) == 0 {
+ return fmt.Errorf("no notebooks to update")
+ }
+
+ // 先把拟定的新图标算好,便于 dry-run 预览与失败回滚。
+ type change struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ OldIcon string `json:"oldIcon"`
+ NewIcon string `json:"newIcon"`
+ }
+ changes := make([]change, 0, len(targets))
+ for _, nb := range targets {
+ changes = append(changes, change{ID: nb.ID, Name: nb.Name, OldIcon: nb.Icon, NewIcon: randomEmoji()})
+ }
+
+ if dryRun {
+ for _, c := range changes {
+ fmt.Printf("[dry-run] Would set notebook %s (%s) icon %s -> %s\n", c.ID, c.Name, c.OldIcon, c.NewIcon)
+ }
+ return nil
+ }
+
+ for _, c := range changes {
+ model.SetBoxIcon(c.ID, c.NewIcon)
+ }
+ util.PushReloadFiletree()
+
+ switch outputFormat {
+ case "json":
+ data, _ := json.MarshalIndent(changes, "", " ")
+ fmt.Println(string(data))
+ default:
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
+ fmt.Fprintln(w, "ID\tNAME\tOLD\tNEW")
+ for _, c := range changes {
+ fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", c.ID, c.Name, c.OldIcon, c.NewIcon)
+ }
+ w.Flush()
+ }
+ return nil
+ },
+}
+
+// builtinEmojiUnicodes 缓存内置 emoji 的全部 unicode 码点(来自 appearance/emojis/conf.json)。
+// 与前端 getRandomEmoji() 数据源一致,但刻意排除用户自定义图片,
+// 保持随机图标风格统一、避免误用用户的重要图片。
+var (
+ builtinEmojiUnicodes []string
+ builtinEmojiUnicodesOnce sync.Once
+)
+
+// loadBuiltinEmojiUnicodes 懒加载内置 emoji 的 unicode 列表。
+// 失败时回退到一个固定码点,调用方始终拿到可用值。
+func loadBuiltinEmojiUnicodes() {
+ builtinEmojiUnicodesOnce.Do(func() {
+ confPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
+ data, err := os.ReadFile(confPath)
+ if err != nil {
+ builtinEmojiUnicodes = []string{"1f4d6"}
+ return
+ }
+
+ var conf []map[string]any
+ if err = gulu.JSON.UnmarshalJSON(data, &conf); err != nil {
+ builtinEmojiUnicodes = []string{"1f4d6"}
+ return
+ }
+
+ for _, category := range conf {
+ items, ok := category["items"].([]any)
+ if !ok {
+ continue
+ }
+ for _, item := range items {
+ e, ok := item.(map[string]any)
+ if !ok {
+ continue
+ }
+ if u, ok := e["unicode"].(string); ok && u != "" {
+ builtinEmojiUnicodes = append(builtinEmojiUnicodes, u)
+ }
+ }
+ }
+
+ if len(builtinEmojiUnicodes) == 0 {
+ builtinEmojiUnicodes = []string{"1f4d6"}
+ }
+ })
+}
+
+// randomEmoji 返回一个随机的内置 emoji unicode 码点(如 "1f4d6")。
+func randomEmoji() string {
+ loadBuiltinEmojiUnicodes()
+ return builtinEmojiUnicodes[rand.Intn(len(builtinEmojiUnicodes))]
+}
+
func init() {
notebookCreateCmd.Flags().String("name", "", "notebook name")
notebookRemoveCmd.Flags().String("id", "", "notebook ID")
@@ -199,6 +380,9 @@ func init() {
notebookRenameCmd.Flags().String("name", "", "new notebook name")
notebookOpenCmd.Flags().String("id", "", "notebook ID")
notebookCloseCmd.Flags().String("id", "", "notebook ID")
+ notebookSetIconCmd.Flags().String("id", "", "notebook ID")
+ notebookSetIconCmd.Flags().String("icon", "", "notebook icon: emoji hex codepoint like \"1f4ca\", emoji character like \"📊\", custom image path like \"1/b3log.png\", or dynamic icon URL like \"api/icon/getDynamicIcon?type=8&color=%23d23f31&content=SiYuan&id=xxx\"")
+ notebookRandomIconCmd.Flags().String("id", "", "notebook ID (optional; omit to update all notebooks)")
rootCmd.AddCommand(notebookCmd)
notebookCmd.AddCommand(notebookListCmd)
@@ -207,4 +391,6 @@ func init() {
notebookCmd.AddCommand(notebookRenameCmd)
notebookCmd.AddCommand(notebookOpenCmd)
notebookCmd.AddCommand(notebookCloseCmd)
+ notebookCmd.AddCommand(notebookSetIconCmd)
+ notebookCmd.AddCommand(notebookRandomIconCmd)
}