Skip to content

Commit

Permalink
[ci skip] update version
Browse files Browse the repository at this point in the history
  • Loading branch information
xztaityozx committed Dec 9, 2018
2 parents 13455f0 + dc9c424 commit 9b61df6
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 435 deletions.
4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions cmd/CustomSource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cmd

import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/mattn/go-pipeline"
)

type CustomSource struct {
Name string
SubName rune
Command string
BeginColumn int
Action string
}

func printCustomSources() {
_, _ = os.Stderr.WriteString("cdx -c [name]\n\n[name]\t\t[command]\n")
for _, v := range config.CustomSource {
_,_ = os.Stderr.WriteString(v.ToString())
}
_ = os.Stderr.Close()
os.Exit(0)
}

func (cs CustomSource) ToString() string {
return fmt.Sprintf("%s\t\t%s\n", cs.Name, cs.Command)
}

func getCustomSource(name rune) (CustomSource, error) {
for _, v := range config.CustomSource {
if (len(v.Name) != 0 && []rune(v.Name)[0] == name) || v.SubName == name{
return v, nil
}
}
return CustomSource{}, errors.New(fmt.Sprint(name, "could not found in custom source"))
}

var HistoryCustomSource CustomSource
var BookmarkCustomSource CustomSource



// 複数のCustomSourceを連結する
func BuildCustomSource(list... CustomSource) CustomSource {

command := "cat"
action := ""

for k, v := range list {
command = fmt.Sprintf(`%s <(%s|awk '{printf "[%d-%%03d] %%s\n",NR,$0}'|column -t)`, command, v.Command, k+1)
if len(v.Action) != 0 {
action = fmt.Sprintf("%s %s;", action,v.Action)
}
}

return CustomSource{
SubName: 'm',
Command: command,
BeginColumn: -1,
Action: action,
}
}

func (cs CustomSource) Equals(second CustomSource) bool {
return cs.Action == second.Action &&
cs.BeginColumn == second.BeginColumn &&
cs.Command == second.Command &&
cs.SubName == second.SubName
}

// FuzzyFinderをつかってパス取得。AfterCommandのインデックスを返す
func (cs CustomSource) GetPathWithFinder(begins []int) (string,error) {

b, err := pipeline.Output(
[]string{"bash", "-c", cs.Command},
config.FuzzyFinder.GetCommand(),
)
if err != nil {
return "", err
}


// 1つ以上の空白で区切る
field := strings.Fields(string(b))

// BeginColumnのIndexを得る
var idx string
for _, value := range field[0] {
if value=='[' {
continue
}
if value == '-' {
break
}
idx = fmt.Sprintf("%s%c",idx,value)
}

beginsIdx, _ := strconv.Atoi(idx)


// 1 indexed => 0 indexed
return strings.Join(field[begins[beginsIdx-1]:]," "), nil
}

38 changes: 38 additions & 0 deletions cmd/CustomSource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import "testing"

func TestAllCustomSource(t *testing.T) {
t.Run("001_BuildMultipleCustomSource", func(t *testing.T) {
src := []CustomSource{
{
SubName: 'f',
Command: "echo command first",
Action: "echo action first",
},
{
SubName: 's',
Command: "echo command second",
Action: "echo action second",
},
{
SubName: 't',
Command: "echo command third",
Action: "echo action third",
},
}

actual := BuildCustomSource(src...)
expect := CustomSource{
SubName: 'm',
BeginColumn: -1,
Command: `cat <(echo command first|awk '{printf "[1-%03d] %s\n",NR,$0}'|column -t) <(echo command second|awk '{printf "[2-%03d] %s\n",NR,$0}'|column -t) <(echo command third|awk '{printf "[3-%03d] %s\n",NR,$0}'|column -t)`,
Action: " echo action first; echo action second; echo action third;",
}

if !actual.Equals(expect) {
t.Fatal(actual, "is not \n", expect)
}

})
}
24 changes: 11 additions & 13 deletions cmd/action.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"
"os/exec"
)
Expand All @@ -20,22 +19,21 @@ func NewAction(command string, dst string) CdxAction {
}
}

