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

     1  //go:build ignore
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/aretext/aretext/input"
    10  	"github.com/aretext/aretext/input/engine"
    11  )
    12  
    13  // This generates state machines for each of the editor input modes.
    14  // These are compiled, written to disk, then embedded in the aretext binary
    15  // so they can be quickly loaded on program startup.
    16  func main() {
    17  	generate(input.NormalModePath, input.NormalModeCommands())
    18  	generate(input.InsertModePath, input.InsertModeCommands())
    19  	generate(input.VisualModePath, input.VisualModeCommands())
    20  	generate(input.MenuModePath, input.MenuModeCommands())
    21  	generate(input.SearchModePath, input.SearchModeCommands())
    22  	generate(input.TaskModePath, input.TaskModeCommands())
    23  	generate(input.TextFieldModePath, input.TextFieldCommands())
    24  }
    25  
    26  func generate(path string, commands []input.Command) {
    27  	fmt.Printf("Generating input state machine %s\n", path)
    28  
    29  	cmdExprs := make([]engine.CmdExpr, 0, len(commands))
    30  	for i, cmd := range commands {
    31  		cmdExprs = append(cmdExprs, engine.CmdExpr{
    32  			CmdId: engine.CmdId(i),
    33  			Expr:  cmd.BuildExpr(),
    34  		})
    35  	}
    36  
    37  	sm, err := engine.Compile(cmdExprs)
    38  	if err != nil {
    39  		fmt.Printf("Error compiling commands for %s: %s", path, err)
    40  		os.Exit(1)
    41  	}
    42  
    43  	data := engine.Serialize(sm)
    44  	if err := os.WriteFile(path, data, 0644); err != nil {
    45  		fmt.Printf("Error writing file %s: %s", path, err)
    46  		os.Exit(1)
    47  	}
    48  }