Skip to content

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

:bug: Correct CLI document search output

Daniel99bcbad59073
diff --git a/kernel/cli/cmd/display_test.go b/kernel/cli/cmd/display_test.go
new file mode 100644
index 0000000000..ac67a94fc9
--- /dev/null
+++ b/kernel/cli/cmd/display_test.go
@@ -0,0 +1,78 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+package cmd
+
+import "testing"
+
+func TestDocumentSearchDisplayFields(t *testing.T) {
+ tests := []struct {
+ name string
+ doc map[string]string
+ wantType string
+ wantID string
+ wantName string
+ }{
+ {
+ name: "document",
+ doc: map[string]string{
+ "path": "/20260722120000-parent1/20260722123000-child01.sy",
+ "hPath": "Notebook/Parent/Document",
+ "box": "20260722110000-boxid01",
+ },
+ wantType: "DOCUMENT",
+ wantID: "20260722123000-child01",
+ wantName: "Document",
+ },
+ {
+ name: "notebook",
+ doc: map[string]string{
+ "path": "/",
+ "hPath": "Notebook/",
+ "box": "20260722110000-boxid01",
+ },
+ wantType: "NOTEBOOK",
+ wantID: "20260722110000-boxid01",
+ wantName: "Notebook",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ typ, id, name := documentSearchDisplayFields(test.doc)
+ if typ != test.wantType || id != test.wantID || name != test.wantName {
+ t.Fatalf("documentSearchDisplayFields() = (%q, %q, %q), want (%q, %q, %q)", typ, id, name, test.wantType, test.wantID, test.wantName)
+ }
+ })
+ }
+}
+
+func TestTruncate(t *testing.T) {
+ tests := []struct {
+ name string
+ value string
+ length int
+ want string
+ }{
+ {name: "ascii unchanged", value: "SiYuan", length: 6, want: "SiYuan"},
+ {name: "ascii truncated", value: "SiYuan", length: 2, want: "Si..."},
+ {name: "Chinese unchanged", value: "思源笔记", length: 4, want: "思源笔记"},
+ {name: "Chinese truncated", value: "思源笔记", length: 2, want: "思源..."},
+ {name: "mixed", value: "SiYuan思源", length: 7, want: "SiYuan思..."},
+ {name: "zero", value: "SiYuan", length: 0, want: "..."},
+ {name: "empty", value: "", length: 0, want: ""},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if got := truncate(test.value, test.length); got != test.want {
+ t.Fatalf("truncate(%q, %d) = %q, want %q", test.value, test.length, got, test.want)
+ }
+ })
+ }
+}
diff --git a/kernel/cli/cmd/document.go b/kernel/cli/cmd/document.go
index dd9c09406b..110af652db 100644
--- a/kernel/cli/cmd/document.go
+++ b/kernel/cli/cmd/document.go
@@ -271,7 +271,7 @@ var documentInfoCmd = &cobra.Command{
default:
fmt.Printf("ID: %s\n", info.ID)
fmt.Printf("RootID: %s\n", info.RootID)
- fmt.Printf("Name: %s\n", info.Name)
+ fmt.Printf("Title: %s\n", info.Name)
fmt.Printf("RefCount: %d\n", info.RefCount)
fmt.Printf("SubFileCount: %d\n", info.SubFileCount)
}
@@ -311,9 +311,10 @@ var documentSearchCmd = &cobra.Command{
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
- fmt.Fprintln(w, "NAME\tID\tHPATH")
+ fmt.Fprintln(w, "TYPE\tID\tNAME\tHPATH")
for _, d := range docs {
- fmt.Fprintf(w, "%s\t%s\t%s\n", d["name"], d["id"], d["hPath"])
+ typ, id, name := documentSearchDisplayFields(d)
+ fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", typ, id, name, d["hPath"])
}
w.Flush()
fmt.Printf("\n%d document(s)\n", len(docs))
@@ -322,6 +323,14 @@ var documentSearchCmd = &cobra.Command{
},
}
+func documentSearchDisplayFields(doc map[string]string) (typ, id, name string) {
+ hPath := strings.TrimSuffix(doc["hPath"], "/")
+ if "/" == doc["path"] {
+ return "NOTEBOOK", doc["box"], path.Base(hPath)
+ }
+ return "DOCUMENT", strings.TrimSuffix(path.Base(doc["path"]), ".sy"), path.Base(hPath)
+}
+
func printDocumentTable(files []*model.File) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "ID\tNAME\tPATH\tSIZE\tCOUNT\tMTIME")
diff --git a/kernel/cli/cmd/search.go b/kernel/cli/cmd/search.go
index f7289ad1bf..ae574eac9a 100644
--- a/kernel/cli/cmd/search.go
+++ b/kernel/cli/cmd/search.go
@@ -187,10 +187,14 @@ func printSearchResult(blocks []*model.Block, matchedBlockCount, matchedRootCoun
}
func truncate(s string, maxLen int) string {
- if len(s) <= maxLen {
+ runes := []rune(s)
+ if len(runes) <= maxLen {
return s
}
- return s[:maxLen] + "..."
+ if maxLen < 1 {
+ return "..."
+ }
+ return string(runes[:maxLen]) + "..."
}
func stringSliceToMap(s []string) map[string]bool {