summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-04-18 00:31:55 +0700
committerShulhan <ms@kilabit.info>2024-04-18 00:31:55 +0700
commit64ea2467ced33bc092d125fb5b329bf81895477f (patch)
tree61291934a959386618887ee6668bacf39e89f7db
parenta6629b6a1f40616dbf3e5f78c76d601f3b89127a (diff)
downloadkbbi-64ea2467ced33bc092d125fb5b329bf81895477f.tar.xz
all: add method Write to the LookupResponse
The LookupResponse print each word and its Lookup information to the out.
-rw-r--r--cmd/kbbi/main.go29
-rw-r--r--lookup_response.go44
2 files changed, 46 insertions, 27 deletions
diff --git a/cmd/kbbi/main.go b/cmd/kbbi/main.go
index c605428..7db6aa6 100644
--- a/cmd/kbbi/main.go
+++ b/cmd/kbbi/main.go
@@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"log"
+ "os"
"sort"
kbbi "git.sr.ht/~shulhan/kbbi"
@@ -64,33 +65,7 @@ func main() {
log.Fatal(err)
}
- for word, wordDef := range resDefinition {
- err = wordDef.Err()
- if err != nil {
- fmt.Printf("!!! %s: %s\n", word, err)
- continue
- }
-
- fmt.Println("===", word)
- if len(wordDef.Message) != 0 {
- fmt.Println(" " + wordDef.Message)
- continue
- }
- if len(wordDef.Root) > 0 {
- fmt.Printf(" Kata dasar: %s\n", wordDef.Root)
- }
- for x, def := range wordDef.Definition {
- fmt.Printf(" Definisi #%d: %s\n", x+1, def.Value)
-
- for y, nomina := range def.Classes {
- fmt.Printf(" Kelas #%d: %s\n", y+1, nomina)
- }
- for z, contoh := range def.Examples {
- fmt.Printf(" Contoh #%d: %s\n", z+1, contoh)
- }
- fmt.Println()
- }
- }
+ resDefinition.Write(os.Stdout)
}
func listRootWords(cl *kbbi.Client) {
diff --git a/lookup_response.go b/lookup_response.go
index 4a6ed07..346bcd8 100644
--- a/lookup_response.go
+++ b/lookup_response.go
@@ -3,5 +3,49 @@
package kbbi
+import (
+ "fmt"
+ "io"
+)
+
// LookupResponse contains mapping of word and its definition.
type LookupResponse map[string]*Word
+
+// Write print each word and its lookup information to the out.
+func (res LookupResponse) Write(out io.Writer) {
+ var (
+ word string
+ wordDef *Word
+ err error
+ )
+ for word, wordDef = range res {
+ err = wordDef.Err()
+ if err != nil {
+ fmt.Fprintf(out, "!!! %s: %s\n", word, err)
+ fmt.Fprintln(out)
+ continue
+ }
+
+ fmt.Fprintln(out, `===`, word)
+ fmt.Fprintln(out)
+ if len(wordDef.Message) != 0 {
+ fmt.Fprintln(out, `?`, wordDef.Message)
+ fmt.Fprintln(out)
+ continue
+ }
+ if len(wordDef.Root) > 0 {
+ fmt.Fprintf(out, "Kata dasar: %s\n", wordDef.Root)
+ }
+ for x, def := range wordDef.Definition {
+ fmt.Fprintf(out, "Definisi #%d: %s\n", x+1, def.Value)
+
+ for y, nomina := range def.Classes {
+ fmt.Fprintf(out, " Kelas #%d: %s\n", y+1, nomina)
+ }
+ for z, contoh := range def.Examples {
+ fmt.Fprintf(out, " Contoh #%d: %s\n", z+1, contoh)
+ }
+ fmt.Fprintln(out)
+ }
+ }
+}