Exemplo n.º 1
0
func main() {

	tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
	s, e := tcell.NewScreen()
	if e != nil {
		fmt.Fprintf(os.Stderr, "%v\n", e)
		os.Exit(1)
	}
	if e = s.Init(); e != nil {
		fmt.Fprintf(os.Stderr, "%v\n", e)
		os.Exit(1)
	}

	s.SetStyle(tcell.StyleDefault.
		Foreground(tcell.ColorBlack).
		Background(tcell.ColorWhite))
	s.Clear()

	quit := make(chan struct{})
	go func() {
		for {
			ev := s.PollEvent()
			switch ev := ev.(type) {
			case *tcell.EventKey:
				switch ev.Key() {
				case tcell.KeyEscape, tcell.KeyEnter:
					close(quit)
					return
				case tcell.KeyCtrlL:
					s.Sync()
				}
			case *tcell.EventResize:
				s.Sync()
			}
		}
	}()

	cnt := 0
	dur := time.Duration(0)
loop:
	for {
		select {
		case <-quit:
			break loop
		case <-time.After(time.Millisecond * 50):
		}
		start := time.Now()
		makebox(s)
		cnt++
		dur += time.Now().Sub(start)
	}

	s.Fini()
	fmt.Printf("Finished %d boxes in %s\n", cnt, dur)
	fmt.Printf("Average is %0.3f ms / box\n", (float64(dur)/float64(cnt))/1000000.0)
}
Exemplo n.º 2
0
func main() {
	flag.Parse()
	if *flagVersion {
		fmt.Println("Micro version:", Version)
		os.Exit(0)
	}

	filename, input, err := LoadInput()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	encoding.Register()
	tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)

	// Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
	InitConfigDir()
	// Load the user's settings
	InitSettings()
	// Load the syntax files, including the colorscheme
	LoadSyntaxFiles()

	buf := NewBuffer(string(input), filename)

	InitScreen()

	// This is just so if we have an error, we can exit cleanly and not completely
	// mess up the terminal being worked in
	defer func() {
		if err := recover(); err != nil {
			screen.Fini()
			fmt.Println("Micro encountered an error:", err)
			// Print the stack trace too
			fmt.Print(errors.Wrap(err, 2).ErrorStack())
			os.Exit(1)
		}
	}()

	messenger = new(Messenger)
	view := NewView(buf)

	for {
		// Display everything
		Redraw(view)

		// Wait for the user's action
		event := screen.PollEvent()

		if searching {
			HandleSearchEvent(event, view)
		} else {
			// Check if we should quit
			switch e := event.(type) {
			case *tcell.EventKey:
				switch e.Key() {
				case tcell.KeyCtrlQ:
					// Make sure not to quit if there are unsaved changes
					if helpOpen {
						view.OpenBuffer(buf)
						helpOpen = false
					} else {
						if view.CanClose("Quit anyway? (yes, no, save) ") {
							screen.Fini()
							os.Exit(0)
						}
					}
				case tcell.KeyCtrlE:
					input, canceled := messenger.Prompt("> ")
					if !canceled {
						HandleCommand(input, view)
					}
				case tcell.KeyCtrlB:
					input, canceled := messenger.Prompt("$ ")
					if !canceled {
						HandleShellCommand(input, view, true)
					}
				case tcell.KeyCtrlG:
					if !helpOpen {
						helpBuffer := NewBuffer(helpTxt, "")
						helpBuffer.name = "Help"
						helpOpen = true
						view.OpenBuffer(helpBuffer)
					} else {
						view.OpenBuffer(buf)
						helpOpen = false
					}
				}
			}

			// Send it to the view
			view.HandleEvent(event)
		}
	}
}