~abyxcos/hatemail

f7d097156279e4768aeb1794298e37d7062b788f — abyxcos 3 years ago 511a911
Key events are now page aware. They are much more sensible about when they try to do things. State is much cleaner.
1 files changed, 47 insertions(+), 37 deletions(-)

M hatemail.go
M hatemail.go => hatemail.go +47 -37
@@ 19,50 19,60 @@ var (
)

func eventHandler(ev *tcell.EventKey) *tcell.EventKey {
	switch {
	// Global stuff
	// XXX: 'q' should only quit in list views. CtrlC should be global.
	case ev.Key() == tcell.KeyCtrlC || ev.Rune() == 'q':
	// Some global keys that function in every context
	// Just stop if we find a CtrlC
	if ev.Key() == tcell.KeyCtrlC {
		app.Stop()
		return nil
	}

	// 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 ev.Key() == tcell.KeyEsc:
		pages.HidePage("email")

	case ev.Rune() == '?':
		if helpToggleTracker < 0 {
			pages.ShowPage("help")
		} else {
			pages.HidePage("help")
		}

		helpToggleTracker = -helpToggleTracker
	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:
				pages.HidePage("help")
			}
	}

	return nil
}

var helpToggleTracker = -1

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