5 "github.com/gdamore/tcell"
6 "github.com/go-errors/errors"
7 "github.com/mattn/go-isatty"
13 synLinesUp = 75 // How many lines up to look to do syntax highlighting
14 synLinesDown = 75 // How many lines down to look to do syntax highlighting
15 doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
19 var screen tcell.Screen
21 // Object to send messages and prompts to the user
22 var messenger *Messenger
24 // LoadInput loads the file input for the editor
25 func LoadInput() (string, []byte, error) {
26 // There are a number of ways micro should start given its input
27 // 1. If it is given a file in os.Args, it should open that
29 // 2. If there is no input file and the input is not a terminal, that means
30 // something is being piped in and the stdin should be opened in an
33 // 3. If there is no input file and the input is a terminal, an empty buffer
36 // These are empty by default so if we get to option 3, we can just returns the
45 // Check that the file exists
46 if _, e := os.Stat(filename); e == nil {
47 input, err = ioutil.ReadFile(filename)
49 } else if !isatty.IsTerminal(os.Stdin.Fd()) {
51 // The input is not a terminal, so something is being piped in
52 // and we should read from stdin
53 input, err = ioutil.ReadAll(os.Stdin)
56 // Option 3, or just return whatever we got
57 return filename, input, err
61 filename, input, err := LoadInput()
69 // Should we enable true color?
70 truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
72 // In order to enable true color, we have to set the TERM to `xterm-truecolor` when
73 // initializing tcell, but after that, we can set the TERM back to whatever it was
74 oldTerm := os.Getenv("TERM")
76 os.Setenv("TERM", "xterm-truecolor")
79 // Load the syntax files, including the colorscheme
83 screen, err = tcell.NewScreen()
88 if err = screen.Init(); err != nil {
93 // Now we can put the TERM back to what it was before
95 os.Setenv("TERM", oldTerm)
98 // This is just so if we have an error, we can exit cleanly and not completely
99 // mess up the terminal being worked in
101 if err := recover(); err != nil {
103 fmt.Println("Micro encountered an error:", err)
104 // Print the stack trace too
105 fmt.Print(errors.Wrap(err, 2).ErrorStack())
111 defStyle := tcell.StyleDefault.
112 Foreground(tcell.ColorDefault).
113 Background(tcell.ColorDefault)
115 // There may be another default style defined in the colorscheme
116 if style, ok := colorscheme["default"]; ok {
120 screen.SetStyle(defStyle)
123 messenger = new(Messenger)
124 view := NewView(NewBuffer(string(input), filename))
127 // Display everything
135 // Wait for the user's action
136 event := screen.PollEvent()
138 // Check if we should quit
139 switch e := event.(type) {
140 case *tcell.EventKey:
143 // Make sure not to quit if there are unsaved changes
144 if view.CanClose("Quit anyway? ") {
152 // Make sure to resize the view if the user resized the terminal while looking at the help text
153 view.Resize(screen.Size())
157 // Send it to the view
158 view.HandleEvent(event)