github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/edit/location.go (about) 1 package edit 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/elves/elvish/parse" 8 "github.com/elves/elvish/store" 9 ) 10 11 // Location mode. 12 13 type location struct { 14 listing 15 store *store.Store 16 candidates []store.Dir 17 } 18 19 func (loc *location) Len() int { 20 return len(loc.candidates) 21 } 22 23 func (loc *location) Show(i, width int) styled { 24 cand := loc.candidates[i] 25 return unstyled(TrimWcWidth(fmt.Sprintf("%4.0f %s", cand.Score, parse.Quote(cand.Path)), width)) 26 } 27 28 func (loc *location) Filter(filter string) int { 29 dirs, err := loc.store.FindDirsLoose(filter) 30 if err != nil { 31 loc.candidates = nil 32 // XXX Should report error 33 // ed.notify("find directories: %v", err) 34 return -1 35 } 36 loc.candidates = dirs 37 if len(dirs) == 0 { 38 return -1 39 } 40 return 0 41 } 42 43 func (loc *location) Accept(i int, ed *Editor) { 44 dir := loc.candidates[i].Path 45 err := os.Chdir(dir) 46 if err == nil { 47 store := ed.store 48 go func() { 49 store.Waits.Add(1) 50 // XXX Error ignored. 51 store.AddDir(dir, 1) 52 store.Waits.Done() 53 Logger.Println("added dir to store:", dir) 54 }() 55 } else { 56 ed.notify("%v", err) 57 } 58 ed.mode = &ed.insert 59 } 60 61 func (loc *location) ModeTitle(i int) string { 62 return " LOCATION " 63 } 64 65 func startLocation(ed *Editor) { 66 if ed.store == nil { 67 ed.notify("%v", ErrStoreOffline) 68 return 69 } 70 loc := &location{store: ed.store} 71 loc.listing = newListing(modeLocation, loc) 72 73 ed.location = loc 74 ed.mode = ed.location 75 }