2
3
镜像自地址 https://github.com/sammy-ette/Hilbish 已同步 2025-08-10 02:52:03 +00:00

feat: use sinks for all nature commands, add writeln function

这个提交包含在:
sammyette 2023-01-20 18:35:10 -04:00
父节点 0e3fd1ff49
当前提交 e510b1103f
签署人:: sammyette
GPG 密钥 ID: 904FC49417B44DCD
共有 8 个文件被更改,包括 47 次插入27 次删除

查看文件

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

查看文件

@ -5,7 +5,7 @@ commander.register('cat', function(args, sinks)
local exit = 0 local exit = 0
if #args == 0 then if #args == 0 then
print [[ sinks.out:writeln [[
usage: cat [file]...]] usage: cat [file]...]]
end end
@ -13,11 +13,11 @@ usage: cat [file]...]]
local f = io.open(fName) local f = io.open(fName)
if f == nil then if f == nil then
exit = 1 exit = 1
print(string.format('cat: %s: no such file or directory', fName)) sinks.out:writeln(string.format('cat: %s: no such file or directory', fName))
goto continue goto continue
end end
sinks.out:write(f:read '*a') sinks.out:writeln(f:read '*a')
::continue:: ::continue::
end end
io.flush() io.flush()

查看文件

@ -4,9 +4,9 @@ local fs = require 'fs'
local dirs = require 'nature.dirs' local dirs = require 'nature.dirs'
dirs.old = hilbish.cwd() dirs.old = hilbish.cwd()
commander.register('cd', function (args) commander.register('cd', function (args, sinks)
if #args > 1 then if #args > 1 then
print("cd: too many arguments") sinks.out:writeln("cd: too many arguments")
return 1 return 1
elseif #args > 0 then elseif #args > 0 then
local path = args[1]:gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv) local path = args[1]:gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
@ -14,14 +14,14 @@ commander.register('cd', function (args)
if path == '-' then if path == '-' then
path = dirs.old path = dirs.old
print(path) sinks.out:writeln(path)
end end
dirs.setOld(hilbish.cwd()) dirs.setOld(hilbish.cwd())
dirs.push(path) dirs.push(path)
local ok, err = pcall(function() fs.cd(path) end) local ok, err = pcall(function() fs.cd(path) end)
if not ok then if not ok then
print(err) sinks.out:writeln(err)
return 1 return 1
end end
bait.throw('cd', path) bait.throw('cd', path)

查看文件

@ -3,9 +3,9 @@ local fs = require 'fs'
local lunacolors = require 'lunacolors' local lunacolors = require 'lunacolors'
local dirs = require 'nature.dirs' local dirs = require 'nature.dirs'
commander.register('cdr', function(args) commander.register('cdr', function(args, sinks)
if not args[1] then if not args[1] then
print(lunacolors.format [[ sinks.out:writeln(lunacolors.format [[
cdr: change directory to one which has been recently visied cdr: change directory to one which has been recently visied
usage: cdr <index> usage: cdr <index>
@ -17,21 +17,21 @@ to get a list of recent directories, use {green}{underline}cdr list{reset}]])
if args[1] == 'list' then if args[1] == 'list' then
local recentDirs = dirs.recentDirs local recentDirs = dirs.recentDirs
if #recentDirs == 0 then if #recentDirs == 0 then
print 'No directories have been visited.' sinks.out:writeln 'No directories have been visited.'
return 1 return 1
end end
print(table.concat(recentDirs, '\n')) sinks.out:writeln(table.concat(recentDirs, '\n'))
return return
end end
local index = tonumber(args[1]) local index = tonumber(args[1])
if not index then if not index then
print(string.format('Received %s as index, which isn\'t a number.', index)) sinks.out:writeln(string.format('Received %s as index, which isn\'t a number.', index))
return 1 return 1
end end
if not dirs.recent(index) then if not dirs.recent(index) then
print(string.format('No recent directory found at index %s.', index)) sinks.out:writeln(string.format('No recent directory found at index %s.', index))
return 1 return 1
end end

查看文件

@ -1,8 +1,8 @@
local commander = require 'commander' local commander = require 'commander'
commander.register('disown', function(args) commander.register('disown', function(args, sinks)
if #hilbish.jobs.all() == 0 then if #hilbish.jobs.all() == 0 then
print 'disown: no current job' sinks.out:writeln 'disown: no current job'
return 1 return 1
end end
@ -10,7 +10,7 @@ commander.register('disown', function(args)
if #args < 0 then if #args < 0 then
id = tonumber(args[1]) id = tonumber(args[1])
if not id then if not id then
print 'disown: invalid id for job' sinks.out:writeln 'disown: invalid id for job'
return 1 return 1
end end
else else
@ -19,7 +19,7 @@ commander.register('disown', function(args)
local ok = pcall(hilbish.jobs.disown, id) local ok = pcall(hilbish.jobs.disown, id)
if not ok then if not ok then
print 'disown: job does not exist' sinks.out:writeln 'disown: job does not exist'
return 2 return 2
end end
end) end)

查看文件

@ -30,7 +30,7 @@ commander.register('doc', function(args, sinks)
f = io.open(moddocPath .. subdocName .. '.md', 'rb') f = io.open(moddocPath .. subdocName .. '.md', 'rb')
end end
if not f then if not f then
sinks.out:write('No documentation found for ' .. mod .. '.') sinks.out:writeln('No documentation found for ' .. mod .. '.')
return return
end end
end end
@ -74,7 +74,7 @@ commander.register('doc', function(args, sinks)
end):gsub('#+.-\n', function(t) end):gsub('#+.-\n', function(t)
return '{bold}{magenta}' .. t .. '{reset}' return '{bold}{magenta}' .. t .. '{reset}'
end)) end))
sinks.out:write(formattedFuncs) sinks.out:writeln(formattedFuncs)
f:close() f:close()
return return
@ -83,7 +83,7 @@ commander.register('doc', function(args, sinks)
return lunacolors.underline(lunacolors.blue(string.gsub(f, '.md', ''))) return lunacolors.underline(lunacolors.blue(string.gsub(f, '.md', '')))
end) end)
sinks.out:write [[ sinks.out:writeln [[
Welcome to Hilbish's doc tool! Here you can find documentation for builtin Welcome to Hilbish's doc tool! Here you can find documentation for builtin
functions and other things. functions and other things.
@ -92,5 +92,5 @@ A section is a module or a literal section and a subdoc is a subsection for it.
Available sections: ]] Available sections: ]]
sinks.out:write(table.concat(modules, ', ') .. '\n') sinks.out:writeln(table.concat(modules, ', '))
end) end)

查看文件

@ -1,15 +1,15 @@
local commander = require 'commander' local commander = require 'commander'
commander.register('fg', function() commander.register('fg', function(_, sinks)
local job = hilbish.jobs.last() local job = hilbish.jobs.last()
if not job then if not job then
print 'fg: no last job' sinks.out:writeln 'fg: no last job'
return 1 return 1
end end
local err = job.foreground() -- waits for job; blocks local err = job.foreground() -- waits for job; blocks
if err then if err then
print('fg: ' .. err) sinks.out:writeln('fg: ' .. err)
return 2 return 2
end end
end) end)

20
sink.go
查看文件

@ -25,6 +25,7 @@ func setupSinkType(rtm *rt.Runtime) {
sinkMethods := rt.NewTable() sinkMethods := rt.NewTable()
sinkFuncs := map[string]util.LuaExport{ sinkFuncs := map[string]util.LuaExport{
"write": {luaSinkWrite, 2, false}, "write": {luaSinkWrite, 2, false},
"writeln": {luaSinkWriteln, 2, false},
} }
util.SetExports(l, sinkMethods, sinkFuncs) util.SetExports(l, sinkMethods, sinkFuncs)
@ -58,6 +59,25 @@ func luaSinkWrite(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil return c.Next(), nil
} }
func luaSinkWriteln(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.CheckNArgs(2); err != nil {
return nil, err
}
s, err := sinkArg(c, 0)
if err != nil {
return nil, err
}
data, err := c.StringArg(1)
if err != nil {
return nil, err
}
s.writer.Write([]byte(data + "\n"))
return c.Next(), nil
}
func newSinkInput(r io.Reader) *sink { func newSinkInput(r io.Reader) *sink {
s := &sink{ s := &sink{
reader: r, reader: r,