aerc

Working clone of aerc-mail.org
git clone git://git.danielmoch.com/aerc.git
Log | Files | Refs | README | LICENSE

aerc.go (2364B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"io"
      6 	"io/ioutil"
      7 	"log"
      8 	"os"
      9 	"time"
     10 
     11 	"github.com/mattn/go-isatty"
     12 
     13 	"git.sr.ht/~sircmpwn/aerc/commands"
     14 	"git.sr.ht/~sircmpwn/aerc/commands/account"
     15 	"git.sr.ht/~sircmpwn/aerc/commands/compose"
     16 	"git.sr.ht/~sircmpwn/aerc/commands/msg"
     17 	"git.sr.ht/~sircmpwn/aerc/commands/msgview"
     18 	"git.sr.ht/~sircmpwn/aerc/commands/terminal"
     19 	"git.sr.ht/~sircmpwn/aerc/config"
     20 	libui "git.sr.ht/~sircmpwn/aerc/lib/ui"
     21 	"git.sr.ht/~sircmpwn/aerc/widgets"
     22 )
     23 
     24 func getCommands(selected libui.Drawable) []*commands.Commands {
     25 	switch selected.(type) {
     26 	case *widgets.AccountView:
     27 		return []*commands.Commands{
     28 			account.AccountCommands,
     29 			msg.MessageCommands,
     30 			commands.GlobalCommands,
     31 		}
     32 	case *widgets.Composer:
     33 		return []*commands.Commands{
     34 			compose.ComposeCommands,
     35 			commands.GlobalCommands,
     36 		}
     37 	case *widgets.MessageViewer:
     38 		return []*commands.Commands{
     39 			msgview.MessageViewCommands,
     40 			msg.MessageCommands,
     41 			commands.GlobalCommands,
     42 		}
     43 	case *widgets.Terminal:
     44 		return []*commands.Commands{
     45 			terminal.TerminalCommands,
     46 			commands.GlobalCommands,
     47 		}
     48 	default:
     49 		return []*commands.Commands{commands.GlobalCommands}
     50 	}
     51 }
     52 
     53 var (
     54 	Prefix   string
     55 	ShareDir string
     56 )
     57 
     58 func main() {
     59 	var (
     60 		logOut io.Writer
     61 		logger *log.Logger
     62 	)
     63 	if !isatty.IsTerminal(os.Stdout.Fd()) {
     64 		logOut = os.Stdout
     65 	} else {
     66 		logOut = ioutil.Discard
     67 	}
     68 	logger = log.New(logOut, "", log.LstdFlags)
     69 	logger.Println("Starting up aerc")
     70 
     71 	conf, err := config.LoadConfig(nil, ShareDir)
     72 	if err != nil {
     73 		fmt.Printf("Failed to load config: %v\n", err)
     74 		os.Exit(1)
     75 	}
     76 
     77 	var (
     78 		aerc *widgets.Aerc
     79 		ui   *libui.UI
     80 	)
     81 	aerc = widgets.NewAerc(conf, logger, func(cmd string) error {
     82 		cmds := getCommands(aerc.SelectedTab())
     83 		for i, set := range cmds {
     84 			err := set.ExecuteCommand(aerc, cmd)
     85 			if _, ok := err.(commands.NoSuchCommand); ok {
     86 				if i == len(cmds)-1 {
     87 					return err
     88 				} else {
     89 					continue
     90 				}
     91 			} else if _, ok := err.(commands.ErrorExit); ok {
     92 				ui.Exit()
     93 				return nil
     94 			} else if err != nil {
     95 				return err
     96 			} else {
     97 				break
     98 			}
     99 		}
    100 		return nil
    101 	})
    102 
    103 	ui, err = libui.Initialize(conf, aerc)
    104 	if err != nil {
    105 		panic(err)
    106 	}
    107 	defer ui.Close()
    108 
    109 	for !ui.ShouldExit() {
    110 		for aerc.Tick() {
    111 			// Continue updating our internal state
    112 		}
    113 		if !ui.Tick() {
    114 			// ~60 FPS
    115 			time.Sleep(16 * time.Millisecond)
    116 		}
    117 	}
    118 }