Exemple #1
0
func (p *Filter) Push(k string) {
	partResult := make(ResultItemSlice, 0)
	s := "/"
	if runtime.GOOS == "windows" {
		s = "\\"
	}
	for _, v := range p.Results {
		if v.Index >= len(v.Line.Cs) {
			continue
		}
		l := v.Line.GetString()
		if config.GetConfig().FuzzyFindMode == config.NameMode && v.Index == 0 {
			li := strings.LastIndex(l, s)
			if li != -1 && li <= len(l)-1 {
				if li == len(l)-1 {
					v.Index = li
				} else {
					v.Index = li + 1
				}
			}
		}
		k = strings.ToLower(k)
		l = strings.ToLower(l[v.Index:])
		if strings.Contains(l, k) {
			v.Index += strings.Index(l, k) + len(k)
			partResult = append(partResult, v)
		}
	}
	if config.GetConfig().DirectoryMode == config.AllMode {
		sort.Sort(partResult)
	}
	p.Results = partResult
}
Exemple #2
0
func GetDirs() []string {
	var retDirs []string
	var dirs []string
	if config.GetConfig().DirectoryMode == config.HisMode {
		GetHis(&dirs)
		retDirs = append(retDirs, dirs...)
	} else if config.GetConfig().DirectoryMode == config.AllMode {
		for _, v := range config.GetConfig().ContainDirs {
			dirs = make([]string, 0)
			GetAllDir(v, &dirs)
			retDirs = append(retDirs, dirs...)
		}
	}
	return retDirs
}
Exemple #3
0
func RmHis(dir string) {
	if dir == "" {
		return
	}
	var dirs []string
	content, _ := ioutil.ReadFile(config.GetConfig().Home + "/.dacecd/.dacecdhis")
	json.Unmarshal(content, &dirs)
	var tmpDirs []string
	for _, v := range dirs {
		if v != dir && v != "" {
			tmpDirs = append(tmpDirs, v)
		}
	}
	if len(tmpDirs) > config.GetConfig().HisCount {
		tmpDirs = tmpDirs[0:config.GetConfig().HisCount]
	}
	w, _ := json.Marshal(tmpDirs)
	ioutil.WriteFile(config.GetConfig().Home+"/.dacecd/.dacecdhis", w, os.ModePerm)
}
Exemple #4
0
func GetHis(dirs *[]string) {
	tmpDirs := make([]string, 0)
	content, _ := ioutil.ReadFile(config.GetConfig().Home + "/.dacecd/.dacecdhis")
	json.Unmarshal(content, &tmpDirs)
	curDir, _ := os.Getwd()
	for _, v := range tmpDirs {
		if v != curDir {
			*dirs = append(*dirs, v)
		}
	}
}
Exemple #5
0
func GetAllDir(root string, dirs *[]string) {
	if root[0:1] == "~" {
		root = strings.Replace(root, "~", config.GetConfig().Home, 1)
	}
	ds, err := ioutil.ReadDir(root)
	if err != nil {
		return
	}
	s := "/"
	if runtime.GOOS == "windows" {
		s = "\\"
	}
	for _, v := range ds {
		if v.Name()[0] == '.' || v.IsDir() == false {
			continue
		}
		*dirs = append(*dirs, root+s+v.Name())
		GetAllDir(root+s+v.Name(), dirs)
	}
}
Exemple #6
0
func main() {
	if len(os.Args) > 1 {
		cd.PushHis(os.Args[1])
		return
	}
	err := termbox.Init()
	if err != nil {
		return
	}
	defer termbox.Close()

	home := config.GetConfig().Home

	os.Mkdir(home+"/.dacecd", os.ModeDir|os.ModePerm)

	w, h := termbox.Size()

	var inputPanel panel.Panel
	inputPanel.Init(0, 0, w, 2, termbox.ColorWhite, termbox.ColorBlack,
		panel.InputType, 0, 0)

	var outputPanel panel.Panel
	outputPanel.Init(0, 2, w, h-4, termbox.ColorWhite, termbox.ColorBlack,
		panel.OutputType, 0, 0)
	dirs := cd.GetDirs()
	outputPanel.InitBuffers(dirs)

	var statePanel panel.Panel
	statePanel.Init(0, h-2, w, 2, termbox.ColorWhite, termbox.ColorBlack,
		panel.OutputType, 0, 0)
	statePanel.InitBuffer(config.GetStateLine())

	inputPanel.Draw()
	outputPanel.Draw()
	statePanel.Draw()
	termbox.Flush()

mainloop:
	for {
		switch ev := termbox.PollEvent(); ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc, termbox.KeyCtrlC:
				ioutil.WriteFile(home+"/.dacecd/command.sh", []byte("cd ."), os.ModePerm)
				break mainloop
			case termbox.KeyArrowDown, termbox.KeyCtrlJ, termbox.KeyCtrlN:
				outputPanel.Down()
			case termbox.KeyCtrlR:
				l := outputPanel.GetSelectLine()
				outputPanel.DelSelectLine()
				cd.RmHis(l.GetString())
			case termbox.KeyCtrlX:
				if config.GetConfig().DirectoryMode == config.HisMode {
					config.GetConfig().DirectoryMode = config.AllMode
				} else if config.GetConfig().DirectoryMode == config.AllMode {
					config.GetConfig().DirectoryMode = config.HisMode
				}
				dirs = cd.GetDirs()
				outputPanel.InitBuffers(dirs)
				for _, v := range inputPanel.GetSelectLine().Cs {
					outputPanel.FilterPush(string(v.Ch))
				}
			case termbox.KeyCtrlD:
				if config.GetConfig().FuzzyFindMode == config.NameMode {
					config.GetConfig().FuzzyFindMode = config.PathMode
				} else if config.GetConfig().FuzzyFindMode == config.PathMode {
					config.GetConfig().FuzzyFindMode = config.NameMode
				}
				dirs = cd.GetDirs()
				outputPanel.InitBuffers(dirs)
				for _, v := range inputPanel.GetSelectLine().Cs {
					outputPanel.FilterPush(string(v.Ch))
				}
			case termbox.KeyArrowUp, termbox.KeyCtrlK, termbox.KeyCtrlP:
				outputPanel.Up()
			case termbox.KeyBackspace:
				inputPanel.Pop()
				outputPanel.InitFilter()
				for _, v := range inputPanel.GetSelectLine().Cs {
					outputPanel.FilterPush(string(v.Ch))
				}
			case termbox.KeyEnter:
				sl := outputPanel.GetSelectLine()
				godir := sl.GetString()
				if runtime.GOOS == "windows" {
					cmd := exec.Command("explorer.exe", "/root,"+godir)
					cmd.Run()
				} else {
					ioutil.WriteFile(home+"/.dacecd/command.sh", []byte("cd "+godir), os.ModePerm)
				}
				cd.PushHis(godir)
				break mainloop
			default:
				if ev.Ch != 0 {
					inputPanel.Push(ev.Ch)
					outputPanel.FilterPush(string(ev.Ch))
				}
			}
		}
		termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
		inputPanel.Draw()
		outputPanel.Draw()
		statePanel.InitBuffer(config.GetStateLine())
		statePanel.Draw()
		termbox.Flush()
	}
}