feat: add job.background and bg command

fg-job
TorchedSammy 2022-05-24 18:21:06 -04:00
parent 43dc00c120
commit 7ac43aa2da
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 51 additions and 1 deletions

23
job.go
View File

@ -112,6 +112,7 @@ func (j *job) lua() rt.Value {
"stop": {j.luaStop, 0, false},
"start": {j.luaStart, 0, false},
"foreground": {j.luaForeground, 0, false},
"background": {j.luaBackground, 0, false},
}
luaJob := rt.NewTable()
util.SetExports(l, luaJob, jobFuncs)
@ -154,7 +155,14 @@ func (j *job) luaForeground(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// lua code can run in other threads and goroutines, so this exists
jobs.foreground = true
err := j.foreground()
// this is kinda funny
// background continues the process incase it got suspended
err := j.background()
if err != nil {
return nil, err
}
err = j.foreground()
if err != nil {
return nil, err
}
@ -163,6 +171,19 @@ func (j *job) luaForeground(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
func (j *job) luaBackground(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if !j.running {
return nil, errors.New("job not running")
}
err := j.background()
if err != nil {
return nil, err
}
return c.Next(), nil
}
type jobHandler struct {
jobs map[int]*job
latestID int

View File

@ -26,3 +26,13 @@ func (j *job) foreground() error {
return nil
}
func (j *job) background() error {
proc := j.handle.Process
if proc == nil {
return nil
}
proc.Signal(syscall.SIGCONT)
return nil
}

View File

@ -9,3 +9,7 @@ import (
func (j *job) foreground() error {
return errors.New("not supported on windows")
}
func (j *job) background() error {
return errors.New("not supported on windows")
}

View File

@ -0,0 +1,15 @@
local commander = require 'commander'
commander.register('bg', function()
local job = hilbish.jobs.last()
if not job then
print 'bg: no last job'
return 1
end
local err = job.background()
if err then
print('bg: ' .. err)
return 2
end
end)