aboutsummaryrefslogtreecommitdiff
path: root/widgets/headerlayout.go
diff options
context:
space:
mode:
authorDaniel Bridges <bridges2@gmail.com>2019-07-22 16:29:07 -0700
committerDrew DeVault <sir@cmpwn.com>2019-07-26 14:22:04 -0400
commit67fb0938a66605a0b6a837005804637b348b250d (patch)
treeb9bb363185b248ae8eed29e7b8388d8a71433d3e /widgets/headerlayout.go
parent1b673b5ea7d06ef914e9d48ff7299f8b5f2119fd (diff)
Support configurable header layout in compose widget
Diffstat (limited to 'widgets/headerlayout.go')
-rw-r--r--widgets/headerlayout.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/widgets/headerlayout.go b/widgets/headerlayout.go
new file mode 100644
index 0000000..c6e6161
--- /dev/null
+++ b/widgets/headerlayout.go
@@ -0,0 +1,41 @@
+package widgets
+
+import (
+ "git.sr.ht/~sircmpwn/aerc/lib/ui"
+ "git.sr.ht/~sircmpwn/aerc/models"
+)
+
+type HeaderLayout [][]string
+
+// forMessage returns a filtered header layout, removing rows whose headers
+// do not appear in the provided message.
+func (layout HeaderLayout) forMessage(msg *models.MessageInfo) HeaderLayout {
+ headers := msg.RFC822Headers
+ result := make(HeaderLayout, 0, len(layout))
+ for _, row := range layout {
+ // To preserve layout alignment, only hide rows if all columns are empty
+ for _, col := range row {
+ if headers.Get(col) != "" {
+ result = append(result, row)
+ break
+ }
+ }
+ }
+ return result
+}
+
+// grid builds a ui grid, populating each cell by calling a callback function
+// with the current header string.
+func (layout HeaderLayout) grid(cb func(string) ui.Drawable) (grid *ui.Grid, height int) {
+ rowCount := len(layout) + 1 // extra row for spacer
+ grid = ui.MakeGrid(rowCount, 1, ui.SIZE_EXACT, ui.SIZE_WEIGHT)
+ for i, cols := range layout {
+ r := ui.MakeGrid(1, len(cols), ui.SIZE_EXACT, ui.SIZE_WEIGHT)
+ for j, col := range cols {
+ r.AddChild(cb(col)).At(0, j)
+ }
+ grid.AddChild(r).At(i, 0)
+ }
+ grid.AddChild(ui.NewFill(' ')).At(rowCount-1, 0)
+ return grid, rowCount
+}