func (act *CdxAction) Run() error {

com := fmt.Sprintf("cd %s && %s", act.Destnation, act.Command)

command := exec.Command("bash", "-c", com)
func (act *CdxAction) Run() {
err := os.Chdir(act.Destnation)
if err != nil {
Fatal(err)
}

if b, err := command.Output(); err != nil {
return err
} else {
act.Output = b
command := exec.Command("bash","-c",act.Command)
act.Output, err = command.Output()
if err != nil {
Fatal(err)
}
return nil
}

func (act CdxAction) Print() {

os.Stderr.Write(act.Output)
os.Stderr.Close()
_:os.Stderr.Write(act.Output)
_:os.Stderr.Close()
}
4 changes: 1 addition & 3 deletions cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func TestAllAction(t *testing.T) {

t.Run("002_Run", func(t *testing.T) {
act := NewAction("echo abc", home)
if err := act.Run(); err != nil {
t.Fatal(err)
}
act.Run()
if bytes.Compare(act.Output, []byte("abc\n")) != 0 {
t.Fatal("Unexpected result : ", act.Output)
}
Expand Down
70 changes: 67 additions & 3 deletions cmd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

func getCdCommand(p string, stderr io.Writer, stdin io.Reader) (string, error) {
func getCdCommand(p string) (string, error) {
if _, err := os.Stat(p); err != nil {
if config.Make {
stdin := os.Stdin
defer stdin.Close()
stderr := os.Stderr
defer stderr.Close()

in := bufio.NewScanner(stdin)
out := bufio.NewWriter(stderr)

// --makeの質問部分
out.Write([]byte(fmt.Sprintf("%s could not found. Make?(y/n)\n>>> ", p)))
out.Flush()

Expand All @@ -34,12 +40,13 @@ func getCdCommand(p string, stderr io.Writer, stdin io.Reader) (string, error) {
}
}

// pathにスペースが入ってた時のエスケープ
p = strings.Replace(p, " ", "\\ ", -1)

AppendRecord(p, config.HistoryFile)
return constructCdCommand(p), nil
}

// 実際に実行するcdコマンドを作る
func constructCdCommand(p string) string {
base := fmt.Sprintf("%s %s", config.Command, p)
if config.NoOutput {
Expand All @@ -48,3 +55,60 @@ func constructCdCommand(p string) string {

return strings.Trim(base, "\n")
}

func GetDestination(args []string) (string,string,error) {

if !useHistory && !useBookmark && len(customSource) == 0 {
// 履歴もブックマークもCSも使わない=>普通のパス指定
p, _ := os.Getwd()
if len(args) != 0 {
p = strings.Replace(args[0], "~", os.Getenv("HOME"), 1)
p, _ = filepath.Abs(p)
}
return p, "",nil
}

// ここからCSの領域

var src []CustomSource
var begins []int
if useBookmark {
src= append(src, BookmarkCustomSource)
begins = append(begins, BookmarkCustomSource.BeginColumn)
}
if useHistory {
src = append(src, HistoryCustomSource)
begins = append(begins, HistoryCustomSource.BeginColumn)
}


m := make(map[rune]bool)

// 複数のCustomSourceをまとめる
for _, value := range []rune(customSource) {
cs, err := getCustomSource(value)
if err != nil {
Fatal(err)
}

// 重複は取り除く
if m[cs.SubName] {
continue
}

m[cs.SubName]=true
begins = append(begins, cs.BeginColumn)
src= append(src, cs)
}

// CustomSourceをBuild
c := BuildCustomSource(src...)


path, err := c.GetPathWithFinder(begins)
if err != nil {
return "","",errors.New("cdx: Finder canceled")
}

return path, c.Action,nil
}
Loading

0 comments on commit 9b61df6

Please sign in to comment.