github.com/aretext/aretext@v1.3.0/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"io"
     8  	"log"
     9  	"os"
    10  	"runtime/debug"
    11  	"runtime/pprof"
    12  	"time"
    13  
    14  	"github.com/gdamore/tcell/v2"
    15  
    16  	"github.com/aretext/aretext/app"
    17  )
    18  
    19  // This variable is set automatically as part of the release process.
    20  // Please do NOT modify the following line.
    21  var version = "dev"
    22  
    23  // These variables are initialized from runtime/debug.BuildInfo.
    24  var (
    25  	vcsRevision string
    26  	vcsTime     time.Time
    27  	vcsModified bool
    28  	goVersion   string
    29  )
    30  
    31  func init() {
    32  	buildInfo, ok := debug.ReadBuildInfo()
    33  	if !ok {
    34  		return
    35  	}
    36  
    37  	goVersion = buildInfo.GoVersion
    38  
    39  	for _, setting := range buildInfo.Settings {
    40  		switch setting.Key {
    41  		case "vcs.revision":
    42  			vcsRevision = setting.Value
    43  		case "vcs.time":
    44  			vcsTime, _ = time.Parse(time.RFC3339, setting.Value)
    45  		case "vcs.modified":
    46  			vcsModified = (setting.Value == "true")
    47  		}
    48  	}
    49  }
    50  
    51  var line = flag.Int("line", 1, "line number to view after opening the document")
    52  var logpath = flag.String("log", "", "log to file")
    53  var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
    54  var editconfig = flag.Bool("editconfig", false, "open the aretext configuration file")
    55  var noconfig = flag.Bool("noconfig", false, "force default configuration")
    56  var versionFlag = flag.Bool("version", false, "print version")
    57  
    58  func main() {
    59  	flag.Usage = printUsage
    60  	flag.Parse()
    61  
    62  	if *versionFlag {
    63  		fmt.Printf("%s @ %s\n", version, vcsRevision)
    64  		return
    65  	}
    66  
    67  	log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile)
    68  	if *logpath != "" {
    69  		logFile, err := os.Create(*logpath)
    70  		if err != nil {
    71  			exitWithError(err)
    72  		}
    73  		defer logFile.Close()
    74  		log.SetOutput(logFile)
    75  	} else {
    76  		log.SetOutput(io.Discard)
    77  	}
    78  
    79  	if *cpuprofile != "" {
    80  		f, err := os.Create(*cpuprofile)
    81  		if err != nil {
    82  			exitWithError(err)
    83  		}
    84  		pprof.StartCPUProfile(f)
    85  		defer pprof.StopCPUProfile()
    86  	}
    87  
    88  	var lineNum uint64
    89  	if *line < 1 {
    90  		exitWithError(errors.New("line number must be at least 1"))
    91  	} else {
    92  		lineNum = uint64(*line) - 1 // convert 1-based line arg to 0-based lineNum.
    93  	}
    94  
    95  	path := flag.Arg(0)
    96  	if *editconfig {
    97  		configPath, err := app.ConfigPath()
    98  		if err != nil {
    99  			exitWithError(err)
   100  		}
   101  		path = configPath
   102  	}
   103  
   104  	err := runEditor(path, lineNum)
   105  	if err != nil {
   106  		exitWithError(err)
   107  	}
   108  }
   109  
   110  func printUsage() {
   111  	f := flag.CommandLine.Output()
   112  	fmt.Fprintf(f, "Usage: %s [options...] [path]\n", os.Args[0])
   113  	flag.PrintDefaults()
   114  }
   115  
   116  func runEditor(path string, lineNum uint64) error {
   117  	log.Printf("version: %s\n", version)
   118  	log.Printf("go version: %s\n", goVersion)
   119  	log.Printf("vcs.revision: %s\n", vcsRevision)
   120  	log.Printf("vcs.time: %s\n", vcsTime)
   121  	log.Printf("vcs.modified: %t\n", vcsModified)
   122  	log.Printf("path arg: %q\n", path)
   123  	log.Printf("lineNum: %d\n", lineNum)
   124  	log.Printf("$TERM env var: %q\n", os.Getenv("TERM"))
   125  
   126  	configRuleSet, err := app.LoadOrCreateConfig(*noconfig)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	screen, err := tcell.NewScreen()
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	if err := screen.Init(); err != nil {
   137  		return err
   138  	}
   139  	defer screen.Fini()
   140  
   141  	screen.EnablePaste()
   142  
   143  	editor := app.NewEditor(screen, path, uint64(lineNum), configRuleSet)
   144  	editor.RunEventLoop()
   145  	return nil
   146  }
   147  
   148  func exitWithError(err error) {
   149  	fmt.Fprintf(os.Stderr, "%v\n", err)
   150  	os.Exit(1)
   151  }