Skip to content

Commit

Permalink
dirtree: add custom virtual directory role
Browse files Browse the repository at this point in the history
Add a new directory role to indicate virtual nodes in the directory
tree. This allows to style the virtual nodes differently and apply
different behaviors in some commands (i.e. rmdir).

Signed-off-by: Koni Marti <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
konimarti authored and rjarry committed Nov 22, 2023
1 parent 85d0936 commit 30d28d0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
24 changes: 24 additions & 0 deletions app/dirtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ func (dt *DirectoryTree) OnVirtualNode(cb func()) {
dt.virtualCb = cb
}

func (dt *DirectoryTree) Selected() string {
if dt.listIdx < 0 || dt.listIdx >= len(dt.list) {
return dt.DirectoryList.Selected()
}
node := dt.list[dt.listIdx]
sep := dt.DirectoryList.worker.PathSeparator()
elems := strings.Split(dt.treeDirs[getAnyUid(node)], sep)
n := countLevels(node)
if n < 0 || n >= len(elems) {
return ""
}
return strings.Join(elems[:(n+1)], sep)
}

func (dt *DirectoryTree) SelectedDirectory() *models.Directory {
if dt.virtual {
return &models.Directory{
Name: dt.Selected(),
Role: models.VirtualRole,
}
}
return dt.DirectoryList.SelectedDirectory()
}

func (dt *DirectoryTree) ClearList() {
dt.list = make([]*types.Thread, 0)
}
Expand Down
7 changes: 7 additions & 0 deletions commands/account/rmdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)

Expand All @@ -31,6 +32,12 @@ func (r RemoveDir) Execute(args []string) error {
return errors.New("Refusing to remove non-empty directory; use -f")
}

if d := acct.Directories().SelectedDirectory(); d != nil {
if d.Role == models.VirtualRole {
return errors.New("Cannot remove a virtual node")
}
}

curDir := acct.SelectedDirectory()
var newDir string
dirFound := false
Expand Down
12 changes: 10 additions & 2 deletions doc/aerc-templates.7.scd
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,16 @@ available always.
{{.RUE}}
```

IANA role of the mailbox, converted to lowercase. aerc uses one custom
role: 'query', which is given to mailboxes from a notmuch query-map
IANA role of the mailbox, converted to lowercase:

```
{{.Role}}
```

*aerc* implements two additional custom roles: A 'query' role is given
to folders from a notmuch query-map
and 'virtual' indicates a virtual node in the directory tree listing:

```
{{if eq .Role "query"}}{{...}}{{else}}{{...}}{{end}}
```
Expand Down
2 changes: 2 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const (
TrashRole Role = "trash"
// Custom aerc roles
QueryRole Role = "query"
// virtual node created by the directory tree
VirtualRole Role = "virtual"
)

type Directory struct {
Expand Down

0 comments on commit 30d28d0

Please sign in to comment.