editor.go (1797B)
1 package editor 2 3 import ( 4 "fmt" 5 "os" 6 "sync" 7 8 "code.dwrz.net/src/pkg/editor/buffer" 9 "code.dwrz.net/src/pkg/editor/canvas" 10 "code.dwrz.net/src/pkg/editor/event" 11 "code.dwrz.net/src/pkg/editor/prompt" 12 "code.dwrz.net/src/pkg/log" 13 "code.dwrz.net/src/pkg/terminal" 14 "code.dwrz.net/src/pkg/terminal/input" 15 ) 16 17 // TODO: add undo and redo. 18 // TODO: add search and replace. 19 type Editor struct { 20 canvas *canvas.Canvas 21 input chan *input.Event 22 log *log.Logger 23 events chan event.Event 24 reader *input.Reader 25 terminal *terminal.Terminal 26 tmpdir string 27 28 mu sync.Mutex 29 active *buffer.Buffer 30 buffers map[string]*buffer.Buffer 31 prompt *prompt.Prompt 32 } 33 34 type Parameters struct { 35 In *os.File 36 Log *log.Logger 37 Out *os.File 38 TempDir string 39 Terminal *terminal.Terminal 40 } 41 42 // TODO: read text piped in from stdin. 43 // TODO: pipe buffer texts through shell command. 44 func New(p Parameters) (*Editor, error) { 45 var editor = &Editor{ 46 buffers: map[string]*buffer.Buffer{}, 47 events: make(chan event.Event), 48 input: make(chan *input.Event), 49 log: p.Log, 50 terminal: p.Terminal, 51 tmpdir: p.TempDir, 52 prompt: prompt.New(prompt.Parameters{Log: p.Log}), 53 } 54 55 // Setup user input. 56 editor.reader = input.New(input.Parameters{ 57 Chan: editor.input, 58 In: p.In, 59 Log: p.Log, 60 }) 61 62 // Setup the canvas. 63 editor.canvas = canvas.New(canvas.Parameters{ 64 Log: p.Log, 65 Out: p.Out, 66 Terminal: p.Terminal, 67 }) 68 69 // Create the initial scratch buffer. 70 scratch, err := buffer.Create(buffer.NewBufferParams{ 71 Name: editor.tmpdir + "/scratch", 72 Log: editor.log, 73 }) 74 if err != nil { 75 return nil, fmt.Errorf( 76 "failed to create scratch buffer: %v", err, 77 ) 78 } 79 editor.active = scratch 80 editor.buffers[scratch.Name()] = scratch 81 82 return editor, nil 83 }