~abyxcos/hatemail

hatemail/hatemail.go -rw-r--r-- 3.8 KiB
1b76ab85abyxcos Use a multi-line string for the help message. 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main

import (
	"log"
	"strconv"
	"strings"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

var (
	app         *tview.Application
	pages       *tview.Pages
	folderList  *tview.List
	messageList *tview.List
	emailHeader *tview.Box
	emailBody   *tview.TextView
)

func helpMessage() string {
	return `Keys:
 Global:
  q	- quit
  ?	- this help message

 Lists:
  j/k	- up/down one message
  g/G	- top/bottom of list
  h/l	- change panes
  o	- open an email

 Email:
  esc	- close an email
`

}

func eventHandler(ev *tcell.EventKey) *tcell.EventKey {
	// Some global keys that function in every context
	// Just stop if we find a CtrlC
	if ev.Key() == tcell.KeyCtrlC {
		app.Stop()
		return nil
	}

	frontPage, _ := pages.GetFrontPage()
	switch frontPage {
	case "index":
		switch {
		case ev.Rune() == 'q':
			app.Stop()
		case ev.Rune() == '?':
			pages.ShowPage("help")

		// List navigation
		case ev.Rune() == 'g':
			app.GetFocus().(*tview.List).SetCurrentItem(0)
		case ev.Rune() == 'G':
			app.GetFocus().(*tview.List).SetCurrentItem(-1)
		case ev.Rune() == 'j':
			app.GetFocus().(*tview.List).SetCurrentItem(app.GetFocus().(*tview.List).GetCurrentItem() + 1)
		case ev.Rune() == 'k':
			app.GetFocus().(*tview.List).SetCurrentItem(app.GetFocus().(*tview.List).GetCurrentItem() - 1)

		case ev.Rune() == 'h':
			app.SetFocus(folderList)
		case ev.Rune() == 'l':
			app.SetFocus(messageList)

		case ev.Rune() == 'o':
			emailHeader.SetTitle(strconv.Itoa(messageList.GetCurrentItem()) + "/" + strconv.Itoa(messageList.GetItemCount()))
			body, _ := messageList.GetItemText(0)
			emailBody.SetText(body)
			pages.ShowPage("email")
		}
	case "email":
		switch {
		case ev.Key() == tcell.KeyEsc:
			pages.HidePage("email")
		case ev.Rune() == '?':
			pages.ShowPage("help")
		}
	case "help":
		switch {
		case ev.Key() == tcell.KeyEsc || ev.Rune() == '?' || ev.Rune() == 'q':
			pages.HidePage("help")
		}
	}

	return nil
}

func main() {
	var err error
	app = tview.NewApplication()

	ipsum := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
	folders := strings.Split(ipsum, " ")

	pages = tview.NewPages()

	// Index view
	grid := tview.NewGrid().
		SetRows(0).
		SetColumns(20, 0).
		SetBorders(false)

	folderList = tview.NewList().
		ShowSecondaryText(false).
		SetHighlightFullLine(true).
		SetWrapAround(false)
	grid.AddItem(folderList, 0, 0, 1, 1, 0, 0, false)
	for _, folder := range folders {
		folderList.AddItem(folder, "", 0, nil)
	}

	messageList = tview.NewList().
		ShowSecondaryText(false).
		SetHighlightFullLine(true).
		SetWrapAround(false)
	grid.AddItem(messageList, 0, 1, 1, 1, 0, 0, true)
	for i := 0; i < 50; i++ {
		messageList.AddItem(strconv.Itoa(i)+" "+ipsum, "", 0, nil)
	}

	// Email view
	email := tview.NewGrid().
		SetRows(5, 0).
		SetColumns(0).
		SetBorders(false)

	emailHeader = tview.NewBox().
		SetBorder(true)
	email.AddItem(emailHeader, 0, 0, 1, 1, 0, 0, false)

	emailBody = tview.NewTextView()
	email.AddItem(emailBody, 1, 0, 1, 1, 0, 0, true)

	// Help view
	help := tview.NewGrid().
		SetRows(0).
		SetColumns(0).
		SetBorders(true)
	helpText := tview.NewTextView().
		SetText(helpMessage())
	help.AddItem(helpText, 0, 0, 1, 1, 0, 0, true)

	// Piece the app together
	pages.AddPage("index", grid, true, true)
	pages.AddPage("email", email, true, false)
	pages.AddPage("help", help, true, false)

	app.SetInputCapture(eventHandler)
	err = app.SetRoot(pages, true).
		SetFocus(pages).
		Run()
	if err != nil {
		log.Panic(err)
	}
}