main.go (1522B)
1 package main 2 3 import ( 4 "context" 5 "os" 6 "os/signal" 7 "path/filepath" 8 "syscall" 9 10 "code.dwrz.net/src/pkg/editor" 11 "code.dwrz.net/src/pkg/log" 12 "code.dwrz.net/src/pkg/terminal" 13 ) 14 15 func main() { 16 var l = log.New(os.Stderr) 17 18 // Setup the main context. 19 ctx, cancel := context.WithCancel(context.Background()) 20 21 // Setup workspace and log file. 22 cdir, err := os.UserCacheDir() 23 if err != nil { 24 l.Error.Fatalf( 25 "failed to determine user cache directory: %v", err, 26 ) 27 } 28 wdir := filepath.Join(cdir, "wen") 29 30 if err := os.MkdirAll(wdir, os.ModeDir|0700); err != nil { 31 l.Error.Fatalf("failed to create tmp dir: %v", err) 32 } 33 34 f, err := os.Create(wdir + "/log") 35 if err != nil { 36 l.Error.Fatalf("failed to create log file: %v", err) 37 } 38 defer f.Close() 39 40 // Retrieve terminal info. 41 t, err := terminal.New(os.Stdin.Fd()) 42 if err != nil { 43 l.Error.Fatalf("failed to get terminal attributes: %v", err) 44 } 45 46 // Create the editor. 47 editor, err := editor.New(editor.Parameters{ 48 In: os.Stdin, 49 Log: log.New(f), 50 Out: os.Stdout, 51 TempDir: wdir, 52 Terminal: t, 53 }) 54 if err != nil { 55 l.Error.Fatalf("failed to create editor: %v", err) 56 } 57 58 // TODO: refactor; handle sigwinch. 59 go func() { 60 var signals = make(chan os.Signal, 1) 61 signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT) 62 63 // Block until we receive a signal. 64 s := <-signals 65 l.Debug.Printf("received signal: %s", s) 66 67 cancel() 68 }() 69 70 // Run the editor. 71 if err := editor.Run(ctx, os.Args[1:]); err != nil { 72 l.Error.Fatal(err) 73 } 74 }