refactor: divide the code into functions

This commit is contained in:
ShuheiKubota 2019-11-18 14:48:15 +09:00
parent 905e9223fb
commit a2d2db8027

87
vvin.go
View file

@ -13,7 +13,6 @@ import (
"github.com/mitchellh/go-ps" "github.com/mitchellh/go-ps"
"github.com/shu-go/gli" "github.com/shu-go/gli"
"github.com/shu-go/rog"
) )
// Version is app version // Version is app version
@ -47,55 +46,11 @@ func (c *globalCmd) Before() error {
return err return err
} }
an := ancestors() win := findFirstTarget(c.Target, wins, ancestors())
if win == nil {
if c.Target == "" {
loopConsole:
for _, p := range an {
for _, w := range wins {
if w.PID == p {
if c.Debug {
rog.Printf("win: %#v", w)
}
c.targetHandle = w.Handle
break loopConsole
}
}
}
} else {
t := strings.ToLower(c.Target)
for _, w := range wins {
ancestor := false
for _, p := range an {
if w.PID == p {
ancestor = true
break
}
}
if c.Debug {
rog.Printf("win: %#v (ancestor? %v)", w, ancestor)
}
if t != "" && !ancestor {
wt := strings.ToLower(w.Title)
if strings.Contains(wt, t) {
c.targetHandle = w.Handle
break
}
} else if t == "" && ancestor {
c.targetHandle = w.Handle
break
}
}
}
if c.targetHandle == 0 {
return errors.New("no target") return errors.New("no target")
} }
c.targetHandle = win.Handle
w, _, _ := getSystemMetrics.Call(SM_CXVIRTUALSCREEN) w, _, _ := getSystemMetrics.Call(SM_CXVIRTUALSCREEN)
h, _, _ := getSystemMetrics.Call(SM_CYVIRTUALSCREEN) h, _, _ := getSystemMetrics.Call(SM_CYVIRTUALSCREEN)
@ -261,3 +216,39 @@ func toInt(s string, max int) int32 {
return int32(i) return int32(i)
} }
} }
func findFirstTarget(title string, wins []*Window, ancestors []int) *Window {
if title == "" {
for _, p := range ancestors {
for _, w := range wins {
if w.PID == p {
return w
}
}
}
} else {
t := strings.ToLower(title)
for _, w := range wins {
ancestor := false
for _, p := range ancestors {
if w.PID == p {
ancestor = true
break
}
}
if t != "" && !ancestor {
wt := strings.ToLower(w.Title)
if strings.Contains(wt, t) {
return w
}
} else if t == "" && ancestor {
return w
}
}
}
return nil
}