| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"os/exec" |
| 8 |
"path/filepath" |
| 9 |
"slices" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/lithammer/fuzzysearch/fuzzy" |
| 14 |
|
| 15 |
"git.sr.ht/~rjarry/aerc/app" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib" |
| 17 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 18 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 19 |
"git.sr.ht/~rjarry/aerc/models" |
| 20 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 21 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 22 |
"go.rockorager.dev/vaxis" |
| 23 |
) |
| 24 |
|
| 25 |
// QuickTerm is an ephemeral terminal for running a single command and quitting. |
| 26 |
func QuickTerm(args []string, stdin io.Reader, silent bool) (*app.Terminal, error) { |
| 27 |
cmd := exec.Command(args[0], args[1:]...) |
| 28 |
pipe, err := cmd.StdinPipe() |
| 29 |
if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
|
| 33 |
term, err := app.NewTerminal(cmd) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
|
| 38 |
term.OnClose = func(err error) { |
| 39 |
if err != nil { |
| 40 |
app.PushError(err.Error()) |
| 41 |
// remove the tab on error, otherwise it gets stuck |
| 42 |
app.RemoveTab(term, false) |
| 43 |
return |
| 44 |
} |
| 45 |
if silent { |
| 46 |
app.RemoveTab(term, true) |
| 47 |
} else { |
| 48 |
app.PushStatus("Process complete, press any key to close.", |
| 49 |
10*time.Second) |
| 50 |
term.OnEvent = func(event vaxis.Event) bool { |
| 51 |
if _, ok := event.(vaxis.Key); ok { |
| 52 |
app.RemoveTab(term, true) |
| 53 |
return true |
| 54 |
} else { |
| 55 |
return false |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
term.OnStart = func() { |
| 62 |
go func() { |
| 63 |
defer log.PanicHandler() |
| 64 |
defer pipe.Close() |
| 65 |
|
| 66 |
if _, err := io.Copy(pipe, stdin); err != nil { |
| 67 |
app.PushError(err.Error()) |
| 68 |
} |
| 69 |
}() |
| 70 |
} |
| 71 |
|
| 72 |
return term, nil |
| 73 |
} |
| 74 |
|
| 75 |
// CompletePath provides filesystem completions given a starting path. |
| 76 |
func CompletePath(path string, onlyDirs bool) []string { |
| 77 |
return completePath(path, onlyDirs, app.SelectedAccountUiConfig().FuzzyComplete) |
| 78 |
} |
| 79 |
|
| 80 |
func completePath(path string, onlyDirs bool, fuzzyComplete bool) []string { |
| 81 |
if path == ".." || strings.HasSuffix(path, "/..") { |
| 82 |
return []string{path + "/"} |
| 83 |
} |
| 84 |
if path == "~" { |
| 85 |
path = xdg.HomeDir() + "/" |
| 86 |
} else if strings.HasPrefix(path, "~/") { |
| 87 |
path = xdg.HomeDir() + strings.TrimPrefix(path, "~") |
| 88 |
} |
| 89 |
includeHidden := path == "." |
| 90 |
if i := strings.LastIndex(path, "/"); i != -1 && i < len(path)-1 { |
| 91 |
includeHidden = strings.HasPrefix(path[i+1:], ".") |
| 92 |
} |
| 93 |
|
| 94 |
const currentDir = "." |
| 95 |
dir, search := filepath.Split(path) |
| 96 |
// for `file` case dir will be `` which does not work in listDir |
| 97 |
if dir == "" { |
| 98 |
dir = currentDir |
| 99 |
} |
| 100 |
|
| 101 |
entries := listDir(dir, includeHidden) |
| 102 |
filteredEntries := make([]string, 0, len(entries)) |
| 103 |
for _, m := range entries { |
| 104 |
testM := m |
| 105 |
if dir != currentDir { |
| 106 |
testM = dir + m |
| 107 |
} |
| 108 |
if isDir(testM) { |
| 109 |
m += "/" |
| 110 |
} else if onlyDirs { |
| 111 |
continue |
| 112 |
} |
| 113 |
filteredEntries = append(filteredEntries, m) |
| 114 |
} |
| 115 |
|
| 116 |
slices.Sort(filteredEntries) |
| 117 |
results := filterList( |
| 118 |
filteredEntries, |
| 119 |
search, |
| 120 |
func(s string) string { |
| 121 |
if dir != currentDir { |
| 122 |
s = dir + s |
| 123 |
} |
| 124 |
return opt.QuoteArg(xdg.TildeHome(s)) |
| 125 |
}, |
| 126 |
fuzzyComplete, |
| 127 |
) |
| 128 |
|
| 129 |
return results |
| 130 |
} |
| 131 |
|
| 132 |
func isDir(path string) bool { |
| 133 |
info, err := os.Stat(path) |
| 134 |
if err != nil { |
| 135 |
return false |
| 136 |
} |
| 137 |
|
| 138 |
return info.IsDir() |
| 139 |
} |
| 140 |
|
| 141 |
// return all filenames in a directory, optionally including hidden files |
| 142 |
func listDir(path string, hidden bool) []string { |
| 143 |
f, err := os.Open(path) |
| 144 |
if err != nil { |
| 145 |
return []string{} |
| 146 |
} |
| 147 |
|
| 148 |
files, err := f.Readdirnames(-1) // read all dir names |
| 149 |
if err != nil { |
| 150 |
return []string{} |
| 151 |
} |
| 152 |
|
| 153 |
if hidden { |
| 154 |
return files |
| 155 |
} |
| 156 |
|
| 157 |
var filtered []string |
| 158 |
for _, g := range files { |
| 159 |
if !strings.HasPrefix(g, ".") { |
| 160 |
filtered = append(filtered, g) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return filtered |
| 165 |
} |
| 166 |
|
| 167 |
// MarkedOrSelected returns either all marked messages if any are marked or the |
| 168 |
// selected message instead |
| 169 |
func MarkedOrSelected(pm app.ProvidesMessages) ([]models.UID, error) { |
| 170 |
// marked has priority over the selected message |
| 171 |
marked, err := pm.MarkedMessages() |
| 172 |
if err != nil { |
| 173 |
return nil, err |
| 174 |
} |
| 175 |
if len(marked) > 0 { |
| 176 |
marked = expandFoldedThreads(pm, marked) |
| 177 |
return marked, nil |
| 178 |
} |
| 179 |
msg, err := pm.SelectedMessage() |
| 180 |
if err != nil { |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
return expandFoldedThreads(pm, []models.UID{msg.Uid}), nil |
| 184 |
} |
| 185 |
|
| 186 |
func expandFoldedThreads(pm app.ProvidesMessages, uids []models.UID) []models.UID { |
| 187 |
store := pm.Store() |
| 188 |
if store == nil { |
| 189 |
return uids |
| 190 |
} |
| 191 |
expanded := make([]models.UID, len(uids)) |
| 192 |
copy(expanded, uids) |
| 193 |
for _, uid := range uids { |
| 194 |
thread, err := store.Thread(uid) |
| 195 |
if err != nil { |
| 196 |
continue |
| 197 |
} |
| 198 |
if thread != nil && thread.FirstChild != nil && thread.FirstChild.Hidden > 0 { |
| 199 |
_ = thread.Walk(func(t *types.Thread, _ int, __ error) error { |
| 200 |
if t.Uid != uid { |
| 201 |
expanded = append(expanded, t.Uid) |
| 202 |
} |
| 203 |
return nil |
| 204 |
}) |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
if len(uids) != len(expanded) { |
| 209 |
log.Debugf("expand folded threads: %v -> %v\n", uids, expanded) |
| 210 |
} |
| 211 |
return expanded |
| 212 |
} |
| 213 |
|
| 214 |
// UidsFromMessageInfos extracts a uid slice from a slice of MessageInfos |
| 215 |
func UidsFromMessageInfos(msgs []*models.MessageInfo) []models.UID { |
| 216 |
uids := make([]models.UID, len(msgs)) |
| 217 |
i := 0 |
| 218 |
for _, msg := range msgs { |
| 219 |
uids[i] = msg.Uid |
| 220 |
i++ |
| 221 |
} |
| 222 |
return uids |
| 223 |
} |
| 224 |
|
| 225 |
func MsgInfoFromUids(store *lib.MessageStore, uids []models.UID, statusInfo func(string)) ([]*models.MessageInfo, error) { |
| 226 |
infos := make([]*models.MessageInfo, len(uids)) |
| 227 |
needHeaders := make([]models.UID, 0) |
| 228 |
for i, uid := range uids { |
| 229 |
var ok bool |
| 230 |
infos[i], ok = store.Messages[uid] |
| 231 |
if !ok { |
| 232 |
return nil, fmt.Errorf("uid not found") |
| 233 |
} |
| 234 |
if infos[i] == nil { |
| 235 |
needHeaders = append(needHeaders, uid) |
| 236 |
} |
| 237 |
} |
| 238 |
if len(needHeaders) > 0 { |
| 239 |
store.FetchHeaders(needHeaders, func(msg types.WorkerMessage) { |
| 240 |
var info string |
| 241 |
switch m := msg.(type) { |
| 242 |
case *types.Done: |
| 243 |
info = "All headers fetched. Please repeat command." |
| 244 |
case *types.Error: |
| 245 |
info = fmt.Sprintf("Encountered error while fetching headers: %v", m.Error) |
| 246 |
} |
| 247 |
if statusInfo != nil { |
| 248 |
statusInfo(info) |
| 249 |
} |
| 250 |
}) |
| 251 |
return nil, fmt.Errorf("Fetching missing message headers. Please wait.") |
| 252 |
} |
| 253 |
return infos, nil |
| 254 |
} |
| 255 |
|
| 256 |
func QuoteSpace(s string) string { |
| 257 |
s, desc, found := strings.Cut(s, "\n") |
| 258 |
s = opt.QuoteArg(s) + " " |
| 259 |
if found { |
| 260 |
s += "\n" + desc |
| 261 |
} |
| 262 |
return s |
| 263 |
} |
| 264 |
|
| 265 |
// FilterList takes a list of valid completions and filters it, either |
| 266 |
// by case smart prefix, or by fuzzy matching |
| 267 |
// An optional post processing function can be passed to prepend, append or |
| 268 |
// quote each value. |
| 269 |
func FilterList( |
| 270 |
valid []string, |
| 271 |
search string, |
| 272 |
postProc func(string) string, |
| 273 |
) []string { |
| 274 |
return filterList(valid, search, postProc, app.SelectedAccountUiConfig().FuzzyComplete) |
| 275 |
} |
| 276 |
|
| 277 |
func filterList( |
| 278 |
valid []string, |
| 279 |
search string, |
| 280 |
postProc func(string) string, |
| 281 |
fuzzyComplete bool, |
| 282 |
) []string { |
| 283 |
if postProc == nil { |
| 284 |
postProc = QuoteSpace |
| 285 |
} |
| 286 |
out := make([]string, 0, len(valid)) |
| 287 |
if fuzzyComplete { |
| 288 |
nonexact := make([]string, 0, len(valid)) |
| 289 |
for _, e := range valid { |
| 290 |
if strings.Contains(strings.ToLower(e), strings.ToLower(search)) { |
| 291 |
out = append(out, postProc(e)) |
| 292 |
} else { |
| 293 |
nonexact = append(nonexact, e) |
| 294 |
} |
| 295 |
} |
| 296 |
matches := fuzzy.RankFindNormalizedFold(search, nonexact) |
| 297 |
for _, v := range matches { |
| 298 |
out = append(out, postProc(v.Target)) |
| 299 |
} |
| 300 |
} else { |
| 301 |
for _, v := range valid { |
| 302 |
if hasCaseSmartPrefix(v, search) { |
| 303 |
out = append(out, postProc(v)) |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
return out |
| 308 |
} |