Change project layout and use go.mod

This commit is contained in:
Zachary Yedidia
2019-02-03 23:17:24 -05:00
parent c7f2c9c704
commit 0612af1590
103 changed files with 154 additions and 194 deletions

View File

@@ -0,0 +1,32 @@
package screen
import (
"bufio"
"fmt"
"os"
"strconv"
)
// TermMessage sends a message to the user in the terminal. This usually occurs before
// micro has been fully initialized -- ie if there is an error in the syntax highlighting
// regular expressions
// The function must be called when the Screen is not initialized
// This will write the message, and wait for the user
// to press and key to continue
func TermMessage(msg ...interface{}) {
screenb := TempFini()
fmt.Println(msg...)
fmt.Print("\nPress enter to continue")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
TempStart(screenb)
}
// TermError sends an error to the user in the terminal. Like TermMessage except formatted
// as an error
func TermError(filename string, lineNum int, err string) {
TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
}

110
internal/screen/screen.go Normal file
View File

@@ -0,0 +1,110 @@
package screen
import (
"fmt"
"os"
"sync"
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/pkg/terminfo"
"github.com/zyedidia/tcell"
)
// Screen is the tcell screen we use to draw to the terminal
// Synchronization is used because we poll the screen on a separate
// thread and sometimes the screen is shut down by the main thread
// (for example on TermMessage) so we don't want to poll a nil/shutdown
// screen. TODO: maybe we should worry about polling and drawing at the
// same time too.
var Screen tcell.Screen
var lock sync.Mutex
var DrawChan chan bool
func Lock() {
lock.Lock()
}
func Unlock() {
lock.Unlock()
}
func Redraw() {
DrawChan <- true
}
// TempFini shuts the screen down temporarily
func TempFini() bool {
screenWasNil := Screen == nil
if !screenWasNil {
Screen.Fini()
Lock()
Screen = nil
}
return screenWasNil
}
// TempStart restarts the screen after it was temporarily disabled
func TempStart(screenWasNil bool) {
if !screenWasNil {
Init()
Unlock()
}
}
// Init creates and initializes the tcell screen
func Init() {
DrawChan = make(chan bool, 8)
// Should we enable true color?
truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
tcelldb := os.Getenv("TCELLDB")
os.Setenv("TCELLDB", config.ConfigDir+"/.tcelldb")
// In order to enable true color, we have to set the TERM to `xterm-truecolor` when
// initializing tcell, but after that, we can set the TERM back to whatever it was
oldTerm := os.Getenv("TERM")
if truecolor {
os.Setenv("TERM", "xterm-truecolor")
}
// Initilize tcell
var err error
Screen, err = tcell.NewScreen()
if err != nil {
if err == tcell.ErrTermNotFound {
err = terminfo.WriteDB(config.ConfigDir + "/.tcelldb")
if err != nil {
fmt.Println(err)
fmt.Println("Fatal: Micro could not create terminal database file", config.ConfigDir+"/.tcelldb")
os.Exit(1)
}
Screen, err = tcell.NewScreen()
if err != nil {
fmt.Println(err)
fmt.Println("Fatal: Micro could not initialize a Screen.")
os.Exit(1)
}
} else {
fmt.Println(err)
fmt.Println("Fatal: Micro could not initialize a Screen.")
os.Exit(1)
}
}
if err = Screen.Init(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Now we can put the TERM back to what it was before
if truecolor {
os.Setenv("TERM", oldTerm)
}
if config.GetGlobalOption("mouse").(bool) {
Screen.EnableMouse()
}
os.Setenv("TCELLDB", tcelldb)
}