Skip to content

Commit

Permalink
[new] tip when input
Browse files Browse the repository at this point in the history
  • Loading branch information
Huweicai committed Feb 11, 2019
1 parent e61df9e commit 62c0116
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 20 deletions.
8 changes: 8 additions & 0 deletions IntegrationTesting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -ev
go build
./goto add test "http://example.com"
./goto add test test test "http://baidu.com"
./goto get test test test
./goto get test
./goto list tes
4 changes: 4 additions & 0 deletions alfred/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ type Mod struct {
Icon *Icon `json:"icon,omitempty"` //From Alfred 3.4.1
Variables map[string]string `json:"variables,omitempty"` //From Alfred 3.4.1
}

type Output struct {
Items []*Item `json:"items"`
}
27 changes: 27 additions & 0 deletions alfred/output.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package alfred

import (
"encoding/json"
"fmt"
"log"
)

func NewSimpleItem(title, subTitle, arg, autoComplete string) *Item {
return &Item{
Title: title,
Expand All @@ -8,3 +14,24 @@ func NewSimpleItem(title, subTitle, arg, autoComplete string) *Item {
Autocomplete: autoComplete,
}
}

func NewOutput() *Output {
return &Output{}
}

func (o *Output) AddSimpleTip(title, subTitle, arg, autoComplete string) {
o.AddTip(NewSimpleItem(title, subTitle, arg, autoComplete))
}

func (o *Output) AddTip(item *Item) {
o.Items = append(o.Items, item)
}

func (o *Output) Show() {
text, err := json.Marshal(o)
if err != nil {
log.Fatalf("json marshal err:%s raw:%+v ", err.Error(), o)
return
}
fmt.Println(string(text))
}
88 changes: 73 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"reflect"
"strings"
Expand All @@ -11,8 +12,9 @@ import (
type ModelType int

const (
configName = "config.yaml"
LoopBackKey = "@"
configName = "config.yaml"
LoopBackKey = "@"
TipSeparator = ", "

TypeUnknown ModelType = iota
TypeVector
Expand Down Expand Up @@ -58,13 +60,61 @@ func (n *Nest) AddScalar(paths []string, url string) {
addScalar(n.Data, paths, url)
}

//alfred
func (n *Nest) ListWithPre(paths []string) map[string]string {
switch len(paths) {
case 0:
return findMapPrefix(n.Data, "")
case 1:
return findMapPrefix(n.Data, paths[0])
default:
out, ok := getByPath(paths[:len(paths)-1], n.Data)
if !ok {
return nil
}
m, ok := out.(map[string]interface{})
if !ok {
return nil
}
return findMapPrefix(m, paths[len(paths)-1])

}

return nil
}

func findMapPrefix(m map[string]interface{}, pre string) map[string]string {
found := make(map[string]string)
for k, v := range m {
if strings.HasPrefix(k, pre) {
switch typeOf(v) {
case TypeVector:
found[k] = extractKeys(v.(map[string]interface{}))
case TypeScalar:
found[k] = v.(string)
}
}
}
return found
}

func extractKeys(m map[string]interface{}) string {
out := ""
for k, _ := range m {
out += k + TipSeparator
}
out = strings.TrimSuffix(out, TipSeparator)
return out
}

func typeOf(i interface{}) ModelType {
switch reflect.TypeOf(i).Kind() {
case reflect.Map:
return TypeVector
case reflect.String:
return TypeScalar
default:
log.Print("WARNNIG unknown type" + reflect.TypeOf(i).Kind().String())
return TypeUnknown
}
}
Expand Down Expand Up @@ -131,31 +181,39 @@ func addScalar(m map[string]interface{}, paths []string, url string) map[string]

//get the specified URL
func getScalar(paths []string, m map[string]interface{}) (scalar string, ok bool) {
if len(paths) == 0 {
if len(paths) == 0 || len(m) == 0 {
return
}
value, got := m[paths[0]]
if !got {
v, ok := getByPath(paths, m)
if !ok {
return
}
switch typeOf(value) {
switch typeOf(v) {
case TypeScalar:
return value.(string), true
return v.(string), true
case TypeVector:
if len(paths) == 1 {
v, ok := value.(map[string]interface{})[LoopBackKey]
if ok {
return v.(string), true
} else {
return "", false
}
v, ok := v.(map[string]interface{})[LoopBackKey]
if ok {
return v.(string), true
} else {
return "", false
}
return getScalar(paths[1:], value.(map[string]interface{}))
default:
return
}
}

func getByPath(paths []string, m map[string]interface{}) (out interface{}, ok bool) {
if len(paths) == 0 || len(m) == 0 {
return
}
out, ok = m[paths[0]]
if !ok || len(paths) == 1 {
return
}
return getByPath(paths[1:], out.(map[string]interface{}))
}

//convert map[interface{}]intreface{} to map[string]interface{}
func toMapStringInterface(m map[interface{}]interface{}) map[string]interface{} {
nm := make(map[string]interface{})
Expand Down
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ import (
"testing"
)

var testNest = Nest{
Data: map[string]interface{}{
"keyA": "123",
"keyB": "123",
"keyC": "123",
"keyD": map[string]interface{}{
"keyA": "123",
"keyB": "123",
},
"keyE": map[string]interface{}{
"keyA": "123",
"keyB": "123",
"keyC": map[string]interface{}{
"keyA": "123",
"keyB": "123",
},
},
},
}

func TestNest_ListWithPre(t *testing.T) {
out := testNest.ListWithPre([]string{"keyE", "keyA"})
println(out)
}

func Test_typeOf(t *testing.T) {
type args struct {
i interface{}
Expand Down
12 changes: 8 additions & 4 deletions handler/common.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package handler

const (
add = "add"
get = "get"
add = "add"
get = "get"
list = "list"
show = "show"
)

var handlers = map[string]func(args []string){
add: Add,
get: Get,
add: Add,
get: Get,
list: List,
show: Show,
}

func GetHandler(name string) func(args []string) {
Expand Down
20 changes: 19 additions & 1 deletion handler/list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package handler

func List(args []string) {
import (
"github.com/Huweicai/goto/alfred"
"github.com/Huweicai/goto/config"
"log"
"strings"
)

func List(args []string) {
nest, err := config.NewNest(config.GetConfigPath())
if err != nil {
log.Fatalf(err.Error())
return
}
outKV := nest.ListWithPre(args)
output := alfred.NewOutput()
for k, v := range outKV {
arg := strings.Join(append(args[:len(args)-1], k), " ")
output.AddSimpleTip(k, v, arg, arg)
}
output.Show()
}

0 comments on commit 62c0116

Please sign in to comment.