]> git.lizzy.rs Git - micro.git/blob - cmd/micro/job.go
Fix draw ordering
[micro.git] / cmd / micro / job.go
1 package main
2
3 import (
4         "bytes"
5         "io"
6         "os/exec"
7         "strings"
8 )
9
10 // Jobs are the way plugins can run processes in the background
11 // A job is simply a process that gets executed asynchronously
12 // There are callbacks for when the job exits, when the job creates stdout
13 // and when the job creates stderr
14
15 // These jobs run in a separate goroutine but the lua callbacks need to be
16 // executed in the main thread (where the Lua VM is running) so they are
17 // put into the jobs channel which gets read by the main loop
18
19 // JobFunction is a representation of a job (this data structure is what is loaded
20 // into the jobs channel)
21 type JobFunction struct {
22         function func(string, ...string)
23         output   string
24         args     []string
25 }
26
27 // A CallbackFile is the data structure that makes it possible to catch stderr and stdout write events
28 type CallbackFile struct {
29         io.Writer
30
31         callback func(string, ...string)
32         args     []string
33 }
34
35 func (f *CallbackFile) Write(data []byte) (int, error) {
36         // This is either stderr or stdout
37         // In either case we create a new job function callback and put it in the jobs channel
38         jobFunc := JobFunction{f.callback, string(data), f.args}
39         jobs <- jobFunc
40         return f.Writer.Write(data)
41 }
42
43 // JobStart starts a process in the background with the given callbacks
44 // It returns an *exec.Cmd as the job id
45 func JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string) *exec.Cmd {
46         split := strings.Split(cmd, " ")
47         args := split[1:]
48         cmdName := split[0]
49
50         // Set up everything correctly if the functions have been provided
51         proc := exec.Command(cmdName, args...)
52         var outbuf bytes.Buffer
53         if onStdout != "" {
54                 proc.Stdout = &CallbackFile{&outbuf, LuaFunctionJob(onStdout), userargs}
55         } else {
56                 proc.Stdout = &outbuf
57         }
58         if onStderr != "" {
59                 proc.Stderr = &CallbackFile{&outbuf, LuaFunctionJob(onStderr), userargs}
60         } else {
61                 proc.Stderr = &outbuf
62         }
63
64         go func() {
65                 // Run the process in the background and create the onExit callback
66                 proc.Run()
67                 jobFunc := JobFunction{LuaFunctionJob(onExit), string(outbuf.Bytes()), userargs}
68                 jobs <- jobFunc
69         }()
70
71         return proc
72 }
73
74 // JobStop kills a job
75 func JobStop(cmd *exec.Cmd) {
76         cmd.Process.Kill()
77 }
78
79 // JobSend sends the given data into the job's stdin stream
80 func JobSend(cmd *exec.Cmd, data string) {
81         stdin, err := cmd.StdinPipe()
82         if err != nil {
83                 return
84         }
85
86         stdin.Write([]byte(data))
87 }