Add raw command to view raw terminal esc codes

This commit is contained in:
Zachary Yedidia 2017-11-29 01:06:16 -05:00
parent acd42df13c
commit bdb699211a
7 changed files with 51 additions and 7 deletions

View file

@ -158,7 +158,7 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
InitLocalSettings(b)
if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
if len(*flagStartPos) == 0 && (b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool)) {
// If either savecursor or saveundo is turned on, we need to load the serialized information
// from ~/.config/micro/buffers
file, err := os.Open(configDir + "/buffers/" + EscapePath(b.AbsPath))

View file

@ -57,6 +57,7 @@ func init() {
"TabSwitch": TabSwitch,
"MemUsage": MemUsage,
"Retab": Retab,
"Raw": Raw,
}
}
@ -113,6 +114,7 @@ func DefaultCommands() map[string]StrCommand {
"tabswitch": {"TabSwitch", []Completion{NoCompletion}},
"memusage": {"MemUsage", []Completion{NoCompletion}},
"retab": {"Retab", []Completion{NoCompletion}},
"raw": {"Raw", []Completion{NoCompletion}},
}
}
@ -203,6 +205,26 @@ func Retab(args []string) {
CurView().Retab(true)
}
func Raw(args []string) {
buf := NewBufferFromString("", "Raw events")
view := NewView(buf)
view.Buf.Insert(view.Cursor.Loc, "Warning: Showing raw event escape codes\n")
view.Buf.Insert(view.Cursor.Loc, "Use CtrlQ to exit\n")
view.Type = vtRaw
tab := NewTabFromView(view)
tab.SetNum(len(tabs))
tabs = append(tabs, tab)
curTab = len(tabs) - 1
if len(tabs) == 2 {
for _, t := range tabs {
for _, v := range t.views {
v.ToggleTabbar()
}
}
}
}
// TabSwitch switches to a given tab either by name or by number
func TabSwitch(args []string) {
if len(args) > 0 {

View file

@ -253,7 +253,7 @@ func LoadAll() {
}
}
// Passing -version as a flag will have micro print out the version number
// Command line flags
var flagVersion = flag.Bool("version", false, "Show the version number and information")
var flagStartPos = flag.String("startpos", "", "LINE,COL to start the cursor at when opening a buffer.")
var flagConfigDir = flag.String("config-dir", "", "Specify a custom location for the configuration directory")

File diff suppressed because one or more lines are too long

@ -1 +1 @@
Subproject commit c92e80b717d7d911424a2c8394af863ceca4abf0
Subproject commit 869faf86d0607cf067597dcb302bee36648ac7ce

View file

@ -1,7 +1,9 @@
package main
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
@ -21,6 +23,7 @@ var (
vtHelp = ViewType{1, true, true}
vtLog = ViewType{2, true, true}
vtScratch = ViewType{3, false, true}
vtRaw = ViewType{4, true, true}
)
// The View struct stores information about a view into a buffer.
@ -496,6 +499,20 @@ func (v *View) SetCursor(c *Cursor) bool {
// HandleEvent handles an event passed by the main loop
func (v *View) HandleEvent(event tcell.Event) {
if v.Type == vtRaw {
v.Buf.Insert(v.Cursor.Loc, reflect.TypeOf(event).String()[7:])
v.Buf.Insert(v.Cursor.Loc, fmt.Sprintf(": %q\n", event.EscSeq()))
switch e := event.(type) {
case *tcell.EventKey:
if e.Key() == tcell.KeyCtrlQ {
v.Quit(true)
}
}
return
}
// This bool determines whether the view is relocated at the end of the function
// By default it's true because most events should cause a relocate
relocate := true
@ -506,7 +523,7 @@ func (v *View) HandleEvent(event tcell.Event) {
case *tcell.EventRaw:
for key, actions := range bindings {
if key.keyCode == -1 {
if e.EscapeCode() == key.escape {
if e.EscSeq() == key.escape {
for _, c := range v.Buf.cursors {
ok := v.SetCursor(c)
if !ok {
@ -712,8 +729,8 @@ func (v *View) DisplayView() {
v.leftCol = 0
}
if v.Type == vtLog {
// Log views should always follow the cursor...
if v.Type == vtLog || v.Type == vtRaw {
// Log or raw views should always follow the cursor...
v.Relocate()
}

View file

@ -80,6 +80,11 @@ Here are the possible commands that you can use.
* `retab`: Replaces all leading tabs with spaces or leading spaces with tabs
depending on the value of `tabstospaces`.
* `raw`: Micro will open a new tab and show the escape sequence for every event
it receives from the terminal. This shows you what micro actually sees from
the terminal and helps you see which bindings aren't possible and why. This
is most useful for debugging keybindings.
---
The following commands are provided by the default plugins: