Compare commits
No commits in common. "master" and "v1.0.0" have entirely different histories.
|
@ -1,6 +0,0 @@
|
||||||
root = true
|
|
||||||
|
|
||||||
[*]
|
|
||||||
charset = utf-8
|
|
||||||
indent_size = 4
|
|
||||||
indent_style = tab
|
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
- [ ] I have reviewed CONTRIBUTING.md.
|
- [ ] I have reviewed CONTRIBUTING.md.
|
||||||
- [ ] My commits and title use the [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/) format.
|
- [ ] My commits and title use the [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/) format.
|
||||||
- [ ] I have documented changes and additions in the CHANGELOG.md.
|
- [ ] I have documented any breaking changes according to [SemVer](https://semver.org/).
|
||||||
---
|
---
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
name: Build
|
name: Build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
- push
|
||||||
branches:
|
- pull_request
|
||||||
- master
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
@ -23,42 +19,22 @@ jobs:
|
||||||
goos: windows
|
goos: windows
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v2
|
||||||
with:
|
with:
|
||||||
submodules: true
|
fetch-depth: 0
|
||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: '1.22.2'
|
go-version: '1.17.7'
|
||||||
- name: Download Task
|
|
||||||
run: 'sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d'
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} ./bin/task
|
run: GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build
|
||||||
- uses: actions/upload-artifact@v4
|
- uses: actions/upload-artifact@v2
|
||||||
if: matrix.goos == 'windows'
|
if: matrix.goos == 'windows'
|
||||||
with:
|
with:
|
||||||
name: hilbish-${{ matrix.goos }}-${{ matrix.goarch }}
|
name: hilbish-${{ matrix.goos }}-${{ matrix.goarch }}.exe
|
||||||
path: |
|
path: hilbish.exe
|
||||||
hilbish.exe
|
- uses: actions/upload-artifact@v2
|
||||||
LICENSE
|
|
||||||
README.md
|
|
||||||
CHANGELOG.md
|
|
||||||
.hilbishrc.lua
|
|
||||||
nature
|
|
||||||
libs
|
|
||||||
docs
|
|
||||||
emmyLuaDocs
|
|
||||||
- uses: actions/upload-artifact@v4
|
|
||||||
if: matrix.goos != 'windows'
|
if: matrix.goos != 'windows'
|
||||||
with:
|
with:
|
||||||
name: hilbish-${{ matrix.goos }}-${{ matrix.goarch }}
|
name: hilbish-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||||
path: |
|
path: hilbish
|
||||||
hilbish
|
|
||||||
LICENSE
|
|
||||||
README.md
|
|
||||||
CHANGELOG.md
|
|
||||||
.hilbishrc.lua
|
|
||||||
nature
|
|
||||||
libs
|
|
||||||
docs
|
|
||||||
emmyLuaDocs
|
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
name: CodeQL
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master ]
|
|
||||||
schedule:
|
|
||||||
- cron: '38 2 * * 4'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: Analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
|
|
||||||
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: Initialize CodeQL
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
|
|
||||||
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
|
|
||||||
# queries: security-extended,security-and-quality
|
|
||||||
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: Autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
|
|
||||||
|
|
||||||
# If the Autobuild fails above, remove it and uncomment the following three lines.
|
|
||||||
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
|
|
||||||
|
|
||||||
# - run: |
|
|
||||||
# echo "Run, Build Application using script"
|
|
||||||
# ./location_of_script_within_repo/buildscript.sh
|
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
|
@ -2,15 +2,14 @@ name: Generate docs
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches: [master]
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
gen:
|
gen:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v2
|
||||||
- name: Run docgen
|
- name: Run docgen
|
||||||
run: go run cmd/docgen/docgen.go
|
run: go run cmd/docgen/docgen.go
|
||||||
- name: Commit new docs
|
- name: Commit new docs
|
||||||
|
|
|
@ -9,7 +9,7 @@ jobs:
|
||||||
create-release:
|
create-release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
- uses: taiki-e/create-gh-release-action@v1
|
- uses: taiki-e/create-gh-release-action@v1
|
||||||
with:
|
with:
|
||||||
title: Hilbish $tag
|
title: Hilbish $tag
|
||||||
|
@ -30,17 +30,11 @@ jobs:
|
||||||
- goarch: arm64
|
- goarch: arm64
|
||||||
goos: windows
|
goos: windows
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
with:
|
- uses: wangyoucao577/go-release-action@v1.25
|
||||||
submodules: true
|
|
||||||
fetch-depth: 0
|
|
||||||
- name: Download Task
|
|
||||||
run: 'sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d'
|
|
||||||
- uses: wangyoucao577/go-release-action@v1
|
|
||||||
with:
|
with:
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
goos: ${{ matrix.goos }}
|
goos: ${{ matrix.goos }}
|
||||||
goarch: ${{ matrix.goarch }}
|
goarch: ${{ matrix.goarch }}
|
||||||
ldflags: '-s -w'
|
|
||||||
binary_name: hilbish
|
binary_name: hilbish
|
||||||
extra_files: LICENSE README.md CHANGELOG.md .hilbishrc.lua nature libs docs emmyLuaDocs
|
extra_files: LICENSE README.md CHANGELOG.md .hilbishrc.lua prelude docs emmyLuaDocs
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
name: Build website
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
tags:
|
|
||||||
- v[0-9]+.*
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
submodules: true
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Hugo
|
|
||||||
uses: peaceiris/actions-hugo@v3
|
|
||||||
with:
|
|
||||||
hugo-version: '0.111.3'
|
|
||||||
extended: true
|
|
||||||
|
|
||||||
- name: Set branch name
|
|
||||||
id: branch
|
|
||||||
run: echo "BRANCH_NAME=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/*/}}" >> "$GITHUB_ENV"
|
|
||||||
|
|
||||||
- name: Fix base URL
|
|
||||||
if: env.BRANCH_NAME != 'master' && github.repository_owner == 'Rosettea'
|
|
||||||
run: sed -i "s%baseURL = 'https://rosettea.github.io/Hilbish/'%baseURL = 'https://rosettea.github.io/Hilbish/versions/${{ env.BRANCH_NAME }}'%" website/config.toml
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: 'cd website && hugo --minify'
|
|
||||||
|
|
||||||
- name: Deploy
|
|
||||||
if: env.BRANCH_NAME == 'master' && github.repository_owner == 'Rosettea'
|
|
||||||
uses: peaceiris/actions-gh-pages@v4
|
|
||||||
with:
|
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
publish_dir: ./website/public
|
|
||||||
keep_files: true
|
|
||||||
- name: Deploy
|
|
||||||
if: env.BRANCH_NAME != 'master' && github.repository_owner == 'Rosettea'
|
|
||||||
uses: peaceiris/actions-gh-pages@v4
|
|
||||||
with:
|
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
publish_dir: ./website/public
|
|
||||||
destination_dir: versions/${{ env.BRANCH_NAME }}
|
|
||||||
keep_files: true
|
|
|
@ -1,9 +1,6 @@
|
||||||
*.exe
|
*.exe
|
||||||
hilbish
|
hilbish
|
||||||
!docs/api/hilbish
|
|
||||||
docgen
|
docgen
|
||||||
!cmd/docgen
|
|
||||||
|
|
||||||
.vim
|
.vim
|
||||||
petals/
|
petals/
|
||||||
.hugo_build.lock
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[submodule "libs/lunacolors"]
|
[submodule "libs/lunacolors"]
|
||||||
path = libs/lunacolors
|
path = libs/lunacolors
|
||||||
url = https://github.com/Rosettea/Lunacolors
|
url = https://github.com/Hilbis/Lunacolors
|
||||||
[submodule "libs/succulent"]
|
[submodule "libs/succulent"]
|
||||||
path = libs/succulent
|
path = libs/succulent
|
||||||
url = https://github.com/Rosettea/Succulent
|
url = https://github.com/Rosettea/Succulent
|
||||||
|
|
|
@ -1,39 +1,20 @@
|
||||||
-- Default Hilbish config
|
-- Default Hilbish config
|
||||||
local hilbish = require 'hilbish'
|
|
||||||
local lunacolors = require 'lunacolors'
|
local lunacolors = require 'lunacolors'
|
||||||
local bait = require 'bait'
|
local bait = require 'bait'
|
||||||
local ansikit = require 'ansikit'
|
local ansikit = require 'ansikit'
|
||||||
|
|
||||||
local unreadCount = 0
|
local function doPrompt(fail, mode)
|
||||||
local running = false
|
|
||||||
local function doPrompt(fail)
|
|
||||||
hilbish.prompt(lunacolors.format(
|
hilbish.prompt(lunacolors.format(
|
||||||
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '∆ '
|
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '∆ '
|
||||||
))
|
))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function doNotifyPrompt()
|
print(lunacolors.format(hilbish.greeting))
|
||||||
if running or unreadCount == hilbish.messages.unreadCount() then return end
|
|
||||||
|
|
||||||
local notifPrompt = string.format('• %s unread notification%s', hilbish.messages.unreadCount(), hilbish.messages.unreadCount() > 1 and 's' or '')
|
|
||||||
unreadCount = hilbish.messages.unreadCount()
|
|
||||||
hilbish.prompt(lunacolors.blue(notifPrompt), 'right')
|
|
||||||
|
|
||||||
hilbish.timeout(function()
|
|
||||||
hilbish.prompt('', 'right')
|
|
||||||
end, 3000)
|
|
||||||
end
|
|
||||||
|
|
||||||
doPrompt()
|
doPrompt()
|
||||||
|
|
||||||
bait.catch('command.preexec', function()
|
|
||||||
running = true
|
|
||||||
end)
|
|
||||||
|
|
||||||
bait.catch('command.exit', function(code)
|
bait.catch('command.exit', function(code)
|
||||||
running = false
|
|
||||||
doPrompt(code ~= 0)
|
doPrompt(code ~= 0)
|
||||||
doNotifyPrompt()
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
bait.catch('hilbish.vimMode', function(mode)
|
bait.catch('hilbish.vimMode', function(mode)
|
||||||
|
@ -42,8 +23,6 @@ bait.catch('hilbish.vimMode', function(mode)
|
||||||
else
|
else
|
||||||
ansikit.cursorStyle(ansikit.lineCursor)
|
ansikit.cursorStyle(ansikit.lineCursor)
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
|
|
||||||
bait.catch('hilbish.notification', function(notif)
|
doPrompt(false, mode)
|
||||||
doNotifyPrompt()
|
|
||||||
end)
|
end)
|
||||||
|
|
429
CHANGELOG.md
|
@ -1,413 +1,6 @@
|
||||||
# 🎀 Changelog
|
# 🎀 Changelog
|
||||||
|
|
||||||
## [2.3.3] - 2024-11-04
|
## [1.0.0] - 2021-03-06
|
||||||
### Fixed
|
|
||||||
- Heredocs having issues
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- Adding `\` at the end of input will add a newline and prompt for more input.
|
|
||||||
|
|
||||||
## [2.3.2] - 2024-07-30
|
|
||||||
### Fixed
|
|
||||||
- Command path searching due to 2.3 changes to the shell interpreter
|
|
||||||
|
|
||||||
## [2.3.1] - 2024-07-27
|
|
||||||
[hehe when you see it release](https://youtu.be/AaAF51Gwbxo?si=rhj2iYuQRkqDa693&t=64)
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- `hilbish.opts.tips` was added to display random tips on start up.
|
|
||||||
Displayed tips can be modified via the `hilbish.tips` table.
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Fix a minor regression related to the cd command not working with relative paths
|
|
||||||
- Updated the motd for 2.3
|
|
||||||
|
|
||||||
## [2.3.0] - 2024-07-20
|
|
||||||
### Added
|
|
||||||
- `commander.registry` function to get all registered commanders.
|
|
||||||
- `fs.pipe` function to get a pair of connected files (a pipe).
|
|
||||||
- Added an alternative 2nd parameter to `hilbish.run`, which is `streams`.
|
|
||||||
`streams` is a table of input and output streams to run the command with.
|
|
||||||
It uses these 3 keys:
|
|
||||||
- `input` as standard input for the command
|
|
||||||
- `out` as standard output
|
|
||||||
- `err` as standard error
|
|
||||||
|
|
||||||
Here is a minimal example of the new usage which allows users to now pipe commands
|
|
||||||
directly via Lua functions:
|
|
||||||
|
|
||||||
```lua
|
|
||||||
local fs = require 'fs'
|
|
||||||
local pr, pw = fs.pipe()
|
|
||||||
hilbish.run('ls -l', {
|
|
||||||
stdout = pw,
|
|
||||||
stderr = pw,
|
|
||||||
})
|
|
||||||
|
|
||||||
pw:close()
|
|
||||||
|
|
||||||
hilbish.run('wc -l', {
|
|
||||||
stdin = pr
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- The `-S` flag will be set to Hilbish's absolute path
|
|
||||||
- Hilbish now builds on any Unix (if any dependencies also work, which should.)
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Fix ansi attributes causing issues with text when cut off in greenhouse
|
|
||||||
- Fix greenhouse appearing on terminal resize
|
|
||||||
- Fix crashes when history goes out of bounds when using history navigation
|
|
||||||
- `exec` command should return if no arg presented
|
|
||||||
- Commanders can now be cancelled by Ctrl-C and wont hang the shell anymore.
|
|
||||||
See [issue 198](https://github.com/Rosettea/Hilbish/issues/198).
|
|
||||||
- Shell interpreter can now preserve its environment and set PWD properly.
|
|
||||||
|
|
||||||
## [2.2.3] - 2024-04-27
|
|
||||||
### Fixed
|
|
||||||
- Highligher and hinter work now, since it was regressed from the previous minor release.
|
|
||||||
- `cat` command no longer prints extra newline at end of each file
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- `cat` command now reads files in chunks, allowing for reading large files
|
|
||||||
|
|
||||||
## [2.2.2] - 2024-04-16
|
|
||||||
### Fixed
|
|
||||||
- Line refresh fixes (less flicker)
|
|
||||||
- Do more checks for a TTY
|
|
||||||
- Panic if ENOTTY is thrown from readline
|
|
||||||
- use `x/term` function to check if a terminal
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- Page Up/Down keybinds for Greenhouse will now scroll up and down the size of the region (a page)
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- Remove usage of `hilbish.goro` in Greenhouse.
|
|
||||||
- Values in `hilbish` table are no longer protected. This means
|
|
||||||
they can be overridden. (#287)
|
|
||||||
|
|
||||||
## [2.2.1] - 2023-12-26
|
|
||||||
### Fixed
|
|
||||||
- Removed a left over debug print
|
|
||||||
- Recover panic in `hilbish.goro`
|
|
||||||
|
|
||||||
## [2.2.0] - 2023-12-25
|
|
||||||
### Added
|
|
||||||
- [Native Modules](https://rosettea.github.io/Hilbish/docs/api/hilbish/hilbish.module/)
|
|
||||||
- Made a few additions to the sink type:
|
|
||||||
- `read()` method for retrieving input (so now the `in` sink of commanders is useful)
|
|
||||||
- `flush()` and `autoFlush()` related to flushing outputs
|
|
||||||
- `pipe` property to check if a sink with input is a pipe (like stdin)
|
|
||||||
- Add fuzzy search to history search (enable via `hilbish.opts.fuzzy = true`)
|
|
||||||
- Show indexes on cdr list and use ~ for home directory.
|
|
||||||
- Fix doc command not displaying correct subdocs when using shorthand api doc access (`doc api hilbish.jobs` as an example)
|
|
||||||
- `hilbish.messages` interface (details in [#219])
|
|
||||||
- `hilbish.notification` signal when a message/notification is sent
|
|
||||||
- `notifyJobFinish` opt to send a notification when background jobs are
|
|
||||||
- `hilbish.goVersion` for the version of Go used to compile Hilbish.
|
|
||||||
completed.
|
|
||||||
- Allow numbered arg substitutions in aliases.
|
|
||||||
- Example: `hilbish.alias('hello', 'echo %1 says hello')` allows the user to run `hello hilbish`
|
|
||||||
which will output `hilbish says hello`.
|
|
||||||
- Greenhouse
|
|
||||||
- Greenhouse is a pager library and program. Basic usage is `greenhouse <file>`
|
|
||||||
- Using this also brings enhancements to the `doc` command like easy
|
|
||||||
navigation of neighboring doc files.
|
|
||||||
Ctrl-N can be used for the table of contents, which views adjacent documentation.
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- Documentation for EVERYTHING has been improved, with more
|
|
||||||
information added, code example, parameter details, etc.
|
|
||||||
You can see the improvements!
|
|
||||||
- Documentation has gotten an uplift in the `doc` command.
|
|
||||||
This includes:
|
|
||||||
- Proper highlighting of code
|
|
||||||
- Paging (via Greenhouse)
|
|
||||||
- Highlighting more markdown things
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Fix panic when runner doesn't return a table
|
|
||||||
- Fix edge case of crash on empty alias resolve
|
|
||||||
- File completion on Windows
|
|
||||||
- Job management commands work now
|
|
||||||
- Fix infinite loop when navigating history without any history. [#252](https://github.com/Rosettea/Hilbish/issues/252)
|
|
||||||
- Return the prefix when calling `hilbish.completions.call`. [#219](https://github.com/Rosettea/Hilbish/issues/219)
|
|
||||||
- Replaced `sed` in-place editing with `grep` and `mv` for compatibility with BSD utils
|
|
||||||
|
|
||||||
## [2.1.2] - 2022-04-10
|
|
||||||
### Removed
|
|
||||||
- Bad april fools code ;(
|
|
||||||
|
|
||||||
## [2.1.1] - 2022-04-01
|
|
||||||
### Added
|
|
||||||
- Validation checks for command input
|
|
||||||
- Improved runtime performance
|
|
||||||
- Validate Lua code
|
|
||||||
|
|
||||||
## [2.1.0] - 2022-02-10
|
|
||||||
### Added
|
|
||||||
- Documented custom userdata types (Job and Timer Objects)
|
|
||||||
- Coming with this fix is also adding the return types for some functions that were missing it
|
|
||||||
- Added a dedicated input and dedicated outputs for commanders (sinks - info at `doc api commander`).
|
|
||||||
- Local docs is used if one of Hilbish's branches is found
|
|
||||||
- Return 1 exit code on doc not found
|
|
||||||
- `hilbish.runner.getCurrent()` to get the current runner
|
|
||||||
- Initialize Hilbish Lua API before handling signals
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- `index` or `_index` subdocs should not show up anymore
|
|
||||||
- `hilbish.which` not working correctly with aliases
|
|
||||||
- Commanders not being able to pipe with commands or any related operator.
|
|
||||||
- Resolve symlinks in completions
|
|
||||||
- Updated `runner-mode` docs
|
|
||||||
- Fix `hilbish.completion` functions panicking when empty input is provided
|
|
||||||
|
|
||||||
## [2.0.1] - 2022-12-28
|
|
||||||
### Fixed
|
|
||||||
- Corrected documentation for hooks, removing outdated `command.no-perm`
|
|
||||||
- Fixed an issue where `cd` with no args would not update the old pwd
|
|
||||||
- Tiny documentation enhancements for the `hilbish.timer` interface
|
|
||||||
|
|
||||||
## [2.0.0] - 2022-12-20
|
|
||||||
**NOTES FOR USERS/PACKAGERS UPDATING:**
|
|
||||||
- Hilbish now uses [Task] insead of Make for builds.
|
|
||||||
- The doc format has been changed from plain text to markdown.
|
|
||||||
**YOU MUST reinstall Hilbish to remove the duplicate, old docs.**
|
|
||||||
- Hilbish will by default install to **`/usr/local`** instead of just `/usr/`
|
|
||||||
when building via Task. This is mainly to avoid conflict of distro packages
|
|
||||||
and local installs, and is the correct place when building from git either way.
|
|
||||||
To keep Hilbish in `/usr`, you must have `PREFIX="/usr/"` when running `task build` or `task install`
|
|
||||||
- Windows is no longer supported. It will build and run, but **will** have problems.
|
|
||||||
If you want to help fix the situation, start a discussion or open an issue and contribute.
|
|
||||||
|
|
||||||
[Task]: https://taskfile.dev/#/
|
|
||||||
|
|
||||||
### Added
|
|
||||||
- Inline hints, akin to fish and the others.
|
|
||||||
To make a handler for hint text, you can set the `hilbish.hinter` function.
|
|
||||||
For more info, look at its docs with the `doc hilbish` command.
|
|
||||||
- Syntax highlighting function. To make a handler for it, set
|
|
||||||
`hilbish.highlighter`. Same thing as the hinter, check `doc hilbish` for
|
|
||||||
more info/docs.
|
|
||||||
- Ctrl+K deletes from the cursor to the end of the line. ([#128](https://github.com/Rosettea/Hilbish/pull/128))
|
|
||||||
- Alt+Backspace as an alternative of Ctrl+W to delete a word. ([#132](https://github.com/Rosettea/Hilbish/pull/132))
|
|
||||||
- Enhanced timer API (`doc timers`)
|
|
||||||
- Don't exit until intervals are stopped/finished when running a non interactive script.
|
|
||||||
- Ctrl+D deletes character below cursor if line isn't empty instead of exiting.
|
|
||||||
- Ctrl+Delete to forward delete a word.
|
|
||||||
- Right prompt ([#140](https://github.com/Rosettea/Hilbish/pull/140))
|
|
||||||
- Ctrl+_ to undo in Emacs input mode.
|
|
||||||
- Emacs style forward/backward word keybinds ([#139](https://github.com/Rosettea/Hilbish/pull/139))
|
|
||||||
- `hilbish.completion.call` to call a completion handler (`doc completions`)
|
|
||||||
- `hilbish.completion.handler` to set a custom handler for completions. This
|
|
||||||
is for everything/anything as opposed to just adding a single command completion.
|
|
||||||
[#122](https://github.com/Rosettea/Hilbish/issues/122)
|
|
||||||
- `fs.abs(path)` to get absolute path.
|
|
||||||
- Nature module (`doc nature`)
|
|
||||||
- `hilbish.jobs.add(cmdstr, args, execPath)` to add a job to the job table.
|
|
||||||
`cmdstr` would be user input, `args` is the args for the command (includes arg0)
|
|
||||||
and `execPath` is absolute path to command executable
|
|
||||||
- `job.add` hook is thrown when a job is added. acts as a unique hook for
|
|
||||||
jobs
|
|
||||||
- `hilbish.jobs.disown(id)` and `disown` builtin to disown a job. `disown`
|
|
||||||
without arguments will disown the last job.
|
|
||||||
- `hilbish.jobs.last()` returns the last added job.
|
|
||||||
- Job output (stdout/stderr) can now be obtained via the `stdout` and `stderr`
|
|
||||||
fields on a job object.
|
|
||||||
- Documentation for jobs is now available via `doc jobs`.
|
|
||||||
- `hilbish.alias.resolve(cmdstr)` to resolve a command alias.
|
|
||||||
- `hilbish.opts` for shell options.
|
|
||||||
- `hilbish.editor` interface for interacting with the line editor that
|
|
||||||
Hilbish uses.
|
|
||||||
- `hilbish.vim` interface to dynamically get/set vim registers.
|
|
||||||
Example usage: `hilbish.vim.registers['a'] = 'hello'`. You can also
|
|
||||||
get the mode with it via `hilbish.vim.mode`
|
|
||||||
- `hilbish.version` interface for more info about Hilbish's version. This
|
|
||||||
includes git commit, branch, and (new!!) release name.
|
|
||||||
- Added `fg` and `bg` builtins
|
|
||||||
- `job.foreground()` and `job.background()`, when `job` is a job object,
|
|
||||||
foreground and backgrounds a job respectively.
|
|
||||||
- Friendlier functions to the `hilbish.runner` interface, which also allow
|
|
||||||
having and using multiple runners.
|
|
||||||
- A few new functions to the `fs` module:
|
|
||||||
- `fs.basename(path)` gets the basename of path
|
|
||||||
- `fs.dir(path)` gets the directory part of path
|
|
||||||
- `fs.glob(pattern)` globs files and directories based on patterns
|
|
||||||
- `fs.join(dirs...)` joins directories by OS dir separator
|
|
||||||
- .. and 2 properties
|
|
||||||
- `fs.pathSep` is the separator for filesystem paths and directories
|
|
||||||
- `fs.pathListSep` is the separator for $PATH env entries
|
|
||||||
- Lua modules located in `hilbish.userDir.data .. '/hilbish/start'` (like `~/.local/share/hilbish/start/foo/init.lua`)
|
|
||||||
will be ran on startup
|
|
||||||
- `hilbish.init` hook, thrown after Hilbish has initialized Lua side
|
|
||||||
- Message of the day on startup (`hilbish.motd`), mainly intended as quick
|
|
||||||
small news pieces for releases. It is printed by default. To disable it,
|
|
||||||
set `hilbish.opts.motd` to false.
|
|
||||||
- `history` opt has been added and is true by default. Setting it to false
|
|
||||||
disables commands being added to history.
|
|
||||||
- `hilbish.rawInput` hook for input from the readline library
|
|
||||||
- Completion of files in quotes
|
|
||||||
- A new and "safer" event emitter has been added. This causes a performance deficit, but avoids a lot of
|
|
||||||
random errors introduced with the new Lua runtime (see [#197])
|
|
||||||
- `bait.release(name, catcher)` removes `handler` for the named `event`
|
|
||||||
- `exec`, `clear` and `cat` builtin commands
|
|
||||||
- `hilbish.cancel` hook thrown when user cancels input with Ctrl-C
|
|
||||||
- 1st item on history is now inserted when history search menu is opened ([#148])
|
|
||||||
- Documentation has been improved vastly!
|
|
||||||
|
|
||||||
[#148]: https://github.com/Rosettea/Hilbish/issues/148
|
|
||||||
[#197]: https://github.com/Rosettea/Hilbish/issues/197
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- **Breaking Change:** Upgraded to Lua 5.4.
|
|
||||||
This is probably one of (if not the) biggest things in this release.
|
|
||||||
To recap quickly on what matters (mostly):
|
|
||||||
- `os.execute` returns 3 values instead of 1 (but you should be using `hilbish.run`)
|
|
||||||
- I/O operations must be flushed (`io.flush()`)
|
|
||||||
- **Breaking Change:** MacOS config paths now match Linux.
|
|
||||||
- Overrides on the `hilbish` table are no longer permitted.
|
|
||||||
- **Breaking Change:** Runner functions are now required to return a table.
|
|
||||||
It can (at the moment) have 4 variables:
|
|
||||||
- `input` (user input)
|
|
||||||
- `exitCode` (exit code)
|
|
||||||
- `error` (error message)
|
|
||||||
- `continue` (whether to prompt for more input)
|
|
||||||
User input has been added to the return to account for runners wanting to
|
|
||||||
prompt for continued input, and to add it properly to history. `continue`
|
|
||||||
got added so that it would be easier for runners to get continued input
|
|
||||||
without having to actually handle it at all.
|
|
||||||
- **Breaking Change:** Job objects and timers are now Lua userdata instead
|
|
||||||
of a table, so their functions require you to call them with a colon instead
|
|
||||||
of a dot. (ie. `job.stop()` -> `job:stop()`)
|
|
||||||
- All `fs` module functions which take paths now implicitly expand ~ to home.
|
|
||||||
- **Breaking Change:** `hilbish.greeting` has been moved to an opt (`hilbish.opts.greeting`) and is
|
|
||||||
always printed by default. To disable it, set the opt to false.
|
|
||||||
- **Breaking Change:** `command.no-perm` hook has been replaced with `command.not-executable`
|
|
||||||
- History is now fetched from Lua, which means users can override `hilbish.history`
|
|
||||||
methods to make it act how they want.
|
|
||||||
- `guide` has been removed. See the [website](https://rosettea.github.io/Hilbish/)
|
|
||||||
for general tips and documentation
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- If in Vim replace mode, input at the end of the line inserts instead of
|
|
||||||
replacing the last character.
|
|
||||||
- Make forward delete work how its supposed to.
|
|
||||||
- Prompt refresh not working properly.
|
|
||||||
- Crashing on input in xterm. ([#131](https://github.com/Rosettea/Hilbish/pull/131))
|
|
||||||
- Make delete key work on st ([#131](https://github.com/Rosettea/Hilbish/pull/131))
|
|
||||||
- `hilbish.login` being the wrong value.
|
|
||||||
- Put full input in history if prompted for continued input
|
|
||||||
- Don't put alias expanded command in history (sound familiar?)
|
|
||||||
- Handle cases of stdin being nonblocking (in the case of [#136](https://github.com/Rosettea/Hilbish/issues/136))
|
|
||||||
- Don't prompt for continued input if non interactive
|
|
||||||
- Don't insert unhandled control keys.
|
|
||||||
- Handle sh syntax error in alias
|
|
||||||
- Use invert for completion menu selection highlight instead of specific
|
|
||||||
colors. Brings an improvement on light themes, or themes that don't follow
|
|
||||||
certain color rules.
|
|
||||||
- Home/End keys now go to the actual start/end of the input.
|
|
||||||
- Input getting cut off on enter in certain cases.
|
|
||||||
- Go to the next line properly if input reaches end of terminal width.
|
|
||||||
- Cursor position with CJK characters has been corrected ([#145](https://github.com/Rosettea/Hilbish/pull/145))
|
|
||||||
- Files with same name as parent folder in completions getting cut off [#130](https://github.com/Rosettea/Hilbish/issues/130))
|
|
||||||
- `hilbish.which` now works with commanders and aliases.
|
|
||||||
- Background jobs no longer take stdin so they do not interfere with shell
|
|
||||||
input.
|
|
||||||
- Full name of completion entry is used instead of being cut off
|
|
||||||
- Completions are fixed in cases where the query/line is an alias alone
|
|
||||||
where it can also resolve to the beginning of command names.
|
|
||||||
(reference [this commit](https://github.com/Rosettea/Hilbish/commit/2790982ad123115c6ddbc5764677fdca27668cea))
|
|
||||||
for explanation.
|
|
||||||
- Jobs now throw `job.done` and set running to false when stopped via
|
|
||||||
Lua `job.stop` function.
|
|
||||||
- Jobs are always started in sh exec handler now instead of only successful start.
|
|
||||||
- SIGTERM is handled properly now, which means stopping jobs and timers.
|
|
||||||
- Fix panic on trailing newline on pasted multiline text.
|
|
||||||
- Completions will no longer be refreshed if the prompt refreshes while the
|
|
||||||
menu is open.
|
|
||||||
- Print error on search fail instead of panicking
|
|
||||||
- Windows related fixes:
|
|
||||||
- `hilbish.dataDir` now has tilde (`~`) expanded.
|
|
||||||
- Arrow keys now work on Windows terminals.
|
|
||||||
- Escape codes now work.
|
|
||||||
- Escape percentage symbols in completion entries, so you will no longer see
|
|
||||||
an error of missing format variable
|
|
||||||
- Fix an error with sh syntax in aliases
|
|
||||||
- Prompt now works with east asian characters (CJK)
|
|
||||||
- Set back the prompt to normal after exiting the continue prompt with ctrl-d
|
|
||||||
- Take into account newline in input when calculating input width. Prevents
|
|
||||||
extra reprinting of the prompt, but input with newlines inserted is still a problem
|
|
||||||
- Put cursor at the end of input when exiting $EDITOR with Vim mode bind
|
|
||||||
- Calculate width of virtual input properly (completion candidates)
|
|
||||||
- Users can now tab complete files with spaces while quoted or with escaped spaces.
|
|
||||||
This means a query of `Files\ to\ ` with file names of `Files to tab complete` and `Files to complete`
|
|
||||||
will result in the files being completed.
|
|
||||||
- Fixed grid menu display if cell width ends up being the width of the terminal
|
|
||||||
- Cut off item names in grid menu if its longer than cell width
|
|
||||||
- Fix completion search menu disappearing
|
|
||||||
- Make binary completion work with bins that have spaces in the name
|
|
||||||
- Completion paths having duplicated characters if it's escaped
|
|
||||||
- Get custom completion command properly to call from Lua
|
|
||||||
- Put proper command on the line when using up and down arrow keys to go through command history
|
|
||||||
- Don't do anything if length of input rune slice is 0 ([commit for explanation](https://github.com/Rosettea/Hilbish/commit/8d40179a73fe5942707cd43f9c0463dee53eedd8))
|
|
||||||
|
|
||||||
## [2.0.0-rc1] - 2022-09-14
|
|
||||||
This is a pre-release version of Hilbish for testing. To see the changelog,
|
|
||||||
refer to the `Unreleased` section of the [full changelog](CHANGELOG.md)
|
|
||||||
(version 2.0.0 for future reference).
|
|
||||||
|
|
||||||
## [1.2.0] - 2022-03-17
|
|
||||||
### Added
|
|
||||||
- Job Management additions
|
|
||||||
- `job.start` and `job.done` hooks (`doc hooks job`)
|
|
||||||
- `hilbish.jobs` interface (`get(id)` function gets a job object via `id`, `all()` gets all)
|
|
||||||
- Customizable runner/exec mode
|
|
||||||
- However Hilbish runs interactive user input can now be changed Lua side (`doc runner-mode`)
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- `vimMode` doc is now `vim-mode`
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Make sure input which is supposed to go in history goes there
|
|
||||||
- Cursor is right at the end of input on history search
|
|
||||||
|
|
||||||
## [1.1.0] - 2022-03-17
|
|
||||||
### Added
|
|
||||||
- `hilbish.vimAction` hook (`doc vimMode actions`)
|
|
||||||
- `command.not-executable` hook (will replace `command.no-perm` in a future release)
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
- Check if interactive before adding to history
|
|
||||||
- Escape in vim mode exits all modes and not only insert
|
|
||||||
- Make 2nd line in prompt empty if entire prompt is 1 line
|
|
||||||
- Completion menu doesnt appear if there is only 1 result
|
|
||||||
- Ignore SIGQUIT, which caused a panic unhandled
|
|
||||||
- Remove hostname in greeting on Windows
|
|
||||||
- Handle PATH binaries properly on Windows
|
|
||||||
- Fix removal of dot in the beginning of folders/files that have them for file complete
|
|
||||||
- Fix prompt being set to the continue prompt even when exited
|
|
||||||
|
|
||||||
## [1.0.4] - 2022-03-12
|
|
||||||
### Fixed
|
|
||||||
- Panic when history directory doesn't exist
|
|
||||||
|
|
||||||
## [1.0.3] - 2022-03-12
|
|
||||||
### Fixed
|
|
||||||
- Removed duplicate executable suggestions
|
|
||||||
- User input is added to history now instead of what's ran by Hilbish
|
|
||||||
- Formatting issue with prompt on no input
|
|
||||||
|
|
||||||
## [1.0.2] - 2022-03-06
|
|
||||||
### Fixed
|
|
||||||
- Cases where Hilbish's history directory doesn't exist will no longer cause a panic
|
|
||||||
|
|
||||||
## [1.0.1] - 2022-03-06
|
|
||||||
### Fixed
|
|
||||||
- Using `hilbish.appendPath` will no longer result in string spam (debugging thing left being)
|
|
||||||
- Prompt gets set properly on startup
|
|
||||||
|
|
||||||
## [1.0.0] - 2022-03-06
|
|
||||||
### Added
|
### Added
|
||||||
- MacOS is now officialy supported, default compile time vars have been added
|
- MacOS is now officialy supported, default compile time vars have been added
|
||||||
for it
|
for it
|
||||||
|
@ -780,26 +373,6 @@ This input for example will prompt for more input to complete:
|
||||||
|
|
||||||
First "stable" release of Hilbish.
|
First "stable" release of Hilbish.
|
||||||
|
|
||||||
[2.3.3]: https://github.com/Rosettea/Hilbish/compare/v2.3.2...v2.3.3
|
|
||||||
[2.3.2]: https://github.com/Rosettea/Hilbish/compare/v2.3.1...v2.3.2
|
|
||||||
[2.3.1]: https://github.com/Rosettea/Hilbish/compare/v2.3.0...v2.3.1
|
|
||||||
[2.3.0]: https://github.com/Rosettea/Hilbish/compare/v2.2.3...v2.3.0
|
|
||||||
[2.2.3]: https://github.com/Rosettea/Hilbish/compare/v2.2.2...v2.2.3
|
|
||||||
[2.2.2]: https://github.com/Rosettea/Hilbish/compare/v2.2.1...v2.2.2
|
|
||||||
[2.2.1]: https://github.com/Rosettea/Hilbish/compare/v2.2.0...v2.2.1
|
|
||||||
[2.2.0]: https://github.com/Rosettea/Hilbish/compare/v2.1.0...v2.2.0
|
|
||||||
[2.1.2]: https://github.com/Rosettea/Hilbish/compare/v2.1.1...v2.1.2
|
|
||||||
[2.1.1]: https://github.com/Rosettea/Hilbish/compare/v2.1.0...v2.1.1
|
|
||||||
[2.1.0]: https://github.com/Rosettea/Hilbish/compare/v2.0.1...v2.1.0
|
|
||||||
[2.0.1]: https://github.com/Rosettea/Hilbish/compare/v2.0.0...v2.0.1
|
|
||||||
[2.0.0]: https://github.com/Rosettea/Hilbish/compare/v1.2.0...v2.0.0
|
|
||||||
[2.0.0-rc1]: https://github.com/Rosettea/Hilbish/compare/v1.2.0...v2.0.0-rc1
|
|
||||||
[1.2.0]: https://github.com/Rosettea/Hilbish/compare/v1.1.4...v1.2.0
|
|
||||||
[1.1.0]: https://github.com/Rosettea/Hilbish/compare/v1.0.4...v1.1.0
|
|
||||||
[1.0.4]: https://github.com/Rosettea/Hilbish/compare/v1.0.3...v1.0.4
|
|
||||||
[1.0.3]: https://github.com/Rosettea/Hilbish/compare/v1.0.2...v1.0.3
|
|
||||||
[1.0.2]: https://github.com/Rosettea/Hilbish/compare/v1.0.1...v1.0.2
|
|
||||||
[1.0.1]: https://github.com/Rosettea/Hilbish/compare/v1.0.0...v1.0.1
|
|
||||||
[1.0.0]: https://github.com/Rosettea/Hilbish/compare/v0.7.1...v1.0.0
|
[1.0.0]: https://github.com/Rosettea/Hilbish/compare/v0.7.1...v1.0.0
|
||||||
[0.7.1]: https://github.com/Rosettea/Hilbish/compare/v0.7.0...v0.7.1
|
[0.7.1]: https://github.com/Rosettea/Hilbish/compare/v0.7.0...v0.7.1
|
||||||
[0.7.0]: https://github.com/Rosettea/Hilbish/compare/v0.6.1...v0.7.0
|
[0.7.0]: https://github.com/Rosettea/Hilbish/compare/v0.6.1...v0.7.0
|
||||||
|
|
|
@ -29,9 +29,8 @@ For any code contributions (Lua and/or Go), you should follow these rules:
|
||||||
1. Ensure that any new install or build dependencies are documented in
|
1. Ensure that any new install or build dependencies are documented in
|
||||||
the README.md and pull request.
|
the README.md and pull request.
|
||||||
|
|
||||||
2. Mention any and all changes, feature additons, removals, etc. This includes
|
2. Mention any and all changes, this includes useful file locations and
|
||||||
useful file locations and breaking changes. Document them in the [changelog](CHANGELOG.md)
|
breaking changes.
|
||||||
in the pull request.
|
|
||||||
|
|
||||||
3. We use [Semver](http://semver.org/) for versioning and
|
3. We use [Semver](http://semver.org/) for versioning and
|
||||||
[Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
|
[Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
|
||||||
|
|
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2021-2023 Rosettea
|
Copyright (c) 2022 Rosettea
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
PREFIX ?= /usr
|
||||||
|
DESTDIR ?=
|
||||||
|
BINDIR ?= $(PREFIX)/bin
|
||||||
|
LIBDIR ?= $(PREFIX)/share/hilbish
|
||||||
|
|
||||||
|
build:
|
||||||
|
@go build -ldflags "-s -w"
|
||||||
|
|
||||||
|
dev:
|
||||||
|
@go build -ldflags "-s -w -X main.version=$(shell git describe --tags)"
|
||||||
|
|
||||||
|
install:
|
||||||
|
@install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish"
|
||||||
|
@mkdir -p "$(DESTDIR)$(LIBDIR)"
|
||||||
|
@cp libs docs emmyLuaDocs prelude .hilbishrc.lua "$(DESTDIR)$(LIBDIR)" -r
|
||||||
|
@grep "$(DESTDIR)$(BINDIR)/hilbish" -qxF /etc/shells || echo "$(DESTDIR)$(BINDIR)/hilbish" >> /etc/shells
|
||||||
|
@echo "Hilbish Installed"
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
@rm -vrf \
|
||||||
|
"$(DESTDIR)$(BINDIR)/hilbish" \
|
||||||
|
"$(DESTDIR)$(LIBDIR)"
|
||||||
|
@sed -i '/hilbish/d' /etc/shells
|
||||||
|
@echo "Hilbish Uninstalled"
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@go clean
|
||||||
|
|
||||||
|
all: build install
|
||||||
|
|
||||||
|
.PHONY: install uninstall build dev clean
|
102
README.md
|
@ -1,52 +1,62 @@
|
||||||
> [!TIP]
|
<div align="center">
|
||||||
> Check out [Hilbish: Midnight Edition](https://github.com/Rosettea/Hilbish/tree/midnight-edition) if you want to use C Lua, LuaJIT or anything related!
|
<img src="./assets/hilbish-flower.png" width=128><br>
|
||||||
|
<img src="./assets/hilbish-text.png" width=256><br>
|
||||||
|
<blockquote>
|
||||||
|
🌺 The flower shell. A comfy and nice little shell for Lua users and fans!
|
||||||
|
</blockquote>
|
||||||
|
<p align="center">
|
||||||
|
<img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/Rosettea/Hilbish?style=flat-square">
|
||||||
|
<img alt="GitHub commits since latest release (by date)" src="https://img.shields.io/github/commits-since/Rosettea/Hilbish/latest?style=flat-square">
|
||||||
|
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/Rosettea/Hilbish?style=flat-square"><br>
|
||||||
|
<a href="https://github.com/Rosettea/Hilbish/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22"><img src="https://img.shields.io/github/issues/Hilbis/Hilbish/help%20wanted?style=flat-square&color=green" alt="help wanted"></a>
|
||||||
|
<a href="https://github.com/Rosettea/Hilbish/blob/master/LICENSE"><img alt="GitHub license" src="https://img.shields.io/github/license/Rosettea/Hilbish?style=flat-square"></a>
|
||||||
|
<a href="https://discord.gg/3PDdcQz"><img alt="Discord" src="https://img.shields.io/discord/732357621503229962?color=blue&style=flat-square"></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<img src="./assets/hilbish-logo-and-text.png" width=512><br>
|
Hilbish is a Unix-y shell which uses Lua for scripting. Things like the prompt,
|
||||||
<blockquote>
|
general configuration and such are done with Lua.
|
||||||
🌓 The Moon-powered shell! A comfy and extensible shell for Lua fans! 🌺 ✨
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/Rosettea/Hilbish?style=flat-square"><img alt="GitHub commits since latest release (by date)" src="https://img.shields.io/github/commits-since/Rosettea/Hilbish/latest?style=flat-square"><img alt="GitHub contributors" src="https://img.shields.io/github/contributors/Rosettea/Hilbish?style=flat-square"><br>
|
For interactive use, it uses a library to run sh which works on all
|
||||||
<a href="https://github.com/Rosettea/Hilbish/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22"><img src="https://img.shields.io/github/issues/Hilbis/Hilbish/help%20wanted?style=flat-square&color=green" alt="help wanted"></a>
|
platforms Hilbish can be compiled for. It can also act as a Lua REPL if you want
|
||||||
<a href="https://github.com/Rosettea/Hilbish/blob/master/LICENSE"><img alt="GitHub license" src="https://img.shields.io/github/license/Rosettea/Hilbish?style=flat-square"></a>
|
it to be.
|
||||||
<a href="https://discord.gg/3PDdcQz"><img alt="Discord" src="https://img.shields.io/discord/732357621503229962?color=blue&style=flat-square"></a>
|
|
||||||
<br>
|
|
||||||
|
|
||||||
Hilbish is an extensible shell designed to be highly customizable.
|
|
||||||
|
|
||||||
It is configured in Lua, and provides a good range of features.
|
|
||||||
It aims to be easy to use for anyone, and powerful enough for
|
|
||||||
those who need more.
|
|
||||||
|
|
||||||
The motivation for choosing Lua was that its simpler and better to use
|
|
||||||
than old shell scripts. It's fine for basic interactive shell uses,
|
|
||||||
and supports [both Lua and Sh interactively](https://rosettea.github.io/Hilbish/docs/features/runner-mode/).
|
|
||||||
|
|
||||||
That's the only place Hilbish can use traditional shell syntax though;
|
|
||||||
everything else is Lua and aims to be infinitely configurable.
|
|
||||||
|
|
||||||
If something isn't, open an issue!
|
|
||||||
|
|
||||||
# Screenshots
|
# Screenshots
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<img src="gallery/tab.png">
|
<img src="gallery/default.png"><br><br>
|
||||||
|
<img src="gallery/terminal.png"><br><br>
|
||||||
|
<img src="gallery/pillprompt.png">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
# Getting Hilbish
|
# Installation
|
||||||
**NOTE:** Hilbish is not guaranteed to work properly on Windows, starting
|
**NOTE:** Hilbish is currently only officially supported and tested on Linux
|
||||||
from the 2.0 version. It will still be able to compile, but functionality
|
|
||||||
may be lacking. If you want to contribute to make the situation better,
|
|
||||||
comment on the Windows discussion.
|
|
||||||
|
|
||||||
You can check the [install page](https://rosettea.github.io/Hilbish/install/)
|
## Prebuilt binaries
|
||||||
on the website for distributed binaries from GitHub or other package repositories.
|
Go [here](https://nightly.link/Rosettea/Hilbish/workflows/build/master) for
|
||||||
Otherwise, continue reading for steps on compiling.
|
builds on the master branch.
|
||||||
|
|
||||||
## Prerequisites
|
## AUR
|
||||||
- [Go 1.22+](https://go.dev)
|
[![AUR maintainer](https://img.shields.io/aur/maintainer/hilbish?logo=arch-linux&style=flat-square)](https://aur.archlinux.org/packages/hilbish)
|
||||||
- [Task](https://taskfile.dev/installation/) (**Go on the hyperlink here to see Task's install method for your OS.**)
|
Arch Linux users can install Hilbish from the AUR with the following command:
|
||||||
|
```sh
|
||||||
|
yay -S hilbish
|
||||||
|
```
|
||||||
|
|
||||||
## Build
|
[![AUR maintainer](https://img.shields.io/aur/maintainer/hilbish?logo=arch-linux&style=flat-square)](https://aur.archlinux.org/packages/hilbish-git)
|
||||||
|
Or from the latest `master` commit with:
|
||||||
|
```sh
|
||||||
|
yay -S hilbish-git
|
||||||
|
```
|
||||||
|
|
||||||
|
## Nixpkgs
|
||||||
|
Nix/NixOS users can install Hilbish from the central repository, nixpkgs, through the usual ways.
|
||||||
|
If you're new to nix you should probably read up on how to do that [here](https://nixos.wiki/wiki/Cheatsheet).
|
||||||
|
|
||||||
|
## Manual Build
|
||||||
|
### Prerequisites
|
||||||
|
- [Go 1.17+](https://go.dev)
|
||||||
|
|
||||||
|
#### Build
|
||||||
First, clone Hilbish. The recursive is required, as some Lua libraries
|
First, clone Hilbish. The recursive is required, as some Lua libraries
|
||||||
are submodules.
|
are submodules.
|
||||||
```sh
|
```sh
|
||||||
|
@ -57,24 +67,24 @@ go get -d ./...
|
||||||
|
|
||||||
To build, run:
|
To build, run:
|
||||||
```
|
```
|
||||||
task
|
make dev
|
||||||
```
|
```
|
||||||
|
|
||||||
Or, if you want a stable branch, run these commands:
|
Or, if you want a stable branch, run these commands:
|
||||||
```
|
```
|
||||||
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
|
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
|
||||||
task build
|
make build
|
||||||
```
|
```
|
||||||
|
|
||||||
After you did all that, run `sudo task install` to install Hilbish globally.
|
After you did all that, run `sudo make install` to install Hilbish globally.
|
||||||
|
|
||||||
# Contributing
|
# Contributing
|
||||||
Any kind of contributions are welcome! Hilbish is very easy to contribute to.
|
Any kind of contributions to Hilbish are welcome!
|
||||||
Read [CONTRIBUTING.md](CONTRIBUTING.md) as a guideline to doing so.
|
Read [CONTRIBUTING.md](CONTRIBUTING.md) before getting started.
|
||||||
|
|
||||||
**Thanks to everyone below who's contributed!**
|
**Thanks to everyone below who's contributed!**
|
||||||
<a href="https://github.com/Rosettea/Hilbish/graphs/contributors">
|
<a href="https://github.com/Hilbis/Hilbish/graphs/contributors">
|
||||||
<img src="https://contrib.rocks/image?repo=Rosettea/Hilbish" />
|
<img src="https://contrib.rocks/image?repo=Hilbis/Hilbish" />
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
*Made with [contributors-img](https://contrib.rocks).*
|
*Made with [contributors-img](https://contrib.rocks).*
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
# https://taskfile.dev
|
|
||||||
|
|
||||||
version: '3'
|
|
||||||
|
|
||||||
vars:
|
|
||||||
PREFIX: '{{default "/usr/local" .PREFIX}}'
|
|
||||||
bindir__: '{{.PREFIX}}/bin'
|
|
||||||
BINDIR: '{{default .bindir__ .BINDIR}}'
|
|
||||||
libdir__: '{{.PREFIX}}/share/hilbish'
|
|
||||||
LIBDIR: '{{default .libdir__ .LIBDIR}}'
|
|
||||||
goflags__: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}}"'
|
|
||||||
GOFLAGS: '{{default .goflags__ .GOFLAGS}}'
|
|
||||||
|
|
||||||
tasks:
|
|
||||||
default:
|
|
||||||
cmds:
|
|
||||||
- go build {{.GOFLAGS}}
|
|
||||||
vars:
|
|
||||||
GOFLAGS: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}} -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"'
|
|
||||||
|
|
||||||
default-nocgo:
|
|
||||||
cmds:
|
|
||||||
- CGO_ENABLED=0 go build {{.GOFLAGS}}
|
|
||||||
vars:
|
|
||||||
GOFLAGS: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}} -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"'
|
|
||||||
|
|
||||||
build:
|
|
||||||
cmds:
|
|
||||||
- go build {{.GOFLAGS}}
|
|
||||||
|
|
||||||
build-nocgo:
|
|
||||||
cmds:
|
|
||||||
- CGO_ENABLED=0 go build {{.GOFLAGS}}
|
|
||||||
|
|
||||||
install:
|
|
||||||
cmds:
|
|
||||||
- install -v -d "{{.DESTDIR}}{{.BINDIR}}/" && install -m 0755 -v hilbish "{{.DESTDIR}}{{.BINDIR}}/hilbish"
|
|
||||||
- mkdir -p "{{.DESTDIR}}{{.LIBDIR}}"
|
|
||||||
- cp -r libs docs emmyLuaDocs nature .hilbishrc.lua {{.DESTDIR}}{{.LIBDIR}}
|
|
||||||
- grep -qxF "{{.DESTDIR}}{{.BINDIR}}/hilbish" /etc/shells || echo "{{.DESTDIR}}{{.BINDIR}}/hilbish" >> /etc/shells
|
|
||||||
|
|
||||||
uninstall:
|
|
||||||
cmds:
|
|
||||||
- rm -vrf
|
|
||||||
"{{.DESTDIR}}{{.BINDIR}}/hilbish"
|
|
||||||
"{{.DESTDIR}}{{.LIBDIR}}"
|
|
||||||
- grep -v 'hilbish' /etc/shells > /tmp/shells.hilbish_uninstall && mv /tmp/shells.hilbish_uninstall /etc/shells
|
|
159
aliases.go
|
@ -1,87 +1,60 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"hilbish/util"
|
"github.com/yuin/gopher-lua"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var aliases *aliasModule
|
var aliases *hilbishAliases
|
||||||
|
|
||||||
type aliasModule struct {
|
type hilbishAliases struct {
|
||||||
aliases map[string]string
|
aliases map[string]string
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize aliases map
|
// initialize aliases map
|
||||||
func newAliases() *aliasModule {
|
func NewAliases() *hilbishAliases {
|
||||||
return &aliasModule{
|
return &hilbishAliases{
|
||||||
aliases: make(map[string]string),
|
aliases: make(map[string]string),
|
||||||
mu: &sync.RWMutex{},
|
mu: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aliasModule) Add(alias, cmd string) {
|
func (h *hilbishAliases) Add(alias, cmd string) {
|
||||||
a.mu.Lock()
|
h.mu.Lock()
|
||||||
defer a.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
a.aliases[alias] = cmd
|
h.aliases[alias] = cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aliasModule) All() map[string]string {
|
func (h *hilbishAliases) All() map[string]string {
|
||||||
return a.aliases
|
return h.aliases
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aliasModule) Delete(alias string) {
|
func (h *hilbishAliases) Delete(alias string) {
|
||||||
a.mu.Lock()
|
h.mu.Lock()
|
||||||
defer a.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
delete(a.aliases, alias)
|
delete(h.aliases, alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aliasModule) Resolve(cmdstr string) string {
|
func (h *hilbishAliases) Resolve(cmdstr string) string {
|
||||||
a.mu.RLock()
|
h.mu.RLock()
|
||||||
defer a.mu.RUnlock()
|
defer h.mu.RUnlock()
|
||||||
|
|
||||||
arg, _ := regexp.Compile(`[\\]?%\d+`)
|
|
||||||
|
|
||||||
args, _ := splitInput(cmdstr)
|
|
||||||
if len(args) == 0 {
|
|
||||||
// this shouldnt reach but...????
|
|
||||||
return cmdstr
|
|
||||||
}
|
|
||||||
|
|
||||||
for a.aliases[args[0]] != "" {
|
|
||||||
alias := a.aliases[args[0]]
|
|
||||||
alias = arg.ReplaceAllStringFunc(alias, func(a string) string {
|
|
||||||
idx, _ := strconv.Atoi(a[1:])
|
|
||||||
if strings.HasPrefix(a, "\\") || idx == 0 {
|
|
||||||
return strings.TrimPrefix(a, "\\")
|
|
||||||
}
|
|
||||||
|
|
||||||
if idx + 1 > len(args) {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
val := args[idx]
|
|
||||||
args = cut(args, idx)
|
|
||||||
cmdstr = strings.Join(args, " ")
|
|
||||||
|
|
||||||
return val
|
|
||||||
})
|
|
||||||
|
|
||||||
|
args := strings.Split(cmdstr, " ")
|
||||||
|
for h.aliases[args[0]] != "" {
|
||||||
|
alias := h.aliases[args[0]]
|
||||||
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
||||||
cmdArgs, _ := splitInput(cmdstr)
|
cmdArgs, _ := splitInput(cmdstr)
|
||||||
args = cmdArgs
|
args = cmdArgs
|
||||||
|
|
||||||
if a.aliases[args[0]] == alias {
|
if h.aliases[args[0]] == alias {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if a.aliases[args[0]] != "" {
|
if h.aliases[args[0]] != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,83 +64,41 @@ func (a *aliasModule) Resolve(cmdstr string) string {
|
||||||
|
|
||||||
// lua section
|
// lua section
|
||||||
|
|
||||||
// #interface aliases
|
func (h *hilbishAliases) Loader(L *lua.LState) *lua.LTable {
|
||||||
// command aliasing
|
|
||||||
// The alias interface deals with all command aliases in Hilbish.
|
|
||||||
func (a *aliasModule) Loader(rtm *rt.Runtime) *rt.Table {
|
|
||||||
// create a lua module with our functions
|
// create a lua module with our functions
|
||||||
hshaliasesLua := map[string]util.LuaExport{
|
hshaliasesLua := map[string]lua.LGFunction{
|
||||||
"add": util.LuaExport{hlalias, 2, false},
|
"add": h.luaAdd,
|
||||||
"list": util.LuaExport{a.luaList, 0, false},
|
"list": h.luaList,
|
||||||
"del": util.LuaExport{a.luaDelete, 1, false},
|
"del": h.luaDelete,
|
||||||
"resolve": util.LuaExport{a.luaResolve, 1, false},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mod := rt.NewTable()
|
mod := L.SetFuncs(L.NewTable(), hshaliasesLua)
|
||||||
util.SetExports(rtm, mod, hshaliasesLua)
|
|
||||||
|
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
|
|
||||||
// #interface aliases
|
func (h *hilbishAliases) luaAdd(L *lua.LState) int {
|
||||||
// add(alias, cmd)
|
alias := L.CheckString(1)
|
||||||
// This is an alias (ha) for the [hilbish.alias](../#alias) function.
|
cmd := L.CheckString(2)
|
||||||
// --- @param alias string
|
h.Add(alias, cmd)
|
||||||
// --- @param cmd string
|
|
||||||
func _hlalias() {}
|
|
||||||
|
|
||||||
// #interface aliases
|
return 0
|
||||||
// list() -> table[string, string]
|
|
||||||
// Get a table of all aliases, with string keys as the alias and the value as the command.
|
|
||||||
// #returns table[string, string]
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
hilbish.aliases.add('hi', 'echo hi')
|
|
||||||
|
|
||||||
local aliases = hilbish.aliases.list()
|
|
||||||
-- -> {hi = 'echo hi'}
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (a *aliasModule) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
aliasesList := rt.NewTable()
|
|
||||||
for k, v := range a.All() {
|
|
||||||
aliasesList.Set(rt.StringValue(k), rt.StringValue(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(aliasesList)), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// #interface aliases
|
func (h *hilbishAliases) luaList(L *lua.LState) int {
|
||||||
// delete(name)
|
aliasesList := L.NewTable()
|
||||||
// Removes an alias.
|
for k, v := range h.All() {
|
||||||
// #param name string
|
aliasesList.RawSetString(k, lua.LString(v))
|
||||||
func (a *aliasModule) luaDelete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
alias, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
a.Delete(alias)
|
|
||||||
|
|
||||||
return c.Next(), nil
|
L.Push(aliasesList)
|
||||||
|
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// #interface aliases
|
func (h *hilbishAliases) luaDelete(L *lua.LState) int {
|
||||||
// resolve(alias) -> string?
|
alias := L.CheckString(1)
|
||||||
// Resolves an alias to its original command. Will thrown an error if the alias doesn't exist.
|
h.Delete(alias)
|
||||||
// #param alias string
|
|
||||||
// #returns string
|
|
||||||
func (a *aliasModule) luaResolve(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
alias, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
resolved := a.Resolve(alias)
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.StringValue(resolved)), nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 18 KiB |
|
@ -7,301 +7,29 @@ import (
|
||||||
"go/doc"
|
"go/doc"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
|
|
||||||
md "github.com/atsushinee/go-markdown-generator/doc"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var header = `---
|
type EmmyPiece struct {
|
||||||
title: %s %s
|
|
||||||
description: %s
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
`
|
|
||||||
|
|
||||||
type emmyPiece struct {
|
|
||||||
DocPiece *docPiece
|
|
||||||
Annotations []string
|
|
||||||
Params []string // we only need to know param name to put in function
|
|
||||||
FuncName string
|
FuncName string
|
||||||
|
Docs []string
|
||||||
|
Params []string // we only need to know param name to put in function
|
||||||
}
|
}
|
||||||
|
type DocPiece struct {
|
||||||
type module struct {
|
|
||||||
Types []docPiece
|
|
||||||
Docs []docPiece
|
|
||||||
Fields []docPiece
|
|
||||||
Properties []docPiece
|
|
||||||
ShortDescription string
|
|
||||||
Description string
|
|
||||||
ParentModule string
|
|
||||||
HasInterfaces bool
|
|
||||||
HasTypes bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type param struct{
|
|
||||||
Name string
|
|
||||||
Type string
|
|
||||||
Doc []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type docPiece struct {
|
|
||||||
Doc []string
|
Doc []string
|
||||||
FuncSig string
|
FuncSig string
|
||||||
FuncName string
|
FuncName string
|
||||||
Interfacing string
|
|
||||||
ParentModule string
|
|
||||||
GoFuncName string
|
|
||||||
IsInterface bool
|
|
||||||
IsMember bool
|
|
||||||
IsType bool
|
|
||||||
Fields []docPiece
|
|
||||||
Properties []docPiece
|
|
||||||
Params []param
|
|
||||||
Tags map[string][]tag
|
|
||||||
}
|
|
||||||
|
|
||||||
type tag struct {
|
|
||||||
id string
|
|
||||||
fields []string
|
|
||||||
startIdx int
|
|
||||||
}
|
|
||||||
|
|
||||||
var docs = make(map[string]module)
|
|
||||||
var interfaceDocs = make(map[string]module)
|
|
||||||
var emmyDocs = make(map[string][]emmyPiece)
|
|
||||||
var typeTable = make(map[string][]string) // [0] = parentMod, [1] = interfaces
|
|
||||||
var prefix = map[string]string{
|
|
||||||
"main": "hl",
|
|
||||||
"hilbish": "hl",
|
|
||||||
"fs": "f",
|
|
||||||
"commander": "c",
|
|
||||||
"bait": "b",
|
|
||||||
"terminal": "term",
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTagsAndDocs(docs string) (map[string][]tag, []string) {
|
|
||||||
pts := strings.Split(docs, "\n")
|
|
||||||
parts := []string{}
|
|
||||||
tags := make(map[string][]tag)
|
|
||||||
|
|
||||||
for idx, part := range pts {
|
|
||||||
if strings.HasPrefix(part, "#") {
|
|
||||||
tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ")
|
|
||||||
if tags[tagParts[0]] == nil {
|
|
||||||
var id string
|
|
||||||
if len(tagParts) > 1 {
|
|
||||||
id = tagParts[1]
|
|
||||||
}
|
|
||||||
tags[tagParts[0]] = []tag{
|
|
||||||
{id: id, startIdx: idx},
|
|
||||||
}
|
|
||||||
if len(tagParts) >= 2 {
|
|
||||||
tags[tagParts[0]][0].fields = tagParts[2:]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if tagParts[0] == "example" {
|
|
||||||
exampleIdx := tags["example"][0].startIdx
|
|
||||||
exampleCode := pts[exampleIdx+1:idx]
|
|
||||||
|
|
||||||
tags["example"][0].fields = exampleCode
|
|
||||||
parts = strings.Split(strings.Replace(strings.Join(parts, "\n"), strings.TrimPrefix(strings.Join(exampleCode, "\n"), "#example\n"), "", -1), "\n")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fleds := []string{}
|
|
||||||
if len(tagParts) >= 2 {
|
|
||||||
fleds = tagParts[2:]
|
|
||||||
}
|
|
||||||
tags[tagParts[0]] = append(tags[tagParts[0]], tag{
|
|
||||||
id: tagParts[1],
|
|
||||||
fields: fleds,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
parts = append(parts, part)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tags, parts
|
|
||||||
}
|
|
||||||
|
|
||||||
func docPieceTag(tagName string, tags map[string][]tag) []docPiece {
|
|
||||||
dps := []docPiece{}
|
|
||||||
for _, tag := range tags[tagName] {
|
|
||||||
dps = append(dps, docPiece{
|
|
||||||
FuncName: tag.id,
|
|
||||||
Doc: tag.fields,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return dps
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupDocType(mod string, typ *doc.Type) *docPiece {
|
|
||||||
docs := strings.TrimSpace(typ.Doc)
|
|
||||||
tags, doc := getTagsAndDocs(docs)
|
|
||||||
|
|
||||||
if tags["type"] == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
inInterface := tags["interface"] != nil
|
|
||||||
|
|
||||||
var interfaces string
|
|
||||||
typeName := strings.ToUpper(string(typ.Name[0])) + typ.Name[1:]
|
|
||||||
typeDoc := []string{}
|
|
||||||
|
|
||||||
if inInterface {
|
|
||||||
interfaces = tags["interface"][0].id
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := docPieceTag("field", tags)
|
|
||||||
properties := docPieceTag("property", tags)
|
|
||||||
|
|
||||||
for _, d := range doc {
|
|
||||||
if strings.HasPrefix(d, "---") {
|
|
||||||
// TODO: document types in lua
|
|
||||||
/*
|
|
||||||
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
|
||||||
emmyLinePieces := strings.Split(emmyLine, " ")
|
|
||||||
emmyType := emmyLinePieces[0]
|
|
||||||
if emmyType == "@param" {
|
|
||||||
em.Params = append(em.Params, emmyLinePieces[1])
|
|
||||||
}
|
|
||||||
if emmyType == "@vararg" {
|
|
||||||
em.Params = append(em.Params, "...") // add vararg
|
|
||||||
}
|
|
||||||
em.Annotations = append(em.Annotations, d)
|
|
||||||
*/
|
|
||||||
} else {
|
|
||||||
typeDoc = append(typeDoc, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var isMember bool
|
|
||||||
if tags["member"] != nil {
|
|
||||||
isMember = true
|
|
||||||
}
|
|
||||||
parentMod := mod
|
|
||||||
dps := &docPiece{
|
|
||||||
Doc: typeDoc,
|
|
||||||
FuncName: typeName,
|
|
||||||
Interfacing: interfaces,
|
|
||||||
IsInterface: inInterface,
|
|
||||||
IsMember: isMember,
|
|
||||||
IsType: true,
|
|
||||||
ParentModule: parentMod,
|
|
||||||
Fields: fields,
|
|
||||||
Properties: properties,
|
|
||||||
Tags: tags,
|
|
||||||
}
|
|
||||||
|
|
||||||
typeTable[strings.ToLower(typeName)] = []string{parentMod, interfaces}
|
|
||||||
|
|
||||||
return dps
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupDoc(mod string, fun *doc.Func) *docPiece {
|
|
||||||
docs := strings.TrimSpace(fun.Doc)
|
|
||||||
tags, parts := getTagsAndDocs(docs)
|
|
||||||
|
|
||||||
// i couldnt fit this into the condition below for some reason so here's a goto!
|
|
||||||
if tags["member"] != nil {
|
|
||||||
goto start
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!strings.HasPrefix(fun.Name, prefix[mod]) && tags["interface"] == nil) || (strings.ToLower(fun.Name) == "loader" && tags["interface"] == nil) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
start:
|
|
||||||
inInterface := tags["interface"] != nil
|
|
||||||
var interfaces string
|
|
||||||
funcsig := parts[0]
|
|
||||||
doc := parts[1:]
|
|
||||||
funcName := strings.TrimPrefix(fun.Name, prefix[mod])
|
|
||||||
funcdoc := []string{}
|
|
||||||
|
|
||||||
if inInterface {
|
|
||||||
interfaces = tags["interface"][0].id
|
|
||||||
funcName = interfaces + "." + strings.Split(funcsig, "(")[0]
|
|
||||||
}
|
|
||||||
em := emmyPiece{FuncName: funcName}
|
|
||||||
|
|
||||||
fields := docPieceTag("field", tags)
|
|
||||||
properties := docPieceTag("property", tags)
|
|
||||||
var params []param
|
|
||||||
if paramsRaw := tags["param"]; paramsRaw != nil {
|
|
||||||
params = make([]param, len(paramsRaw))
|
|
||||||
for i, p := range paramsRaw {
|
|
||||||
params[i] = param{
|
|
||||||
Name: p.id,
|
|
||||||
Type: p.fields[0],
|
|
||||||
Doc: p.fields[1:],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range doc {
|
|
||||||
if strings.HasPrefix(d, "---") {
|
|
||||||
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
|
||||||
emmyLinePieces := strings.Split(emmyLine, " ")
|
|
||||||
emmyType := emmyLinePieces[0]
|
|
||||||
if emmyType == "@param" {
|
|
||||||
em.Params = append(em.Params, emmyLinePieces[1])
|
|
||||||
}
|
|
||||||
if emmyType == "@vararg" {
|
|
||||||
em.Params = append(em.Params, "...") // add vararg
|
|
||||||
}
|
|
||||||
em.Annotations = append(em.Annotations, d)
|
|
||||||
} else {
|
|
||||||
funcdoc = append(funcdoc, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var isMember bool
|
|
||||||
if tags["member"] != nil {
|
|
||||||
isMember = true
|
|
||||||
}
|
|
||||||
var parentMod string
|
|
||||||
if inInterface {
|
|
||||||
parentMod = mod
|
|
||||||
}
|
|
||||||
dps := &docPiece{
|
|
||||||
Doc: funcdoc,
|
|
||||||
FuncSig: funcsig,
|
|
||||||
FuncName: funcName,
|
|
||||||
Interfacing: interfaces,
|
|
||||||
GoFuncName: strings.ToLower(fun.Name),
|
|
||||||
IsInterface: inInterface,
|
|
||||||
IsMember: isMember,
|
|
||||||
ParentModule: parentMod,
|
|
||||||
Fields: fields,
|
|
||||||
Properties: properties,
|
|
||||||
Params: params,
|
|
||||||
Tags: tags,
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(dps.GoFuncName, strings.ToLower("loader")) {
|
|
||||||
dps.Doc = parts
|
|
||||||
}
|
|
||||||
em.DocPiece = dps
|
|
||||||
|
|
||||||
emmyDocs[mod] = append(emmyDocs[mod], em)
|
|
||||||
return dps
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// feel free to clean this up
|
||||||
|
// it works, dont really care about the code
|
||||||
func main() {
|
func main() {
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
os.Mkdir("docs", 0777)
|
os.Mkdir("docs", 0777)
|
||||||
os.Mkdir("docs/api", 0777)
|
|
||||||
os.Mkdir("emmyLuaDocs", 0777)
|
os.Mkdir("emmyLuaDocs", 0777)
|
||||||
|
|
||||||
|
|
||||||
dirs := []string{"./"}
|
dirs := []string{"./"}
|
||||||
filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error {
|
filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error {
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
|
@ -323,356 +51,114 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interfaceModules := make(map[string]*module)
|
prefix := map[string]string{
|
||||||
|
"hilbish": "hl",
|
||||||
|
"fs": "f",
|
||||||
|
"commander": "c",
|
||||||
|
"bait": "b",
|
||||||
|
"terminal": "term",
|
||||||
|
}
|
||||||
|
docs := make(map[string][]DocPiece)
|
||||||
|
emmyDocs := make(map[string][]EmmyPiece)
|
||||||
|
|
||||||
for l, f := range pkgs {
|
for l, f := range pkgs {
|
||||||
p := doc.New(f, "./", doc.AllDecls)
|
p := doc.New(f, "./", doc.AllDecls)
|
||||||
pieces := []docPiece{}
|
|
||||||
typePieces := []docPiece{}
|
|
||||||
mod := l
|
|
||||||
if mod == "main" {
|
|
||||||
mod = "hilbish"
|
|
||||||
}
|
|
||||||
var hasInterfaces bool
|
|
||||||
for _, t := range p.Funcs {
|
for _, t := range p.Funcs {
|
||||||
piece := setupDoc(mod, t)
|
mod := l
|
||||||
if piece == nil {
|
if strings.HasPrefix(t.Name, "hl") { mod = "hilbish" }
|
||||||
continue
|
if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { continue }
|
||||||
|
parts := strings.Split(strings.TrimSpace(t.Doc), "\n")
|
||||||
|
funcsig := parts[0]
|
||||||
|
doc := parts[1:]
|
||||||
|
funcdoc := []string{}
|
||||||
|
em := EmmyPiece{FuncName: strings.TrimPrefix(t.Name, prefix[mod])}
|
||||||
|
for _, d := range doc {
|
||||||
|
if strings.HasPrefix(d, "---") {
|
||||||
|
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
||||||
|
emmyLinePieces := strings.Split(emmyLine, " ")
|
||||||
|
emmyType := emmyLinePieces[0]
|
||||||
|
if emmyType == "@param" {
|
||||||
|
em.Params = append(em.Params, emmyLinePieces[1])
|
||||||
|
}
|
||||||
|
em.Docs = append(em.Docs, d)
|
||||||
|
} else {
|
||||||
|
funcdoc = append(funcdoc, d)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pieces = append(pieces, *piece)
|
dps := DocPiece{
|
||||||
if piece.IsInterface {
|
Doc: funcdoc,
|
||||||
hasInterfaces = true
|
FuncSig: funcsig,
|
||||||
|
FuncName: strings.TrimPrefix(t.Name, prefix[mod]),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
docs[mod] = append(docs[mod], dps)
|
||||||
|
emmyDocs[mod] = append(emmyDocs[mod], em)
|
||||||
}
|
}
|
||||||
for _, t := range p.Types {
|
for _, t := range p.Types {
|
||||||
typePiece := setupDocType(mod, t)
|
|
||||||
if typePiece != nil {
|
|
||||||
typePieces = append(typePieces, *typePiece)
|
|
||||||
if typePiece.IsInterface {
|
|
||||||
hasInterfaces = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, m := range t.Methods {
|
for _, m := range t.Methods {
|
||||||
piece := setupDoc(mod, m)
|
if !strings.HasPrefix(m.Name, prefix[l]) || m.Name == "Loader" { continue }
|
||||||
if piece == nil {
|
parts := strings.Split(strings.TrimSpace(m.Doc), "\n")
|
||||||
continue
|
funcsig := parts[0]
|
||||||
|
doc := parts[1:]
|
||||||
|
funcdoc := []string{}
|
||||||
|
em := EmmyPiece{FuncName: strings.TrimPrefix(m.Name, prefix[l])}
|
||||||
|
for _, d := range doc {
|
||||||
|
if strings.HasPrefix(d, "---") {
|
||||||
|
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
||||||
|
emmyLinePieces := strings.Split(emmyLine, " ")
|
||||||
|
emmyType := emmyLinePieces[0]
|
||||||
|
if emmyType == "@param" {
|
||||||
|
em.Params = append(em.Params, emmyLinePieces[1])
|
||||||
|
}
|
||||||
|
em.Docs = append(em.Docs, d)
|
||||||
|
} else {
|
||||||
|
funcdoc = append(funcdoc, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dps := DocPiece{
|
||||||
|
Doc: funcdoc,
|
||||||
|
FuncSig: funcsig,
|
||||||
|
FuncName: strings.TrimPrefix(m.Name, prefix[l]),
|
||||||
}
|
}
|
||||||
|
|
||||||
pieces = append(pieces, *piece)
|
docs[l] = append(docs[l], dps)
|
||||||
if piece.IsInterface {
|
emmyDocs[l] = append(emmyDocs[l], em)
|
||||||
hasInterfaces = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags, descParts := getTagsAndDocs(strings.TrimSpace(p.Doc))
|
|
||||||
shortDesc := descParts[0]
|
|
||||||
desc := descParts[1:]
|
|
||||||
filteredPieces := []docPiece{}
|
|
||||||
filteredTypePieces := []docPiece{}
|
|
||||||
for _, piece := range pieces {
|
|
||||||
if !piece.IsInterface {
|
|
||||||
filteredPieces = append(filteredPieces, piece)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
modname := piece.ParentModule + "." + piece.Interfacing
|
|
||||||
if interfaceModules[modname] == nil {
|
|
||||||
interfaceModules[modname] = &module{
|
|
||||||
ParentModule: piece.ParentModule,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasSuffix(piece.GoFuncName, strings.ToLower("loader")) {
|
|
||||||
shortDesc := piece.Doc[0]
|
|
||||||
desc := piece.Doc[1:]
|
|
||||||
interfaceModules[modname].ShortDescription = shortDesc
|
|
||||||
interfaceModules[modname].Description = strings.Join(desc, "\n")
|
|
||||||
interfaceModules[modname].Fields = piece.Fields
|
|
||||||
interfaceModules[modname].Properties = piece.Properties
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
interfaceModules[modname].Docs = append(interfaceModules[modname].Docs, piece)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, piece := range typePieces {
|
|
||||||
if !piece.IsInterface {
|
|
||||||
filteredTypePieces = append(filteredTypePieces, piece)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
modname := piece.ParentModule + "." + piece.Interfacing
|
|
||||||
if interfaceModules[modname] == nil {
|
|
||||||
interfaceModules[modname] = &module{
|
|
||||||
ParentModule: piece.ParentModule,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interfaceModules[modname].Types = append(interfaceModules[modname].Types, piece)
|
|
||||||
}
|
|
||||||
|
|
||||||
docs[mod] = module{
|
|
||||||
Types: filteredTypePieces,
|
|
||||||
Docs: filteredPieces,
|
|
||||||
ShortDescription: shortDesc,
|
|
||||||
Description: strings.Join(desc, "\n"),
|
|
||||||
HasInterfaces: hasInterfaces,
|
|
||||||
Properties: docPieceTag("property", tags),
|
|
||||||
Fields: docPieceTag("field", tags),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, mod := range interfaceModules {
|
|
||||||
docs[key] = *mod
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(len(docs) * 2)
|
|
||||||
|
|
||||||
for mod, v := range docs {
|
for mod, v := range docs {
|
||||||
docPath := "docs/api/" + mod + ".md"
|
if mod == "main" { continue }
|
||||||
if v.HasInterfaces {
|
f, _ := os.Create("docs/" + mod + ".txt")
|
||||||
os.Mkdir("docs/api/" + mod, 0777)
|
for _, dps := range v {
|
||||||
os.Remove(docPath) // remove old doc path if it exists
|
f.WriteString(dps.FuncSig + " > ")
|
||||||
docPath = "docs/api/" + mod + "/_index.md"
|
for _, doc := range dps.Doc {
|
||||||
|
if !strings.HasPrefix(doc, "---") {
|
||||||
|
f.WriteString(doc + "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.WriteString("\n")
|
||||||
}
|
}
|
||||||
if v.ParentModule != "" {
|
|
||||||
docPath = "docs/api/" + v.ParentModule + "/" + mod + ".md"
|
|
||||||
}
|
|
||||||
|
|
||||||
go func(modname, docPath string, modu module) {
|
|
||||||
defer wg.Done()
|
|
||||||
modOrIface := "Module"
|
|
||||||
if modu.ParentModule != "" {
|
|
||||||
modOrIface = "Module"
|
|
||||||
}
|
|
||||||
lastHeader := ""
|
|
||||||
|
|
||||||
f, _ := os.Create(docPath)
|
|
||||||
f.WriteString(fmt.Sprintf(header, modOrIface, modname, modu.ShortDescription))
|
|
||||||
typeTag, _ := regexp.Compile(`\B@\w+`)
|
|
||||||
modDescription := typeTag.ReplaceAllStringFunc(strings.Replace(strings.Replace(modu.Description, "<", `\<`, -1), "{{\\<", "{{<", -1), func(typ string) string {
|
|
||||||
typName := typ[1:]
|
|
||||||
typLookup := typeTable[strings.ToLower(typName)]
|
|
||||||
ifaces := typLookup[0] + "." + typLookup[1] + "/"
|
|
||||||
if typLookup[1] == "" {
|
|
||||||
ifaces = ""
|
|
||||||
}
|
|
||||||
linkedTyp := fmt.Sprintf("/Hilbish/docs/api/%s/%s#%s", typLookup[0], ifaces, strings.ToLower(typName))
|
|
||||||
return fmt.Sprintf(`<a href="%s" style="text-decoration: none;">%s</a>`, linkedTyp, typName)
|
|
||||||
})
|
|
||||||
f.WriteString(fmt.Sprintf("## Introduction\n%s\n\n", modDescription))
|
|
||||||
if len(modu.Docs) != 0 {
|
|
||||||
funcCount := 0
|
|
||||||
for _, dps := range modu.Docs {
|
|
||||||
if dps.IsMember {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
funcCount++
|
|
||||||
}
|
|
||||||
|
|
||||||
f.WriteString("## Functions\n")
|
|
||||||
lastHeader = "functions"
|
|
||||||
|
|
||||||
mdTable := md.NewTable(funcCount, 2)
|
|
||||||
mdTable.SetTitle(0, "")
|
|
||||||
mdTable.SetTitle(1, "")
|
|
||||||
|
|
||||||
diff := 0
|
|
||||||
for i, dps := range modu.Docs {
|
|
||||||
if dps.IsMember {
|
|
||||||
diff++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
mdTable.SetContent(i - diff, 0, fmt.Sprintf(`<a href="#%s">%s</a>`, dps.FuncName, dps.FuncSig))
|
|
||||||
if len(dps.Doc) == 0 {
|
|
||||||
fmt.Printf("WARNING! Function %s on module %s has no documentation!\n", dps.FuncName, modname)
|
|
||||||
} else {
|
|
||||||
mdTable.SetContent(i - diff, 1, dps.Doc[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.WriteString(mdTable.String())
|
|
||||||
f.WriteString("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(modu.Fields) != 0 {
|
|
||||||
f.WriteString("## Static module fields\n")
|
|
||||||
|
|
||||||
mdTable := md.NewTable(len(modu.Fields), 2)
|
|
||||||
mdTable.SetTitle(0, "")
|
|
||||||
mdTable.SetTitle(1, "")
|
|
||||||
|
|
||||||
|
|
||||||
for i, dps := range modu.Fields {
|
|
||||||
mdTable.SetContent(i, 0, dps.FuncName)
|
|
||||||
mdTable.SetContent(i, 1, strings.Join(dps.Doc, " "))
|
|
||||||
}
|
|
||||||
f.WriteString(mdTable.String())
|
|
||||||
f.WriteString("\n")
|
|
||||||
}
|
|
||||||
if len(modu.Properties) != 0 {
|
|
||||||
f.WriteString("## Object properties\n")
|
|
||||||
|
|
||||||
mdTable := md.NewTable(len(modu.Fields), 2)
|
|
||||||
mdTable.SetTitle(0, "")
|
|
||||||
mdTable.SetTitle(1, "")
|
|
||||||
|
|
||||||
|
|
||||||
for i, dps := range modu.Properties {
|
|
||||||
mdTable.SetContent(i, 0, dps.FuncName)
|
|
||||||
mdTable.SetContent(i, 1, strings.Join(dps.Doc, " "))
|
|
||||||
}
|
|
||||||
f.WriteString(mdTable.String())
|
|
||||||
f.WriteString("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(modu.Docs) != 0 {
|
|
||||||
if lastHeader != "functions" {
|
|
||||||
f.WriteString("## Functions\n")
|
|
||||||
}
|
|
||||||
for _, dps := range modu.Docs {
|
|
||||||
if dps.IsMember {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
f.WriteString(fmt.Sprintf("<hr>\n<div id='%s'>", dps.FuncName))
|
|
||||||
htmlSig := typeTag.ReplaceAllStringFunc(strings.Replace(modname + "." + dps.FuncSig, "<", `\<`, -1), func(typ string) string {
|
|
||||||
typName := typ[1:]
|
|
||||||
typLookup := typeTable[strings.ToLower(typName)]
|
|
||||||
ifaces := typLookup[0] + "." + typLookup[1] + "/"
|
|
||||||
if typLookup[1] == "" {
|
|
||||||
ifaces = ""
|
|
||||||
}
|
|
||||||
linkedTyp := fmt.Sprintf("/Hilbish/docs/api/%s/%s#%s", typLookup[0], ifaces, strings.ToLower(typName))
|
|
||||||
return fmt.Sprintf(`<a href="%s" style="text-decoration: none;" id="lol">%s</a>`, linkedTyp, typName)
|
|
||||||
})
|
|
||||||
f.WriteString(fmt.Sprintf(`
|
|
||||||
<h4 class='heading'>
|
|
||||||
%s
|
|
||||||
<a href="#%s" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
`, htmlSig, dps.FuncName))
|
|
||||||
for _, doc := range dps.Doc {
|
|
||||||
if !strings.HasPrefix(doc, "---") && doc != "" {
|
|
||||||
f.WriteString(doc + " \n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.WriteString("\n#### Parameters\n")
|
|
||||||
if len(dps.Params) == 0 {
|
|
||||||
f.WriteString("This function has no parameters. \n")
|
|
||||||
}
|
|
||||||
for _, p := range dps.Params {
|
|
||||||
isVariadic := false
|
|
||||||
typ := p.Type
|
|
||||||
if strings.HasPrefix(p.Type, "...") {
|
|
||||||
isVariadic = true
|
|
||||||
typ = p.Type[3:]
|
|
||||||
}
|
|
||||||
|
|
||||||
f.WriteString(fmt.Sprintf("`%s` **`%s`**", typ, p.Name))
|
|
||||||
if isVariadic {
|
|
||||||
f.WriteString(" (This type is variadic. You can pass an infinite amount of parameters with this type.)")
|
|
||||||
}
|
|
||||||
f.WriteString(" \n")
|
|
||||||
f.WriteString(strings.Join(p.Doc, " "))
|
|
||||||
f.WriteString("\n\n")
|
|
||||||
}
|
|
||||||
if codeExample := dps.Tags["example"]; codeExample != nil {
|
|
||||||
f.WriteString("#### Example\n")
|
|
||||||
f.WriteString(fmt.Sprintf("```lua\n%s\n```\n", strings.Join(codeExample[0].fields, "\n")))
|
|
||||||
}
|
|
||||||
f.WriteString("</div>")
|
|
||||||
f.WriteString("\n\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(modu.Types) != 0 {
|
|
||||||
f.WriteString("## Types\n")
|
|
||||||
for _, dps := range modu.Types {
|
|
||||||
f.WriteString("<hr>\n\n")
|
|
||||||
f.WriteString(fmt.Sprintf("## %s\n", dps.FuncName))
|
|
||||||
for _, doc := range dps.Doc {
|
|
||||||
if !strings.HasPrefix(doc, "---") {
|
|
||||||
f.WriteString(doc + "\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(dps.Properties) != 0 {
|
|
||||||
f.WriteString("## Object properties\n")
|
|
||||||
|
|
||||||
mdTable := md.NewTable(len(dps.Properties), 2)
|
|
||||||
mdTable.SetTitle(0, "")
|
|
||||||
mdTable.SetTitle(1, "")
|
|
||||||
|
|
||||||
for i, d := range dps.Properties {
|
|
||||||
mdTable.SetContent(i, 0, d.FuncName)
|
|
||||||
mdTable.SetContent(i, 1, strings.Join(d.Doc, " "))
|
|
||||||
}
|
|
||||||
f.WriteString(mdTable.String())
|
|
||||||
f.WriteString("\n")
|
|
||||||
}
|
|
||||||
f.WriteString("\n")
|
|
||||||
f.WriteString("### Methods\n")
|
|
||||||
for _, dps := range modu.Docs {
|
|
||||||
if !dps.IsMember {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
htmlSig := typeTag.ReplaceAllStringFunc(strings.Replace(dps.FuncSig, "<", `\<`, -1), func(typ string) string {
|
|
||||||
typName := regexp.MustCompile(`\w+`).FindString(typ[1:])
|
|
||||||
typLookup := typeTable[strings.ToLower(typName)]
|
|
||||||
fmt.Printf("%+q, \n", typLookup)
|
|
||||||
linkedTyp := fmt.Sprintf("/Hilbish/docs/api/%s/%s/#%s", typLookup[0], typLookup[0] + "." + typLookup[1], strings.ToLower(typName))
|
|
||||||
return fmt.Sprintf(`<a href="#%s" style="text-decoration: none;">%s</a>`, linkedTyp, typName)
|
|
||||||
})
|
|
||||||
f.WriteString(fmt.Sprintf("#### %s\n", htmlSig))
|
|
||||||
for _, doc := range dps.Doc {
|
|
||||||
if !strings.HasPrefix(doc, "---") {
|
|
||||||
f.WriteString(doc + "\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.WriteString("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(mod, docPath, v)
|
|
||||||
|
|
||||||
go func(md, modname string, modu module) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
if modu.ParentModule != "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ff, _ := os.Create("emmyLuaDocs/" + modname + ".lua")
|
|
||||||
ff.WriteString("--- @meta\n\nlocal " + modname + " = {}\n\n")
|
|
||||||
for _, em := range emmyDocs[modname] {
|
|
||||||
if strings.HasSuffix(em.DocPiece.GoFuncName, strings.ToLower("loader")) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
dps := em.DocPiece
|
|
||||||
funcdocs := dps.Doc
|
|
||||||
ff.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n")
|
|
||||||
if len(em.Annotations) != 0 {
|
|
||||||
ff.WriteString(strings.Join(em.Annotations, "\n") + "\n")
|
|
||||||
}
|
|
||||||
accessor := "."
|
|
||||||
if dps.IsMember {
|
|
||||||
accessor = ":"
|
|
||||||
}
|
|
||||||
signature := strings.Split(dps.FuncSig, " ->")[0]
|
|
||||||
var intrface string
|
|
||||||
if dps.IsInterface {
|
|
||||||
intrface = "." + dps.Interfacing
|
|
||||||
}
|
|
||||||
ff.WriteString("function " + modname + intrface + accessor + signature + " end\n\n")
|
|
||||||
}
|
|
||||||
ff.WriteString("return " + modname + "\n")
|
|
||||||
}(mod, mod, v)
|
|
||||||
}
|
}
|
||||||
wg.Wait()
|
|
||||||
|
for mod, v := range emmyDocs {
|
||||||
|
if mod == "main" { continue }
|
||||||
|
f, _ := os.Create("emmyLuaDocs/" + mod + ".lua")
|
||||||
|
f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n")
|
||||||
|
for _, em := range v {
|
||||||
|
var funcdocs []string
|
||||||
|
for _, dps := range docs[mod] {
|
||||||
|
if dps.FuncName == em.FuncName {
|
||||||
|
funcdocs = dps.Doc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n")
|
||||||
|
if len(em.Docs) != 0 {
|
||||||
|
f.WriteString(strings.Join(em.Docs, "\n") + "\n")
|
||||||
|
}
|
||||||
|
f.WriteString("function " + mod + "." + em.FuncName + "(" + strings.Join(em.Params, ", ") + ") end\n\n")
|
||||||
|
}
|
||||||
|
f.WriteString("return " + mod + "\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,146 +0,0 @@
|
||||||
local fs = require 'fs'
|
|
||||||
local emmyPattern = '^%-%-%- (.+)'
|
|
||||||
local modpattern = '^%-+ @module (%w+)'
|
|
||||||
local pieces = {}
|
|
||||||
|
|
||||||
local files = fs.readdir 'nature'
|
|
||||||
for _, fname in ipairs(files) do
|
|
||||||
local isScript = fname:match'%.lua$'
|
|
||||||
if not isScript then goto continue end
|
|
||||||
|
|
||||||
local f = io.open(string.format('nature/%s', fname))
|
|
||||||
local header = f:read '*l'
|
|
||||||
local mod = header:match(modpattern)
|
|
||||||
if not mod then goto continue end
|
|
||||||
|
|
||||||
print(fname, mod)
|
|
||||||
pieces[mod] = {}
|
|
||||||
|
|
||||||
local docPiece = {}
|
|
||||||
local lines = {}
|
|
||||||
local lineno = 0
|
|
||||||
for line in f:lines() do
|
|
||||||
lineno = lineno + 1
|
|
||||||
lines[lineno] = line
|
|
||||||
|
|
||||||
if line == header then goto continue2 end
|
|
||||||
if not line:match(emmyPattern) then
|
|
||||||
if line:match '^function' then
|
|
||||||
local pattern = (string.format('^function %s%%.', mod) .. '(%w+)')
|
|
||||||
local funcName = line:match(pattern)
|
|
||||||
if not funcName then goto continue2 end
|
|
||||||
|
|
||||||
local dps = {
|
|
||||||
description = {},
|
|
||||||
params = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
local offset = 1
|
|
||||||
while true do
|
|
||||||
local prev = lines[lineno - offset]
|
|
||||||
|
|
||||||
local docline = prev:match '^%-+ (.+)'
|
|
||||||
if docline then
|
|
||||||
local emmy = docline:match '@(%w+)'
|
|
||||||
local cut = 0
|
|
||||||
|
|
||||||
if emmy then cut = emmy:len() + 3 end
|
|
||||||
local emmythings = string.split(docline:sub(cut), ' ')
|
|
||||||
|
|
||||||
if emmy then
|
|
||||||
if emmy == 'param' then
|
|
||||||
table.insert(dps.params, 1, {
|
|
||||||
name = emmythings[1],
|
|
||||||
type = emmythings[2]
|
|
||||||
})
|
|
||||||
end
|
|
||||||
else
|
|
||||||
table.insert(dps.description, 1, docline)
|
|
||||||
end
|
|
||||||
offset = offset + 1
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
pieces[mod][funcName] = dps
|
|
||||||
end
|
|
||||||
docPiece = {}
|
|
||||||
goto continue2
|
|
||||||
end
|
|
||||||
|
|
||||||
table.insert(docPiece, line)
|
|
||||||
::continue2::
|
|
||||||
end
|
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
|
|
||||||
local header = [[---
|
|
||||||
title: %s %s
|
|
||||||
description: %s
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Nature"
|
|
||||||
---
|
|
||||||
|
|
||||||
]]
|
|
||||||
|
|
||||||
for iface, dps in pairs(pieces) do
|
|
||||||
local mod = iface:match '(%w+)%.' or 'nature'
|
|
||||||
local path = string.format('docs/%s/%s.md', mod, iface)
|
|
||||||
fs.mkdir(fs.dir(path), true)
|
|
||||||
local f <close> = io.open(path, 'w')
|
|
||||||
f:write(string.format(header, 'Module', iface, 'No description.'))
|
|
||||||
print(f)
|
|
||||||
|
|
||||||
print(mod, path)
|
|
||||||
|
|
||||||
for func, docs in pairs(dps) do
|
|
||||||
f:write(string.format('<hr>\n<div id=\'%s\'>', func))
|
|
||||||
local sig = string.format('%s.%s(', iface, func)
|
|
||||||
for idx, param in ipairs(docs.params) do
|
|
||||||
sig = sig .. ((param.name:gsub('%?$', '')))
|
|
||||||
if idx ~= #docs.params then sig = sig .. ', ' end
|
|
||||||
end
|
|
||||||
sig = sig .. ')'
|
|
||||||
f:write(string.format([[
|
|
||||||
<h4 class='heading'>
|
|
||||||
%s
|
|
||||||
<a href="#%s" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
]], sig, func))
|
|
||||||
|
|
||||||
f:write(table.concat(docs.description, '\n') .. '\n')
|
|
||||||
f:write '#### Parameters\n'
|
|
||||||
if #docs.params == 0 then
|
|
||||||
f:write 'This function has no parameters. \n'
|
|
||||||
end
|
|
||||||
for _, param in ipairs(docs.params) do
|
|
||||||
f:write(string.format('`%s` **`%s`**\n', param.name:gsub('%?$', ''), param.type))
|
|
||||||
end
|
|
||||||
--[[
|
|
||||||
local params = table.filter(docs, function(t)
|
|
||||||
return t:match '^%-%-%- @param'
|
|
||||||
end)
|
|
||||||
for i, str in ipairs(params) do
|
|
||||||
if i ~= 1 then
|
|
||||||
f:write ', '
|
|
||||||
end
|
|
||||||
f:write(str:match '^%-%-%- @param ([%w]+) ')
|
|
||||||
end
|
|
||||||
f:write(')\n')
|
|
||||||
|
|
||||||
for _, str in ipairs(docs) do
|
|
||||||
if not str:match '^%-%-%- @' then
|
|
||||||
f:write(str:match '^%-%-%- (.+)' .. '\n')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
]]--
|
|
||||||
f:write('</div>')
|
|
||||||
f:write('\n\n')
|
|
||||||
end
|
|
||||||
end
|
|
365
complete.go
|
@ -1,122 +1,57 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"hilbish/util"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var charEscapeMap = []string{
|
func fileComplete(query, ctx string, fields []string) []string {
|
||||||
"\"", "\\\"",
|
|
||||||
"'", "\\'",
|
|
||||||
"`", "\\`",
|
|
||||||
" ", "\\ ",
|
|
||||||
"(", "\\(",
|
|
||||||
")", "\\)",
|
|
||||||
"[", "\\[",
|
|
||||||
"]", "\\]",
|
|
||||||
"$", "\\$",
|
|
||||||
"&", "\\&",
|
|
||||||
"*", "\\*",
|
|
||||||
">", "\\>",
|
|
||||||
"<", "\\<",
|
|
||||||
"|", "\\|",
|
|
||||||
}
|
|
||||||
var charEscapeMapInvert = invert(charEscapeMap)
|
|
||||||
var escapeReplaer = strings.NewReplacer(charEscapeMap...)
|
|
||||||
var escapeInvertReplaer = strings.NewReplacer(charEscapeMapInvert...)
|
|
||||||
|
|
||||||
func invert(m []string) []string {
|
|
||||||
newM := make([]string, len(charEscapeMap))
|
|
||||||
for i := range m {
|
|
||||||
if (i + 1) % 2 == 0 {
|
|
||||||
newM[i] = m[i - 1]
|
|
||||||
newM[i - 1] = m[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newM
|
|
||||||
}
|
|
||||||
|
|
||||||
func splitForFile(str string) []string {
|
|
||||||
split := []string{}
|
|
||||||
sb := &strings.Builder{}
|
|
||||||
quoted := false
|
|
||||||
|
|
||||||
for i, r := range str {
|
|
||||||
if r == '"' {
|
|
||||||
quoted = !quoted
|
|
||||||
sb.WriteRune(r)
|
|
||||||
} else if r == ' ' && str[i - 1] == '\\' {
|
|
||||||
sb.WriteRune(r)
|
|
||||||
} else if !quoted && r == ' ' {
|
|
||||||
split = append(split, sb.String())
|
|
||||||
sb.Reset()
|
|
||||||
} else {
|
|
||||||
sb.WriteRune(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(str, " ") {
|
|
||||||
split = append(split, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
if sb.Len() > 0 {
|
|
||||||
split = append(split, sb.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
return split
|
|
||||||
}
|
|
||||||
|
|
||||||
func fileComplete(query, ctx string, fields []string) ([]string, string) {
|
|
||||||
q := splitForFile(ctx)
|
|
||||||
path := ""
|
|
||||||
if len(q) != 0 {
|
|
||||||
path = q[len(q) - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return matchPath(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
|
||||||
q := splitForFile(ctx)
|
|
||||||
query = ""
|
|
||||||
if len(q) != 0 {
|
|
||||||
query = q[len(q) - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
var completions []string
|
var completions []string
|
||||||
|
|
||||||
prefixes := []string{"./", "../", "/", "~/"}
|
prefixes := []string{"./", "../", "/", "~/"}
|
||||||
for _, prefix := range prefixes {
|
for _, prefix := range prefixes {
|
||||||
if strings.HasPrefix(query, prefix) {
|
if strings.HasPrefix(query, prefix) {
|
||||||
fileCompletions, filePref := matchPath(query)
|
completions, _ = matchPath(strings.Replace(query, "~", curuser.HomeDir, 1), query)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(completions) == 0 && len(fields) > 1 {
|
||||||
|
completions, _ = matchPath("./" + query, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
return completions
|
||||||
|
}
|
||||||
|
|
||||||
|
func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
||||||
|
var completions []string
|
||||||
|
|
||||||
|
prefixes := []string{"./", "../", "/", "~/"}
|
||||||
|
for _, prefix := range prefixes {
|
||||||
|
if strings.HasPrefix(query, prefix) {
|
||||||
|
fileCompletions := fileComplete(query, ctx, fields)
|
||||||
if len(fileCompletions) != 0 {
|
if len(fileCompletions) != 0 {
|
||||||
for _, f := range fileCompletions {
|
for _, f := range fileCompletions {
|
||||||
fullPath, _ := filepath.Abs(util.ExpandHome(query + strings.TrimPrefix(f, filePref)))
|
name := strings.Replace(query + f, "~", curuser.HomeDir, 1)
|
||||||
if err := findExecutable(escapeInvertReplaer.Replace(fullPath), false, true); err != nil {
|
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
completions = append(completions, f)
|
completions = append(completions, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return completions, filePref
|
return completions, ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter out executables, but in path
|
// filter out executables, but in path
|
||||||
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
||||||
|
// print dir to stderr for debugging
|
||||||
// search for an executable which matches our query string
|
// search for an executable which matches our query string
|
||||||
if matches, err := filepath.Glob(filepath.Join(dir, query + "*")); err == nil {
|
if matches, err := filepath.Glob(filepath.Join(dir, query + "*")); err == nil {
|
||||||
// get basename from matches
|
// get basename from matches
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
// check if we have execute permissions for our match
|
// check if we have execute permissions for our match
|
||||||
err := findExecutable(match, true, false)
|
if info, err := os.Stat(match); err == nil && info.Mode().Perm() & 0100 == 0 {
|
||||||
if err != nil {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// get basename from match
|
// get basename from match
|
||||||
|
@ -128,246 +63,46 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add lua registered commands to completions
|
// add lua registered commands to completions
|
||||||
for cmdName := range cmds.Commands {
|
for cmdName := range commands {
|
||||||
if strings.HasPrefix(cmdName, query) {
|
if strings.HasPrefix(cmdName, query) {
|
||||||
completions = append(completions, cmdName)
|
completions = append(completions, cmdName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
completions = removeDupes(completions)
|
|
||||||
|
|
||||||
return completions, query
|
return completions, query
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchPath(query string) ([]string, string) {
|
func matchPath(path, pref string) ([]string, error) {
|
||||||
oldQuery := query
|
|
||||||
query = strings.TrimPrefix(query, "\"")
|
|
||||||
var entries []string
|
var entries []string
|
||||||
var baseName string
|
matches, err := filepath.Glob(path + "*")
|
||||||
|
if err == nil {
|
||||||
query = escapeInvertReplaer.Replace(query)
|
args := []string{
|
||||||
path, _ := filepath.Abs(util.ExpandHome(filepath.Dir(query)))
|
"\"", "\\\"",
|
||||||
if string(query) == "" {
|
"'", "\\'",
|
||||||
// filepath base below would give us "."
|
"`", "\\`",
|
||||||
// which would cause a match of only dotfiles
|
" ", "\\ ",
|
||||||
path, _ = filepath.Abs(".")
|
"(", "\\(",
|
||||||
} else if !strings.HasSuffix(query, string(os.PathSeparator)) {
|
")", "\\)",
|
||||||
baseName = filepath.Base(query)
|
"[", "\\[",
|
||||||
}
|
"]", "\\]",
|
||||||
|
|
||||||
files, _ := os.ReadDir(path)
|
|
||||||
for _, entry := range files {
|
|
||||||
// should we handle errors here?
|
|
||||||
file, err := entry.Info()
|
|
||||||
if err == nil && file.Mode() & os.ModeSymlink != 0 {
|
|
||||||
path, err := filepath.EvalSymlinks(filepath.Join(path, file.Name()))
|
|
||||||
if err == nil {
|
|
||||||
file, err = os.Lstat(path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(file.Name(), baseName) {
|
r := strings.NewReplacer(args...)
|
||||||
entry := file.Name()
|
for _, match := range matches {
|
||||||
if file.IsDir() {
|
name := filepath.Base(match)
|
||||||
entry = entry + string(os.PathSeparator)
|
p := filepath.Base(pref)
|
||||||
|
if pref == "" {
|
||||||
|
p = ""
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(oldQuery, "\"") {
|
name = strings.TrimPrefix(name, p)
|
||||||
entry = escapeFilename(entry)
|
matchFull, _ := filepath.Abs(match)
|
||||||
|
if info, err := os.Stat(matchFull); err == nil && info.IsDir() {
|
||||||
|
name = name + string(os.PathSeparator)
|
||||||
}
|
}
|
||||||
entries = append(entries, entry)
|
name = r.Replace(name)
|
||||||
|
entries = append(entries, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(oldQuery, "\"") {
|
|
||||||
baseName = escapeFilename(baseName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return entries, baseName
|
return entries, err
|
||||||
}
|
|
||||||
|
|
||||||
func escapeFilename(fname string) string {
|
|
||||||
return escapeReplaer.Replace(fname)
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface completion
|
|
||||||
// tab completions
|
|
||||||
// The completions interface deals with tab completions.
|
|
||||||
func completionLoader(rtm *rt.Runtime) *rt.Table {
|
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"bins": {hcmpBins, 3, false},
|
|
||||||
"call": {hcmpCall, 4, false},
|
|
||||||
"files": {hcmpFiles, 3, false},
|
|
||||||
"handler": {hcmpHandler, 2, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
mod := rt.NewTable()
|
|
||||||
util.SetExports(rtm, mod, exports)
|
|
||||||
|
|
||||||
return mod
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface completion
|
|
||||||
// bins(query, ctx, fields) -> entries (table), prefix (string)
|
|
||||||
// Return binaries/executables based on the provided parameters.
|
|
||||||
// This function is meant to be used as a helper in a command completion handler.
|
|
||||||
// #param query string
|
|
||||||
// #param ctx string
|
|
||||||
// #param fields table
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
-- an extremely simple completer for sudo.
|
|
||||||
hilbish.complete('command.sudo', function(query, ctx, fields)
|
|
||||||
table.remove(fields, 1)
|
|
||||||
if #fields[1] then
|
|
||||||
-- return commands because sudo runs a command as root..!
|
|
||||||
|
|
||||||
local entries, pfx = hilbish.completion.bins(query, ctx, fields)
|
|
||||||
return {
|
|
||||||
type = 'grid',
|
|
||||||
items = entries
|
|
||||||
}, pfx
|
|
||||||
end
|
|
||||||
|
|
||||||
-- ... else suggest files or anything else ..
|
|
||||||
end)
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func hcmpBins(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
query, ctx, fds, err := getCompleteParams(t, c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
completions, pfx := binaryComplete(query, ctx, fds)
|
|
||||||
luaComps := rt.NewTable()
|
|
||||||
|
|
||||||
for i, comp := range completions {
|
|
||||||
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface completion
|
|
||||||
// call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
|
|
||||||
// Calls a completer function. This is mainly used to call a command completer, which will have a `name`
|
|
||||||
// in the form of `command.name`, example: `command.git`.
|
|
||||||
// You can check the Completions doc or `doc completions` for info on the `completionGroups` return value.
|
|
||||||
// #param name string
|
|
||||||
// #param query string
|
|
||||||
// #param ctx string
|
|
||||||
// #param fields table
|
|
||||||
func hcmpCall(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.CheckNArgs(4); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
completer, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
query, err := c.StringArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ctx, err := c.StringArg(2)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fields, err := c.TableArg(3)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var completecb *rt.Closure
|
|
||||||
var ok bool
|
|
||||||
if completecb, ok = luaCompletions[completer]; !ok {
|
|
||||||
return nil, errors.New("completer " + completer + " does not exist")
|
|
||||||
}
|
|
||||||
|
|
||||||
// we must keep the holy 80 cols
|
|
||||||
cont := c.Next()
|
|
||||||
err = rt.Call(l.MainThread(), rt.FunctionValue(completecb),
|
|
||||||
[]rt.Value{rt.StringValue(query), rt.StringValue(ctx), rt.TableValue(fields)},
|
|
||||||
cont)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cont, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface completion
|
|
||||||
// files(query, ctx, fields) -> entries (table), prefix (string)
|
|
||||||
// Returns file matches based on the provided parameters.
|
|
||||||
// This function is meant to be used as a helper in a command completion handler.
|
|
||||||
// #param query string
|
|
||||||
// #param ctx string
|
|
||||||
// #param fields table
|
|
||||||
func hcmpFiles(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
query, ctx, fds, err := getCompleteParams(t, c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
completions, pfx := fileComplete(query, ctx, fds)
|
|
||||||
luaComps := rt.NewTable()
|
|
||||||
|
|
||||||
for i, comp := range completions {
|
|
||||||
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface completion
|
|
||||||
// handler(line, pos)
|
|
||||||
// This function contains the general completion handler for Hilbish. This function handles
|
|
||||||
// completion of everything, which includes calling other command handlers, binaries, and files.
|
|
||||||
// This function can be overriden to supply a custom handler. Note that alias resolution is required to be done in this function.
|
|
||||||
// #param line string The current Hilbish command line
|
|
||||||
// #param pos number Numerical position of the cursor
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
-- stripped down version of the default implementation
|
|
||||||
function hilbish.completion.handler(line, pos)
|
|
||||||
local query = fields[#fields]
|
|
||||||
|
|
||||||
if #fields == 1 then
|
|
||||||
-- call bins handler here
|
|
||||||
else
|
|
||||||
-- call command completer or files completer here
|
|
||||||
end
|
|
||||||
end
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func hcmpHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func getCompleteParams(t *rt.Thread, c *rt.GoCont) (string, string, []string, error) {
|
|
||||||
if err := c.CheckNArgs(3); err != nil {
|
|
||||||
return "", "", []string{}, err
|
|
||||||
}
|
|
||||||
query, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", []string{}, err
|
|
||||||
}
|
|
||||||
ctx, err := c.StringArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", []string{}, err
|
|
||||||
}
|
|
||||||
fields, err := c.TableArg(2)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", []string{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var fds []string
|
|
||||||
util.ForEach(fields, func(k rt.Value, v rt.Value) {
|
|
||||||
if v.Type() == rt.StringType {
|
|
||||||
fds = append(fds, v.AsString())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return query, ctx, fds, err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
---
|
|
||||||
title: Introduction
|
|
||||||
layout: doc
|
|
||||||
weight: -1
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
Hilbish is a hyper-extensible shell mainly intended for interactive use.
|
|
||||||
To enhance the interactive experience, Hilbish comes with a wide range
|
|
||||||
of features and sane defaults, including a nice looking prompt,
|
|
||||||
advanced completion menus and history search.
|
|
||||||
|
|
||||||
Here documents some of the features of Hilbish and the Lua API.
|
|
|
@ -1,9 +0,0 @@
|
||||||
---
|
|
||||||
title: API
|
|
||||||
layout: doc
|
|
||||||
weight: -100
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
Welcome to the API documentation for Hilbish. This documents Lua functions
|
|
||||||
provided by Hilbish.
|
|
167
docs/api/bait.md
|
@ -1,167 +0,0 @@
|
||||||
---
|
|
||||||
title: Module bait
|
|
||||||
description: the event emitter
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
Bait is the event emitter for Hilbish. Much like Node.js and
|
|
||||||
its `events` system, many actions in Hilbish emit events.
|
|
||||||
Unlike Node.js, Hilbish events are global. So make sure to
|
|
||||||
pick a unique name!
|
|
||||||
|
|
||||||
Usage of the Bait module consists of userstanding
|
|
||||||
event-driven architecture, but it's pretty simple:
|
|
||||||
If you want to act on a certain event, you can `catch` it.
|
|
||||||
You can act on events via callback functions.
|
|
||||||
|
|
||||||
Examples of this are in the Hilbish default config!
|
|
||||||
Consider this part of it:
|
|
||||||
```lua
|
|
||||||
bait.catch('command.exit', function(code)
|
|
||||||
running = false
|
|
||||||
doPrompt(code ~= 0)
|
|
||||||
doNotifyPrompt()
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
|
|
||||||
What this does is, whenever the `command.exit` event is thrown,
|
|
||||||
this function will set the user prompt.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#catch">catch(name, cb)</a>|Catches an event. This function can be used to act on events.|
|
|
||||||
|<a href="#catchOnce">catchOnce(name, cb)</a>|Catches an event, but only once. This will remove the hook immediately after it runs for the first time.|
|
|
||||||
|<a href="#hooks">hooks(name) -> table</a>|Returns a table of functions that are hooked on an event with the corresponding `name`.|
|
|
||||||
|<a href="#release">release(name, catcher)</a>|Removes the `catcher` for the event with `name`.|
|
|
||||||
|<a href="#throw">throw(name, ...args)</a>|Throws a hook with `name` with the provided `args`.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='catch'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
bait.catch(name, cb)
|
|
||||||
<a href="#catch" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Catches an event. This function can be used to act on events.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
The name of the hook.
|
|
||||||
|
|
||||||
`function` **`cb`**
|
|
||||||
The function that will be called when the hook is thrown.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
bait.catch('hilbish.exit', function()
|
|
||||||
print 'Goodbye Hilbish!'
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='catchOnce'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
bait.catchOnce(name, cb)
|
|
||||||
<a href="#catchOnce" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
The name of the event
|
|
||||||
|
|
||||||
`function` **`cb`**
|
|
||||||
The function that will be called when the event is thrown.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='hooks'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
bait.hooks(name) -> table
|
|
||||||
<a href="#hooks" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns a table of functions that are hooked on an event with the corresponding `name`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
The name of the hook
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='release'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
bait.release(name, catcher)
|
|
||||||
<a href="#release" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Removes the `catcher` for the event with `name`.
|
|
||||||
For this to work, `catcher` has to be the same function used to catch
|
|
||||||
an event, like one saved to a variable.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
Name of the event the hook is on
|
|
||||||
|
|
||||||
`function` **`catcher`**
|
|
||||||
Hook function to remove
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
local hookCallback = function() print 'hi' end
|
|
||||||
|
|
||||||
bait.catch('event', hookCallback)
|
|
||||||
|
|
||||||
-- a little while later....
|
|
||||||
bait.release('event', hookCallback)
|
|
||||||
-- and now hookCallback will no longer be ran for the event.
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='throw'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
bait.throw(name, ...args)
|
|
||||||
<a href="#throw" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Throws a hook with `name` with the provided `args`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
The name of the hook.
|
|
||||||
|
|
||||||
`any` **`args`** (This type is variadic. You can pass an infinite amount of parameters with this type.)
|
|
||||||
The arguments to pass to the hook.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
bait.throw('greeting', 'world')
|
|
||||||
|
|
||||||
-- This can then be listened to via
|
|
||||||
bait.catch('gretting', function(greetTo)
|
|
||||||
print('Hello ' .. greetTo)
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,113 +0,0 @@
|
||||||
---
|
|
||||||
title: Module commander
|
|
||||||
description: library for custom commands
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
Commander is the library which handles Hilbish commands. This makes
|
|
||||||
the user able to add Lua-written commands to their shell without making
|
|
||||||
a separate script in a bin folder. Instead, you may simply use the Commander
|
|
||||||
library in your Hilbish config.
|
|
||||||
|
|
||||||
```lua
|
|
||||||
local commander = require 'commander'
|
|
||||||
|
|
||||||
commander.register('hello', function(args, sinks)
|
|
||||||
sinks.out:writeln 'Hello world!'
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
|
|
||||||
In this example, a command with the name of `hello` is created
|
|
||||||
that will print `Hello world!` to output. One question you may
|
|
||||||
have is: What is the `sinks` parameter?
|
|
||||||
|
|
||||||
The `sinks` parameter is a table with 3 keys: `input`, `out`, and `err`.
|
|
||||||
There is an `in` alias to `input`, but it requires using the string accessor syntax (`sinks['in']`)
|
|
||||||
as `in` is also a Lua keyword, so `input` is preferred for use.
|
|
||||||
All of them are a <a href="/Hilbish/docs/api/hilbish/#sink" style="text-decoration: none;">Sink</a>.
|
|
||||||
In the future, `sinks.in` will be removed.
|
|
||||||
|
|
||||||
- `in` is the standard input.
|
|
||||||
You may use the read functions on this sink to get input from the user.
|
|
||||||
- `out` is standard output.
|
|
||||||
This is usually where command output should go.
|
|
||||||
- `err` is standard error.
|
|
||||||
This sink is for writing errors, as the name would suggest.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#deregister">deregister(name)</a>|Removes the named command. Note that this will only remove Commander-registered commands.|
|
|
||||||
|<a href="#register">register(name, cb)</a>|Adds a new command with the given `name`. When Hilbish has to run a command with a name,|
|
|
||||||
|<a href="#registry">registry() -> table</a>|Returns all registered commanders. Returns a list of tables with the following keys:|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='deregister'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
commander.deregister(name)
|
|
||||||
<a href="#deregister" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Removes the named command. Note that this will only remove Commander-registered commands.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
Name of the command to remove.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='register'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
commander.register(name, cb)
|
|
||||||
<a href="#register" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Adds a new command with the given `name`. When Hilbish has to run a command with a name,
|
|
||||||
it will run the function providing the arguments and sinks.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
Name of the command
|
|
||||||
|
|
||||||
`function` **`cb`**
|
|
||||||
Callback to handle command invocation
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- When you run the command `hello` in the shell, it will print `Hello world`.
|
|
||||||
-- If you run it with, for example, `hello Hilbish`, it will print 'Hello Hilbish'
|
|
||||||
commander.register('hello', function(args, sinks)
|
|
||||||
local name = 'world'
|
|
||||||
if #args > 0 then name = args[1] end
|
|
||||||
|
|
||||||
sinks.out:writeln('Hello ' .. name)
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='registry'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
commander.registry() -> table
|
|
||||||
<a href="#registry" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns all registered commanders. Returns a list of tables with the following keys:
|
|
||||||
- `exec`: The function used to run the commander. Commanders require args and sinks to be passed.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
257
docs/api/fs.md
|
@ -1,257 +0,0 @@
|
||||||
---
|
|
||||||
title: Module fs
|
|
||||||
description: filesystem interaction and functionality library
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
The fs module provides filesystem functions to Hilbish. While Lua's standard
|
|
||||||
library has some I/O functions, they're missing a lot of the basics. The `fs`
|
|
||||||
library offers more functions and will work on any operating system Hilbish does.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#abs">abs(path) -> string</a>|Returns an absolute version of the `path`.|
|
|
||||||
|<a href="#basename">basename(path) -> string</a>|Returns the "basename," or the last part of the provided `path`. If path is empty,|
|
|
||||||
|<a href="#cd">cd(dir)</a>|Changes Hilbish's directory to `dir`.|
|
|
||||||
|<a href="#dir">dir(path) -> string</a>|Returns the directory part of `path`. If a file path like|
|
|
||||||
|<a href="#glob">glob(pattern) -> matches (table)</a>|Match all files based on the provided `pattern`.|
|
|
||||||
|<a href="#join">join(...path) -> string</a>|Takes any list of paths and joins them based on the operating system's path separator.|
|
|
||||||
|<a href="#mkdir">mkdir(name, recursive)</a>|Creates a new directory with the provided `name`.|
|
|
||||||
|<a href="#pipe">fpipe() -> File, File</a>|Returns a pair of connected files, also known as a pipe.|
|
|
||||||
|<a href="#readdir">readdir(path) -> table[string]</a>|Returns a list of all files and directories in the provided path.|
|
|
||||||
|<a href="#stat">stat(path) -> {}</a>|Returns the information about a given `path`.|
|
|
||||||
|
|
||||||
## Static module fields
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|pathSep|The operating system's path separator.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='abs'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.abs(path) -> string
|
|
||||||
<a href="#abs" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns an absolute version of the `path`.
|
|
||||||
This can be used to resolve short paths like `..` to `/home/user`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`path`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='basename'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.basename(path) -> string
|
|
||||||
<a href="#basename" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the "basename," or the last part of the provided `path`. If path is empty,
|
|
||||||
`.` will be returned.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`path`**
|
|
||||||
Path to get the base name of.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='cd'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.cd(dir)
|
|
||||||
<a href="#cd" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Changes Hilbish's directory to `dir`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`dir`**
|
|
||||||
Path to change directory to.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='dir'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.dir(path) -> string
|
|
||||||
<a href="#dir" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the directory part of `path`. If a file path like
|
|
||||||
`~/Documents/doc.txt` then this function will return `~/Documents`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`path`**
|
|
||||||
Path to get the directory for.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='glob'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.glob(pattern) -> matches (table)
|
|
||||||
<a href="#glob" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Match all files based on the provided `pattern`.
|
|
||||||
For the syntax' refer to Go's filepath.Match function: https://pkg.go.dev/path/filepath#Match
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`pattern`**
|
|
||||||
Pattern to compare files with.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
--[[
|
|
||||||
Within a folder that contains the following files:
|
|
||||||
a.txt
|
|
||||||
init.lua
|
|
||||||
code.lua
|
|
||||||
doc.pdf
|
|
||||||
]]--
|
|
||||||
local matches = fs.glob './*.lua'
|
|
||||||
print(matches)
|
|
||||||
-- -> {'init.lua', 'code.lua'}
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='join'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.join(...path) -> string
|
|
||||||
<a href="#join" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Takes any list of paths and joins them based on the operating system's path separator.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`path`** (This type is variadic. You can pass an infinite amount of parameters with this type.)
|
|
||||||
Paths to join together
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- This prints the directory for Hilbish's config!
|
|
||||||
print(fs.join(hilbish.userDir.config, 'hilbish'))
|
|
||||||
-- -> '/home/user/.config/hilbish' on Linux
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='mkdir'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.mkdir(name, recursive)
|
|
||||||
<a href="#mkdir" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Creates a new directory with the provided `name`.
|
|
||||||
With `recursive`, mkdir will create parent directories.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
Name of the directory
|
|
||||||
|
|
||||||
`boolean` **`recursive`**
|
|
||||||
Whether to create parent directories for the provided name
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- This will create the directory foo, then create the directory bar in the
|
|
||||||
-- foo directory. If recursive is false in this case, it will fail.
|
|
||||||
fs.mkdir('./foo/bar', true)
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='pipe'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.fpipe() -> File, File
|
|
||||||
<a href="#pipe" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns a pair of connected files, also known as a pipe.
|
|
||||||
The type returned is a Lua file, same as returned from `io` functions.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='readdir'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.readdir(path) -> table[string]
|
|
||||||
<a href="#readdir" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns a list of all files and directories in the provided path.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`dir`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='stat'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
fs.stat(path) -> {}
|
|
||||||
<a href="#stat" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the information about a given `path`.
|
|
||||||
The returned table contains the following values:
|
|
||||||
name (string) - Name of the path
|
|
||||||
size (number) - Size of the path in bytes
|
|
||||||
mode (string) - Unix permission mode in an octal format string (with leading 0)
|
|
||||||
isDir (boolean) - If the path is a directory
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`path`**
|
|
||||||
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
local inspect = require 'inspect'
|
|
||||||
|
|
||||||
local stat = fs.stat '~'
|
|
||||||
print(inspect(stat))
|
|
||||||
--[[
|
|
||||||
Would print the following:
|
|
||||||
{
|
|
||||||
isDir = true,
|
|
||||||
mode = "0755",
|
|
||||||
name = "username",
|
|
||||||
size = 12288
|
|
||||||
}
|
|
||||||
]]--
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,544 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish
|
|
||||||
description: the core Hilbish API
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The Hilbish module includes the core API, containing
|
|
||||||
interfaces and functions which directly relate to shell functionality.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#alias">alias(cmd, orig)</a>|Sets an alias, with a name of `cmd` to another command.|
|
|
||||||
|<a href="#appendPath">appendPath(dir)</a>|Appends the provided dir to the command path (`$PATH`)|
|
|
||||||
|<a href="#complete">complete(scope, cb)</a>|Registers a completion handler for the specified scope.|
|
|
||||||
|<a href="#cwd">cwd() -> string</a>|Returns the current directory of the shell.|
|
|
||||||
|<a href="#exec">exec(cmd)</a>|Replaces the currently running Hilbish instance with the supplied command.|
|
|
||||||
|<a href="#goro">goro(fn)</a>|Puts `fn` in a Goroutine.|
|
|
||||||
|<a href="#highlighter">highlighter(line)</a>|Line highlighter handler.|
|
|
||||||
|<a href="#hinter">hinter(line, pos)</a>|The command line hint handler. It gets called on every key insert to|
|
|
||||||
|<a href="#inputMode">inputMode(mode)</a>|Sets the input mode for Hilbish's line reader.|
|
|
||||||
|<a href="#interval">interval(cb, time) -> @Timer</a>|Runs the `cb` function every specified amount of `time`.|
|
|
||||||
|<a href="#multiprompt">multiprompt(str)</a>|Changes the text prompt when Hilbish asks for more input.|
|
|
||||||
|<a href="#prependPath">prependPath(dir)</a>|Prepends `dir` to $PATH.|
|
|
||||||
|<a href="#prompt">prompt(str, typ)</a>|Changes the shell prompt to the provided string.|
|
|
||||||
|<a href="#read">read(prompt) -> input (string)</a>|Read input from the user, using Hilbish's line editor/input reader.|
|
|
||||||
|<a href="#run">run(cmd, streams) -> exitCode (number), stdout (string), stderr (string)</a>|Runs `cmd` in Hilbish's shell script interpreter.|
|
|
||||||
|<a href="#runnerMode">runnerMode(mode)</a>|Sets the execution/runner mode for interactive Hilbish.|
|
|
||||||
|<a href="#timeout">timeout(cb, time) -> @Timer</a>|Executed the `cb` function after a period of `time`.|
|
|
||||||
|<a href="#which">which(name) -> string</a>|Checks if `name` is a valid command.|
|
|
||||||
|
|
||||||
## Static module fields
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|ver|The version of Hilbish|
|
|
||||||
|goVersion|The version of Go that Hilbish was compiled with|
|
|
||||||
|user|Username of the user|
|
|
||||||
|host|Hostname of the machine|
|
|
||||||
|dataDir|Directory for Hilbish data files, including the docs and default modules|
|
|
||||||
|interactive|Is Hilbish in an interactive shell?|
|
|
||||||
|login|Is Hilbish the login shell?|
|
|
||||||
|vimMode|Current Vim input mode of Hilbish (will be nil if not in Vim input mode)|
|
|
||||||
|exitCode|Exit code of the last executed command|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='alias'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.alias(cmd, orig)
|
|
||||||
<a href="#alias" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Sets an alias, with a name of `cmd` to another command.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmd`**
|
|
||||||
Name of the alias
|
|
||||||
|
|
||||||
`string` **`orig`**
|
|
||||||
Command that will be aliased
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- With this, "ga file" will turn into "git add file"
|
|
||||||
hilbish.alias('ga', 'git add')
|
|
||||||
|
|
||||||
-- Numbered substitutions are supported here!
|
|
||||||
hilbish.alias('dircount', 'ls %1 | wc -l')
|
|
||||||
-- "dircount ~" would count how many files are in ~ (home directory).
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='appendPath'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.appendPath(dir)
|
|
||||||
<a href="#appendPath" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Appends the provided dir to the command path (`$PATH`)
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string|table` **`dir`**
|
|
||||||
Directory (or directories) to append to path
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
hilbish.appendPath '~/go/bin'
|
|
||||||
-- Will add ~/go/bin to the command path.
|
|
||||||
|
|
||||||
-- Or do multiple:
|
|
||||||
hilbish.appendPath {
|
|
||||||
'~/go/bin',
|
|
||||||
'~/.local/bin'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='complete'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.complete(scope, cb)
|
|
||||||
<a href="#complete" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Registers a completion handler for the specified scope.
|
|
||||||
A `scope` is expected to be `command.<cmd>`,
|
|
||||||
replacing <cmd> with the name of the command (for example `command.git`).
|
|
||||||
The documentation for completions, under Features/Completions or `doc completions`
|
|
||||||
provides more details.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`scope`**
|
|
||||||
|
|
||||||
|
|
||||||
`function` **`cb`**
|
|
||||||
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- This is a very simple example. Read the full doc for completions for details.
|
|
||||||
hilbish.complete('command.sudo', function(query, ctx, fields)
|
|
||||||
if #fields == 0 then
|
|
||||||
-- complete for commands
|
|
||||||
local comps, pfx = hilbish.completion.bins(query, ctx, fields)
|
|
||||||
local compGroup = {
|
|
||||||
items = comps, -- our list of items to complete
|
|
||||||
type = 'grid' -- what our completions will look like.
|
|
||||||
}
|
|
||||||
|
|
||||||
return {compGroup}, pfx
|
|
||||||
end
|
|
||||||
|
|
||||||
-- otherwise just be boring and return files
|
|
||||||
|
|
||||||
local comps, pfx = hilbish.completion.files(query, ctx, fields)
|
|
||||||
local compGroup = {
|
|
||||||
items = comps,
|
|
||||||
type = 'grid'
|
|
||||||
}
|
|
||||||
|
|
||||||
return {compGroup}, pfx
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='cwd'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.cwd() -> string
|
|
||||||
<a href="#cwd" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the current directory of the shell.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='exec'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.exec(cmd)
|
|
||||||
<a href="#exec" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Replaces the currently running Hilbish instance with the supplied command.
|
|
||||||
This can be used to do an in-place restart.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmd`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='goro'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.goro(fn)
|
|
||||||
<a href="#goro" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Puts `fn` in a Goroutine.
|
|
||||||
This can be used to run any function in another thread at the same time as other Lua code.
|
|
||||||
**NOTE: THIS FUNCTION MAY CRASH HILBISH IF OUTSIDE VARIABLES ARE ACCESSED.**
|
|
||||||
**This is a limitation of the Lua runtime.**
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`function` **`fn`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='highlighter'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.highlighter(line)
|
|
||||||
<a href="#highlighter" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Line highlighter handler.
|
|
||||||
This is mainly for syntax highlighting, but in reality could set the input
|
|
||||||
of the prompt to *display* anything. The callback is passed the current line
|
|
||||||
and is expected to return a line that will be used as the input display.
|
|
||||||
Note that to set a highlighter, one has to override this function.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`line`**
|
|
||||||
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
--This code will highlight all double quoted strings in green.
|
|
||||||
function hilbish.highlighter(line)
|
|
||||||
return line:gsub('"%w+"', function(c) return lunacolors.green(c) end)
|
|
||||||
end
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='hinter'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.hinter(line, pos)
|
|
||||||
<a href="#hinter" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
The command line hint handler. It gets called on every key insert to
|
|
||||||
determine what text to use as an inline hint. It is passed the current
|
|
||||||
line and cursor position. It is expected to return a string which is used
|
|
||||||
as the text for the hint. This is by default a shim. To set hints,
|
|
||||||
override this function with your custom handler.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`line`**
|
|
||||||
|
|
||||||
|
|
||||||
`number` **`pos`**
|
|
||||||
Position of cursor in line. Usually equals string.len(line)
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- this will display "hi" after the cursor in a dimmed color.
|
|
||||||
function hilbish.hinter(line, pos)
|
|
||||||
return 'hi'
|
|
||||||
end
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='inputMode'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.inputMode(mode)
|
|
||||||
<a href="#inputMode" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Sets the input mode for Hilbish's line reader.
|
|
||||||
`emacs` is the default. Setting it to `vim` changes behavior of input to be
|
|
||||||
Vim-like with modes and Vim keybinds.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`mode`**
|
|
||||||
Can be set to either `emacs` or `vim`
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='interval'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.interval(cb, time) -> <a href="/Hilbish/docs/api/hilbish/hilbish.timers/#timer" style="text-decoration: none;" id="lol">Timer</a>
|
|
||||||
<a href="#interval" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Runs the `cb` function every specified amount of `time`.
|
|
||||||
This creates a timer that ticking immediately.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`function` **`cb`**
|
|
||||||
|
|
||||||
|
|
||||||
`number` **`time`**
|
|
||||||
Time in milliseconds.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='multiprompt'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.multiprompt(str)
|
|
||||||
<a href="#multiprompt" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Changes the text prompt when Hilbish asks for more input.
|
|
||||||
This will show up when text is incomplete, like a missing quote
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`str`**
|
|
||||||
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
--[[
|
|
||||||
imagine this is your text input:
|
|
||||||
user ~ ∆ echo "hey
|
|
||||||
|
|
||||||
but there's a missing quote! hilbish will now prompt you so the terminal
|
|
||||||
will look like:
|
|
||||||
user ~ ∆ echo "hey
|
|
||||||
--> ...!"
|
|
||||||
|
|
||||||
so then you get
|
|
||||||
user ~ ∆ echo "hey
|
|
||||||
--> ...!"
|
|
||||||
hey ...!
|
|
||||||
]]--
|
|
||||||
hilbish.multiprompt '-->'
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='prependPath'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.prependPath(dir)
|
|
||||||
<a href="#prependPath" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Prepends `dir` to $PATH.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`dir`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='prompt'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.prompt(str, typ)
|
|
||||||
<a href="#prompt" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Changes the shell prompt to the provided string.
|
|
||||||
There are a few verbs that can be used in the prompt text.
|
|
||||||
These will be formatted and replaced with the appropriate values.
|
|
||||||
`%d` - Current working directory
|
|
||||||
`%u` - Name of current user
|
|
||||||
`%h` - Hostname of device
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`str`**
|
|
||||||
|
|
||||||
|
|
||||||
`string` **`typ?`**
|
|
||||||
Type of prompt, being left or right. Left by default.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- the default hilbish prompt without color
|
|
||||||
hilbish.prompt '%u %d ∆'
|
|
||||||
-- or something of old:
|
|
||||||
hilbish.prompt '%u@%h :%d $'
|
|
||||||
-- prompt: user@hostname: ~/directory $
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='read'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.read(prompt) -> input (string)
|
|
||||||
<a href="#read" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Read input from the user, using Hilbish's line editor/input reader.
|
|
||||||
This is a separate instance from the one Hilbish actually uses.
|
|
||||||
Returns `input`, will be nil if Ctrl-D is pressed, or an error occurs.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`prompt?`**
|
|
||||||
Text to print before input, can be empty.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='run'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.run(cmd, streams) -> exitCode (number), stdout (string), stderr (string)
|
|
||||||
<a href="#run" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Runs `cmd` in Hilbish's shell script interpreter.
|
|
||||||
The `streams` parameter specifies the output and input streams the command should use.
|
|
||||||
For example, to write command output to a sink.
|
|
||||||
As a table, the caller can directly specify the standard output, error, and input
|
|
||||||
streams of the command with the table keys `out`, `err`, and `input` respectively.
|
|
||||||
As a boolean, it specifies whether the command should use standard output or return its output streams.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmd`**
|
|
||||||
|
|
||||||
|
|
||||||
`table|boolean` **`streams`**
|
|
||||||
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
|
|
||||||
// This code is the same as `ls -l | wc -l`
|
|
||||||
local fs = require 'fs'
|
|
||||||
local pr, pw = fs.pipe()
|
|
||||||
hilbish.run('ls -l', {
|
|
||||||
stdout = pw,
|
|
||||||
stderr = pw,
|
|
||||||
})
|
|
||||||
|
|
||||||
pw:close()
|
|
||||||
|
|
||||||
hilbish.run('wc -l', {
|
|
||||||
stdin = pr
|
|
||||||
})
|
|
||||||
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='runnerMode'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.runnerMode(mode)
|
|
||||||
<a href="#runnerMode" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Sets the execution/runner mode for interactive Hilbish.
|
|
||||||
This determines whether Hilbish wll try to run input as Lua
|
|
||||||
and/or sh or only do one of either.
|
|
||||||
Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua),
|
|
||||||
sh, and lua. It also accepts a function, to which if it is passed one
|
|
||||||
will call it to execute user input instead.
|
|
||||||
Read [about runner mode](../features/runner-mode) for more information.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string|function` **`mode`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='timeout'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.timeout(cb, time) -> <a href="/Hilbish/docs/api/hilbish/hilbish.timers/#timer" style="text-decoration: none;" id="lol">Timer</a>
|
|
||||||
<a href="#timeout" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Executed the `cb` function after a period of `time`.
|
|
||||||
This creates a Timer that starts ticking immediately.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`function` **`cb`**
|
|
||||||
|
|
||||||
|
|
||||||
`number` **`time`**
|
|
||||||
Time to run in milliseconds.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='which'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.which(name) -> string
|
|
||||||
<a href="#which" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Checks if `name` is a valid command.
|
|
||||||
Will return the path of the binary, or a basename if it's a commander.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
## Types
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## Sink
|
|
||||||
A sink is a structure that has input and/or output to/from
|
|
||||||
a desination.
|
|
||||||
|
|
||||||
### Methods
|
|
||||||
#### autoFlush(auto)
|
|
||||||
Sets/toggles the option of automatically flushing output.
|
|
||||||
A call with no argument will toggle the value.
|
|
||||||
|
|
||||||
#### flush()
|
|
||||||
Flush writes all buffered input to the sink.
|
|
||||||
|
|
||||||
#### read() -> string
|
|
||||||
Reads a liine of input from the sink.
|
|
||||||
|
|
||||||
#### readAll() -> string
|
|
||||||
Reads all input from the sink.
|
|
||||||
|
|
||||||
#### write(str)
|
|
||||||
Writes data to a sink.
|
|
||||||
|
|
||||||
#### writeln(str)
|
|
||||||
Writes data to a sink with a newline at the end.
|
|
||||||
|
|
|
@ -1,91 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.aliases
|
|
||||||
description: command aliasing
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The alias interface deals with all command aliases in Hilbish.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#aliases.add">add(alias, cmd)</a>|This is an alias (ha) for the [hilbish.alias](../#alias) function.|
|
|
||||||
|<a href="#aliases.delete">delete(name)</a>|Removes an alias.|
|
|
||||||
|<a href="#aliases.list">list() -> table[string, string]</a>|Get a table of all aliases, with string keys as the alias and the value as the command.|
|
|
||||||
|<a href="#aliases.resolve">resolve(alias) -> string?</a>|Resolves an alias to its original command. Will thrown an error if the alias doesn't exist.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='aliases.add'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.aliases.add(alias, cmd)
|
|
||||||
<a href="#aliases.add" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
This is an alias (ha) for the [hilbish.alias](../#alias) function.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='aliases.delete'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.aliases.delete(name)
|
|
||||||
<a href="#aliases.delete" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Removes an alias.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='aliases.list'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.aliases.list() -> table[string, string]
|
|
||||||
<a href="#aliases.list" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Get a table of all aliases, with string keys as the alias and the value as the command.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
hilbish.aliases.add('hi', 'echo hi')
|
|
||||||
|
|
||||||
local aliases = hilbish.aliases.list()
|
|
||||||
-- -> {hi = 'echo hi'}
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='aliases.resolve'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.aliases.resolve(alias) -> string?
|
|
||||||
<a href="#aliases.resolve" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Resolves an alias to its original command. Will thrown an error if the alias doesn't exist.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`alias`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,149 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.completion
|
|
||||||
description: tab completions
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The completions interface deals with tab completions.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#completion.bins">bins(query, ctx, fields) -> entries (table), prefix (string)</a>|Return binaries/executables based on the provided parameters.|
|
|
||||||
|<a href="#completion.call">call(name, query, ctx, fields) -> completionGroups (table), prefix (string)</a>|Calls a completer function. This is mainly used to call a command completer, which will have a `name`|
|
|
||||||
|<a href="#completion.files">files(query, ctx, fields) -> entries (table), prefix (string)</a>|Returns file matches based on the provided parameters.|
|
|
||||||
|<a href="#completion.handler">handler(line, pos)</a>|This function contains the general completion handler for Hilbish. This function handles|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='completion.bins'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.completion.bins(query, ctx, fields) -> entries (table), prefix (string)
|
|
||||||
<a href="#completion.bins" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Return binaries/executables based on the provided parameters.
|
|
||||||
This function is meant to be used as a helper in a command completion handler.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`query`**
|
|
||||||
|
|
||||||
|
|
||||||
`string` **`ctx`**
|
|
||||||
|
|
||||||
|
|
||||||
`table` **`fields`**
|
|
||||||
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- an extremely simple completer for sudo.
|
|
||||||
hilbish.complete('command.sudo', function(query, ctx, fields)
|
|
||||||
table.remove(fields, 1)
|
|
||||||
if #fields[1] then
|
|
||||||
-- return commands because sudo runs a command as root..!
|
|
||||||
|
|
||||||
local entries, pfx = hilbish.completion.bins(query, ctx, fields)
|
|
||||||
return {
|
|
||||||
type = 'grid',
|
|
||||||
items = entries
|
|
||||||
}, pfx
|
|
||||||
end
|
|
||||||
|
|
||||||
-- ... else suggest files or anything else ..
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='completion.call'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.completion.call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
|
|
||||||
<a href="#completion.call" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Calls a completer function. This is mainly used to call a command completer, which will have a `name`
|
|
||||||
in the form of `command.name`, example: `command.git`.
|
|
||||||
You can check the Completions doc or `doc completions` for info on the `completionGroups` return value.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`name`**
|
|
||||||
|
|
||||||
|
|
||||||
`string` **`query`**
|
|
||||||
|
|
||||||
|
|
||||||
`string` **`ctx`**
|
|
||||||
|
|
||||||
|
|
||||||
`table` **`fields`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='completion.files'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.completion.files(query, ctx, fields) -> entries (table), prefix (string)
|
|
||||||
<a href="#completion.files" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns file matches based on the provided parameters.
|
|
||||||
This function is meant to be used as a helper in a command completion handler.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`query`**
|
|
||||||
|
|
||||||
|
|
||||||
`string` **`ctx`**
|
|
||||||
|
|
||||||
|
|
||||||
`table` **`fields`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='completion.handler'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.completion.handler(line, pos)
|
|
||||||
<a href="#completion.handler" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
This function contains the general completion handler for Hilbish. This function handles
|
|
||||||
completion of everything, which includes calling other command handlers, binaries, and files.
|
|
||||||
This function can be overriden to supply a custom handler. Note that alias resolution is required to be done in this function.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`line`**
|
|
||||||
The current Hilbish command line
|
|
||||||
|
|
||||||
`number` **`pos`**
|
|
||||||
Numerical position of the cursor
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
-- stripped down version of the default implementation
|
|
||||||
function hilbish.completion.handler(line, pos)
|
|
||||||
local query = fields[#fields]
|
|
||||||
|
|
||||||
if #fields == 1 then
|
|
||||||
-- call bins handler here
|
|
||||||
else
|
|
||||||
-- call command completer or files completer here
|
|
||||||
end
|
|
||||||
end
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,103 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.editor
|
|
||||||
description: interactions for Hilbish's line reader
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The hilbish.editor interface provides functions to
|
|
||||||
directly interact with the line editor in use.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#editor.getLine">getLine() -> string</a>|Returns the current input line.|
|
|
||||||
|<a href="#editor.getVimRegister">getVimRegister(register) -> string</a>|Returns the text that is at the register.|
|
|
||||||
|<a href="#editor.insert">insert(text)</a>|Inserts text into the Hilbish command line.|
|
|
||||||
|<a href="#editor.getChar">getChar() -> string</a>|Reads a keystroke from the user. This is in a format of something like Ctrl-L.|
|
|
||||||
|<a href="#editor.setVimRegister">setVimRegister(register, text)</a>|Sets the vim register at `register` to hold the passed text.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='editor.getLine'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.editor.getLine() -> string
|
|
||||||
<a href="#editor.getLine" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the current input line.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='editor.getVimRegister'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.editor.getVimRegister(register) -> string
|
|
||||||
<a href="#editor.getVimRegister" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the text that is at the register.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`register`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='editor.insert'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.editor.insert(text)
|
|
||||||
<a href="#editor.insert" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Inserts text into the Hilbish command line.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`text`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='editor.getChar'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.editor.getChar() -> string
|
|
||||||
<a href="#editor.getChar" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Reads a keystroke from the user. This is in a format of something like Ctrl-L.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='editor.setVimRegister'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.editor.setVimRegister(register, text)
|
|
||||||
<a href="#editor.setVimRegister" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Sets the vim register at `register` to hold the passed text.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`text`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,102 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.history
|
|
||||||
description: command history
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The history interface deals with command history.
|
|
||||||
This includes the ability to override functions to change the main
|
|
||||||
method of saving history.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#history.add">add(cmd)</a>|Adds a command to the history.|
|
|
||||||
|<a href="#history.all">all() -> table</a>|Retrieves all history as a table.|
|
|
||||||
|<a href="#history.clear">clear()</a>|Deletes all commands from the history.|
|
|
||||||
|<a href="#history.get">get(index)</a>|Retrieves a command from the history based on the `index`.|
|
|
||||||
|<a href="#history.size">size() -> number</a>|Returns the amount of commands in the history.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='history.add'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.history.add(cmd)
|
|
||||||
<a href="#history.add" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Adds a command to the history.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmd`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='history.all'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.history.all() -> table
|
|
||||||
<a href="#history.all" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Retrieves all history as a table.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='history.clear'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.history.clear()
|
|
||||||
<a href="#history.clear" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Deletes all commands from the history.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='history.get'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.history.get(index)
|
|
||||||
<a href="#history.get" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Retrieves a command from the history based on the `index`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`number` **`index`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='history.size'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.history.size() -> number
|
|
||||||
<a href="#history.size" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the amount of commands in the history.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,146 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.jobs
|
|
||||||
description: background job management
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
Manage interactive jobs in Hilbish via Lua.
|
|
||||||
|
|
||||||
Jobs are the name of background tasks/commands. A job can be started via
|
|
||||||
interactive usage or with the functions defined below for use in external runners.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#jobs.add">add(cmdstr, args, execPath)</a>|Creates a new job. This function does not run the job. This function is intended to be|
|
|
||||||
|<a href="#jobs.all">all() -> table[@Job]</a>|Returns a table of all job objects.|
|
|
||||||
|<a href="#jobs.disown">disown(id)</a>|Disowns a job. This simply deletes it from the list of jobs without stopping it.|
|
|
||||||
|<a href="#jobs.get">get(id) -> @Job</a>|Get a job object via its ID.|
|
|
||||||
|<a href="#jobs.last">last() -> @Job</a>|Returns the last added job to the table.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='jobs.add'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.jobs.add(cmdstr, args, execPath)
|
|
||||||
<a href="#jobs.add" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Creates a new job. This function does not run the job. This function is intended to be
|
|
||||||
used by runners, but can also be used to create jobs via Lua. Commanders cannot be ran as jobs.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmdstr`**
|
|
||||||
String that a user would write for the job
|
|
||||||
|
|
||||||
`table` **`args`**
|
|
||||||
Arguments for the commands. Has to include the name of the command.
|
|
||||||
|
|
||||||
`string` **`execPath`**
|
|
||||||
Binary to use to run the command. Needs to be an absolute path.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
```lua
|
|
||||||
hilbish.jobs.add('go build', {'go', 'build'}, '/usr/bin/go')
|
|
||||||
```
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='jobs.all'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.jobs.all() -> table[<a href="/Hilbish/docs/api/hilbish/hilbish.jobs/#job" style="text-decoration: none;" id="lol">Job</a>]
|
|
||||||
<a href="#jobs.all" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns a table of all job objects.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='jobs.disown'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.jobs.disown(id)
|
|
||||||
<a href="#jobs.disown" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Disowns a job. This simply deletes it from the list of jobs without stopping it.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`number` **`id`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='jobs.get'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.jobs.get(id) -> <a href="/Hilbish/docs/api/hilbish/hilbish.jobs/#job" style="text-decoration: none;" id="lol">Job</a>
|
|
||||||
<a href="#jobs.get" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Get a job object via its ID.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='jobs.last'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.jobs.last() -> <a href="/Hilbish/docs/api/hilbish/hilbish.jobs/#job" style="text-decoration: none;" id="lol">Job</a>
|
|
||||||
<a href="#jobs.last" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Returns the last added job to the table.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
## Types
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## Job
|
|
||||||
The Job type describes a Hilbish job.
|
|
||||||
## Object properties
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|cmd|The user entered command string for the job.|
|
|
||||||
|running|Whether the job is running or not.|
|
|
||||||
|id|The ID of the job in the job table|
|
|
||||||
|pid|The Process ID|
|
|
||||||
|exitCode|The last exit code of the job.|
|
|
||||||
|stdout|The standard output of the job. This just means the normal logs of the process.|
|
|
||||||
|stderr|The standard error stream of the process. This (usually) includes error messages of the job.|
|
|
||||||
|
|
||||||
|
|
||||||
### Methods
|
|
||||||
#### background()
|
|
||||||
Puts a job in the background. This acts the same as initially running a job.
|
|
||||||
|
|
||||||
#### foreground()
|
|
||||||
Puts a job in the foreground. This will cause it to run like it was
|
|
||||||
executed normally and wait for it to complete.
|
|
||||||
|
|
||||||
#### start()
|
|
||||||
Starts running the job.
|
|
||||||
|
|
||||||
#### stop()
|
|
||||||
Stops the job from running.
|
|
||||||
|
|
|
@ -1,73 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.module
|
|
||||||
description: native module loading
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
The hilbish.module interface provides a function to load
|
|
||||||
Hilbish plugins/modules. Hilbish modules are Go-written
|
|
||||||
plugins (see https://pkg.go.dev/plugin) that are used to add functionality
|
|
||||||
to Hilbish that cannot be written in Lua for any reason.
|
|
||||||
|
|
||||||
Note that you don't ever need to use the load function that is here as
|
|
||||||
modules can be loaded with a `require` call like Lua C modules, and the
|
|
||||||
search paths can be changed with the `paths` property here.
|
|
||||||
|
|
||||||
To make a valid native module, the Go plugin has to export a Loader function
|
|
||||||
with a signature like so: `func(*rt.Runtime) rt.Value`.
|
|
||||||
|
|
||||||
`rt` in this case refers to the Runtime type at
|
|
||||||
https://pkg.go.dev/github.com/arnodel/golua@master/runtime#Runtime
|
|
||||||
|
|
||||||
Hilbish uses this package as its Lua runtime. You will need to read
|
|
||||||
it to use it for a native plugin.
|
|
||||||
|
|
||||||
Here is some code for an example plugin:
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Loader(rtm *rt.Runtime) rt.Value {
|
|
||||||
return rt.StringValue("hello world!")
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
This can be compiled with `go build -buildmode=plugin plugin.go`.
|
|
||||||
If you attempt to require and print the result (`print(require 'plugin')`), it will show "hello world!"
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#module.load">load(path)</a>|Loads a module at the designated `path`.|
|
|
||||||
|
|
||||||
## Static module fields
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|paths|A list of paths to search when loading native modules. This is in the style of Lua search paths and will be used when requiring native modules. Example: `?.so;?/?.so`|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='module.load'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.module.load(path)
|
|
||||||
<a href="#module.load" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Loads a module at the designated `path`.
|
|
||||||
It will throw if any error occurs.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`path`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.os
|
|
||||||
description: operating system info
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
Provides simple text information properties about the current operating system.
|
|
||||||
This mainly includes the name and version.
|
|
||||||
|
|
||||||
## Static module fields
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|family|Family name of the current OS|
|
|
||||||
|name|Pretty name of the current OS|
|
|
||||||
|version|Version of the current OS|
|
|
||||||
|
|
|
@ -1,116 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.runner
|
|
||||||
description: interactive command runner customization
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The runner interface contains functions that allow the user to change
|
|
||||||
how Hilbish interprets interactive input.
|
|
||||||
Users can add and change the default runner for interactive input to any
|
|
||||||
language or script of their choosing. A good example is using it to
|
|
||||||
write command in Fennel.
|
|
||||||
|
|
||||||
Runners are functions that evaluate user input. The default runners in
|
|
||||||
Hilbish can run shell script and Lua code.
|
|
||||||
|
|
||||||
A runner is passed the input and has to return a table with these values.
|
|
||||||
All are not required, only the useful ones the runner needs to return.
|
|
||||||
(So if there isn't an error, just omit `err`.)
|
|
||||||
|
|
||||||
- `exitCode` (number): Exit code of the command
|
|
||||||
- `input` (string): The text input of the user. This is used by Hilbish to append extra input, in case
|
|
||||||
more is requested.
|
|
||||||
- `err` (string): A string that represents an error from the runner.
|
|
||||||
This should only be set when, for example, there is a syntax error.
|
|
||||||
It can be set to a few special values for Hilbish to throw the right
|
|
||||||
hooks and have a better looking message.
|
|
||||||
- `\<command>: not-found` will throw a `command.not-found` hook
|
|
||||||
based on what `\<command>` is.
|
|
||||||
- `\<command>: not-executable` will throw a `command.not-executable` hook.
|
|
||||||
- `continue` (boolean): Whether Hilbish should prompt the user for no input
|
|
||||||
- `newline` (boolean): Whether a newline should be added at the end of `input`.
|
|
||||||
|
|
||||||
Here is a simple example of a fennel runner. It falls back to
|
|
||||||
shell script if fennel eval has an error.
|
|
||||||
```lua
|
|
||||||
local fennel = require 'fennel'
|
|
||||||
|
|
||||||
hilbish.runnerMode(function(input)
|
|
||||||
local ok = pcall(fennel.eval, input)
|
|
||||||
if ok then
|
|
||||||
return {
|
|
||||||
input = input
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
return hilbish.runner.sh(input)
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#runner.setMode">setMode(cb)</a>|This is the same as the `hilbish.runnerMode` function.|
|
|
||||||
|<a href="#runner.lua">lua(cmd)</a>|Evaluates `cmd` as Lua input. This is the same as using `dofile`|
|
|
||||||
|<a href="#runner.sh">sh(cmd)</a>|Runs a command in Hilbish's shell script interpreter.|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='runner.setMode'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.runner.setMode(cb)
|
|
||||||
<a href="#runner.setMode" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
This is the same as the `hilbish.runnerMode` function.
|
|
||||||
It takes a callback, which will be used to execute all interactive input.
|
|
||||||
In normal cases, neither callbacks should be overrided by the user,
|
|
||||||
as the higher level functions listed below this will handle it.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`function` **`cb`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='runner.lua'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.runner.lua(cmd)
|
|
||||||
<a href="#runner.lua" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Evaluates `cmd` as Lua input. This is the same as using `dofile`
|
|
||||||
or `load`, but is appropriated for the runner interface.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmd`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='runner.sh'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.runner.sh(cmd)
|
|
||||||
<a href="#runner.sh" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Runs a command in Hilbish's shell script interpreter.
|
|
||||||
This is the equivalent of using `source`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`string` **`cmd`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.timers
|
|
||||||
description: timeout and interval API
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
If you ever want to run a piece of code on a timed interval, or want to wait
|
|
||||||
a few seconds, you don't have to rely on timing tricks, as Hilbish has a
|
|
||||||
timer API to set intervals and timeouts.
|
|
||||||
|
|
||||||
These are the simple functions `hilbish.interval` and `hilbish.timeout` (doc
|
|
||||||
accessible with `doc hilbish`, or `Module hilbish` on the Website).
|
|
||||||
|
|
||||||
An example of usage:
|
|
||||||
```lua
|
|
||||||
local t = hilbish.timers.create(hilbish.timers.TIMEOUT, 5000, function()
|
|
||||||
print 'hello!'
|
|
||||||
end)
|
|
||||||
|
|
||||||
t:start()
|
|
||||||
print(t.running) // true
|
|
||||||
```
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#timers.create">create(type, time, callback) -> @Timer</a>|Creates a timer that runs based on the specified `time`.|
|
|
||||||
|<a href="#timers.get">get(id) -> @Timer</a>|Retrieves a timer via its ID.|
|
|
||||||
|
|
||||||
## Static module fields
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|INTERVAL|Constant for an interval timer type|
|
|
||||||
|TIMEOUT|Constant for a timeout timer type|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='timers.create'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.timers.create(type, time, callback) -> <a href="/Hilbish/docs/api/hilbish/hilbish.timers/#timer" style="text-decoration: none;" id="lol">Timer</a>
|
|
||||||
<a href="#timers.create" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Creates a timer that runs based on the specified `time`.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`number` **`type`**
|
|
||||||
What kind of timer to create, can either be `hilbish.timers.INTERVAL` or `hilbish.timers.TIMEOUT`
|
|
||||||
|
|
||||||
`number` **`time`**
|
|
||||||
The amount of time the function should run in milliseconds.
|
|
||||||
|
|
||||||
`function` **`callback`**
|
|
||||||
The function to run for the timer.
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='timers.get'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
hilbish.timers.get(id) -> <a href="/Hilbish/docs/api/hilbish/hilbish.timers/#timer" style="text-decoration: none;" id="lol">Timer</a>
|
|
||||||
<a href="#timers.get" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Retrieves a timer via its ID.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
`number` **`id`**
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
## Types
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## Timer
|
|
||||||
The Job type describes a Hilbish timer.
|
|
||||||
## Object properties
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|type|What type of timer it is|
|
|
||||||
|running|If the timer is running|
|
|
||||||
|duration|The duration in milliseconds that the timer will run|
|
|
||||||
|
|
||||||
|
|
||||||
### Methods
|
|
||||||
#### start()
|
|
||||||
Starts a timer.
|
|
||||||
|
|
||||||
#### stop()
|
|
||||||
Stops a timer.
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
---
|
|
||||||
title: Module hilbish.userDir
|
|
||||||
description: user-related directories
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
This interface just contains properties to know about certain user directories.
|
|
||||||
It is equivalent to XDG on Linux and gets the user's preferred directories
|
|
||||||
for configs and data.
|
|
||||||
|
|
||||||
## Static module fields
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|config|The user's config directory|
|
|
||||||
|data|The user's directory for program data|
|
|
||||||
|
|
|
@ -1,81 +0,0 @@
|
||||||
---
|
|
||||||
title: Module terminal
|
|
||||||
description: low level terminal library
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "API"
|
|
||||||
---
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
The terminal library is a simple and lower level library for certain terminal interactions.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|||
|
|
||||||
|----|----|
|
|
||||||
|<a href="#restoreState">restoreState()</a>|Restores the last saved state of the terminal|
|
|
||||||
|<a href="#saveState">saveState()</a>|Saves the current state of the terminal.|
|
|
||||||
|<a href="#setRaw">setRaw()</a>|Puts the terminal into raw mode.|
|
|
||||||
|<a href="#size">size()</a>|Gets the dimensions of the terminal. Returns a table with `width` and `height`|
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='restoreState'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
terminal.restoreState()
|
|
||||||
<a href="#restoreState" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Restores the last saved state of the terminal
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='saveState'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
terminal.saveState()
|
|
||||||
<a href="#saveState" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Saves the current state of the terminal.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='setRaw'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
terminal.setRaw()
|
|
||||||
<a href="#setRaw" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Puts the terminal into raw mode.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='size'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
terminal.size()
|
|
||||||
<a href="#size" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
|
||||||
NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal.
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
catch(name, cb) > Catches a hook with `name`. Runs the `cb` when it is thrown
|
||||||
|
|
||||||
|
catchOnce(name, cb) > Same as catch, but only runs the `cb` once and then removes the hook
|
||||||
|
|
||||||
|
throw(name, ...args) > Throws a hook with `name` with the provided `args`
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
deregister(name) > Deregisters any command registered with `name`
|
||||||
|
|
||||||
|
register(name, cb) > Register a command with `name` that runs `cb` when ran
|
||||||
|
|
|
@ -1,78 +0,0 @@
|
||||||
---
|
|
||||||
title: Completions
|
|
||||||
description: Tab completion for commands.
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Features"
|
|
||||||
---
|
|
||||||
|
|
||||||
Completions for commands can be created with the [`hilbish.complete`](../api/hilbish#complete)
|
|
||||||
function. See the link for how to use it.
|
|
||||||
|
|
||||||
To create completions for a command is simple.
|
|
||||||
The callback will be passed 3 parameters:
|
|
||||||
- `query` (string): The text that the user is currently trying to complete.
|
|
||||||
This should be used to match entries.
|
|
||||||
- `ctx` (string): Contains the entire line. Use this if
|
|
||||||
more text is needed to be parsed for context.
|
|
||||||
- `fields` (string): The `ctx` split up by spaces.
|
|
||||||
|
|
||||||
In most cases, the completer just uses `fields` to check the amount
|
|
||||||
and `query` on what to match entries on.
|
|
||||||
|
|
||||||
In order to return your results, it has to go within a "completion group."
|
|
||||||
Then you return a table of completion groups and a prefix. The prefix will
|
|
||||||
usually just be the `query`.
|
|
||||||
|
|
||||||
Hilbish allows one to mix completion menus of different types, so
|
|
||||||
a grid menu and a list menu can be used and complete and display at the same time.
|
|
||||||
A completion group is a table with these keys:
|
|
||||||
- `type` (string): type of completion menu, either `grid` or `list`.
|
|
||||||
- `items` (table): a list of items.
|
|
||||||
|
|
||||||
The requirements of the `items` table is different based on the
|
|
||||||
`type`. If it is a `grid`, it can simply be a table of strings.
|
|
||||||
|
|
||||||
Otherwise if it is a `list` then each entry can
|
|
||||||
either be a string or a table.
|
|
||||||
Example:
|
|
||||||
```lua
|
|
||||||
local cg = {
|
|
||||||
items = {
|
|
||||||
'list item 1',
|
|
||||||
['--command-flag-here'] = {'this does a thing', '--the-flag-alias'}
|
|
||||||
},
|
|
||||||
type = 'list'
|
|
||||||
}
|
|
||||||
local cg2 = {
|
|
||||||
items = {'just', 'a bunch', 'of items', 'here', 'hehe'},
|
|
||||||
type = 'grid'
|
|
||||||
}
|
|
||||||
|
|
||||||
return {cg, cg2}, prefix
|
|
||||||
```
|
|
||||||
|
|
||||||
Which looks like this:
|
|
||||||
{{< video src="https://safe.saya.moe/t4CiLK6dgPbD.mp4" >}}
|
|
||||||
|
|
||||||
# Completion Handler
|
|
||||||
Like most parts of Hilbish, it's made to be extensible and
|
|
||||||
customizable. The default handler for completions in general can
|
|
||||||
be overwritten to provide more advanced completions if needed.
|
|
||||||
This usually doesn't need to be done though, unless you know
|
|
||||||
what you're doing.
|
|
||||||
|
|
||||||
The default completion handler provides 3 things:
|
|
||||||
binaries (with a plain name requested to complete, those in
|
|
||||||
$PATH), files, or command completions. It will try to run a handler
|
|
||||||
for the command or fallback to file completions.
|
|
||||||
|
|
||||||
To overwrite it, just assign a function to `hilbish.completion.handler` like so:
|
|
||||||
```lua
|
|
||||||
-- line is the entire line as a string
|
|
||||||
-- pos is the position of the cursor.
|
|
||||||
function hilbish.completion.handler(line, pos)
|
|
||||||
-- do things
|
|
||||||
end
|
|
||||||
```
|
|
26
docs/faq.md
|
@ -1,26 +0,0 @@
|
||||||
---
|
|
||||||
title: Frequently Asked Questions
|
|
||||||
layout: doc
|
|
||||||
weight: -20
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
# Is Hilbish POSIX compliant?
|
|
||||||
No, it is not. POSIX compliance is a non-goal. Perhaps in the future,
|
|
||||||
someone would be able to write a native plugin to support shell scripting
|
|
||||||
(which would be against it's main goal, but ....)
|
|
||||||
|
|
||||||
# Windows Support?
|
|
||||||
It compiles for Windows (CI ensures it does), but otherwise it is not
|
|
||||||
directly supported. If you'd like to improve this situation,
|
|
||||||
checkout [the discussion](https://github.com/Rosettea/Hilbish/discussions/165).
|
|
||||||
|
|
||||||
# Why?
|
|
||||||
Hilbish emerged from the desire of a Lua configured shell.
|
|
||||||
It was the initial reason that it was created, but now it's more:
|
|
||||||
to be hyper extensible, simpler and more user friendly.
|
|
||||||
|
|
||||||
# Does it have "autocompletion" or "tab completion"
|
|
||||||
Of course! This is a modern shell. Hilbish provides a way for users
|
|
||||||
to write tab completion for any command and/or the whole shell.
|
|
||||||
Inline hinting and syntax highlighting are also available.
|
|
|
@ -1,11 +0,0 @@
|
||||||
---
|
|
||||||
title: Features
|
|
||||||
layout: doc
|
|
||||||
weight: -40
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
Hilbish has a wide range of features to enhance the user's experience
|
|
||||||
new ones are always being added. If there is something missing here or
|
|
||||||
something you would like to see, please [start a discussion](https://github.com/Rosettea/Hilbish/discussions)
|
|
||||||
or comment on any existing ones which match your request.
|
|
|
@ -1,39 +0,0 @@
|
||||||
---
|
|
||||||
title: Notification
|
|
||||||
description: Get notified of shell actions.
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Features"
|
|
||||||
---
|
|
||||||
|
|
||||||
Hilbish features a simple notification system which can be
|
|
||||||
used by other plugins and parts of the shell to notify the user
|
|
||||||
of various actions. This is used via the `hilbish.message` interface.
|
|
||||||
|
|
||||||
A `message` is defined as a table with the following properties:
|
|
||||||
- `icon`: A unicode/emoji icon for the notification.
|
|
||||||
- `title`: The title of the message
|
|
||||||
- `text`: Message text/body
|
|
||||||
- `channel`: The source of the message. This should be a
|
|
||||||
unique and easily readable text identifier.
|
|
||||||
- `summary`: A short summary of the notification and message.
|
|
||||||
If this is not present and you are using this to display messages,
|
|
||||||
you should take part of the `text` instead.
|
|
||||||
|
|
||||||
The `hilbish.message` interface provides the following functions:
|
|
||||||
- `send(message)`: Sends a message and emits the `hilbish.notification`
|
|
||||||
signal. DO NOT emit the `hilbish.notification` signal directly, or
|
|
||||||
the message will not be stored by the message handler.
|
|
||||||
- `read(idx)`: Marks message at `idx` as read.
|
|
||||||
- `delete(idx)`: Removes message at `idx`.
|
|
||||||
- `readAll()`: Marks all messages as read.
|
|
||||||
- `clear()`: Deletes all messages.
|
|
||||||
|
|
||||||
There are a few simple use cases of this notification/messaging system.
|
|
||||||
It could also be used as some "inter-shell" messaging system (???) but
|
|
||||||
is intended to display to users.
|
|
||||||
|
|
||||||
An example is notifying users of completed jobs/commands ran in the background.
|
|
||||||
Any Hilbish-native command (think the upcoming Greenhouse pager) can display
|
|
||||||
it.
|
|
|
@ -1,78 +0,0 @@
|
||||||
---
|
|
||||||
title: Options
|
|
||||||
description: Simple customizable options.
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Features"
|
|
||||||
---
|
|
||||||
|
|
||||||
Opts are simple toggle or value options a user can set in Hilbish.
|
|
||||||
As toggles, there are things like `autocd` or history saving. As values,
|
|
||||||
there is the `motd` which the user can either change to a custom string or disable.
|
|
||||||
|
|
||||||
Opts are accessed from the `hilbish.opts` table. Here they can either
|
|
||||||
be read or modified
|
|
||||||
|
|
||||||
### `autocd`
|
|
||||||
#### Value: `boolean`
|
|
||||||
#### Default: `false`
|
|
||||||
|
|
||||||
The autocd opt makes it so that lone directories attempted to be executed are
|
|
||||||
instead set as the shell's directory.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
```
|
|
||||||
~/Directory
|
|
||||||
∆ ~
|
|
||||||
~
|
|
||||||
∆ Downloads
|
|
||||||
~/Downloads
|
|
||||||
∆ ../Documents
|
|
||||||
~/Documents
|
|
||||||
∆
|
|
||||||
```
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
### `history`
|
|
||||||
#### Value: `boolean`
|
|
||||||
#### Default: `true`
|
|
||||||
Sets whether command history will be saved or not.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
### `greeting`
|
|
||||||
#### Value: `boolean` or `string`
|
|
||||||
The greeting is the message that Hilbish shows on startup
|
|
||||||
(the one which says Welcome to Hilbish).
|
|
||||||
|
|
||||||
This can be set to either true/false to enable/disable or a custom greeting string.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
### `motd`
|
|
||||||
#### Value: `boolean`
|
|
||||||
#### Default: `true`
|
|
||||||
The message of the day shows the current major.minor version and
|
|
||||||
includes a small range of things added in the current release.
|
|
||||||
|
|
||||||
This can be set to `false` to disable the message.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
### `fuzzy`
|
|
||||||
#### Value: `boolean`
|
|
||||||
#### Default: `false`
|
|
||||||
Toggles the functionality of fuzzy history searching, usable
|
|
||||||
via the menu in Ctrl-R. Fuzzy searching is an approximate searching
|
|
||||||
method, which means results that match *closest* will be shown instead
|
|
||||||
of an exact match.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
### `notifyJobFinish`
|
|
||||||
#### Value: `boolean`
|
|
||||||
#### Default: `true`
|
|
||||||
If this is enabled, when a background job is finished,
|
|
||||||
a [notification](../notifications) will be sent.
|
|
|
@ -1,53 +0,0 @@
|
||||||
---
|
|
||||||
title: Runner Mode
|
|
||||||
description: Customize the interactive script/command runner.
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Features"
|
|
||||||
---
|
|
||||||
|
|
||||||
Hilbish allows you to change how interactive text can be interpreted.
|
|
||||||
This is mainly due to the fact that the default method Hilbish uses
|
|
||||||
is that it runs Lua first and then falls back to shell script.
|
|
||||||
|
|
||||||
In some cases, someone might want to switch to just shell script to avoid
|
|
||||||
it while interactive but still have a Lua config, or go full Lua to use
|
|
||||||
Hilbish as a REPL. This also allows users to add alternative languages like
|
|
||||||
Fennel as the interactive script runner.
|
|
||||||
|
|
||||||
Runner mode can also be used to handle specific kinds of input before
|
|
||||||
evaluating like normal, which is how [Link.hsh](https://github.com/TorchedSammy/Link.hsh)
|
|
||||||
handles links.
|
|
||||||
|
|
||||||
The "runner mode" of Hilbish is customizable via `hilbish.runnerMode`,
|
|
||||||
which determines how Hilbish will run user input. By default, this is
|
|
||||||
set to `hybrid` which is the previously mentioned behaviour of running Lua
|
|
||||||
first then going to shell script. If you want the reverse order, you can
|
|
||||||
set it to `hybridRev` and for isolated modes there is `sh` and `lua`
|
|
||||||
respectively.
|
|
||||||
|
|
||||||
You can also set it to a function, which will be called everytime Hilbish
|
|
||||||
needs to run interactive input. For more detail, see the [API documentation](../../api/hilbish/hilbish.runner)
|
|
||||||
|
|
||||||
The `hilbish.runner` interface is an alternative to using `hilbish.runnerMode`
|
|
||||||
and also provides the shell script and Lua runner functions that Hilbish itself uses.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
These are the "low level" functions for the `hilbish.runner` interface.
|
|
||||||
|
|
||||||
+ setMode(mode) > The same as `hilbish.runnerMode`
|
|
||||||
+ sh(input) -> table > Runs `input` in Hilbish's sh interpreter
|
|
||||||
+ lua(input) -> table > Evals `input` as Lua code
|
|
||||||
|
|
||||||
These functions should be preferred over the previous ones.
|
|
||||||
+ setCurrent(mode) > The same as `setMode`, but works with runners managed
|
|
||||||
via the functions below.
|
|
||||||
+ add(name, runner) > Adds a runner to a table of available runners. The `runner`
|
|
||||||
argument is either a function or a table with a run callback.
|
|
||||||
+ set(name, runner) > The same as `add` but requires passing a table and
|
|
||||||
overwrites if the `name`d runner already exists.
|
|
||||||
+ get(name) > runner > Gets a runner by name. It is a table with at least a
|
|
||||||
run function, to run input.
|
|
||||||
+ exec(cmd, runnerName) > Runs `cmd` with a runner. If `runnerName` isn't passed,
|
|
||||||
the current runner mode is used.
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
cd(dir) > Changes directory to `dir`
|
||||||
|
|
||||||
|
mkdir(name, recursive) > Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
||||||
|
|
||||||
|
readdir(dir) > Returns a table of files in `dir`
|
||||||
|
|
||||||
|
stat(path) > Returns info about `path`
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
---
|
|
||||||
title: Getting Started
|
|
||||||
layout: doc
|
|
||||||
weight: -10
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
To start Hilbish, open a terminal. If Hilbish has been installed and is not the
|
|
||||||
default shell, you can simply run `hilbish` to start it. This will launch
|
|
||||||
a normal interactive session.
|
|
||||||
To exit, you can either run the `exit` command or hit Ctrl+D.
|
|
||||||
|
|
||||||
# Setting as Default
|
|
||||||
## Login shell
|
|
||||||
There are a few ways to make Hilbish your default shell. A simple way is
|
|
||||||
to make it your user/login shell.
|
|
||||||
|
|
||||||
{{< warning `It is not recommended to set Hilbish as your login shell. That
|
|
||||||
is expected to be a POSIX compliant shell, which Hilbish is not. Though if
|
|
||||||
you still decide to do it, there will just be a few variables missing in
|
|
||||||
your environment` >}}
|
|
||||||
|
|
||||||
To do that, simply run `chsh -s /usr/bin/hilbish`.
|
|
||||||
Some distros (namely Fedora) might have `lchsh` instead, which is used like `lchsh <user>`.
|
|
||||||
When prompted, you can put the path for Hilbish.
|
|
||||||
|
|
||||||
## Default with terminal
|
|
||||||
The simpler way is to set the default shell for your terminal. The way of
|
|
||||||
doing this depends on how your terminal settings are configured.
|
|
||||||
|
|
||||||
## Run after login shell
|
|
||||||
Some shells (like zsh) have an rc file, like `.zlogin`, which is ran when the shell session
|
|
||||||
is a login shell. In that file, you can run Hilbish. Example:
|
|
||||||
|
|
||||||
```
|
|
||||||
exec hilbish -S -l
|
|
||||||
```
|
|
||||||
|
|
||||||
This will replace the shell with Hilbish, set $SHELL to Hilbish and launch it as a login shell.
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
Once installation and setup has been done, you can then configure Hilbish.
|
|
||||||
It is configured and scripted via Lua, so the config file is a Lua file.
|
|
||||||
You can use any pure Lua library to do whatever you want.
|
|
||||||
|
|
||||||
Hilbish's sample configuration is usually located in `hilbish.dataDir .. '/.hilbishrc.lua'`.
|
|
||||||
You can print that path via Lua to see what it is: `print(hilbish.dataDir .. '/.hilbishrc.lua')`.
|
|
||||||
As an example, it will usually will result in `/usr/share/hilbish/.hilbishrc.lua` on Linux.
|
|
||||||
|
|
||||||
To edit your user configuration, you can copy that file to `hilbish.userDir.config .. '/hilbish/init.lua'`,
|
|
||||||
which follows XDG on Linux and MacOS, and is located in %APPDATA% on Windows.
|
|
||||||
|
|
||||||
As the directory is usually `~/.config` on Linux, you can run this command to copy it:
|
|
||||||
`cp /usr/share/hilbish/.hilbishrc.lua ~/.config/hilbish/init.lua`
|
|
||||||
|
|
||||||
Now you can get to editing it. Since it's just a Lua file, having basic
|
|
||||||
knowledge of Lua would help. All of Lua's standard libraries and functions
|
|
||||||
from Lua 5.4 are available. Hilbish has some custom and modules that are
|
|
||||||
available. To see them, you can run the `doc` command. This also works as
|
|
||||||
general documentation for other things.
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
alias(cmd, orig) > Sets an alias of `orig` to `cmd`
|
||||||
|
|
||||||
|
appendPath(dir) > Appends `dir` to $PATH
|
||||||
|
|
||||||
|
complete(scope, cb) > Registers a completion handler for `scope`.
|
||||||
|
A `scope` is currently only expected to be `command.<cmd>`,
|
||||||
|
replacing <cmd> with the name of the command (for example `command.git`).
|
||||||
|
`cb` must be a function that returns a table of "completion groups."
|
||||||
|
A completion group is a table with the keys `items` and `type`.
|
||||||
|
`items` being a table of items and `type` being the display type of
|
||||||
|
`grid` (the normal file completion display) or `list` (with a description)
|
||||||
|
|
||||||
|
cwd() > Returns the current directory of the shell
|
||||||
|
|
||||||
|
exec(cmd) > Replaces running hilbish with `cmd`
|
||||||
|
|
||||||
|
goro(fn) > Puts `fn` in a goroutine
|
||||||
|
|
||||||
|
inputMode(mode) > Sets the input mode for Hilbish's line reader. Accepts either emacs for vim
|
||||||
|
|
||||||
|
interval(cb, time) > Runs the `cb` function every `time` milliseconds
|
||||||
|
|
||||||
|
multiprompt(str) > Changes the continued line prompt to `str`
|
||||||
|
|
||||||
|
prependPath(dir) > Prepends `dir` to $PATH
|
||||||
|
|
||||||
|
prompt(str) > Changes the shell prompt to `str`
|
||||||
|
There are a few verbs that can be used in the prompt text.
|
||||||
|
These will be formatted and replaced with the appropriate values.
|
||||||
|
`%d` - Current working directory
|
||||||
|
`%u` - Name of current user
|
||||||
|
`%h` - Hostname of device
|
||||||
|
|
||||||
|
read(prompt) -> input? > Read input from the user, using Hilbish's line editor/input reader.
|
||||||
|
This is a separate instance from the one Hilbish actually uses.
|
||||||
|
Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
|
||||||
|
|
||||||
|
run(cmd) > Runs `cmd` in Hilbish's sh interpreter.
|
||||||
|
|
||||||
|
timeout(cb, time) > Runs the `cb` function after `time` in milliseconds
|
||||||
|
|
||||||
|
which(binName) > Searches for an executable called `binName` in the directories of $PATH
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
---
|
|
||||||
title: Signals
|
|
||||||
description:
|
|
||||||
layout: doc
|
|
||||||
weight: -50
|
|
||||||
menu:
|
|
||||||
docs
|
|
||||||
---
|
|
||||||
|
|
||||||
Signals are global events emitted with the [Bait](../api/bait) module.
|
|
||||||
For more detail on how to use these signals, you may check the Bait page.
|
|
|
@ -1,67 +0,0 @@
|
||||||
---
|
|
||||||
title: Command
|
|
||||||
description:
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Signals"
|
|
||||||
---
|
|
||||||
|
|
||||||
## command.preexec
|
|
||||||
Thrown right before a command is executed.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
`string` **`input`**
|
|
||||||
The raw string that the user typed. This will include the text
|
|
||||||
without changes applied to it (argument substitution, alias expansion,
|
|
||||||
etc.)
|
|
||||||
|
|
||||||
`string` **`cmdStr`**
|
|
||||||
The command that will be directly executed by the current runner.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## command.exit
|
|
||||||
Thrown after the user's ran command is finished.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
`number` **`code`**
|
|
||||||
The exit code of what was executed.
|
|
||||||
|
|
||||||
`string` **`cmdStr`**
|
|
||||||
The command or code that was executed
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## command.not-found
|
|
||||||
Thrown if the command attempted to execute was not found.
|
|
||||||
This can be used to customize the text printed when a command is not found.
|
|
||||||
Example:
|
|
||||||
```lua
|
|
||||||
local bait = require 'bait'
|
|
||||||
-- Remove any present handlers on `command.not-found`
|
|
||||||
|
|
||||||
local notFoundHooks = bait.hooks 'command.not-found'
|
|
||||||
for _, hook in ipairs(notFoundHooks) do
|
|
||||||
bait.release('command.not-found', hook)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- then assign custom
|
|
||||||
bait.catch('command.not-found', function(cmd)
|
|
||||||
print(string.format('The command "%s" was not found.', cmd))
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
`string` **`cmdStr`**
|
|
||||||
The name of the command.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## command.not-executable
|
|
||||||
Thrown when the user attempts to run a file that is not executable
|
|
||||||
(like a text file, or Unix binary without +x permission).
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
`string` **`cmdStr`**
|
|
||||||
The name of the command.
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
+ `command.exit` -> code, cmdStr > Thrown when a command exits.
|
||||||
|
`code` is the exit code of the command, and `cmdStr` is the command that was run.
|
||||||
|
|
||||||
|
+ `command.not-found` -> cmdStr > Thrown when a command is not found.
|
||||||
|
|
||||||
|
+ `command.no-perm` -> cmdStr > Thrown when Hilbish attempts to execute a file but
|
||||||
|
has no permission.
|
|
@ -1,47 +0,0 @@
|
||||||
---
|
|
||||||
title: Hilbish
|
|
||||||
description:
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Signals"
|
|
||||||
---
|
|
||||||
|
|
||||||
## hilbish.exit
|
|
||||||
Sent when Hilbish is going to exit.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
This signal returns no variables.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## hilbish.vimMode
|
|
||||||
Sent when the Vim mode of Hilbish is changed (like from insert to normal mode).
|
|
||||||
This can be used to change the prompt and notify based on Vim mode.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
`string` **`modeName`**
|
|
||||||
The mode that has been set.
|
|
||||||
Can be these values: `insert`, `normal`, `delete` or `replace`
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## hilbish.cancel
|
|
||||||
Sent when the user cancels their command input with Ctrl-C
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
This signal returns no variables.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## hilbish.notification
|
|
||||||
Thrown when a [notification](../../features/notifications) is sent.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
`table` **`notification`**
|
|
||||||
The notification. The properties are defined in the link above.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
+ `hilbish.vimAction` -> actionName, args > Sent when the user does a "vim action," being something
|
|
||||||
like yanking or pasting text. See `doc vim-mode actions` for more info.
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
+ `hilbish.exit` > Sent when Hilbish is about to exit.
|
||||||
|
|
||||||
|
+ `hilbish.vimMode` > Sent when Hilbish's Vim mode is changed (example insert to normal mode)
|
|
@ -0,0 +1,8 @@
|
||||||
|
Here is a list of bait hooks that are thrown by Hilbish. If a hook is related
|
||||||
|
to a command, it will have the `command` scope, as example.
|
||||||
|
|
||||||
|
Here is the format for a doc for a hook:
|
||||||
|
+ <hook name> -> <args> > <description>
|
||||||
|
|
||||||
|
`<args>` just means the arguments of the hook. If a hook doc has the format
|
||||||
|
of `arg...`, it means the hook can take/recieve any number of `arg`.
|
|
@ -1,7 +0,0 @@
|
||||||
Note: `job` refers to a job object. You can check `doc jobs` for more
|
|
||||||
detail.
|
|
||||||
|
|
||||||
+ `job.start` -> job > Thrown when a new background job starts.
|
|
||||||
|
|
||||||
+ `job.done` -> job > Thrown when a background jobs exits.
|
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
---
|
|
||||||
title: Signal
|
|
||||||
description:
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Signals"
|
|
||||||
---
|
|
||||||
|
|
||||||
## signal.sigint
|
|
||||||
Thrown when Hilbish receive the SIGINT signal,
|
|
||||||
aka when Ctrl-C is pressed.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
This signal returns no variables.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## signal.resize
|
|
||||||
Thrown when the terminal is resized.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
This signal returns no variables.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## signal.sigusr1
|
|
||||||
Thrown when SIGUSR1 is sent to Hilbish.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
This signal returns no variables.
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
## signal.sigusr2
|
|
||||||
Thrown when SIGUSR2 is sent to Hilbish.
|
|
||||||
|
|
||||||
#### Variables
|
|
||||||
This signal returns no variables.
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
+ `signal.sigint` > Sent when Hilbish receives SIGINT (on Ctrl-C).
|
||||||
|
|
||||||
|
+ `signal.resize` > Sent when the terminal is resized.
|
||||||
|
|
||||||
|
+ `signal.sigusr1`
|
||||||
|
|
||||||
|
+ `signal.sigusr2`
|
48
docs/jobs.md
|
@ -1,48 +0,0 @@
|
||||||
(This has mainly been replaced by [hilbish.jobs](../api/hilbish.jobs)).
|
|
||||||
|
|
||||||
Hilbish has pretty standard job control. It's missing one or two things,
|
|
||||||
but works well. One thing which is different from other shells
|
|
||||||
(besides Hilbish) itself is the API for jobs, and of course it's in Lua.
|
|
||||||
You can add jobs, stop and delete (disown) them and even get output.
|
|
||||||
|
|
||||||
# Job Interface
|
|
||||||
The job interface refers to `hilbish.jobs`.
|
|
||||||
## Functions
|
|
||||||
(Note that in the list here, they're called from `hilbish.jobs`, so
|
|
||||||
a listing of `foo` would mean `hilbish.jobs.foo`)
|
|
||||||
|
|
||||||
- `all()` -> {jobs}: Returns a table of all jobs.
|
|
||||||
- `last()` -> job: Returns the last added job.
|
|
||||||
- `get(id)` -> job: Get a job by its ID.
|
|
||||||
- `add(cmdstr, args, execPath)` -> job: Adds a new job to the job table.
|
|
||||||
Note that this does not run the command; You have to start it manually.
|
|
||||||
`cmdstr` is the user's input for the job, `args` is a table of arguments
|
|
||||||
for the command. It includes arg0 (don't set it as entry 0 in the table)
|
|
||||||
and `execPath` is an absolute path for the command executable.
|
|
||||||
- `disown(id)`: Removes a job by ID from the job table.
|
|
||||||
|
|
||||||
# Job Object
|
|
||||||
A job object is a piece of `userdata`. All the functions of a job require
|
|
||||||
you to call them with a colon, since they are *methods* for the job object.
|
|
||||||
Example: hilbish.jobs.last():foreground()
|
|
||||||
Which will foreground the last job.
|
|
||||||
|
|
||||||
You can still have a job object for a disowned job,
|
|
||||||
it just won't be *working* anywhere. :^)
|
|
||||||
|
|
||||||
## Properties
|
|
||||||
- `cmd`: command string
|
|
||||||
- `running`: boolean whether the job is running
|
|
||||||
- `id`: unique id for the job
|
|
||||||
- `pid`: process id for the job
|
|
||||||
- `exitCode`: exit code of the job
|
|
||||||
In ordinary cases you'd prefer to use the `id` instead of `pid`.
|
|
||||||
The `id` is unique to Hilbish and is how you get jobs with the
|
|
||||||
`hilbish.jobs` interface. It may also not describe the job entirely.
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
- `stop()`: Stops the job.
|
|
||||||
- `start()`: Starts the job.
|
|
||||||
- `foreground()`: Set the job as the current running foreground process, or
|
|
||||||
run it in the foreground after it has been suspended.
|
|
||||||
- `background()`: Run the job in the background after it has been suspended.
|
|
|
@ -1,41 +0,0 @@
|
||||||
---
|
|
||||||
title: Lunacolors
|
|
||||||
layout: doc
|
|
||||||
weight: -60
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
Lunacolors is an ANSI color/styling library for Lua. It is included
|
|
||||||
by default in standard Hilbish distributions to provide easy styling
|
|
||||||
for things like prompts and text.
|
|
||||||
|
|
||||||
For simple usage, a single color or style is enough. For example,
|
|
||||||
you can just use `lunacolors.blue 'Hello world'` and that'll return
|
|
||||||
blue text which you can print. This includes styles like bold,
|
|
||||||
underline, etc.
|
|
||||||
|
|
||||||
In other usage, you may want to use a format string instead of having
|
|
||||||
multiple nested functions for different styles. This is where the format
|
|
||||||
function comes in. You can used named keywords to style a section of text.
|
|
||||||
|
|
||||||
The list of arguments are:
|
|
||||||
Colors:
|
|
||||||
- black
|
|
||||||
- red
|
|
||||||
- green
|
|
||||||
- yellow
|
|
||||||
- blue
|
|
||||||
- magenta
|
|
||||||
- cyan
|
|
||||||
- white
|
|
||||||
Styles:
|
|
||||||
- bold
|
|
||||||
- dim
|
|
||||||
- italic
|
|
||||||
- underline
|
|
||||||
- invert
|
|
||||||
|
|
||||||
For the colors, there are background and bright variants. The background
|
|
||||||
color variants have a suffix of `Bg` and bright has a prefix of `bright`.
|
|
||||||
Note that appropriate camel casing has to be applied to them. So bright
|
|
||||||
blue would be `brightBlue` and background cyan would be `cyanBg`.
|
|
|
@ -1,19 +0,0 @@
|
||||||
---
|
|
||||||
title: Nature
|
|
||||||
layout: doc
|
|
||||||
weight: -90
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
A bit after creation, we have the outside nature. Little plants, seeds,
|
|
||||||
growing to their final phase: a full plant. A lot of Hilbish itself is
|
|
||||||
written in Go, but there are parts made in Lua, being most builtins
|
|
||||||
(`doc`, `cd`, cdr), completions, and other things.
|
|
||||||
|
|
||||||
Hilbish's Lua core module is called `nature`.
|
|
||||||
It runs after Hilbish's Go core does.
|
|
||||||
|
|
||||||
# Nature Modules
|
|
||||||
Currently, `nature` provides 1 intended public module: `nature.dirs`.
|
|
||||||
It is a simple API for managing recent directories and old
|
|
||||||
current working directory.
|
|
|
@ -1,79 +0,0 @@
|
||||||
---
|
|
||||||
title: Module dirs
|
|
||||||
description: No description.
|
|
||||||
layout: doc
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Nature"
|
|
||||||
---
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='setOld'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
dirs.setOld(d)
|
|
||||||
<a href="#setOld" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Sets the old directory string.
|
|
||||||
#### Parameters
|
|
||||||
`d` **`string`**
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='push'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
dirs.push()
|
|
||||||
<a href="#push" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Add `d` to the recent directories list.
|
|
||||||
#### Parameters
|
|
||||||
This function has no parameters.
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='peak'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
dirs.peak(num)
|
|
||||||
<a href="#peak" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Look at `num` amount of recent directories, starting from the latest.
|
|
||||||
#### Parameters
|
|
||||||
`num` **`number`**
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='pop'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
dirs.pop(num)
|
|
||||||
<a href="#pop" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Remove the specified amount of dirs from the recent directories list.
|
|
||||||
#### Parameters
|
|
||||||
`num` **`number`**
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
<div id='recent'>
|
|
||||||
<h4 class='heading'>
|
|
||||||
dirs.recent(idx)
|
|
||||||
<a href="#recent" class='heading-link'>
|
|
||||||
<i class="fas fa-paperclip"></i>
|
|
||||||
</a>
|
|
||||||
</h4>
|
|
||||||
|
|
||||||
Get entry from recent directories list based on index.
|
|
||||||
#### Parameters
|
|
||||||
`idx` **`number`**
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
setRaw() > Puts the terminal in raw mode
|
||||||
|
|
||||||
|
restoreState() > Restores the last saved state of the terminal
|
||||||
|
|
||||||
|
saveState() > Saves the current state of the terminal
|
||||||
|
|
||||||
|
size() > Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
||||||
|
Note: this is not the size in relation to the dimensions of the display
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
This has been moved to the `hilbish.timers` API doc (accessible by `doc api hilbish.timers`)
|
|
|
@ -1,11 +0,0 @@
|
||||||
---
|
|
||||||
title: Vim Mode
|
|
||||||
layout: doc
|
|
||||||
weight: -90
|
|
||||||
menu: docs
|
|
||||||
---
|
|
||||||
|
|
||||||
Hilbish has a Vim binding input mode accessible for use.
|
|
||||||
It can be enabled with the `hilbish.inputMode` function (check `doc hilbish`).
|
|
||||||
|
|
||||||
This is documentation for everything relating to it.
|
|
|
@ -1,25 +0,0 @@
|
||||||
---
|
|
||||||
title: Actions
|
|
||||||
layout: doc
|
|
||||||
weight: -80
|
|
||||||
menu:
|
|
||||||
docs:
|
|
||||||
parent: "Vim Mode"
|
|
||||||
---
|
|
||||||
|
|
||||||
Vim actions are essentially just when a user uses a Vim keybind.
|
|
||||||
Things like yanking and pasting are Vim actions.
|
|
||||||
This is not an "offical Vim thing," just a Hilbish thing.
|
|
||||||
|
|
||||||
The `hilbish.vimAction` hook is thrown whenever a Vim action occurs.
|
|
||||||
It passes 2 arguments: the action name, and an array (table) of args
|
|
||||||
relating to it.
|
|
||||||
|
|
||||||
Here is documentation for what the table of args will hold for an
|
|
||||||
appropriate Vim action.
|
|
||||||
|
|
||||||
- `yank`: register, yankedText
|
|
||||||
The first argument for the yank action is the register yankedText goes to.
|
|
||||||
|
|
||||||
- `paste`: register, pastedText
|
|
||||||
The first argument for the paste action is the register pastedText is taken from.
|
|
108
editor.go
|
@ -1,108 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"hilbish/util"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// #interface editor
|
|
||||||
// interactions for Hilbish's line reader
|
|
||||||
// The hilbish.editor interface provides functions to
|
|
||||||
// directly interact with the line editor in use.
|
|
||||||
func editorLoader(rtm *rt.Runtime) *rt.Table {
|
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"insert": {editorInsert, 1, false},
|
|
||||||
"setVimRegister": {editorSetRegister, 1, false},
|
|
||||||
"getVimRegister": {editorGetRegister, 2, false},
|
|
||||||
"getLine": {editorGetLine, 0, false},
|
|
||||||
"readChar": {editorReadChar, 0, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
mod := rt.NewTable()
|
|
||||||
util.SetExports(rtm, mod, exports)
|
|
||||||
|
|
||||||
return mod
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface editor
|
|
||||||
// insert(text)
|
|
||||||
// Inserts text into the Hilbish command line.
|
|
||||||
// #param text string
|
|
||||||
func editorInsert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
text, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
lr.rl.Insert(text)
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface editor
|
|
||||||
// setVimRegister(register, text)
|
|
||||||
// Sets the vim register at `register` to hold the passed text.
|
|
||||||
// #aram register string
|
|
||||||
// #param text string
|
|
||||||
func editorSetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
register, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
text, err := c.StringArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
lr.rl.SetRegisterBuf(register, []rune(text))
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface editor
|
|
||||||
// getVimRegister(register) -> string
|
|
||||||
// Returns the text that is at the register.
|
|
||||||
// #param register string
|
|
||||||
func editorGetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
register, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := lr.rl.GetFromRegister(register)
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface editor
|
|
||||||
// getLine() -> string
|
|
||||||
// Returns the current input line.
|
|
||||||
// #returns string
|
|
||||||
func editorGetLine(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
buf := lr.rl.GetLine()
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface editor
|
|
||||||
// getChar() -> string
|
|
||||||
// Reads a keystroke from the user. This is in a format of something like Ctrl-L.
|
|
||||||
func editorReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
buf := lr.rl.ReadChar()
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
|
||||||
}
|
|
|
@ -2,27 +2,19 @@
|
||||||
|
|
||||||
local bait = {}
|
local bait = {}
|
||||||
|
|
||||||
--- Catches an event. This function can be used to act on events.
|
--- Catches a hook with `name`. Runs the `cb` when it is thrown
|
||||||
---
|
--- @param name string
|
||||||
---
|
--- @param cb function
|
||||||
function bait.catch(name, cb) end
|
function bait.catch(name, cb) end
|
||||||
|
|
||||||
--- Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
|
--- Same as catch, but only runs the `cb` once and then removes the hook
|
||||||
|
--- @param name string
|
||||||
|
--- @param cb function
|
||||||
function bait.catchOnce(name, cb) end
|
function bait.catchOnce(name, cb) end
|
||||||
|
|
||||||
--- Returns a table of functions that are hooked on an event with the corresponding `name`.
|
--- Throws a hook with `name` with the provided `args`
|
||||||
function bait.hooks(name) end
|
--- @param name string
|
||||||
|
--- @vararg any
|
||||||
--- Removes the `catcher` for the event with `name`.
|
function bait.throw(name) end
|
||||||
--- For this to work, `catcher` has to be the same function used to catch
|
|
||||||
--- an event, like one saved to a variable.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function bait.release(name, catcher) end
|
|
||||||
|
|
||||||
--- Throws a hook with `name` with the provided `args`.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function bait.throw(name, ...args) end
|
|
||||||
|
|
||||||
return bait
|
return bait
|
||||||
|
|
|
@ -2,17 +2,13 @@
|
||||||
|
|
||||||
local commander = {}
|
local commander = {}
|
||||||
|
|
||||||
--- Removes the named command. Note that this will only remove Commander-registered commands.
|
--- Deregisters any command registered with `name`
|
||||||
|
--- @param name string
|
||||||
function commander.deregister(name) end
|
function commander.deregister(name) end
|
||||||
|
|
||||||
--- Adds a new command with the given `name`. When Hilbish has to run a command with a name,
|
--- Register a command with `name` that runs `cb` when ran
|
||||||
--- it will run the function providing the arguments and sinks.
|
--- @param name string
|
||||||
---
|
--- @param cb function
|
||||||
---
|
|
||||||
function commander.register(name, cb) end
|
function commander.register(name, cb) end
|
||||||
|
|
||||||
--- Returns all registered commanders. Returns a list of tables with the following keys:
|
|
||||||
--- - `exec`: The function used to run the commander. Commanders require args and sinks to be passed.
|
|
||||||
function commander.registry() end
|
|
||||||
|
|
||||||
return commander
|
return commander
|
||||||
|
|
|
@ -2,53 +2,22 @@
|
||||||
|
|
||||||
local fs = {}
|
local fs = {}
|
||||||
|
|
||||||
--- Returns an absolute version of the `path`.
|
--- Changes directory to `dir`
|
||||||
--- This can be used to resolve short paths like `..` to `/home/user`.
|
--- @param dir string
|
||||||
function fs.abs(path) end
|
|
||||||
|
|
||||||
--- Returns the "basename," or the last part of the provided `path`. If path is empty,
|
|
||||||
--- `.` will be returned.
|
|
||||||
function fs.basename(path) end
|
|
||||||
|
|
||||||
--- Changes Hilbish's directory to `dir`.
|
|
||||||
function fs.cd(dir) end
|
function fs.cd(dir) end
|
||||||
|
|
||||||
--- Returns the directory part of `path`. If a file path like
|
--- Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
||||||
--- `~/Documents/doc.txt` then this function will return `~/Documents`.
|
--- @param name string
|
||||||
function fs.dir(path) end
|
--- @param recursive boolean
|
||||||
|
|
||||||
--- Match all files based on the provided `pattern`.
|
|
||||||
--- For the syntax' refer to Go's filepath.Match function: https://pkg.go.dev/path/filepath#Match
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function fs.glob(pattern) end
|
|
||||||
|
|
||||||
--- Takes any list of paths and joins them based on the operating system's path separator.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function fs.join(...path) end
|
|
||||||
|
|
||||||
--- Creates a new directory with the provided `name`.
|
|
||||||
--- With `recursive`, mkdir will create parent directories.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function fs.mkdir(name, recursive) end
|
function fs.mkdir(name, recursive) end
|
||||||
|
|
||||||
--- Returns a pair of connected files, also known as a pipe.
|
--- Returns a table of files in `dir`
|
||||||
--- The type returned is a Lua file, same as returned from `io` functions.
|
--- @param dir string
|
||||||
function fs.fpipe() end
|
--- @return table
|
||||||
|
function fs.readdir(dir) end
|
||||||
|
|
||||||
--- Returns a list of all files and directories in the provided path.
|
--- Returns info about `path`
|
||||||
function fs.readdir(path) end
|
--- @param path string
|
||||||
|
|
||||||
--- Returns the information about a given `path`.
|
|
||||||
--- The returned table contains the following values:
|
|
||||||
--- name (string) - Name of the path
|
|
||||||
--- size (number) - Size of the path in bytes
|
|
||||||
--- mode (string) - Unix permission mode in an octal format string (with leading 0)
|
|
||||||
--- isDir (boolean) - If the path is a directory
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function fs.stat(path) end
|
function fs.stat(path) end
|
||||||
|
|
||||||
return fs
|
return fs
|
||||||
|
|
|
@ -2,264 +2,80 @@
|
||||||
|
|
||||||
local hilbish = {}
|
local hilbish = {}
|
||||||
|
|
||||||
--- This is an alias (ha) for the [hilbish.alias](../#alias) function.
|
--- Sets an alias of `orig` to `cmd`
|
||||||
--- @param alias string
|
|
||||||
--- @param cmd string
|
--- @param cmd string
|
||||||
function hilbish.aliases.add(alias, cmd) end
|
--- @param orig string
|
||||||
|
|
||||||
--- This is the same as the `hilbish.runnerMode` function.
|
|
||||||
--- It takes a callback, which will be used to execute all interactive input.
|
|
||||||
--- In normal cases, neither callbacks should be overrided by the user,
|
|
||||||
--- as the higher level functions listed below this will handle it.
|
|
||||||
function hilbish.runner.setMode(cb) end
|
|
||||||
|
|
||||||
--- Returns the current input line.
|
|
||||||
function hilbish.editor.getLine() end
|
|
||||||
|
|
||||||
--- Returns the text that is at the register.
|
|
||||||
function hilbish.editor.getVimRegister(register) end
|
|
||||||
|
|
||||||
--- Inserts text into the Hilbish command line.
|
|
||||||
function hilbish.editor.insert(text) end
|
|
||||||
|
|
||||||
--- Reads a keystroke from the user. This is in a format of something like Ctrl-L.
|
|
||||||
function hilbish.editor.getChar() end
|
|
||||||
|
|
||||||
--- Sets the vim register at `register` to hold the passed text.
|
|
||||||
function hilbish.editor.setVimRegister(register, text) end
|
|
||||||
|
|
||||||
--- Return binaries/executables based on the provided parameters.
|
|
||||||
--- This function is meant to be used as a helper in a command completion handler.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function hilbish.completion.bins(query, ctx, fields) end
|
|
||||||
|
|
||||||
--- Calls a completer function. This is mainly used to call a command completer, which will have a `name`
|
|
||||||
--- in the form of `command.name`, example: `command.git`.
|
|
||||||
--- You can check the Completions doc or `doc completions` for info on the `completionGroups` return value.
|
|
||||||
function hilbish.completion.call(name, query, ctx, fields) end
|
|
||||||
|
|
||||||
--- Returns file matches based on the provided parameters.
|
|
||||||
--- This function is meant to be used as a helper in a command completion handler.
|
|
||||||
function hilbish.completion.files(query, ctx, fields) end
|
|
||||||
|
|
||||||
--- This function contains the general completion handler for Hilbish. This function handles
|
|
||||||
--- completion of everything, which includes calling other command handlers, binaries, and files.
|
|
||||||
--- This function can be overriden to supply a custom handler. Note that alias resolution is required to be done in this function.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function hilbish.completion.handler(line, pos) end
|
|
||||||
|
|
||||||
--- Sets an alias, with a name of `cmd` to another command.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function hilbish.alias(cmd, orig) end
|
function hilbish.alias(cmd, orig) end
|
||||||
|
|
||||||
--- Appends the provided dir to the command path (`$PATH`)
|
--- Appends `dir` to $PATH
|
||||||
---
|
--- @param dir string|table
|
||||||
---
|
|
||||||
function hilbish.appendPath(dir) end
|
function hilbish.appendPath(dir) end
|
||||||
|
|
||||||
--- Registers a completion handler for the specified scope.
|
--- Registers a completion handler for `scope`.
|
||||||
--- A `scope` is expected to be `command.<cmd>`,
|
--- A `scope` is currently only expected to be `command.<cmd>`,
|
||||||
--- replacing <cmd> with the name of the command (for example `command.git`).
|
--- replacing <cmd> with the name of the command (for example `command.git`).
|
||||||
--- The documentation for completions, under Features/Completions or `doc completions`
|
--- `cb` must be a function that returns a table of "completion groups."
|
||||||
--- provides more details.
|
--- A completion group is a table with the keys `items` and `type`.
|
||||||
---
|
--- `items` being a table of items and `type` being the display type of
|
||||||
---
|
--- `grid` (the normal file completion display) or `list` (with a description)
|
||||||
|
--- @param scope string
|
||||||
|
--- @param cb function
|
||||||
function hilbish.complete(scope, cb) end
|
function hilbish.complete(scope, cb) end
|
||||||
|
|
||||||
--- Returns the current directory of the shell.
|
--- Returns the current directory of the shell
|
||||||
function hilbish.cwd() end
|
function hilbish.cwd() end
|
||||||
|
|
||||||
--- Replaces the currently running Hilbish instance with the supplied command.
|
--- Replaces running hilbish with `cmd`
|
||||||
--- This can be used to do an in-place restart.
|
--- @param cmd string
|
||||||
function hilbish.exec(cmd) end
|
function hilbish.exec(cmd) end
|
||||||
|
|
||||||
--- Puts `fn` in a Goroutine.
|
--- Puts `fn` in a goroutine
|
||||||
--- This can be used to run any function in another thread at the same time as other Lua code.
|
--- @param fn function
|
||||||
--- **NOTE: THIS FUNCTION MAY CRASH HILBISH IF OUTSIDE VARIABLES ARE ACCESSED.**
|
|
||||||
--- **This is a limitation of the Lua runtime.**
|
|
||||||
function hilbish.goro(fn) end
|
function hilbish.goro(fn) end
|
||||||
|
|
||||||
--- Line highlighter handler.
|
--- Sets the input mode for Hilbish's line reader. Accepts either emacs for vim
|
||||||
--- This is mainly for syntax highlighting, but in reality could set the input
|
--- @param mode string
|
||||||
--- of the prompt to *display* anything. The callback is passed the current line
|
|
||||||
--- and is expected to return a line that will be used as the input display.
|
|
||||||
--- Note that to set a highlighter, one has to override this function.
|
|
||||||
---
|
|
||||||
function hilbish.highlighter(line) end
|
|
||||||
|
|
||||||
--- The command line hint handler. It gets called on every key insert to
|
|
||||||
--- determine what text to use as an inline hint. It is passed the current
|
|
||||||
--- line and cursor position. It is expected to return a string which is used
|
|
||||||
--- as the text for the hint. This is by default a shim. To set hints,
|
|
||||||
--- override this function with your custom handler.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function hilbish.hinter(line, pos) end
|
|
||||||
|
|
||||||
--- Sets the input mode for Hilbish's line reader.
|
|
||||||
--- `emacs` is the default. Setting it to `vim` changes behavior of input to be
|
|
||||||
--- Vim-like with modes and Vim keybinds.
|
|
||||||
function hilbish.inputMode(mode) end
|
function hilbish.inputMode(mode) end
|
||||||
|
|
||||||
--- Runs the `cb` function every specified amount of `time`.
|
--- Runs the `cb` function every `time` milliseconds
|
||||||
--- This creates a timer that ticking immediately.
|
--- @param cb function
|
||||||
|
--- @param time number
|
||||||
function hilbish.interval(cb, time) end
|
function hilbish.interval(cb, time) end
|
||||||
|
|
||||||
--- Changes the text prompt when Hilbish asks for more input.
|
--- Changes the continued line prompt to `str`
|
||||||
--- This will show up when text is incomplete, like a missing quote
|
--- @param str string
|
||||||
---
|
function hilbish.mlprompt(str) end
|
||||||
---
|
|
||||||
function hilbish.multiprompt(str) end
|
|
||||||
|
|
||||||
--- Prepends `dir` to $PATH.
|
--- Prepends `dir` to $PATH
|
||||||
|
--- @param dir string
|
||||||
function hilbish.prependPath(dir) end
|
function hilbish.prependPath(dir) end
|
||||||
|
|
||||||
--- Changes the shell prompt to the provided string.
|
--- Changes the shell prompt to `str`
|
||||||
--- There are a few verbs that can be used in the prompt text.
|
--- There are a few verbs that can be used in the prompt text.
|
||||||
--- These will be formatted and replaced with the appropriate values.
|
--- These will be formatted and replaced with the appropriate values.
|
||||||
--- `%d` - Current working directory
|
--- `%d` - Current working directory
|
||||||
--- `%u` - Name of current user
|
--- `%u` - Name of current user
|
||||||
--- `%h` - Hostname of device
|
--- `%h` - Hostname of device
|
||||||
---
|
--- @param str string
|
||||||
function hilbish.prompt(str, typ) end
|
function hilbish.prompt(str) end
|
||||||
|
|
||||||
--- Read input from the user, using Hilbish's line editor/input reader.
|
--- Read input from the user, using Hilbish's line editor/input reader.
|
||||||
--- This is a separate instance from the one Hilbish actually uses.
|
--- This is a separate instance from the one Hilbish actually uses.
|
||||||
--- Returns `input`, will be nil if Ctrl-D is pressed, or an error occurs.
|
--- Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
|
||||||
|
--- @param prompt string
|
||||||
function hilbish.read(prompt) end
|
function hilbish.read(prompt) end
|
||||||
|
|
||||||
--- Runs `cmd` in Hilbish's shell script interpreter.
|
--- Runs `cmd` in Hilbish's sh interpreter.
|
||||||
--- The `streams` parameter specifies the output and input streams the command should use.
|
--- @param cmd string
|
||||||
--- For example, to write command output to a sink.
|
function hilbish.run(cmd) end
|
||||||
--- As a table, the caller can directly specify the standard output, error, and input
|
|
||||||
--- streams of the command with the table keys `out`, `err`, and `input` respectively.
|
|
||||||
--- As a boolean, it specifies whether the command should use standard output or return its output streams.
|
|
||||||
---
|
|
||||||
function hilbish.run(cmd, streams) end
|
|
||||||
|
|
||||||
--- Sets the execution/runner mode for interactive Hilbish.
|
--- Runs the `cb` function after `time` in milliseconds
|
||||||
--- This determines whether Hilbish wll try to run input as Lua
|
--- @param cb function
|
||||||
--- and/or sh or only do one of either.
|
--- @param time number
|
||||||
--- Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua),
|
|
||||||
--- sh, and lua. It also accepts a function, to which if it is passed one
|
|
||||||
--- will call it to execute user input instead.
|
|
||||||
--- Read [about runner mode](../features/runner-mode) for more information.
|
|
||||||
function hilbish.runnerMode(mode) end
|
|
||||||
|
|
||||||
--- Executed the `cb` function after a period of `time`.
|
|
||||||
--- This creates a Timer that starts ticking immediately.
|
|
||||||
function hilbish.timeout(cb, time) end
|
function hilbish.timeout(cb, time) end
|
||||||
|
|
||||||
--- Checks if `name` is a valid command.
|
--- Searches for an executable called `binName` in the directories of $PATH
|
||||||
--- Will return the path of the binary, or a basename if it's a commander.
|
--- @param binName string
|
||||||
function hilbish.which(name) end
|
function hilbish.which(binName) end
|
||||||
|
|
||||||
--- Puts a job in the background. This acts the same as initially running a job.
|
|
||||||
function hilbish.jobs:background() end
|
|
||||||
|
|
||||||
--- Puts a job in the foreground. This will cause it to run like it was
|
|
||||||
--- executed normally and wait for it to complete.
|
|
||||||
function hilbish.jobs:foreground() end
|
|
||||||
|
|
||||||
--- Evaluates `cmd` as Lua input. This is the same as using `dofile`
|
|
||||||
--- or `load`, but is appropriated for the runner interface.
|
|
||||||
function hilbish.runner.lua(cmd) end
|
|
||||||
|
|
||||||
--- Sets/toggles the option of automatically flushing output.
|
|
||||||
--- A call with no argument will toggle the value.
|
|
||||||
--- @param auto boolean|nil
|
|
||||||
function hilbish:autoFlush(auto) end
|
|
||||||
|
|
||||||
--- Flush writes all buffered input to the sink.
|
|
||||||
function hilbish:flush() end
|
|
||||||
|
|
||||||
--- Reads a liine of input from the sink.
|
|
||||||
--- @returns string
|
|
||||||
function hilbish:read() end
|
|
||||||
|
|
||||||
--- Reads all input from the sink.
|
|
||||||
--- @returns string
|
|
||||||
function hilbish:readAll() end
|
|
||||||
|
|
||||||
--- Writes data to a sink.
|
|
||||||
function hilbish:write(str) end
|
|
||||||
|
|
||||||
--- Writes data to a sink with a newline at the end.
|
|
||||||
function hilbish:writeln(str) end
|
|
||||||
|
|
||||||
--- Starts running the job.
|
|
||||||
function hilbish.jobs:start() end
|
|
||||||
|
|
||||||
--- Stops the job from running.
|
|
||||||
function hilbish.jobs:stop() end
|
|
||||||
|
|
||||||
--- Loads a module at the designated `path`.
|
|
||||||
--- It will throw if any error occurs.
|
|
||||||
function hilbish.module.load(path) end
|
|
||||||
|
|
||||||
--- Runs a command in Hilbish's shell script interpreter.
|
|
||||||
--- This is the equivalent of using `source`.
|
|
||||||
function hilbish.runner.sh(cmd) end
|
|
||||||
|
|
||||||
--- Starts a timer.
|
|
||||||
function hilbish.timers:start() end
|
|
||||||
|
|
||||||
--- Stops a timer.
|
|
||||||
function hilbish.timers:stop() end
|
|
||||||
|
|
||||||
--- Removes an alias.
|
|
||||||
function hilbish.aliases.delete(name) end
|
|
||||||
|
|
||||||
--- Get a table of all aliases, with string keys as the alias and the value as the command.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function hilbish.aliases.list() end
|
|
||||||
|
|
||||||
--- Resolves an alias to its original command. Will thrown an error if the alias doesn't exist.
|
|
||||||
function hilbish.aliases.resolve(alias) end
|
|
||||||
|
|
||||||
--- Creates a new job. This function does not run the job. This function is intended to be
|
|
||||||
--- used by runners, but can also be used to create jobs via Lua. Commanders cannot be ran as jobs.
|
|
||||||
---
|
|
||||||
---
|
|
||||||
function hilbish.jobs.add(cmdstr, args, execPath) end
|
|
||||||
|
|
||||||
--- Returns a table of all job objects.
|
|
||||||
function hilbish.jobs.all() end
|
|
||||||
|
|
||||||
--- Disowns a job. This simply deletes it from the list of jobs without stopping it.
|
|
||||||
function hilbish.jobs.disown(id) end
|
|
||||||
|
|
||||||
--- Get a job object via its ID.
|
|
||||||
--- @param id number
|
|
||||||
--- @returns Job
|
|
||||||
function hilbish.jobs.get(id) end
|
|
||||||
|
|
||||||
--- Returns the last added job to the table.
|
|
||||||
function hilbish.jobs.last() end
|
|
||||||
|
|
||||||
--- Adds a command to the history.
|
|
||||||
function hilbish.history.add(cmd) end
|
|
||||||
|
|
||||||
--- Retrieves all history as a table.
|
|
||||||
function hilbish.history.all() end
|
|
||||||
|
|
||||||
--- Deletes all commands from the history.
|
|
||||||
function hilbish.history.clear() end
|
|
||||||
|
|
||||||
--- Retrieves a command from the history based on the `index`.
|
|
||||||
function hilbish.history.get(index) end
|
|
||||||
|
|
||||||
--- Returns the amount of commands in the history.
|
|
||||||
function hilbish.history.size() end
|
|
||||||
|
|
||||||
--- Creates a timer that runs based on the specified `time`.
|
|
||||||
function hilbish.timers.create(type, time, callback) end
|
|
||||||
|
|
||||||
--- Retrieves a timer via its ID.
|
|
||||||
function hilbish.timers.get(id) end
|
|
||||||
|
|
||||||
return hilbish
|
return hilbish
|
||||||
|
|
|
@ -2,17 +2,17 @@
|
||||||
|
|
||||||
local terminal = {}
|
local terminal = {}
|
||||||
|
|
||||||
|
--- Puts the terminal in raw mode
|
||||||
|
function terminal.raw() end
|
||||||
|
|
||||||
--- Restores the last saved state of the terminal
|
--- Restores the last saved state of the terminal
|
||||||
function terminal.restoreState() end
|
function terminal.restoreState() end
|
||||||
|
|
||||||
--- Saves the current state of the terminal.
|
--- Saves the current state of the terminal
|
||||||
function terminal.saveState() end
|
function terminal.saveState() end
|
||||||
|
|
||||||
--- Puts the terminal into raw mode.
|
|
||||||
function terminal.setRaw() end
|
|
||||||
|
|
||||||
--- Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
--- Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
||||||
--- NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal.
|
--- Note: this is not the size in relation to the dimensions of the display
|
||||||
function terminal.size() end
|
function terminal.size() end
|
||||||
|
|
||||||
return terminal
|
return terminal
|
||||||
|
|
567
exec.go
|
@ -1,221 +1,30 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"os/exec"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
"github.com/yuin/gopher-lua"
|
||||||
"mvdan.cc/sh/v3/shell"
|
"mvdan.cc/sh/v3/shell"
|
||||||
//"github.com/yuin/gopher-lua/parse"
|
//"github.com/yuin/gopher-lua/parse"
|
||||||
"mvdan.cc/sh/v3/interp"
|
"mvdan.cc/sh/v3/interp"
|
||||||
"mvdan.cc/sh/v3/syntax"
|
"mvdan.cc/sh/v3/syntax"
|
||||||
"mvdan.cc/sh/v3/expand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var errNotExec = errors.New("not executable")
|
func runInput(input, origInput string) {
|
||||||
var errNotFound = errors.New("not found")
|
|
||||||
var runnerMode rt.Value = rt.StringValue("hybrid")
|
|
||||||
|
|
||||||
type streams struct {
|
|
||||||
stdout io.Writer
|
|
||||||
stderr io.Writer
|
|
||||||
stdin io.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
type execError struct{
|
|
||||||
typ string
|
|
||||||
cmd string
|
|
||||||
code int
|
|
||||||
colon bool
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e execError) Error() string {
|
|
||||||
return fmt.Sprintf("%s: %s", e.cmd, e.typ)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e execError) sprint() error {
|
|
||||||
sep := " "
|
|
||||||
if e.colon {
|
|
||||||
sep = ": "
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("hilbish: %s%s%s", e.cmd, sep, e.err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func isExecError(err error) (execError, bool) {
|
|
||||||
if exErr, ok := err.(execError); ok {
|
|
||||||
return exErr, true
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Split(err.Error(), ": ")
|
|
||||||
knownTypes := []string{
|
|
||||||
"not-found",
|
|
||||||
"not-executable",
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(fields) > 1 && contains(knownTypes, fields[1]) {
|
|
||||||
var colon bool
|
|
||||||
var e error
|
|
||||||
switch fields[1] {
|
|
||||||
case "not-found":
|
|
||||||
e = errNotFound
|
|
||||||
case "not-executable":
|
|
||||||
colon = true
|
|
||||||
e = errNotExec
|
|
||||||
}
|
|
||||||
|
|
||||||
return execError{
|
|
||||||
cmd: fields[0],
|
|
||||||
typ: fields[1],
|
|
||||||
colon: colon,
|
|
||||||
err: e,
|
|
||||||
}, true
|
|
||||||
}
|
|
||||||
|
|
||||||
return execError{}, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func runInput(input string, priv bool) {
|
|
||||||
running = true
|
running = true
|
||||||
cmdString := aliases.Resolve(input)
|
cmdString := aliases.Resolve(input)
|
||||||
hooks.Emit("command.preexec", input, cmdString)
|
|
||||||
|
|
||||||
rerun:
|
hooks.Em.Emit("command.preexec", input, cmdString)
|
||||||
var exitCode uint8
|
|
||||||
var err error
|
|
||||||
var cont bool
|
|
||||||
var newline bool
|
|
||||||
// save incase it changes while prompting (For some reason)
|
|
||||||
currentRunner := runnerMode
|
|
||||||
if currentRunner.Type() == rt.StringType {
|
|
||||||
switch currentRunner.AsString() {
|
|
||||||
case "hybrid":
|
|
||||||
_, _, err = handleLua(input)
|
|
||||||
if err == nil {
|
|
||||||
cmdFinish(0, input, priv)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
input, exitCode, cont, newline, err = handleSh(input)
|
|
||||||
case "hybridRev":
|
|
||||||
_, _, _, _, err = handleSh(input)
|
|
||||||
if err == nil {
|
|
||||||
cmdFinish(0, input, priv)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
input, exitCode, err = handleLua(input)
|
|
||||||
case "lua":
|
|
||||||
input, exitCode, err = handleLua(input)
|
|
||||||
case "sh":
|
|
||||||
input, exitCode, cont, newline, err = handleSh(input)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// can only be a string or function so
|
|
||||||
var runnerErr error
|
|
||||||
input, exitCode, cont, newline, runnerErr, err = runLuaRunner(currentRunner, input)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
cmdFinish(124, input, priv)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// yep, we only use `err` to check for lua eval error
|
|
||||||
// our actual error should only be a runner provided error at this point
|
|
||||||
// command not found type, etc
|
|
||||||
err = runnerErr
|
|
||||||
}
|
|
||||||
|
|
||||||
if cont {
|
|
||||||
input, err = continuePrompt(input, newline)
|
|
||||||
if err == nil {
|
|
||||||
goto rerun
|
|
||||||
} else if err == io.EOF {
|
|
||||||
lr.SetPrompt(fmtPrompt(prompt))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
if exErr, ok := isExecError(err); ok {
|
|
||||||
hooks.Emit("command." + exErr.typ, exErr.cmd)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cmdFinish(exitCode, input, priv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func reprompt(input string, newline bool) (string, error) {
|
|
||||||
for {
|
|
||||||
/*
|
|
||||||
if strings.HasSuffix(input, "\\") {
|
|
||||||
input = strings.TrimSuffix(input, "\\") + "\n"
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
in, err := continuePrompt(input, newline)
|
|
||||||
if err != nil {
|
|
||||||
lr.SetPrompt(fmtPrompt(prompt))
|
|
||||||
return input, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return in, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8, continued bool, newline bool, runnerErr, err error) {
|
|
||||||
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false)
|
|
||||||
err = rt.Call(l.MainThread(), runr, []rt.Value{rt.StringValue(userInput)}, term)
|
|
||||||
if err != nil {
|
|
||||||
return "", 124, false, false, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var runner *rt.Table
|
|
||||||
var ok bool
|
|
||||||
runnerRet := term.Get(0)
|
|
||||||
if runner, ok = runnerRet.TryTable(); !ok {
|
|
||||||
fmt.Fprintln(os.Stderr, "runner did not return a table")
|
|
||||||
exitCode = 125
|
|
||||||
input = userInput
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if code, ok := runner.Get(rt.StringValue("exitCode")).TryInt(); ok {
|
|
||||||
exitCode = uint8(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if inp, ok := runner.Get(rt.StringValue("input")).TryString(); ok {
|
|
||||||
input = inp
|
|
||||||
}
|
|
||||||
|
|
||||||
if errStr, ok := runner.Get(rt.StringValue("err")).TryString(); ok {
|
|
||||||
runnerErr = fmt.Errorf("%s", errStr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok {
|
|
||||||
continued = c
|
|
||||||
}
|
|
||||||
|
|
||||||
if nl, ok := runner.Get(rt.StringValue("newline")).TryBool(); ok {
|
|
||||||
newline = nl
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleLua(input string) (string, uint8, error) {
|
|
||||||
cmdString := aliases.Resolve(input)
|
|
||||||
// First try to load input, essentially compiling to bytecode
|
// First try to load input, essentially compiling to bytecode
|
||||||
chunk, err := l.CompileAndLoadLuaChunk("", []byte(cmdString), rt.TableValue(l.GlobalEnv()))
|
fn, err := l.LoadString(cmdString)
|
||||||
if err != nil && noexecute {
|
if err != nil && noexecute {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
/* if lerr, ok := err.(*lua.ApiError); ok {
|
/* if lerr, ok := err.(*lua.ApiError); ok {
|
||||||
|
@ -224,110 +33,62 @@ func handleLua(input string) (string, uint8, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
return cmdString, 125, err
|
return
|
||||||
}
|
}
|
||||||
// And if there's no syntax errors and -n isnt provided, run
|
// And if there's no syntax errors and -n isnt provided, run
|
||||||
if !noexecute {
|
if !noexecute {
|
||||||
if chunk != nil {
|
l.Push(fn)
|
||||||
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
err = l.PCall(0, lua.MultRet, nil)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return cmdString, 0, nil
|
cmdFinish(0, cmdString, origInput)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmdString, 125, err
|
// Last option: use sh interpreter
|
||||||
}
|
err = execCommand(cmdString, origInput)
|
||||||
|
|
||||||
func handleSh(cmdString string) (input string, exitCode uint8, cont bool, newline bool, runErr error) {
|
|
||||||
shRunner := hshMod.Get(rt.StringValue("runner")).AsTable().Get(rt.StringValue("sh"))
|
|
||||||
var err error
|
|
||||||
input, exitCode, cont, newline, runErr, err = runLuaRunner(shRunner, cmdString)
|
|
||||||
if err != nil {
|
|
||||||
runErr = err
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func execSh(cmdString string) (input string, exitcode uint8, cont bool, newline bool, e error) {
|
|
||||||
_, _, err := execCommand(cmdString, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If input is incomplete, start multiline prompting
|
// If input is incomplete, start multiline prompting
|
||||||
if syntax.IsIncomplete(err) {
|
if syntax.IsIncomplete(err) {
|
||||||
if !interactive {
|
for {
|
||||||
return cmdString, 126, false, false, err
|
cmdString, err = continuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = execCommand(cmdString, origInput)
|
||||||
|
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
|
||||||
|
continue
|
||||||
|
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||||
|
cmdFinish(code, cmdString, origInput)
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
cmdFinish(1, cmdString, origInput)
|
||||||
|
} else {
|
||||||
|
cmdFinish(0, cmdString, origInput)
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
newline := false
|
|
||||||
if strings.Contains(err.Error(), "unclosed here-document") {
|
|
||||||
newline = true
|
|
||||||
}
|
|
||||||
return cmdString, 126, true, newline, err
|
|
||||||
} else {
|
} else {
|
||||||
if code, ok := interp.IsExitStatus(err); ok {
|
if code, ok := interp.IsExitStatus(err); ok {
|
||||||
return cmdString, code, false, false, nil
|
cmdFinish(code, cmdString, origInput)
|
||||||
} else {
|
} else {
|
||||||
return cmdString, 126, false, false, err
|
cmdFinish(126, cmdString, origInput)
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
cmdFinish(0, cmdString, origInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmdString, 0, false, false, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run command in sh interpreter
|
// Run command in sh interpreter
|
||||||
func execCommand(cmd string, strms *streams) (io.Writer, io.Writer, error) {
|
func execCommand(cmd, old string) error {
|
||||||
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if strms == nil {
|
exechandle := func(ctx context.Context, args []string) error {
|
||||||
strms = &streams{}
|
|
||||||
}
|
|
||||||
|
|
||||||
if strms.stdout == nil {
|
|
||||||
strms.stdout = os.Stdout
|
|
||||||
}
|
|
||||||
|
|
||||||
if strms.stderr == nil {
|
|
||||||
strms.stderr = os.Stderr
|
|
||||||
}
|
|
||||||
|
|
||||||
if strms.stdin == nil {
|
|
||||||
strms.stdin = os.Stdin
|
|
||||||
}
|
|
||||||
|
|
||||||
interp.StdIO(strms.stdin, strms.stdout, strms.stderr)(runner)
|
|
||||||
interp.Env(nil)(runner)
|
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
printer := syntax.NewPrinter()
|
|
||||||
|
|
||||||
var bg bool
|
|
||||||
for _, stmt := range file.Stmts {
|
|
||||||
bg = false
|
|
||||||
if stmt.Background {
|
|
||||||
bg = true
|
|
||||||
printer.Print(buf, stmt.Cmd)
|
|
||||||
|
|
||||||
stmtStr := buf.String()
|
|
||||||
buf.Reset()
|
|
||||||
jobs.add(stmtStr, []string{}, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
interp.ExecHandler(execHandle(bg))(runner)
|
|
||||||
err = runner.Run(context.TODO(), stmt)
|
|
||||||
if err != nil {
|
|
||||||
return strms.stdout, strms.stderr, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strms.stdout, strms.stderr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func execHandle(bg bool) interp.ExecHandlerFunc {
|
|
||||||
return func(ctx context.Context, args []string) error {
|
|
||||||
_, argstring := splitInput(strings.Join(args, " "))
|
_, argstring := splitInput(strings.Join(args, " "))
|
||||||
// i dont really like this but it works
|
// i dont really like this but it works
|
||||||
if aliases.All()[args[0]] != "" {
|
if aliases.All()[args[0]] != "" {
|
||||||
|
@ -340,231 +101,92 @@ func execHandle(bg bool) interp.ExecHandlerFunc {
|
||||||
|
|
||||||
// If alias was found, use command alias
|
// If alias was found, use command alias
|
||||||
argstring = aliases.Resolve(argstring)
|
argstring = aliases.Resolve(argstring)
|
||||||
var err error
|
args, _ = shell.Fields(argstring, nil)
|
||||||
args, err = shell.Fields(argstring, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If command is defined in Lua then run it
|
// If command is defined in Lua then run it
|
||||||
luacmdArgs := rt.NewTable()
|
luacmdArgs := l.NewTable()
|
||||||
for i, str := range args[1:] {
|
for _, str := range args[1:] {
|
||||||
luacmdArgs.Set(rt.IntValue(int64(i + 1)), rt.StringValue(str))
|
luacmdArgs.Append(lua.LString(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
hc := interp.HandlerCtx(ctx)
|
if commands[args[0]] != nil {
|
||||||
if cmd := cmds.Commands[args[0]]; cmd != nil {
|
err := l.CallByParam(lua.P{
|
||||||
stdin := newSinkInput(hc.Stdin)
|
Fn: commands[args[0]],
|
||||||
stdout := newSinkOutput(hc.Stdout)
|
NRet: 1,
|
||||||
stderr := newSinkOutput(hc.Stderr)
|
Protect: true,
|
||||||
|
}, luacmdArgs)
|
||||||
|
|
||||||
sinks := rt.NewTable()
|
|
||||||
sinks.Set(rt.StringValue("in"), rt.UserDataValue(stdin.ud))
|
|
||||||
sinks.Set(rt.StringValue("input"), rt.UserDataValue(stdin.ud))
|
|
||||||
sinks.Set(rt.StringValue("out"), rt.UserDataValue(stdout.ud))
|
|
||||||
sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud))
|
|
||||||
|
|
||||||
t := rt.NewThread(l)
|
|
||||||
sig := make(chan os.Signal)
|
|
||||||
exit := make(chan bool)
|
|
||||||
|
|
||||||
luaexitcode := rt.IntValue(63)
|
|
||||||
var err error
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
exit <- true
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
signal.Notify(sig, os.Interrupt)
|
|
||||||
select {
|
|
||||||
case <-sig:
|
|
||||||
t.KillContext()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
luaexitcode, err = rt.Call1(t, rt.FunctionValue(cmd), rt.TableValue(luacmdArgs), rt.TableValue(sinks))
|
|
||||||
exit <- true
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-exit
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Error in command:\n" + err.Error())
|
fmt.Fprintln(os.Stderr,
|
||||||
|
"Error in command:\n\n" + err.Error())
|
||||||
return interp.NewExitStatus(1)
|
return interp.NewExitStatus(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
luaexitcode := l.Get(-1)
|
||||||
var exitcode uint8
|
var exitcode uint8
|
||||||
|
|
||||||
if code, ok := luaexitcode.TryInt(); ok {
|
l.Pop(1)
|
||||||
|
|
||||||
|
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
|
||||||
exitcode = uint8(code)
|
exitcode = uint8(code)
|
||||||
} else if luaexitcode != rt.NilValue {
|
|
||||||
// deregister commander
|
|
||||||
delete(cmds.Commands, args[0])
|
|
||||||
fmt.Fprintf(os.Stderr, "Commander did not return number for exit code. %s, you're fired.\n", args[0])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmdFinish(exitcode, argstring, old)
|
||||||
return interp.NewExitStatus(exitcode)
|
return interp.NewExitStatus(exitcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := lookpath(args[0])
|
err := lookpath(args[0])
|
||||||
if err == errNotExec {
|
if err == os.ErrPermission {
|
||||||
return execError{
|
hooks.Em.Emit("command.no-perm", args[0])
|
||||||
typ: "not-executable",
|
return interp.NewExitStatus(126)
|
||||||
cmd: args[0],
|
|
||||||
code: 126,
|
|
||||||
colon: true,
|
|
||||||
err: errNotExec,
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return execError{
|
hooks.Em.Emit("command.not-found", args[0])
|
||||||
typ: "not-found",
|
return interp.NewExitStatus(127)
|
||||||
cmd: args[0],
|
|
||||||
code: 127,
|
|
||||||
err: errNotFound,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
killTimeout := 2 * time.Second
|
return interp.DefaultExecHandler(2 * time.Second)(ctx, args)
|
||||||
// from here is basically copy-paste of the default exec handler from
|
|
||||||
// sh/interp but with our job handling
|
|
||||||
|
|
||||||
env := hc.Env
|
|
||||||
envList := make([]string, 0, 64)
|
|
||||||
env.Each(func(name string, vr expand.Variable) bool {
|
|
||||||
if name == "PATH" {
|
|
||||||
pathEnv := os.Getenv("PATH")
|
|
||||||
envList = append(envList, "PATH="+pathEnv)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vr.IsSet() {
|
|
||||||
// If a variable is set globally but unset in the
|
|
||||||
// runner, we need to ensure it's not part of the final
|
|
||||||
// list. Seems like zeroing the element is enough.
|
|
||||||
// This is a linear search, but this scenario should be
|
|
||||||
// rare, and the number of variables shouldn't be large.
|
|
||||||
for i, kv := range envList {
|
|
||||||
if strings.HasPrefix(kv, name+"=") {
|
|
||||||
envList[i] = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if vr.Exported && vr.Kind == expand.String {
|
|
||||||
envList = append(envList, name+"="+vr.String())
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
cmd := exec.Cmd{
|
|
||||||
Path: path,
|
|
||||||
Args: args,
|
|
||||||
Env: envList,
|
|
||||||
Dir: hc.Dir,
|
|
||||||
Stdin: hc.Stdin,
|
|
||||||
Stdout: hc.Stdout,
|
|
||||||
Stderr: hc.Stderr,
|
|
||||||
}
|
|
||||||
|
|
||||||
var j *job
|
|
||||||
if bg {
|
|
||||||
j = jobs.getLatest()
|
|
||||||
j.setHandle(&cmd)
|
|
||||||
err = j.start()
|
|
||||||
} else {
|
|
||||||
err = cmd.Start()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
if done := ctx.Done(); done != nil {
|
|
||||||
go func() {
|
|
||||||
<-done
|
|
||||||
|
|
||||||
if killTimeout <= 0 || runtime.GOOS == "windows" {
|
|
||||||
cmd.Process.Signal(os.Kill)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: don't temporarily leak this goroutine
|
|
||||||
// if the program stops itself with the
|
|
||||||
// interrupt.
|
|
||||||
go func() {
|
|
||||||
time.Sleep(killTimeout)
|
|
||||||
cmd.Process.Signal(os.Kill)
|
|
||||||
}()
|
|
||||||
cmd.Process.Signal(os.Interrupt)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
exit := handleExecErr(err)
|
|
||||||
|
|
||||||
if bg {
|
|
||||||
j.exitCode = int(exit)
|
|
||||||
j.finish()
|
|
||||||
}
|
|
||||||
return interp.NewExitStatus(exit)
|
|
||||||
}
|
}
|
||||||
|
runner, _ := interp.New(
|
||||||
|
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
||||||
|
interp.ExecHandler(exechandle),
|
||||||
|
)
|
||||||
|
err = runner.Run(context.TODO(), file)
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleExecErr(err error) (exit uint8) {
|
// custom lookpath function so we know if a command is found *and* has execute permission
|
||||||
ctx := context.TODO()
|
func lookpath(file string) error {
|
||||||
|
skip := []string{"./", "/", "../", "~/"}
|
||||||
switch x := err.(type) {
|
|
||||||
case *exec.ExitError:
|
|
||||||
// started, but errored - default to 1 if OS
|
|
||||||
// doesn't have exit statuses
|
|
||||||
if status, ok := x.Sys().(syscall.WaitStatus); ok {
|
|
||||||
if status.Signaled() {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
exit = uint8(128 + status.Signal())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
exit = uint8(status.ExitStatus())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
exit = 1
|
|
||||||
return
|
|
||||||
case *exec.Error:
|
|
||||||
// did not start
|
|
||||||
//fmt.Fprintf(hc.Stderr, "%v\n", err)
|
|
||||||
exit = 127
|
|
||||||
default: return
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
func lookpath(file string) (string, error) { // custom lookpath function so we know if a command is found *and* is executable
|
|
||||||
var skip []string
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
skip = []string{"./", "../", "~/", "C:"}
|
|
||||||
} else {
|
|
||||||
skip = []string{"./", "/", "../", "~/"}
|
|
||||||
}
|
|
||||||
for _, s := range skip {
|
for _, s := range skip {
|
||||||
if strings.HasPrefix(file, s) {
|
if strings.HasPrefix(file, s) {
|
||||||
return file, findExecutable(file, false, false)
|
err := findExecutable(file)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
||||||
path := filepath.Join(dir, file)
|
path := filepath.Join(dir, file)
|
||||||
err := findExecutable(path, true, false)
|
err := findExecutable(path)
|
||||||
if err == errNotExec {
|
if err == os.ErrPermission {
|
||||||
return "", err
|
return err
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
return path, nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", os.ErrNotExist
|
return os.ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
func findExecutable(name string) error {
|
||||||
|
f, err := os.Stat(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return os.ErrPermission
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitInput(input string) ([]string, string) {
|
func splitInput(input string) ([]string, string) {
|
||||||
|
@ -620,10 +242,11 @@ func splitInput(input string) ([]string, string) {
|
||||||
return cmdArgs, cmdstr.String()
|
return cmdArgs, cmdstr.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdFinish(code uint8, cmdstr string, private bool) {
|
func cmdFinish(code uint8, cmdstr, oldInput string) {
|
||||||
util.SetField(l, hshMod, "exitCode", rt.IntValue(int64(code)))
|
// if input has space at the beginning, dont put in history
|
||||||
// using AsValue (to convert to lua type) on an interface which is an int
|
if !strings.HasPrefix(oldInput, " ") || interactive {
|
||||||
// results in it being unknown in lua .... ????
|
handleHistory(cmdstr)
|
||||||
// so we allow the hook handler to take lua runtime Values
|
}
|
||||||
hooks.Emit("command.exit", rt.IntValue(int64(code)), cmdstr, private)
|
util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
|
||||||
|
hooks.Em.Emit("command.exit", code, cmdstr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
//go:build unix
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
var bgProcAttr *syscall.SysProcAttr = &syscall.SysProcAttr{
|
|
||||||
Setpgid: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
func findExecutable(path string, inPath, dirs bool) error {
|
|
||||||
f, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if dirs {
|
|
||||||
if m := f.Mode(); m & 0111 != 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errNotExec
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
//go:build windows
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
var bgProcAttr *syscall.SysProcAttr = &syscall.SysProcAttr{
|
|
||||||
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
|
|
||||||
}
|
|
||||||
|
|
||||||
func findExecutable(path string, inPath, dirs bool) error {
|
|
||||||
nameExt := filepath.Ext(path)
|
|
||||||
pathExts := filepath.SplitList(os.Getenv("PATHEXT"))
|
|
||||||
if inPath {
|
|
||||||
if nameExt == "" {
|
|
||||||
for _, ext := range pathExts {
|
|
||||||
_, err := os.Stat(path + ext)
|
|
||||||
if err == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if err == nil {
|
|
||||||
if contains(pathExts, nameExt) { return nil }
|
|
||||||
return errNotExec
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if err == nil {
|
|
||||||
if contains(pathExts, nameExt) { return nil }
|
|
||||||
return errNotExec
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.ErrNotExist
|
|
||||||
}
|
|
BIN
gallery/tab.png
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 114 KiB |
38
go.mod
|
@ -1,37 +1,21 @@
|
||||||
module hilbish
|
module hilbish
|
||||||
|
|
||||||
go 1.21
|
go 1.16
|
||||||
|
|
||||||
toolchain go1.22.2
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/arnodel/golua v0.0.0-20230215163904-e0b5347eaaa1
|
github.com/blackfireio/osinfo v1.0.3
|
||||||
github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504
|
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
|
||||||
github.com/blackfireio/osinfo v1.0.5
|
github.com/maxlandon/readline v0.1.0-beta.0.20211027085530-2b76cabb8036
|
||||||
github.com/maxlandon/readline v1.0.14
|
|
||||||
github.com/pborman/getopt v1.1.0
|
github.com/pborman/getopt v1.1.0
|
||||||
github.com/sahilm/fuzzy v0.1.1
|
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9
|
||||||
golang.org/x/sys v0.22.0
|
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
|
||||||
golang.org/x/term v0.22.0
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
|
||||||
mvdan.cc/sh/v3 v3.8.0
|
layeh.com/gopher-luar v1.0.10
|
||||||
|
mvdan.cc/sh/v3 v3.4.3
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
replace mvdan.cc/sh/v3 => github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20220306140409-795a84b00b4e
|
||||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
|
|
||||||
github.com/arnodel/strftime v0.1.6 // indirect
|
|
||||||
github.com/evilsocket/islazy v1.11.0 // indirect
|
|
||||||
github.com/kylelemons/godebug v1.1.0 // indirect
|
|
||||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
|
||||||
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 // indirect
|
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
|
||||||
golang.org/x/sync v0.7.0 // indirect
|
|
||||||
golang.org/x/text v0.14.0 // indirect
|
|
||||||
)
|
|
||||||
|
|
||||||
replace mvdan.cc/sh/v3 => github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20240815163633-562273e09b73
|
replace github.com/maxlandon/readline => github.com/Rosettea/readline-1 v0.0.0-20220305123014-31d4d4214c93
|
||||||
|
|
||||||
replace github.com/maxlandon/readline => ./readline
|
|
||||||
|
|
||||||
replace layeh.com/gopher-luar => github.com/layeh/gopher-luar v1.0.10
|
replace layeh.com/gopher-luar => github.com/layeh/gopher-luar v1.0.10
|
||||||
|
|
||||||
replace github.com/arnodel/golua => github.com/Rosettea/golua v0.0.0-20241104031959-5551ea280f23
|
|
||||||
|
|
98
go.sum
|
@ -1,46 +1,68 @@
|
||||||
github.com/Rosettea/golua v0.0.0-20241104031959-5551ea280f23 h1:mUZnT0gmDEmTkqXsbnDbuJ3CNil7DCOMiCQYgjbKIdI=
|
github.com/Rosettea/readline-1 v0.0.0-20220302012429-9ce5d23760f7 h1:LoY+kBKqMQqBcilRpVvifBTVve84asa3btpx3D/+IvM=
|
||||||
github.com/Rosettea/golua v0.0.0-20241104031959-5551ea280f23/go.mod h1:9jzpYPiU2is0HVGCiuIOBSXdergHUW44IEjmuN1UrIE=
|
github.com/Rosettea/readline-1 v0.0.0-20220302012429-9ce5d23760f7/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||||
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20240815163633-562273e09b73 h1:zTTUJqNnrF2qf4LgygN8Oae5Uxn6ewH0hA8jyTCHfXw=
|
github.com/Rosettea/readline-1 v0.0.0-20220305004552-071c22768119 h1:rGsc30WTD5hk+oiXrAKsAIwZn5qBeTAdr29y3HhJh9E=
|
||||||
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20240815163633-562273e09b73/go.mod h1:YZalN5H7WNQw3DGij6IvHsEhn5YMW7M2FCwG6gnfKy4=
|
github.com/Rosettea/readline-1 v0.0.0-20220305004552-071c22768119/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||||
|
github.com/Rosettea/readline-1 v0.0.0-20220305123014-31d4d4214c93 h1:SmOkAEm3O7si8CURZSsSN0ZxCQ8IGiiulw8LMZ1V1Yc=
|
||||||
|
github.com/Rosettea/readline-1 v0.0.0-20220305123014-31d4d4214c93/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||||
|
github.com/Rosettea/readline-1 v0.1.0-beta.0.20211207003625-341c7985ad7d h1:KBttN41h/tPahmpaZavviwQ8q4rCkt5CD0HdVmfgPVA=
|
||||||
|
github.com/Rosettea/readline-1 v0.1.0-beta.0.20211207003625-341c7985ad7d/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||||
|
github.com/Rosettea/readline-1 v0.1.0-beta.0.20220228022904-61f5e4493011 h1:+a61iNamZiO3Xru+l/1qtpKqqltVfWEm2r/rxH9hXxY=
|
||||||
|
github.com/Rosettea/readline-1 v0.1.0-beta.0.20220228022904-61f5e4493011/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||||
|
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20211022004519-f67a49cb50f5 h1:ygwVRX8gf5MHA0VzSgOdscCEoAJLjM8joEotfQPgAd0=
|
||||||
|
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20211022004519-f67a49cb50f5/go.mod h1:R09vh/04ILvP2Gj8/Z9Jd0Dh0ZIvaucowMEs6abQpWs=
|
||||||
|
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20220306140409-795a84b00b4e h1:P2XupP8SaylWaudD1DqbWtZ3mIa8OsE9635LmR+Q+lg=
|
||||||
|
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20220306140409-795a84b00b4e/go.mod h1:R09vh/04ILvP2Gj8/Z9Jd0Dh0ZIvaucowMEs6abQpWs=
|
||||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
|
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
|
||||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
|
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
|
||||||
github.com/arnodel/strftime v0.1.6 h1:0hc0pUvk8KhEMXE+htyaOUV42zNcf/csIbjzEFCJqsw=
|
github.com/blackfireio/osinfo v1.0.3 h1:Yk2t2GTPjBcESv6nDSWZKO87bGMQgO+Hi9OoXPpxX8c=
|
||||||
github.com/arnodel/strftime v0.1.6/go.mod h1:5NbK5XqYK8QpRZpqKNt4OlxLtIB8cotkLk4KTKzJfWs=
|
github.com/blackfireio/osinfo v1.0.3/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA=
|
||||||
github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504 h1:R1/AOzdMbopSliUTTEHvHbyNmnZ3YxY5GvdhTkpPsSY=
|
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 h1:xz6Nv3zcwO2Lila35hcb0QloCQsc38Al13RNEzWRpX4=
|
||||||
github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504/go.mod h1:kHBCvAXJIatTX1pw6tLiOspjGc3MhUDRlog9yrCUS+k=
|
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9/go.mod h1:2wSM9zJkl1UQEFZgSd68NfCgRz1VL1jzy/RjCg+ULrs=
|
||||||
github.com/blackfireio/osinfo v1.0.5 h1:6hlaWzfcpb87gRmznVf7wSdhysGqLRz9V/xuSdCEXrA=
|
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||||
github.com/blackfireio/osinfo v1.0.5/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA=
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||||
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
|
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||||
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/evilsocket/islazy v1.11.0 h1:B5w6uuS6ki6iDG+aH/RFeoMb8ijQh/pGabewqp2UeJ0=
|
github.com/creack/pty v1.1.15 h1:cKRCLMj3Ddm54bKSpemfQ8AtYFBhAI2MPmdys22fBdc=
|
||||||
github.com/evilsocket/islazy v1.11.0/go.mod h1:muYH4x5MB5YRdkxnrOtrXLIBX6LySj1uFIqys94LKdo=
|
github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
|
||||||
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
|
github.com/evilsocket/islazy v1.10.6 h1:MFq000a1ByoumoJWlytqg0qon0KlBeUfPsDjY0hK0bo=
|
||||||
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
|
github.com/evilsocket/islazy v1.10.6/go.mod h1:OrwQGYg3DuZvXUfmH+KIZDjwTCbrjy48T24TUpGqVVw=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
|
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/layeh/gopher-luar v1.0.10 h1:8NIv4MX1Arz96kK4buGK1D87DyDxKZyq6KKvJ2diHp0=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/layeh/gopher-luar v1.0.10/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=
|
||||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
|
||||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
|
||||||
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 h1:LiZB1h0GIcudcDci2bxbqI6DXV8bF8POAnArqvRrIyw=
|
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 h1:LiZB1h0GIcudcDci2bxbqI6DXV8bF8POAnArqvRrIyw=
|
||||||
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0/go.mod h1:F/7q8/HZz+TXjlsoZQQKVYvXTZaFH4QRa3y+j1p7MS0=
|
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0/go.mod h1:F/7q8/HZz+TXjlsoZQQKVYvXTZaFH4QRa3y+j1p7MS0=
|
||||||
github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0=
|
github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0=
|
||||||
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
|
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||||
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
|
github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451 h1:d1PiN4RxzIFXCJTvRkvSkKqwtRAl5ZV4lATKtQI0B7I=
|
||||||
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
|
||||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
|
||||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 h1:k/gmLsJDWwWqbLCur2yWnJzwQEKRcAHXo6seXGuSwWw=
|
||||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
||||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||||
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
|
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210925032602-92d5a993a665/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 h1:BXxu8t6QN0G1uff4bzZzSkpsax8+ALqTGUtz08QrV00=
|
||||||
|
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
|
||||||
|
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20210916214954-140adaaadfaf/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||||
|
mvdan.cc/editorconfig v0.2.0/go.mod h1:lvnnD3BNdBYkhq+B4uBuFFKatfp02eB6HixDvEz91C0=
|
||||||
|
|
|
@ -1,376 +1,70 @@
|
||||||
// the event emitter
|
|
||||||
/*
|
|
||||||
Bait is the event emitter for Hilbish. Much like Node.js and
|
|
||||||
its `events` system, many actions in Hilbish emit events.
|
|
||||||
Unlike Node.js, Hilbish events are global. So make sure to
|
|
||||||
pick a unique name!
|
|
||||||
|
|
||||||
Usage of the Bait module consists of userstanding
|
|
||||||
event-driven architecture, but it's pretty simple:
|
|
||||||
If you want to act on a certain event, you can `catch` it.
|
|
||||||
You can act on events via callback functions.
|
|
||||||
|
|
||||||
Examples of this are in the Hilbish default config!
|
|
||||||
Consider this part of it:
|
|
||||||
```lua
|
|
||||||
bait.catch('command.exit', function(code)
|
|
||||||
running = false
|
|
||||||
doPrompt(code ~= 0)
|
|
||||||
doNotifyPrompt()
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
|
|
||||||
What this does is, whenever the `command.exit` event is thrown,
|
|
||||||
this function will set the user prompt.
|
|
||||||
*/
|
|
||||||
package bait
|
package bait
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
"github.com/chuckpreslar/emission"
|
||||||
"github.com/arnodel/golua/lib/packagelib"
|
"github.com/yuin/gopher-lua"
|
||||||
|
"layeh.com/gopher-luar"
|
||||||
)
|
)
|
||||||
|
|
||||||
type listenerType int
|
|
||||||
const (
|
|
||||||
goListener listenerType = iota
|
|
||||||
luaListener
|
|
||||||
)
|
|
||||||
|
|
||||||
// Recoverer is a function which is called when a panic occurs in an event.
|
|
||||||
type Recoverer func(event string, handler *Listener, err interface{})
|
|
||||||
|
|
||||||
// Listener is a struct that holds the handler for an event.
|
|
||||||
type Listener struct{
|
|
||||||
typ listenerType
|
|
||||||
once bool
|
|
||||||
caller func(...interface{})
|
|
||||||
luaCaller *rt.Closure
|
|
||||||
}
|
|
||||||
|
|
||||||
type Bait struct{
|
type Bait struct{
|
||||||
Loader packagelib.Loader
|
Em *emission.Emitter
|
||||||
recoverer Recoverer
|
|
||||||
handlers map[string][]*Listener
|
|
||||||
rtm *rt.Runtime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Bait instance.
|
func New() Bait {
|
||||||
func New(rtm *rt.Runtime) *Bait {
|
emitter := emission.NewEmitter()
|
||||||
b := &Bait{
|
emitter.RecoverWith(func(hookname, hookfunc interface{}, err error) {
|
||||||
handlers: make(map[string][]*Listener),
|
emitter.Off(hookname, hookfunc)
|
||||||
rtm: rtm,
|
fmt.Println(err)
|
||||||
}
|
})
|
||||||
b.Loader = packagelib.Loader{
|
return Bait{
|
||||||
Load: b.loaderFunc,
|
Em: emitter,
|
||||||
Name: "bait",
|
|
||||||
}
|
|
||||||
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emit throws an event.
|
|
||||||
func (b *Bait) Emit(event string, args ...interface{}) {
|
|
||||||
handles := b.handlers[event]
|
|
||||||
if handles == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for idx, handle := range handles {
|
|
||||||
defer func() {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
b.callRecoverer(event, handle, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if handle.typ == luaListener {
|
|
||||||
funcVal := rt.FunctionValue(handle.luaCaller)
|
|
||||||
var luaArgs []rt.Value
|
|
||||||
for _, arg := range args {
|
|
||||||
var luarg rt.Value
|
|
||||||
switch arg.(type) {
|
|
||||||
case rt.Value: luarg = arg.(rt.Value)
|
|
||||||
default: luarg = rt.AsValue(arg)
|
|
||||||
}
|
|
||||||
luaArgs = append(luaArgs, luarg)
|
|
||||||
}
|
|
||||||
_, err := rt.Call1(b.rtm.MainThread(), funcVal, luaArgs...)
|
|
||||||
if err != nil {
|
|
||||||
if event != "error" {
|
|
||||||
b.Emit("error", event, handle.luaCaller, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// if there is an error in an error event handler, panic instead
|
|
||||||
// (calls the go recoverer function)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handle.caller(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if handle.once {
|
|
||||||
b.removeListener(event, idx)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// On adds a Go function handler for an event.
|
func (b *Bait) Loader(L *lua.LState) int {
|
||||||
func (b *Bait) On(event string, handler func(...interface{})) *Listener {
|
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{})
|
||||||
listener := &Listener{
|
|
||||||
typ: goListener,
|
|
||||||
caller: handler,
|
|
||||||
}
|
|
||||||
|
|
||||||
b.addListener(event, listener)
|
util.Document(L, mod,
|
||||||
return listener
|
`Bait is the event emitter for Hilbish. Why name it bait?
|
||||||
}
|
Because it throws hooks that you can catch (emits events
|
||||||
|
that you can listen to) and because why not, fun naming
|
||||||
|
is fun. This is what you will use if you want to listen
|
||||||
|
in on hooks to know when certain things have happened,
|
||||||
|
like when you've changed directory, a command has
|
||||||
|
failed, etc. To find all available hooks, see doc hooks.`)
|
||||||
|
|
||||||
// OnLua adds a Lua function handler for an event.
|
L.SetField(mod, "throw", luar.New(L, b.bthrow))
|
||||||
func (b *Bait) OnLua(event string, handler *rt.Closure) *Listener {
|
L.SetField(mod, "catch", luar.New(L, b.bcatch))
|
||||||
listener :=&Listener{
|
L.SetField(mod, "catchOnce", luar.New(L, b.bcatchOnce))
|
||||||
typ: luaListener,
|
|
||||||
luaCaller: handler,
|
|
||||||
}
|
|
||||||
b.addListener(event, listener)
|
|
||||||
|
|
||||||
return listener
|
L.Push(mod)
|
||||||
}
|
|
||||||
|
|
||||||
// Off removes a Go function handler for an event.
|
return 1
|
||||||
func (b *Bait) Off(event string, listener *Listener) {
|
|
||||||
handles := b.handlers[event]
|
|
||||||
|
|
||||||
for i, handle := range handles {
|
|
||||||
if handle == listener {
|
|
||||||
b.removeListener(event, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// OffLua removes a Lua function handler for an event.
|
|
||||||
func (b *Bait) OffLua(event string, handler *rt.Closure) {
|
|
||||||
handles := b.handlers[event]
|
|
||||||
|
|
||||||
for i, handle := range handles {
|
|
||||||
if handle.luaCaller == handler {
|
|
||||||
b.removeListener(event, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once adds a Go function listener for an event that only runs once.
|
|
||||||
func (b *Bait) Once(event string, handler func(...interface{})) *Listener {
|
|
||||||
listener := &Listener{
|
|
||||||
typ: goListener,
|
|
||||||
once: true,
|
|
||||||
caller: handler,
|
|
||||||
}
|
|
||||||
b.addListener(event, listener)
|
|
||||||
|
|
||||||
return listener
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnceLua adds a Lua function listener for an event that only runs once.
|
|
||||||
func (b *Bait) OnceLua(event string, handler *rt.Closure) *Listener {
|
|
||||||
listener := &Listener{
|
|
||||||
typ: luaListener,
|
|
||||||
once: true,
|
|
||||||
luaCaller: handler,
|
|
||||||
}
|
|
||||||
b.addListener(event, listener)
|
|
||||||
|
|
||||||
return listener
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetRecoverer sets the function to be executed when a panic occurs in an event.
|
|
||||||
func (b *Bait) SetRecoverer(recoverer Recoverer) {
|
|
||||||
b.recoverer = recoverer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bait) addListener(event string, listener *Listener) {
|
|
||||||
if b.handlers[event] == nil {
|
|
||||||
b.handlers[event] = []*Listener{}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.handlers[event] = append(b.handlers[event], listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func (b *Bait) removeListener(event string, idx int) {
|
|
||||||
b.handlers[event][idx] = b.handlers[event][len(b.handlers[event]) - 1]
|
|
||||||
|
|
||||||
b.handlers[event] = b.handlers[event][:len(b.handlers[event]) - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) {
|
|
||||||
if b.recoverer == nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
b.recoverer(event, handler, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bait) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"catch": util.LuaExport{b.bcatch, 2, false},
|
|
||||||
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
|
|
||||||
"throw": util.LuaExport{b.bthrow, 1, true},
|
|
||||||
"release": util.LuaExport{b.brelease, 2, false},
|
|
||||||
"hooks": util.LuaExport{b.bhooks, 1, false},
|
|
||||||
}
|
|
||||||
mod := rt.NewTable()
|
|
||||||
util.SetExports(rtm, mod, exports)
|
|
||||||
|
|
||||||
return rt.TableValue(mod), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) {
|
|
||||||
funcVal := rt.FunctionValue(catcher)
|
|
||||||
var luaArgs []rt.Value
|
|
||||||
for _, arg := range args {
|
|
||||||
var luarg rt.Value
|
|
||||||
switch arg.(type) {
|
|
||||||
case rt.Value: luarg = arg.(rt.Value)
|
|
||||||
default: luarg = rt.AsValue(arg)
|
|
||||||
}
|
|
||||||
luaArgs = append(luaArgs, luarg)
|
|
||||||
}
|
|
||||||
_, err := rt.Call1(t, funcVal, luaArgs...)
|
|
||||||
if err != nil {
|
|
||||||
e := rt.NewError(rt.StringValue(err.Error()))
|
|
||||||
e = e.AddContext(c.Next(), 1)
|
|
||||||
// panicking here won't actually cause hilbish to panic and instead will
|
|
||||||
// print the error and remove the hook (look at emission recover from above)
|
|
||||||
panic(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// catch(name, cb)
|
|
||||||
// Catches an event. This function can be used to act on events.
|
|
||||||
// #param name string The name of the hook.
|
|
||||||
// #param cb function The function that will be called when the hook is thrown.
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
bait.catch('hilbish.exit', function()
|
|
||||||
print 'Goodbye Hilbish!'
|
|
||||||
end)
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
name, catcher, err := util.HandleStrCallback(t, c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
b.OnLua(name, catcher)
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// catchOnce(name, cb)
|
|
||||||
// Catches an event, but only once. This will remove the hook immediately after it runs for the first time.
|
|
||||||
// #param name string The name of the event
|
|
||||||
// #param cb function The function that will be called when the event is thrown.
|
|
||||||
func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
name, catcher, err := util.HandleStrCallback(t, c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
b.OnceLua(name, catcher)
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// hooks(name) -> table
|
|
||||||
// Returns a table of functions that are hooked on an event with the corresponding `name`.
|
|
||||||
// #param name string The name of the hook
|
|
||||||
// #returns table<function>
|
|
||||||
func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
evName, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
noHooks := errors.New("no hooks for event " + evName)
|
|
||||||
|
|
||||||
handlers := b.handlers[evName]
|
|
||||||
if handlers == nil {
|
|
||||||
return nil, noHooks
|
|
||||||
}
|
|
||||||
|
|
||||||
luaHandlers := rt.NewTable()
|
|
||||||
for _, handler := range handlers {
|
|
||||||
if handler.typ != luaListener { continue }
|
|
||||||
luaHandlers.Set(rt.IntValue(luaHandlers.Len() + 1), rt.FunctionValue(handler.luaCaller))
|
|
||||||
}
|
|
||||||
|
|
||||||
if luaHandlers.Len() == 0 {
|
|
||||||
return nil, noHooks
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(luaHandlers)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// release(name, catcher)
|
|
||||||
// Removes the `catcher` for the event with `name`.
|
|
||||||
// For this to work, `catcher` has to be the same function used to catch
|
|
||||||
// an event, like one saved to a variable.
|
|
||||||
// #param name string Name of the event the hook is on
|
|
||||||
// #param catcher function Hook function to remove
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
local hookCallback = function() print 'hi' end
|
|
||||||
|
|
||||||
bait.catch('event', hookCallback)
|
|
||||||
|
|
||||||
-- a little while later....
|
|
||||||
bait.release('event', hookCallback)
|
|
||||||
-- and now hookCallback will no longer be ran for the event.
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
name, catcher, err := util.HandleStrCallback(t, c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
b.OffLua(name, catcher)
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// throw(name, ...args)
|
// throw(name, ...args)
|
||||||
// #param name string The name of the hook.
|
// Throws a hook with `name` with the provided `args`
|
||||||
// #param args ...any The arguments to pass to the hook.
|
// --- @param name string
|
||||||
// Throws a hook with `name` with the provided `args`.
|
// --- @vararg any
|
||||||
/*
|
func (b *Bait) bthrow(name string, args ...interface{}) {
|
||||||
#example
|
b.Em.Emit(name, args...)
|
||||||
bait.throw('greeting', 'world')
|
}
|
||||||
|
|
||||||
-- This can then be listened to via
|
// catch(name, cb)
|
||||||
bait.catch('gretting', function(greetTo)
|
// Catches a hook with `name`. Runs the `cb` when it is thrown
|
||||||
print('Hello ' .. greetTo)
|
// --- @param name string
|
||||||
end)
|
// --- @param cb function
|
||||||
#example
|
func (b *Bait) bcatch(name string, catcher func(...interface{})) {
|
||||||
*/
|
b.Em.On(name, catcher)
|
||||||
func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
}
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
// catchOnce(name, cb)
|
||||||
}
|
// Same as catch, but only runs the `cb` once and then removes the hook
|
||||||
name, err := c.StringArg(0)
|
// --- @param name string
|
||||||
if err != nil {
|
// --- @param cb function
|
||||||
return nil, err
|
func (b *Bait) bcatchOnce(name string, catcher func(...interface{})) {
|
||||||
}
|
b.Em.Once(name, catcher)
|
||||||
ifaceSlice := make([]interface{}, len(c.Etc()))
|
|
||||||
for i, v := range c.Etc() {
|
|
||||||
ifaceSlice[i] = v
|
|
||||||
}
|
|
||||||
b.Emit(name, ifaceSlice...)
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,133 +1,54 @@
|
||||||
// library for custom commands
|
|
||||||
/*
|
|
||||||
Commander is the library which handles Hilbish commands. This makes
|
|
||||||
the user able to add Lua-written commands to their shell without making
|
|
||||||
a separate script in a bin folder. Instead, you may simply use the Commander
|
|
||||||
library in your Hilbish config.
|
|
||||||
|
|
||||||
```lua
|
|
||||||
local commander = require 'commander'
|
|
||||||
|
|
||||||
commander.register('hello', function(args, sinks)
|
|
||||||
sinks.out:writeln 'Hello world!'
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
|
|
||||||
In this example, a command with the name of `hello` is created
|
|
||||||
that will print `Hello world!` to output. One question you may
|
|
||||||
have is: What is the `sinks` parameter?
|
|
||||||
|
|
||||||
The `sinks` parameter is a table with 3 keys: `input`, `out`, and `err`.
|
|
||||||
There is an `in` alias to `input`, but it requires using the string accessor syntax (`sinks['in']`)
|
|
||||||
as `in` is also a Lua keyword, so `input` is preferred for use.
|
|
||||||
All of them are a @Sink.
|
|
||||||
In the future, `sinks.in` will be removed.
|
|
||||||
|
|
||||||
- `in` is the standard input.
|
|
||||||
You may use the read functions on this sink to get input from the user.
|
|
||||||
- `out` is standard output.
|
|
||||||
This is usually where command output should go.
|
|
||||||
- `err` is standard error.
|
|
||||||
This sink is for writing errors, as the name would suggest.
|
|
||||||
*/
|
|
||||||
package commander
|
package commander
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
"hilbish/golibs/bait"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
"github.com/chuckpreslar/emission"
|
||||||
"github.com/arnodel/golua/lib/packagelib"
|
"github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Commander struct{
|
type Commander struct{
|
||||||
Events *bait.Bait
|
Events *emission.Emitter
|
||||||
Loader packagelib.Loader
|
|
||||||
Commands map[string]*rt.Closure
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(rtm *rt.Runtime) *Commander {
|
func New() Commander {
|
||||||
c := &Commander{
|
return Commander{
|
||||||
Events: bait.New(rtm),
|
Events: emission.NewEmitter(),
|
||||||
Commands: make(map[string]*rt.Closure),
|
|
||||||
}
|
}
|
||||||
c.Loader = packagelib.Loader{
|
|
||||||
Load: c.loaderFunc,
|
|
||||||
Name: "commander",
|
|
||||||
}
|
|
||||||
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
func (c *Commander) Loader(L *lua.LState) int {
|
||||||
exports := map[string]util.LuaExport{
|
exports := map[string]lua.LGFunction{
|
||||||
"register": util.LuaExport{c.cregister, 2, false},
|
"register": c.cregister,
|
||||||
"deregister": util.LuaExport{c.cderegister, 1, false},
|
"deregister": c.cderegister,
|
||||||
"registry": util.LuaExport{c.cregistry, 0, false},
|
|
||||||
}
|
}
|
||||||
mod := rt.NewTable()
|
mod := L.SetFuncs(L.NewTable(), exports)
|
||||||
util.SetExports(rtm, mod, exports)
|
util.Document(L, mod, "Commander is Hilbish's custom command library, a way to write commands in Lua.")
|
||||||
|
L.Push(mod)
|
||||||
|
|
||||||
return rt.TableValue(mod), nil
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// register(name, cb)
|
// register(name, cb)
|
||||||
// Adds a new command with the given `name`. When Hilbish has to run a command with a name,
|
// Register a command with `name` that runs `cb` when ran
|
||||||
// it will run the function providing the arguments and sinks.
|
// --- @param name string
|
||||||
// #param name string Name of the command
|
// --- @param cb function
|
||||||
// #param cb function Callback to handle command invocation
|
func (c *Commander) cregister(L *lua.LState) int {
|
||||||
/*
|
cmdName := L.CheckString(1)
|
||||||
#example
|
cmd := L.CheckFunction(2)
|
||||||
-- When you run the command `hello` in the shell, it will print `Hello world`.
|
|
||||||
-- If you run it with, for example, `hello Hilbish`, it will print 'Hello Hilbish'
|
|
||||||
commander.register('hello', function(args, sinks)
|
|
||||||
local name = 'world'
|
|
||||||
if #args > 0 then name = args[1] end
|
|
||||||
|
|
||||||
sinks.out:writeln('Hello ' .. name)
|
c.Events.Emit("commandRegister", cmdName, cmd)
|
||||||
end)
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
|
||||||
cmdName, cmd, err := util.HandleStrCallback(t, ct)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Commands[cmdName] = cmd
|
return 0
|
||||||
|
|
||||||
return ct.Next(), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// deregister(name)
|
// deregister(name)
|
||||||
// Removes the named command. Note that this will only remove Commander-registered commands.
|
// Deregisters any command registered with `name`
|
||||||
// #param name string Name of the command to remove.
|
// --- @param name string
|
||||||
func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
func (c *Commander) cderegister(L *lua.LState) int {
|
||||||
if err := ct.Check1Arg(); err != nil {
|
cmdName := L.CheckString(1)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cmdName, err := ct.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(c.Commands, cmdName)
|
c.Events.Emit("commandDeregister", cmdName)
|
||||||
|
|
||||||
return ct.Next(), err
|
return 0
|
||||||
}
|
|
||||||
|
|
||||||
// registry() -> table
|
|
||||||
// Returns all registered commanders. Returns a list of tables with the following keys:
|
|
||||||
// - `exec`: The function used to run the commander. Commanders require args and sinks to be passed.
|
|
||||||
// #returns table
|
|
||||||
func (c *Commander) cregistry(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
|
||||||
registryLua := rt.NewTable()
|
|
||||||
for cmdName, cmd := range c.Commands {
|
|
||||||
cmdTbl := rt.NewTable()
|
|
||||||
cmdTbl.Set(rt.StringValue("exec"), rt.FunctionValue(cmd))
|
|
||||||
|
|
||||||
registryLua.Set(rt.StringValue(cmdName), rt.TableValue(cmdTbl))
|
|
||||||
}
|
|
||||||
|
|
||||||
return ct.PushingNext1(t.Runtime, rt.TableValue(registryLua)), nil
|
|
||||||
}
|
}
|
||||||
|
|
368
golibs/fs/fs.go
|
@ -1,235 +1,58 @@
|
||||||
// filesystem interaction and functionality library
|
// The fs module provides easy and simple access to filesystem functions and other
|
||||||
/*
|
// things, and acts an addition to the Lua standard library's I/O and fs functions.
|
||||||
The fs module provides filesystem functions to Hilbish. While Lua's standard
|
|
||||||
library has some I/O functions, they're missing a lot of the basics. The `fs`
|
|
||||||
library offers more functions and will work on any operating system Hilbish does.
|
|
||||||
#field pathSep The operating system's path separator.
|
|
||||||
*/
|
|
||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
"github.com/yuin/gopher-lua"
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
"github.com/arnodel/golua/lib/packagelib"
|
|
||||||
"github.com/arnodel/golua/lib/iolib"
|
|
||||||
"mvdan.cc/sh/v3/interp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type fs struct{
|
func Loader(L *lua.LState) int {
|
||||||
runner *interp.Runner
|
mod := L.SetFuncs(L.NewTable(), exports)
|
||||||
Loader packagelib.Loader
|
|
||||||
|
util.Document(L, mod, `The fs module provides easy and simple access to
|
||||||
|
filesystem functions and other things, and acts an
|
||||||
|
addition to the Lua standard library's I/O and fs functions.`)
|
||||||
|
|
||||||
|
L.Push(mod)
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(runner *interp.Runner) *fs {
|
var exports = map[string]lua.LGFunction{
|
||||||
f := &fs{
|
"cd": fcd,
|
||||||
runner: runner,
|
"mkdir": fmkdir,
|
||||||
}
|
"stat": fstat,
|
||||||
f.Loader = packagelib.Loader{
|
"readdir": freaddir,
|
||||||
Load: f.loaderFunc,
|
|
||||||
Name: "fs",
|
|
||||||
}
|
|
||||||
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fs) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"cd": util.LuaExport{f.fcd, 1, false},
|
|
||||||
"mkdir": util.LuaExport{f.fmkdir, 2, false},
|
|
||||||
"stat": util.LuaExport{f.fstat, 1, false},
|
|
||||||
"readdir": util.LuaExport{f.freaddir, 1, false},
|
|
||||||
"abs": util.LuaExport{f.fabs, 1, false},
|
|
||||||
"basename": util.LuaExport{f.fbasename, 1, false},
|
|
||||||
"dir": util.LuaExport{f.fdir, 1, false},
|
|
||||||
"glob": util.LuaExport{f.fglob, 1, false},
|
|
||||||
"join": util.LuaExport{f.fjoin, 0, true},
|
|
||||||
"pipe": util.LuaExport{f.fpipe, 0, false},
|
|
||||||
}
|
|
||||||
mod := rt.NewTable()
|
|
||||||
util.SetExports(rtm, mod, exports)
|
|
||||||
mod.Set(rt.StringValue("pathSep"), rt.StringValue(string(os.PathSeparator)))
|
|
||||||
mod.Set(rt.StringValue("pathListSep"), rt.StringValue(string(os.PathListSeparator)))
|
|
||||||
|
|
||||||
return rt.TableValue(mod), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// abs(path) -> string
|
|
||||||
// Returns an absolute version of the `path`.
|
|
||||||
// This can be used to resolve short paths like `..` to `/home/user`.
|
|
||||||
// #param path string
|
|
||||||
// #returns string
|
|
||||||
func (f *fs) fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path = util.ExpandHome(path)
|
|
||||||
|
|
||||||
abspath, err := filepath.Abs(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.StringValue(abspath)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// basename(path) -> string
|
|
||||||
// Returns the "basename," or the last part of the provided `path`. If path is empty,
|
|
||||||
// `.` will be returned.
|
|
||||||
// #param path string Path to get the base name of.
|
|
||||||
// #returns string
|
|
||||||
func (f *fs) fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.StringValue(filepath.Base(path))), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cd(dir)
|
// cd(dir)
|
||||||
// Changes Hilbish's directory to `dir`.
|
// Changes directory to `dir`
|
||||||
// #param dir string Path to change directory to.
|
// --- @param dir string
|
||||||
func (f *fs) fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func fcd(L *lua.LState) int {
|
||||||
if err := c.Check1Arg(); err != nil {
|
path := L.CheckString(1)
|
||||||
return nil, err
|
|
||||||
}
|
err := os.Chdir(strings.TrimSpace(path))
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
e := err.(*os.PathError).Err.Error()
|
||||||
}
|
L.RaiseError(e + ": " + path)
|
||||||
path = util.ExpandHome(strings.TrimSpace(path))
|
|
||||||
|
|
||||||
abspath, _ := filepath.Abs(path)
|
|
||||||
err = os.Chdir(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
interp.Dir(abspath)(f.runner)
|
|
||||||
|
|
||||||
return c.Next(), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// dir(path) -> string
|
|
||||||
// Returns the directory part of `path`. If a file path like
|
|
||||||
// `~/Documents/doc.txt` then this function will return `~/Documents`.
|
|
||||||
// #param path string Path to get the directory for.
|
|
||||||
// #returns string
|
|
||||||
func (f *fs) fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.StringValue(filepath.Dir(path))), nil
|
return 0
|
||||||
}
|
|
||||||
|
|
||||||
// glob(pattern) -> matches (table)
|
|
||||||
// Match all files based on the provided `pattern`.
|
|
||||||
// For the syntax' refer to Go's filepath.Match function: https://pkg.go.dev/path/filepath#Match
|
|
||||||
// #param pattern string Pattern to compare files with.
|
|
||||||
// #returns table A list of file names/paths that match.
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
--[[
|
|
||||||
Within a folder that contains the following files:
|
|
||||||
a.txt
|
|
||||||
init.lua
|
|
||||||
code.lua
|
|
||||||
doc.pdf
|
|
||||||
]]--
|
|
||||||
local matches = fs.glob './*.lua'
|
|
||||||
print(matches)
|
|
||||||
-- -> {'init.lua', 'code.lua'}
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (f *fs) fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
pattern, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
matches, err := filepath.Glob(pattern)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
luaMatches := rt.NewTable()
|
|
||||||
|
|
||||||
for i, match := range matches {
|
|
||||||
luaMatches.Set(rt.IntValue(int64(i + 1)), rt.StringValue(match))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.TableValue(luaMatches)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// join(...path) -> string
|
|
||||||
// Takes any list of paths and joins them based on the operating system's path separator.
|
|
||||||
// #param path ...string Paths to join together
|
|
||||||
// #returns string The joined path.
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
-- This prints the directory for Hilbish's config!
|
|
||||||
print(fs.join(hilbish.userDir.config, 'hilbish'))
|
|
||||||
-- -> '/home/user/.config/hilbish' on Linux
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (f *fs) fjoin(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
strs := make([]string, len(c.Etc()))
|
|
||||||
for i, v := range c.Etc() {
|
|
||||||
if v.Type() != rt.StringType {
|
|
||||||
// +2; go indexes of 0 and first arg from above
|
|
||||||
return nil, fmt.Errorf("bad argument #%d to run (expected string, got %s)", i + 1, v.TypeName())
|
|
||||||
}
|
|
||||||
strs[i] = v.AsString()
|
|
||||||
}
|
|
||||||
|
|
||||||
res := filepath.Join(strs...)
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.StringValue(res)), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// mkdir(name, recursive)
|
// mkdir(name, recursive)
|
||||||
// Creates a new directory with the provided `name`.
|
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
||||||
// With `recursive`, mkdir will create parent directories.
|
// --- @param name string
|
||||||
// #param name string Name of the directory
|
// --- @param recursive boolean
|
||||||
// #param recursive boolean Whether to create parent directories for the provided name
|
func fmkdir(L *lua.LState) int {
|
||||||
/*
|
dirname := L.CheckString(1)
|
||||||
#example
|
recursive := L.ToBool(2)
|
||||||
-- This will create the directory foo, then create the directory bar in the
|
path := strings.TrimSpace(dirname)
|
||||||
-- foo directory. If recursive is false in this case, it will fail.
|
var err error
|
||||||
fs.mkdir('./foo/bar', true)
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (f *fs) fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.CheckNArgs(2); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
recursive, err := c.BoolArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path = util.ExpandHome(strings.TrimSpace(path))
|
|
||||||
|
|
||||||
if recursive {
|
if recursive {
|
||||||
err = os.MkdirAll(path, 0744)
|
err = os.MkdirAll(path, 0744)
|
||||||
|
@ -237,100 +60,51 @@ func (f *fs) fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
err = os.Mkdir(path, 0744)
|
err = os.Mkdir(path, 0744)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
L.RaiseError(err.Error() + ": " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Next(), err
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// fpipe() -> File, File
|
// stat(path)
|
||||||
// Returns a pair of connected files, also known as a pipe.
|
// Returns info about `path`
|
||||||
// The type returned is a Lua file, same as returned from `io` functions.
|
// --- @param path string
|
||||||
// #returns File
|
func fstat(L *lua.LState) int {
|
||||||
// #returns File
|
path := L.CheckString(1)
|
||||||
func (f *fs) fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
rf, wf, err := os.Pipe()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rfLua := iolib.NewFile(rf, 0)
|
|
||||||
wfLua := iolib.NewFile(wf, 0)
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rfLua.Value(t.Runtime), wfLua.Value(t.Runtime)), nil
|
|
||||||
}
|
|
||||||
// readdir(path) -> table[string]
|
|
||||||
// Returns a list of all files and directories in the provided path.
|
|
||||||
// #param dir string
|
|
||||||
// #returns table
|
|
||||||
func (f *fs) freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
dir, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
dir = util.ExpandHome(dir)
|
|
||||||
names := rt.NewTable()
|
|
||||||
|
|
||||||
dirEntries, err := os.ReadDir(dir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for i, entry := range dirEntries {
|
|
||||||
names.Set(rt.IntValue(int64(i + 1)), rt.StringValue(entry.Name()))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(names)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// stat(path) -> {}
|
|
||||||
// Returns the information about a given `path`.
|
|
||||||
// The returned table contains the following values:
|
|
||||||
// name (string) - Name of the path
|
|
||||||
// size (number) - Size of the path in bytes
|
|
||||||
// mode (string) - Unix permission mode in an octal format string (with leading 0)
|
|
||||||
// isDir (boolean) - If the path is a directory
|
|
||||||
// #param path string
|
|
||||||
// #returns table
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
local inspect = require 'inspect'
|
|
||||||
|
|
||||||
local stat = fs.stat '~'
|
|
||||||
print(inspect(stat))
|
|
||||||
--[[
|
|
||||||
Would print the following:
|
|
||||||
{
|
|
||||||
isDir = true,
|
|
||||||
mode = "0755",
|
|
||||||
name = "username",
|
|
||||||
size = 12288
|
|
||||||
}
|
|
||||||
]]--
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (f *fs) fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
path = util.ExpandHome(path)
|
|
||||||
|
|
||||||
pathinfo, err := os.Stat(path)
|
pathinfo, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
L.RaiseError(err.Error() + ": " + path)
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
statTbl := rt.NewTable()
|
statTbl := L.NewTable()
|
||||||
statTbl.Set(rt.StringValue("name"), rt.StringValue(pathinfo.Name()))
|
L.SetField(statTbl, "name", lua.LString(pathinfo.Name()))
|
||||||
statTbl.Set(rt.StringValue("size"), rt.IntValue(pathinfo.Size()))
|
L.SetField(statTbl, "size", lua.LNumber(pathinfo.Size()))
|
||||||
statTbl.Set(rt.StringValue("mode"), rt.StringValue("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
|
L.SetField(statTbl, "mode", lua.LString("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
|
||||||
statTbl.Set(rt.StringValue("isDir"), rt.BoolValue(pathinfo.IsDir()))
|
L.SetField(statTbl, "isDir", lua.LBool(pathinfo.IsDir()))
|
||||||
|
L.Push(statTbl)
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readdir(dir)
|
||||||
|
// Returns a table of files in `dir`
|
||||||
|
// --- @param dir string
|
||||||
|
// --- @return table
|
||||||
|
func freaddir(L *lua.LState) int {
|
||||||
|
dir := L.CheckString(1)
|
||||||
|
names := L.NewTable()
|
||||||
|
|
||||||
|
dirEntries, err := os.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
L.RaiseError(err.Error() + ": " + dir)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
for _, entry := range dirEntries {
|
||||||
|
names.Append(lua.LString(entry.Name()))
|
||||||
|
}
|
||||||
|
|
||||||
|
L.Push(names)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
// low level terminal library
|
|
||||||
// The terminal library is a simple and lower level library for certain terminal interactions.
|
|
||||||
package terminal
|
package terminal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,77 +5,76 @@ import (
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
"github.com/arnodel/golua/lib/packagelib"
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
"github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
var termState *term.State
|
var termState *term.State
|
||||||
var Loader = packagelib.Loader{
|
|
||||||
Load: loaderFunc,
|
func Loader(L *lua.LState) int {
|
||||||
Name: "terminal",
|
mod := L.SetFuncs(L.NewTable(), exports)
|
||||||
|
util.Document(L, mod, "The terminal library is a simple and lower level library for certain terminal interactions.")
|
||||||
|
|
||||||
|
L.Push(mod)
|
||||||
|
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
var exports = map[string]lua.LGFunction{
|
||||||
exports := map[string]util.LuaExport{
|
"setRaw": termraw,
|
||||||
"setRaw": util.LuaExport{termsetRaw, 0, false},
|
"restoreState": termrestoreState,
|
||||||
"restoreState": util.LuaExport{termrestoreState, 0, false},
|
"size": termsize,
|
||||||
"size": util.LuaExport{termsize, 0, false},
|
"saveState": termsaveState,
|
||||||
"saveState": util.LuaExport{termsaveState, 0, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
mod := rt.NewTable()
|
|
||||||
util.SetExports(rtm, mod, exports)
|
|
||||||
|
|
||||||
return rt.TableValue(mod), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// size()
|
// size()
|
||||||
// Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
// Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
||||||
// NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal.
|
// Note: this is not the size in relation to the dimensions of the display
|
||||||
func termsize(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func termsize(L *lua.LState) int {
|
||||||
w, h, err := term.GetSize(int(os.Stdin.Fd()))
|
w, h, err := term.GetSize(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
L.RaiseError(err.Error())
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
dimensions := L.NewTable()
|
||||||
|
L.SetField(dimensions, "width", lua.LNumber(w))
|
||||||
|
L.SetField(dimensions, "height", lua.LNumber(h))
|
||||||
|
|
||||||
dimensions := rt.NewTable()
|
L.Push(dimensions)
|
||||||
dimensions.Set(rt.StringValue("width"), rt.IntValue(int64(w)))
|
return 1
|
||||||
dimensions.Set(rt.StringValue("height"), rt.IntValue(int64(h)))
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(dimensions)), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// saveState()
|
// saveState()
|
||||||
// Saves the current state of the terminal.
|
// Saves the current state of the terminal
|
||||||
func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func termsaveState(L *lua.LState) int {
|
||||||
state, err := term.GetState(int(os.Stdin.Fd()))
|
state, err := term.GetState(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
L.RaiseError(err.Error())
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
termState = state
|
termState = state
|
||||||
return c.Next(), nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// restoreState()
|
// restoreState()
|
||||||
// Restores the last saved state of the terminal
|
// Restores the last saved state of the terminal
|
||||||
func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func termrestoreState(L *lua.LState) int {
|
||||||
err := term.Restore(int(os.Stdin.Fd()), termState)
|
err := term.Restore(int(os.Stdin.Fd()), termState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
L.RaiseError(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Next(), nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// setRaw()
|
// setRaw()
|
||||||
// Puts the terminal into raw mode.
|
// Puts the terminal in raw mode
|
||||||
func termsetRaw(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func termraw(L *lua.LState) int {
|
||||||
_, err := term.MakeRaw(int(os.Stdin.Fd()))
|
_, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
L.RaiseError(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Next(), nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
%global _missing_build_ids_terminate_build 0
|
|
||||||
%global debug_package %{nil}
|
|
||||||
|
|
||||||
Name: hilbish-git
|
|
||||||
Version: {{{ git_tag_version }}}.{{{ git_short_hash }}}
|
|
||||||
Release: 1%{?dist}
|
|
||||||
Summary: The flower shell. A comfy and nice little shell for Lua fans!
|
|
||||||
License: MIT
|
|
||||||
|
|
||||||
Source: {{{ git_dir_pack }}}
|
|
||||||
BuildRequires: git golang go-task
|
|
||||||
Requires: inspect succulent lunacolors
|
|
||||||
|
|
||||||
Url: https://github.com/Rosettea/Hilbish
|
|
||||||
VCS: {{{ git_dir_vcs }}}
|
|
||||||
|
|
||||||
%description
|
|
||||||
Hilbish is a extensible shell (framework). It was made to be very customizable
|
|
||||||
via the Lua programming language. It aims to be easy to use for the casual
|
|
||||||
people but powerful for those who want to tinker more with their shell,
|
|
||||||
the thing used to interface with most of the system.
|
|
||||||
|
|
||||||
The motivation for choosing Lua was that its simpler and better to use
|
|
||||||
than old shell script. It's fine for basic interactive shell uses,
|
|
||||||
but that's the only place Hilbish has shell script; everything else is Lua
|
|
||||||
and aims to be infinitely configurable. If something isn't, open an issue!
|
|
||||||
|
|
||||||
%prep
|
|
||||||
{{{ git_dir_setup_macro }}}
|
|
||||||
sed -i '\|/etc/shells|d' Taskfile.yaml
|
|
||||||
|
|
||||||
%build
|
|
||||||
go-task
|
|
||||||
|
|
||||||
%install
|
|
||||||
go-task install PREFIX=%{buildroot}/usr BINDIR=%{buildroot}/%{_bindir}
|
|
||||||
|
|
||||||
%post
|
|
||||||
if [ "$1" = 1 ]; then
|
|
||||||
if [ ! -f %{_sysconfdir}/shells ] ; then
|
|
||||||
echo "%{_bindir}/hilbish" > %{_sysconfdir}/shells
|
|
||||||
echo "/bin/hilbish" >> %{_sysconfdir}/shells
|
|
||||||
else
|
|
||||||
grep -q "^%{_bindir}/hilbish$" %{_sysconfdir}/shells || echo "%{_bindir}/hilbish" >> %{_sysconfdir}/shells
|
|
||||||
grep -q "^/bin/hilbish$" %{_sysconfdir}/shells || echo "/bin/hilbish" >> %{_sysconfdir}/shells
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
%postun
|
|
||||||
if [ "$1" = 0 ] && [ -f %{_sysconfdir}/shells ] ; then
|
|
||||||
sed -i '\!^%{_bindir}/hilbish$!d' %{_sysconfdir}/shells
|
|
||||||
sed -i '\!^/bin/hilbish$!d' %{_sysconfdir}/shells
|
|
||||||
fi
|
|
||||||
|
|
||||||
%files
|
|
||||||
%doc README.md
|
|
||||||
%license LICENSE
|
|
||||||
%{_bindir}/hilbish
|
|
||||||
%{_datadir}/hilbish
|
|
75
history.go
|
@ -4,86 +4,33 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type luaHistory struct {}
|
|
||||||
|
|
||||||
func (h *luaHistory) Write(line string) (int, error) {
|
|
||||||
histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add"))
|
|
||||||
ln, err := rt.Call1(l.MainThread(), histWrite, rt.StringValue(line))
|
|
||||||
|
|
||||||
var num int64
|
|
||||||
if ln.Type() == rt.IntType {
|
|
||||||
num = ln.AsInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
return int(num), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *luaHistory) GetLine(idx int) (string, error) {
|
|
||||||
histGet := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("get"))
|
|
||||||
lcmd, err := rt.Call1(l.MainThread(), histGet, rt.IntValue(int64(idx)))
|
|
||||||
|
|
||||||
var cmd string
|
|
||||||
if lcmd.Type() == rt.StringType {
|
|
||||||
cmd = lcmd.AsString()
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *luaHistory) Len() int {
|
|
||||||
histSize := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("size"))
|
|
||||||
ln, _ := rt.Call1(l.MainThread(), histSize)
|
|
||||||
|
|
||||||
var num int64
|
|
||||||
if ln.Type() == rt.IntType {
|
|
||||||
num = ln.AsInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
return int(num)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *luaHistory) Dump() interface{} {
|
|
||||||
// hilbish.history interface already has all function, this isnt used in readline
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type fileHistory struct {
|
type fileHistory struct {
|
||||||
items []string
|
items []string
|
||||||
f *os.File
|
f *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFileHistory(path string) *fileHistory {
|
func newFileHistory() (*fileHistory, error) {
|
||||||
dir := filepath.Dir(path)
|
data, err := os.ReadFile(defaultHistPath)
|
||||||
|
|
||||||
err := os.MkdirAll(dir, 0755)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, fs.ErrNotExist) {
|
if !errors.Is(err, fs.ErrNotExist) {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itms := []string{""}
|
||||||
lines := strings.Split(string(data), "\n")
|
lines := strings.Split(string(data), "\n")
|
||||||
itms := make([]string, len(lines) - 1)
|
|
||||||
for i, l := range lines {
|
for i, l := range lines {
|
||||||
if i == len(lines) - 1 {
|
if i == len(lines) - 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
itms[i] = l
|
itms = append(itms, l)
|
||||||
}
|
}
|
||||||
f, err := os.OpenFile(path, os.O_APPEND | os.O_WRONLY | os.O_CREATE, 0755)
|
f, err := os.OpenFile(defaultHistPath, os.O_APPEND | os.O_WRONLY | os.O_CREATE, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fh := &fileHistory{
|
fh := &fileHistory{
|
||||||
|
@ -91,7 +38,7 @@ func newFileHistory(path string) *fileHistory {
|
||||||
f: f,
|
f: f,
|
||||||
}
|
}
|
||||||
|
|
||||||
return fh
|
return fh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *fileHistory) Write(line string) (int, error) {
|
func (h *fileHistory) Write(line string) (int, error) {
|
||||||
|
@ -126,9 +73,3 @@ func (h *fileHistory) Len() int {
|
||||||
func (h *fileHistory) Dump() interface{} {
|
func (h *fileHistory) Dump() interface{} {
|
||||||
return h.items
|
return h.items
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *fileHistory) clear() {
|
|
||||||
h.items = []string{}
|
|
||||||
h.f.Truncate(0)
|
|
||||||
h.f.Sync()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
//go:build windows
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "golang.org/x/sys/windows"
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// vt output (escape codes)
|
|
||||||
var outMode uint32
|
|
||||||
windows.GetConsoleMode(windows.Stdout, &outMode)
|
|
||||||
windows.SetConsoleMode(windows.Stdout, outMode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
|
||||||
|
|
||||||
// vt input
|
|
||||||
var inMode uint32
|
|
||||||
windows.GetConsoleMode(windows.Stdin, &inMode)
|
|
||||||
windows.SetConsoleMode(windows.Stdin, inMode | windows.ENABLE_VIRTUAL_TERMINAL_INPUT)
|
|
||||||
}
|
|
507
job.go
|
@ -1,507 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"sync"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"hilbish/util"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
var jobs *jobHandler
|
|
||||||
var jobMetaKey = rt.StringValue("hshjob")
|
|
||||||
|
|
||||||
// #type
|
|
||||||
// #interface jobs
|
|
||||||
// #property cmd The user entered command string for the job.
|
|
||||||
// #property running Whether the job is running or not.
|
|
||||||
// #property id The ID of the job in the job table
|
|
||||||
// #property pid The Process ID
|
|
||||||
// #property exitCode The last exit code of the job.
|
|
||||||
// #property stdout The standard output of the job. This just means the normal logs of the process.
|
|
||||||
// #property stderr The standard error stream of the process. This (usually) includes error messages of the job.
|
|
||||||
// The Job type describes a Hilbish job.
|
|
||||||
type job struct {
|
|
||||||
cmd string
|
|
||||||
running bool
|
|
||||||
id int
|
|
||||||
pid int
|
|
||||||
exitCode int
|
|
||||||
once bool
|
|
||||||
args []string
|
|
||||||
// save path for a few reasons, one being security (lmao) while the other
|
|
||||||
// would just be so itll be the same binary command always (path changes)
|
|
||||||
path string
|
|
||||||
handle *exec.Cmd
|
|
||||||
cmdout io.Writer
|
|
||||||
cmderr io.Writer
|
|
||||||
stdout *bytes.Buffer
|
|
||||||
stderr *bytes.Buffer
|
|
||||||
ud *rt.UserData
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) start() error {
|
|
||||||
if j.handle == nil || j.once {
|
|
||||||
// cmd cant be reused so make a new one
|
|
||||||
cmd := exec.Cmd{
|
|
||||||
Path: j.path,
|
|
||||||
Args: j.args,
|
|
||||||
}
|
|
||||||
j.setHandle(&cmd)
|
|
||||||
}
|
|
||||||
// bgProcAttr is defined in execfile_<os>.go, it holds a procattr struct
|
|
||||||
// in a simple explanation, it makes signals from hilbish (sigint)
|
|
||||||
// not go to it (child process)
|
|
||||||
j.handle.SysProcAttr = bgProcAttr
|
|
||||||
// reset output buffers
|
|
||||||
j.stdout.Reset()
|
|
||||||
j.stderr.Reset()
|
|
||||||
// make cmd write to both standard output and output buffers for lua access
|
|
||||||
j.handle.Stdout = io.MultiWriter(j.cmdout, j.stdout)
|
|
||||||
j.handle.Stderr = io.MultiWriter(j.cmderr, j.stderr)
|
|
||||||
|
|
||||||
if !j.once {
|
|
||||||
j.once = true
|
|
||||||
}
|
|
||||||
|
|
||||||
err := j.handle.Start()
|
|
||||||
proc := j.getProc()
|
|
||||||
|
|
||||||
j.pid = proc.Pid
|
|
||||||
j.running = true
|
|
||||||
|
|
||||||
hooks.Emit("job.start", rt.UserDataValue(j.ud))
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) stop() {
|
|
||||||
// finish will be called in exec handle
|
|
||||||
proc := j.getProc()
|
|
||||||
if proc != nil {
|
|
||||||
proc.Kill()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) finish() {
|
|
||||||
j.running = false
|
|
||||||
hooks.Emit("job.done", rt.UserDataValue(j.ud))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) wait() {
|
|
||||||
j.handle.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) setHandle(handle *exec.Cmd) {
|
|
||||||
j.handle = handle
|
|
||||||
j.args = handle.Args
|
|
||||||
j.path = handle.Path
|
|
||||||
if handle.Stdout != nil {
|
|
||||||
j.cmdout = handle.Stdout
|
|
||||||
}
|
|
||||||
if handle.Stderr != nil {
|
|
||||||
j.cmderr = handle.Stderr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) getProc() *os.Process {
|
|
||||||
handle := j.handle
|
|
||||||
if handle != nil {
|
|
||||||
return handle.Process
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// #member
|
|
||||||
// start()
|
|
||||||
// Starts running the job.
|
|
||||||
func luaStartJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
j, err := jobArg(c, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !j.running {
|
|
||||||
err := j.start()
|
|
||||||
exit := handleExecErr(err)
|
|
||||||
j.exitCode = int(exit)
|
|
||||||
j.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// #member
|
|
||||||
// stop()
|
|
||||||
// Stops the job from running.
|
|
||||||
func luaStopJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
j, err := jobArg(c, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if j.running {
|
|
||||||
j.stop()
|
|
||||||
j.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// #member
|
|
||||||
// foreground()
|
|
||||||
// Puts a job in the foreground. This will cause it to run like it was
|
|
||||||
// executed normally and wait for it to complete.
|
|
||||||
func luaForegroundJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
j, err := jobArg(c, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !j.running {
|
|
||||||
return nil, errors.New("job not running")
|
|
||||||
}
|
|
||||||
|
|
||||||
// lua code can run in other threads and goroutines, so this exists
|
|
||||||
jobs.foreground = true
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
jobs.foreground = false
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// #member
|
|
||||||
// background()
|
|
||||||
// Puts a job in the background. This acts the same as initially running a job.
|
|
||||||
func luaBackgroundJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
j, err := jobArg(c, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
foreground bool // if job currently in the foreground
|
|
||||||
mu *sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func newJobHandler() *jobHandler {
|
|
||||||
return &jobHandler{
|
|
||||||
jobs: make(map[int]*job),
|
|
||||||
latestID: 0,
|
|
||||||
mu: &sync.RWMutex{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *jobHandler) add(cmd string, args []string, path string) *job {
|
|
||||||
j.mu.Lock()
|
|
||||||
defer j.mu.Unlock()
|
|
||||||
|
|
||||||
j.latestID++
|
|
||||||
jb := &job{
|
|
||||||
cmd: cmd,
|
|
||||||
running: false,
|
|
||||||
id: j.latestID,
|
|
||||||
args: args,
|
|
||||||
path: path,
|
|
||||||
cmdout: os.Stdout,
|
|
||||||
cmderr: os.Stderr,
|
|
||||||
stdout: &bytes.Buffer{},
|
|
||||||
stderr: &bytes.Buffer{},
|
|
||||||
}
|
|
||||||
jb.ud = jobUserData(jb)
|
|
||||||
|
|
||||||
j.jobs[j.latestID] = jb
|
|
||||||
hooks.Emit("job.add", rt.UserDataValue(jb.ud))
|
|
||||||
|
|
||||||
return jb
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *jobHandler) getLatest() *job {
|
|
||||||
j.mu.RLock()
|
|
||||||
defer j.mu.RUnlock()
|
|
||||||
|
|
||||||
return j.jobs[j.latestID]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *jobHandler) disown(id int) error {
|
|
||||||
j.mu.RLock()
|
|
||||||
if j.jobs[id] == nil {
|
|
||||||
return errors.New("job doesnt exist")
|
|
||||||
}
|
|
||||||
j.mu.RUnlock()
|
|
||||||
|
|
||||||
j.mu.Lock()
|
|
||||||
delete(j.jobs, id)
|
|
||||||
j.mu.Unlock()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *jobHandler) stopAll() {
|
|
||||||
j.mu.RLock()
|
|
||||||
defer j.mu.RUnlock()
|
|
||||||
|
|
||||||
for _, jb := range j.jobs {
|
|
||||||
// on exit, unix shell should send sighup to all jobs
|
|
||||||
if jb.running {
|
|
||||||
proc := jb.getProc()
|
|
||||||
proc.Signal(syscall.SIGHUP)
|
|
||||||
jb.wait() // waits for program to exit due to sighup
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// background job management
|
|
||||||
/*
|
|
||||||
Manage interactive jobs in Hilbish via Lua.
|
|
||||||
|
|
||||||
Jobs are the name of background tasks/commands. A job can be started via
|
|
||||||
interactive usage or with the functions defined below for use in external runners. */
|
|
||||||
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
|
|
||||||
jobMethods := rt.NewTable()
|
|
||||||
jFuncs := map[string]util.LuaExport{
|
|
||||||
"stop": {luaStopJob, 1, false},
|
|
||||||
"start": {luaStartJob, 1, false},
|
|
||||||
"foreground": {luaForegroundJob, 1, false},
|
|
||||||
"background": {luaBackgroundJob, 1, false},
|
|
||||||
}
|
|
||||||
util.SetExports(l, jobMethods, jFuncs)
|
|
||||||
|
|
||||||
jobMeta := rt.NewTable()
|
|
||||||
jobIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
j, _ := jobArg(c, 0)
|
|
||||||
|
|
||||||
arg := c.Arg(1)
|
|
||||||
val := jobMethods.Get(arg)
|
|
||||||
|
|
||||||
if val != rt.NilValue {
|
|
||||||
return c.PushingNext1(t.Runtime, val), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
keyStr, _ := arg.TryString()
|
|
||||||
|
|
||||||
switch keyStr {
|
|
||||||
case "cmd": val = rt.StringValue(j.cmd)
|
|
||||||
case "running": val = rt.BoolValue(j.running)
|
|
||||||
case "id": val = rt.IntValue(int64(j.id))
|
|
||||||
case "pid": val = rt.IntValue(int64(j.pid))
|
|
||||||
case "exitCode": val = rt.IntValue(int64(j.exitCode))
|
|
||||||
case "stdout": val = rt.StringValue(string(j.stdout.Bytes()))
|
|
||||||
case "stderr": val = rt.StringValue(string(j.stderr.Bytes()))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, val), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
jobMeta.Set(rt.StringValue("__index"), rt.FunctionValue(rt.NewGoFunction(jobIndex, "__index", 2, false)))
|
|
||||||
l.SetRegistry(jobMetaKey, rt.TableValue(jobMeta))
|
|
||||||
|
|
||||||
jobFuncs := map[string]util.LuaExport{
|
|
||||||
"all": {j.luaAllJobs, 0, false},
|
|
||||||
"last": {j.luaLastJob, 0, false},
|
|
||||||
"get": {j.luaGetJob, 1, false},
|
|
||||||
"add": {j.luaAddJob, 3, false},
|
|
||||||
"disown": {j.luaDisownJob, 1, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
luaJob := rt.NewTable()
|
|
||||||
util.SetExports(rtm, luaJob, jobFuncs)
|
|
||||||
|
|
||||||
return luaJob
|
|
||||||
}
|
|
||||||
|
|
||||||
func jobArg(c *rt.GoCont, arg int) (*job, error) {
|
|
||||||
j, ok := valueToJob(c.Arg(arg))
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("#%d must be a job", arg + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return j, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func valueToJob(val rt.Value) (*job, bool) {
|
|
||||||
u, ok := val.TryUserData()
|
|
||||||
if !ok {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
j, ok := u.Value().(*job)
|
|
||||||
return j, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func jobUserData(j *job) *rt.UserData {
|
|
||||||
jobMeta := l.Registry(jobMetaKey)
|
|
||||||
return rt.NewUserData(j, jobMeta.AsTable())
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// get(id) -> @Job
|
|
||||||
// Get a job object via its ID.
|
|
||||||
// --- @param id number
|
|
||||||
// --- @returns Job
|
|
||||||
func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
j.mu.RLock()
|
|
||||||
defer j.mu.RUnlock()
|
|
||||||
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
jobID, err := c.IntArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
job := j.jobs[int(jobID)]
|
|
||||||
if job == nil {
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.UserDataValue(job.ud)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// add(cmdstr, args, execPath)
|
|
||||||
// Creates a new job. This function does not run the job. This function is intended to be
|
|
||||||
// used by runners, but can also be used to create jobs via Lua. Commanders cannot be ran as jobs.
|
|
||||||
// #param cmdstr string String that a user would write for the job
|
|
||||||
// #param args table Arguments for the commands. Has to include the name of the command.
|
|
||||||
// #param execPath string Binary to use to run the command. Needs to be an absolute path.
|
|
||||||
/*
|
|
||||||
#example
|
|
||||||
hilbish.jobs.add('go build', {'go', 'build'}, '/usr/bin/go')
|
|
||||||
#example
|
|
||||||
*/
|
|
||||||
func (j *jobHandler) luaAddJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.CheckNArgs(3); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cmd, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
largs, err := c.TableArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
execPath, err := c.StringArg(2)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var args []string
|
|
||||||
util.ForEach(largs, func(k rt.Value, v rt.Value) {
|
|
||||||
if v.Type() == rt.StringType {
|
|
||||||
args = append(args, v.AsString())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
jb := j.add(cmd, args, execPath)
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.UserDataValue(jb.ud)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// all() -> table[@Job]
|
|
||||||
// Returns a table of all job objects.
|
|
||||||
// #returns table[Job]
|
|
||||||
func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
j.mu.RLock()
|
|
||||||
defer j.mu.RUnlock()
|
|
||||||
|
|
||||||
jobTbl := rt.NewTable()
|
|
||||||
for id, job := range j.jobs {
|
|
||||||
jobTbl.Set(rt.IntValue(int64(id)), rt.UserDataValue(job.ud))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(jobTbl)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// disown(id)
|
|
||||||
// Disowns a job. This simply deletes it from the list of jobs without stopping it.
|
|
||||||
// #param id number
|
|
||||||
func (j *jobHandler) luaDisownJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
jobID, err := c.IntArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = j.disown(int(jobID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface jobs
|
|
||||||
// last() -> @Job
|
|
||||||
// Returns the last added job to the table.
|
|
||||||
// #returns Job
|
|
||||||
func (j *jobHandler) luaLastJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
j.mu.RLock()
|
|
||||||
defer j.mu.RUnlock()
|
|
||||||
|
|
||||||
job := j.jobs[j.latestID]
|
|
||||||
if job == nil { // incase we dont have any jobs yet
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.UserDataValue(job.ud)), nil
|
|
||||||
}
|
|
38
job_unix.go
|
@ -1,38 +0,0 @@
|
||||||
//go:build unix
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (j *job) foreground() error {
|
|
||||||
if jobs.foreground {
|
|
||||||
return errors.New("(another) job already foregrounded")
|
|
||||||
}
|
|
||||||
|
|
||||||
pgid, _ := syscall.Getpgid(j.pid)
|
|
||||||
// tcsetpgrp
|
|
||||||
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, pgid)
|
|
||||||
proc, _ := os.FindProcess(j.pid)
|
|
||||||
proc.Wait()
|
|
||||||
|
|
||||||
hshPgid, _ := syscall.Getpgid(os.Getpid())
|
|
||||||
unix.IoctlSetPointerInt(0, unix.TIOCSPGRP, hshPgid)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) background() error {
|
|
||||||
proc := j.handle.Process
|
|
||||||
if proc == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
proc.Signal(syscall.SIGCONT)
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
//go:build windows
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (j *job) foreground() error {
|
|
||||||
return errors.New("not supported on windows")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *job) background() error {
|
|
||||||
return errors.New("not supported on windows")
|
|
||||||
}
|
|
|
@ -89,25 +89,21 @@ end
|
||||||
|
|
||||||
ansikit.print = function(text)
|
ansikit.print = function(text)
|
||||||
io.write(ansikit.format(text))
|
io.write(ansikit.format(text))
|
||||||
io.flush()
|
|
||||||
return ansikit
|
return ansikit
|
||||||
end
|
end
|
||||||
|
|
||||||
ansikit.printCode = function(code, terminate)
|
ansikit.printCode = function(code, terminate)
|
||||||
io.write(ansikit.getCode(code, terminate))
|
io.write(ansikit.getCode(code, terminate))
|
||||||
io.flush()
|
|
||||||
return ansikit
|
return ansikit
|
||||||
end
|
end
|
||||||
|
|
||||||
ansikit.printCSI = function(code, endc)
|
ansikit.printCSI = function(code, endc)
|
||||||
io.write(ansikit.getCSI(code, endc))
|
io.write(ansikit.getCSI(code, endc))
|
||||||
io.flush()
|
|
||||||
return ansikit
|
return ansikit
|
||||||
end
|
end
|
||||||
|
|
||||||
ansikit.println = function(text)
|
ansikit.println = function(text)
|
||||||
io.write(ansikit.format(text) .. "\n")
|
print(ansikit.print(text))
|
||||||
io.flush()
|
|
||||||
return ansikit
|
return ansikit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 34a57c964590f89aa065188a588c7b38aff99c28
|
Subproject commit d200fca13df1c47e133b269420223d7e257dbc41
|
73
lua.go
|
@ -4,82 +4,71 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"hilbish/util"
|
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
"hilbish/golibs/commander"
|
"hilbish/golibs/commander"
|
||||||
"hilbish/golibs/fs"
|
"hilbish/golibs/fs"
|
||||||
"hilbish/golibs/terminal"
|
"hilbish/golibs/terminal"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
"github.com/yuin/gopher-lua"
|
||||||
"github.com/arnodel/golua/lib"
|
|
||||||
"github.com/arnodel/golua/lib/debuglib"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var minimalconf = `hilbish.prompt '& '`
|
var minimalconf = `hilbish.prompt '& '`
|
||||||
|
|
||||||
func luaInit() {
|
func luaInit() {
|
||||||
l = rt.New(os.Stdout)
|
l = lua.NewState()
|
||||||
l.PushContext(rt.RuntimeContextDef{
|
l.OpenLibs()
|
||||||
MessageHandler: debuglib.Traceback,
|
|
||||||
})
|
|
||||||
lib.LoadAll(l)
|
|
||||||
setupSinkType(l)
|
|
||||||
|
|
||||||
lib.LoadLibs(l, hilbishLoader)
|
|
||||||
// yes this is stupid, i know
|
// yes this is stupid, i know
|
||||||
util.DoString(l, "hilbish = require 'hilbish'")
|
l.PreloadModule("hilbish", hilbishLoader)
|
||||||
|
l.DoString("hilbish = require 'hilbish'")
|
||||||
|
|
||||||
// Add fs and terminal module module to Lua
|
// Add fs and terminal module module to Lua
|
||||||
f := fs.New(runner)
|
l.PreloadModule("fs", fs.Loader)
|
||||||
lib.LoadLibs(l, f.Loader)
|
l.PreloadModule("terminal", terminal.Loader)
|
||||||
lib.LoadLibs(l, terminal.Loader)
|
|
||||||
|
|
||||||
cmds = commander.New(l)
|
cmds := commander.New()
|
||||||
lib.LoadLibs(l, cmds.Loader)
|
// When a command from Lua is added, register it for use
|
||||||
|
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
|
||||||
hooks = bait.New(l)
|
commands[cmdName] = cmd
|
||||||
hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) {
|
|
||||||
fmt.Println("Error in `error` hook handler:", err)
|
|
||||||
hooks.Off(event, handler)
|
|
||||||
})
|
})
|
||||||
|
cmds.Events.On("commandDeregister", func(cmdName string) {
|
||||||
|
delete(commands, cmdName)
|
||||||
|
})
|
||||||
|
l.PreloadModule("commander", cmds.Loader)
|
||||||
|
|
||||||
lib.LoadLibs(l, hooks.Loader)
|
hooks = bait.New()
|
||||||
|
l.PreloadModule("bait", hooks.Loader)
|
||||||
|
|
||||||
// Add Ctrl-C handler
|
// Add Ctrl-C handler
|
||||||
hooks.On("signal.sigint", func(...interface{}) {
|
hooks.Em.On("signal.sigint", func() {
|
||||||
if !interactive {
|
if !interactive {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
lr.rl.RawInputCallback = func(r []rune) {
|
l.SetGlobal("complete", l.NewFunction(hlcomplete))
|
||||||
hooks.Emit("hilbish.rawInput", string(r))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add more paths that Lua can require from
|
// Add more paths that Lua can require from
|
||||||
_, err := util.DoString(l, "package.path = package.path .. " + requirePaths)
|
l.DoString("package.path = package.path .. " + requirePaths)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "Could not add Hilbish require paths! Libraries will be missing. This shouldn't happen.")
|
|
||||||
}
|
|
||||||
|
|
||||||
err1 := util.DoFile(l, "nature/init.lua")
|
err := l.DoFile("prelude/init.lua")
|
||||||
if err1 != nil {
|
if err != nil {
|
||||||
err2 := util.DoFile(l, preloadPath)
|
err = l.DoFile(preloadPath)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Missing nature module, some functionality and builtins will be missing.")
|
fmt.Fprintln(os.Stderr,
|
||||||
fmt.Fprintln(os.Stderr, "local error:", err1)
|
"Missing preload file, builtins may be missing.")
|
||||||
fmt.Fprintln(os.Stderr, "global install error:", err2)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runConfig(confpath string) {
|
func runConfig(confpath string) {
|
||||||
if !interactive {
|
if !interactive {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := util.DoFile(l, confpath)
|
err := l.DoFile(confpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err, "\nAn error has occured while loading your config! Falling back to minimal default config.")
|
fmt.Fprintln(os.Stderr, err,
|
||||||
util.DoString(l, minimalconf)
|
"\nAn error has occured while loading your config! Falling back to minimal default config.")
|
||||||
|
|
||||||
|
l.DoString(minimalconf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
175
main.go
|
@ -2,47 +2,39 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"hilbish/util"
|
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
"hilbish/golibs/commander"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
"github.com/pborman/getopt"
|
"github.com/pborman/getopt"
|
||||||
|
"github.com/yuin/gopher-lua"
|
||||||
"github.com/maxlandon/readline"
|
"github.com/maxlandon/readline"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
"mvdan.cc/sh/v3/interp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
l *rt.Runtime
|
l *lua.LState
|
||||||
lr *lineReader
|
lr *lineReader
|
||||||
|
|
||||||
luaCompletions = map[string]*rt.Closure{}
|
commands = map[string]*lua.LFunction{}
|
||||||
|
luaCompletions = map[string]*lua.LFunction{}
|
||||||
|
|
||||||
confDir string
|
confDir string
|
||||||
userDataDir string
|
userDataDir string
|
||||||
curuser *user.User
|
curuser *user.User
|
||||||
|
|
||||||
hooks *bait.Bait
|
hooks bait.Bait
|
||||||
cmds *commander.Commander
|
|
||||||
defaultConfPath string
|
defaultConfPath string
|
||||||
defaultHistPath string
|
defaultHistPath string
|
||||||
runner *interp.Runner
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runner, _ = interp.New()
|
|
||||||
curuser, _ = user.Current()
|
curuser, _ = user.Current()
|
||||||
homedir := curuser.HomeDir
|
homedir := curuser.HomeDir
|
||||||
confDir, _ = os.UserConfigDir()
|
confDir, _ = os.UserConfigDir()
|
||||||
|
@ -51,7 +43,7 @@ func main() {
|
||||||
|
|
||||||
// i honestly dont know what directories to use for this
|
// i honestly dont know what directories to use for this
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "linux", "darwin":
|
case "linux":
|
||||||
userDataDir = getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")
|
userDataDir = getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")
|
||||||
default:
|
default:
|
||||||
// this is fine on windows, dont know about others
|
// this is fine on windows, dont know about others
|
||||||
|
@ -60,18 +52,16 @@ func main() {
|
||||||
|
|
||||||
if defaultConfDir == "" {
|
if defaultConfDir == "" {
|
||||||
// we'll add *our* default if its empty (wont be if its changed comptime)
|
// we'll add *our* default if its empty (wont be if its changed comptime)
|
||||||
defaultConfDir = filepath.Join(confDir, "hilbish")
|
defaultConfPath = filepath.Join(confDir, "hilbish", "init.lua")
|
||||||
} else {
|
} else {
|
||||||
// else do ~ substitution
|
// else do ~ substitution
|
||||||
defaultConfDir = filepath.Join(util.ExpandHome(defaultConfDir), "hilbish")
|
defaultConfPath = filepath.Join(strings.Replace(defaultConfDir, "~", homedir, 1), "init.lua")
|
||||||
}
|
}
|
||||||
defaultConfPath = filepath.Join(defaultConfDir, "init.lua")
|
|
||||||
if defaultHistDir == "" {
|
if defaultHistDir == "" {
|
||||||
defaultHistDir = filepath.Join(userDataDir, "hilbish")
|
defaultHistPath = filepath.Join(userDataDir, "hilbish", ".hilbish-history")
|
||||||
} else {
|
} else {
|
||||||
defaultHistDir = filepath.Join(util.ExpandHome(defaultHistDir), "hilbish")
|
defaultHistPath = filepath.Join(strings.Replace(defaultHistDir, "~", homedir, 1), ".hilbish-history")
|
||||||
}
|
}
|
||||||
defaultHistPath = filepath.Join(defaultHistDir, ".hilbish-history")
|
|
||||||
helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags")
|
helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags")
|
||||||
verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version")
|
verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version")
|
||||||
setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path")
|
setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path")
|
||||||
|
@ -95,7 +85,7 @@ func main() {
|
||||||
interactive = true
|
interactive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 || !term.IsTerminal(int(os.Stdin.Fd())) {
|
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
|
||||||
interactive = false
|
interactive = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,26 +103,18 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if *verflag {
|
if *verflag {
|
||||||
fmt.Printf("Hilbish %s\nCompiled with %s\n", getVersion(), runtime.Version())
|
fmt.Printf("Hilbish %s\n", version)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set $SHELL if the user wants to
|
// Set $SHELL if the user wants to
|
||||||
if *setshflag {
|
if *setshflag {
|
||||||
os.Setenv("SHELL", "hilbish")
|
os.Setenv("SHELL", os.Args[0])
|
||||||
|
|
||||||
path, err := exec.LookPath("hilbish")
|
|
||||||
if err == nil {
|
|
||||||
os.Setenv("SHELL", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lr = newLineReader("", false)
|
|
||||||
luaInit()
|
|
||||||
|
|
||||||
go handleSignals()
|
go handleSignals()
|
||||||
|
luaInit()
|
||||||
|
lr = newLineReader("", false)
|
||||||
// If user's config doesn't exixt,
|
// If user's config doesn't exixt,
|
||||||
if _, err := os.Stat(defaultConfPath); os.IsNotExist(err) && *configflag == defaultConfPath {
|
if _, err := os.Stat(defaultConfPath); os.IsNotExist(err) && *configflag == defaultConfPath {
|
||||||
// Read default from current directory
|
// Read default from current directory
|
||||||
|
@ -153,82 +135,66 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
runConfig(*configflag)
|
runConfig(*configflag)
|
||||||
}
|
}
|
||||||
hooks.Emit("hilbish.init")
|
|
||||||
|
|
||||||
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
|
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
|
||||||
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
|
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
text := scanner.Text()
|
text := scanner.Text()
|
||||||
runInput(text, true)
|
runInput(text, text)
|
||||||
}
|
}
|
||||||
exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if *cmdflag != "" {
|
if *cmdflag != "" {
|
||||||
runInput(*cmdflag, true)
|
runInput(*cmdflag, *cmdflag)
|
||||||
}
|
}
|
||||||
|
|
||||||
if getopt.NArgs() > 0 {
|
if getopt.NArgs() > 0 {
|
||||||
luaArgs := rt.NewTable()
|
luaArgs := l.NewTable()
|
||||||
for i, arg := range getopt.Args() {
|
for _, arg := range getopt.Args() {
|
||||||
luaArgs.Set(rt.IntValue(int64(i)), rt.StringValue(arg))
|
luaArgs.Append(lua.LString(arg))
|
||||||
}
|
}
|
||||||
|
|
||||||
l.GlobalEnv().Set(rt.StringValue("args"), rt.TableValue(luaArgs))
|
l.SetGlobal("args", luaArgs)
|
||||||
err := util.DoFile(l, getopt.Arg(0))
|
err := l.DoFile(getopt.Arg(0))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
initialized = true
|
|
||||||
input:
|
input:
|
||||||
for interactive {
|
for interactive {
|
||||||
|
lr.SetPrompt(fmtPrompt(prompt))
|
||||||
running = false
|
running = false
|
||||||
|
|
||||||
input, err := lr.Read()
|
input, err := lr.Read()
|
||||||
|
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
// Exit if user presses ^D (ctrl + d)
|
// Exit if user presses ^D (ctrl + d)
|
||||||
hooks.Emit("hilbish.exit")
|
hooks.Em.Emit("hilbish.exit")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == readline.CtrlC {
|
if err != readline.CtrlC {
|
||||||
fmt.Println("^C")
|
|
||||||
hooks.Emit("hilbish.cancel")
|
|
||||||
} else {
|
|
||||||
// If we get a completely random error, print
|
// If we get a completely random error, print
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
if errors.Is(err, syscall.ENOTTY) {
|
|
||||||
// what are we even doing here?
|
|
||||||
panic("not a tty")
|
|
||||||
}
|
|
||||||
<-make(chan struct{})
|
|
||||||
}
|
}
|
||||||
|
fmt.Println("^C")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var priv bool
|
oldInput := input
|
||||||
if strings.HasPrefix(input, " ") {
|
|
||||||
priv = true
|
|
||||||
}
|
|
||||||
|
|
||||||
input = strings.TrimSpace(input)
|
input = strings.TrimSpace(input)
|
||||||
if len(input) == 0 {
|
if len(input) == 0 {
|
||||||
running = true
|
hooks.Em.Emit("command.exit", 0)
|
||||||
hooks.Emit("command.exit", 0)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasSuffix(input, "\\") {
|
if strings.HasSuffix(input, "\\") {
|
||||||
print("\n")
|
|
||||||
for {
|
for {
|
||||||
input, err = continuePrompt(strings.TrimSuffix(input, "\\") + "\n", false)
|
input, err = continuePrompt(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
running = true
|
|
||||||
lr.SetPrompt(fmtPrompt(prompt))
|
|
||||||
goto input // continue inside nested loop
|
goto input // continue inside nested loop
|
||||||
}
|
}
|
||||||
if !strings.HasSuffix(input, "\\") {
|
if !strings.HasSuffix(input, "\\") {
|
||||||
|
@ -237,7 +203,7 @@ input:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runInput(input, priv)
|
runInput(input, oldInput)
|
||||||
|
|
||||||
termwidth, _, err := term.GetSize(0)
|
termwidth, _, err := term.GetSize(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -245,28 +211,18 @@ input:
|
||||||
}
|
}
|
||||||
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func continuePrompt(prev string, newline bool) (string, error) {
|
func continuePrompt(prev string) (string, error) {
|
||||||
hooks.Emit("multiline", nil)
|
hooks.Em.Emit("multiline", nil)
|
||||||
lr.SetPrompt(multilinePrompt)
|
lr.SetPrompt(multilinePrompt)
|
||||||
|
|
||||||
cont, err := lr.Read()
|
cont, err := lr.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
cont = strings.TrimSpace(cont)
|
||||||
|
|
||||||
if newline {
|
return prev + strings.TrimSuffix(cont, "\n"), nil
|
||||||
cont = "\n" + cont
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasSuffix(cont, "\\") {
|
|
||||||
cont = strings.TrimSuffix(cont, "\\") + "\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
return prev + cont, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This semi cursed function formats our prompt (obviously)
|
// This semi cursed function formats our prompt (obviously)
|
||||||
|
@ -274,7 +230,9 @@ func fmtPrompt(prompt string) string {
|
||||||
host, _ := os.Hostname()
|
host, _ := os.Hostname()
|
||||||
cwd, _ := os.Getwd()
|
cwd, _ := os.Getwd()
|
||||||
|
|
||||||
cwd = util.AbbrevHome(cwd)
|
if strings.HasPrefix(cwd, curuser.HomeDir) {
|
||||||
|
cwd = "~" + strings.TrimPrefix(cwd, curuser.HomeDir)
|
||||||
|
}
|
||||||
username := curuser.Username
|
username := curuser.Username
|
||||||
// this will be baked into binary since GOOS is a constant
|
// this will be baked into binary since GOOS is a constant
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
|
@ -300,57 +258,8 @@ func fmtPrompt(prompt string) string {
|
||||||
return nprompt
|
return nprompt
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeDupes(slice []string) []string {
|
func handleHistory(cmd string) {
|
||||||
all := make(map[string]bool)
|
lr.AddHistory(cmd)
|
||||||
newSlice := []string{}
|
// TODO: load history again (history shared between sessions like this ye)
|
||||||
for _, item := range slice {
|
|
||||||
if _, val := all[item]; !val {
|
|
||||||
all[item] = true
|
|
||||||
newSlice = append(newSlice, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newSlice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []string, e string) bool {
|
|
||||||
for _, a := range s {
|
|
||||||
if strings.ToLower(a) == strings.ToLower(e) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func exit(code int) {
|
|
||||||
jobs.stopAll()
|
|
||||||
|
|
||||||
// wait for all timers to finish before exiting.
|
|
||||||
// only do that when not interactive
|
|
||||||
if !interactive {
|
|
||||||
timers.wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
os.Exit(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getVersion() string {
|
|
||||||
v := strings.Builder{}
|
|
||||||
|
|
||||||
v.WriteString(ver)
|
|
||||||
if gitBranch != "" && gitBranch != "HEAD" {
|
|
||||||
v.WriteString("-" + gitBranch)
|
|
||||||
}
|
|
||||||
|
|
||||||
if gitCommit != "" {
|
|
||||||
v.WriteString("." + gitCommit)
|
|
||||||
}
|
|
||||||
|
|
||||||
v.WriteString(" (" + releaseName + ")")
|
|
||||||
|
|
||||||
return v.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func cut(slice []string, idx int) []string {
|
|
||||||
return append(slice[:idx], slice[idx + 1:]...)
|
|
||||||
}
|
|
||||||
|
|
93
module.go
|
@ -1,93 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"plugin"
|
|
||||||
|
|
||||||
"hilbish/util"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// #interface module
|
|
||||||
// native module loading
|
|
||||||
// #field paths A list of paths to search when loading native modules. This is in the style of Lua search paths and will be used when requiring native modules. Example: `?.so;?/?.so`
|
|
||||||
/*
|
|
||||||
The hilbish.module interface provides a function to load
|
|
||||||
Hilbish plugins/modules. Hilbish modules are Go-written
|
|
||||||
plugins (see https://pkg.go.dev/plugin) that are used to add functionality
|
|
||||||
to Hilbish that cannot be written in Lua for any reason.
|
|
||||||
|
|
||||||
Note that you don't ever need to use the load function that is here as
|
|
||||||
modules can be loaded with a `require` call like Lua C modules, and the
|
|
||||||
search paths can be changed with the `paths` property here.
|
|
||||||
|
|
||||||
To make a valid native module, the Go plugin has to export a Loader function
|
|
||||||
with a signature like so: `func(*rt.Runtime) rt.Value`.
|
|
||||||
|
|
||||||
`rt` in this case refers to the Runtime type at
|
|
||||||
https://pkg.go.dev/github.com/arnodel/golua@master/runtime#Runtime
|
|
||||||
|
|
||||||
Hilbish uses this package as its Lua runtime. You will need to read
|
|
||||||
it to use it for a native plugin.
|
|
||||||
|
|
||||||
Here is some code for an example plugin:
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Loader(rtm *rt.Runtime) rt.Value {
|
|
||||||
return rt.StringValue("hello world!")
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
This can be compiled with `go build -buildmode=plugin plugin.go`.
|
|
||||||
If you attempt to require and print the result (`print(require 'plugin')`), it will show "hello world!"
|
|
||||||
*/
|
|
||||||
func moduleLoader(rtm *rt.Runtime) *rt.Table {
|
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"load": {moduleLoad, 2, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
mod := rt.NewTable()
|
|
||||||
util.SetExports(rtm, mod, exports)
|
|
||||||
|
|
||||||
return mod
|
|
||||||
}
|
|
||||||
|
|
||||||
// #interface module
|
|
||||||
// load(path)
|
|
||||||
// Loads a module at the designated `path`.
|
|
||||||
// It will throw if any error occurs.
|
|
||||||
// #param path string
|
|
||||||
func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
||||||
if err := c.CheckNArgs(1); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
path, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := plugin.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
value, err := p.Lookup("Loader")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
loader, ok := value.(func(*rt.Runtime) rt.Value)
|
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
val := loader(t.Runtime)
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, val), nil
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
local commander = require 'commander'
|
|
||||||
|
|
||||||
commander.register('bg', function(_, sinks)
|
|
||||||
local job = hilbish.jobs.last()
|
|
||||||
if not job then
|
|
||||||
sinks.out:writeln 'bg: no last job'
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local err = job:background()
|
|
||||||
if err then
|
|
||||||
sinks.out:writeln('bg: ' .. err)
|
|
||||||
return 2
|
|
||||||
end
|
|
||||||
end)
|
|
|
@ -1,31 +0,0 @@
|
||||||
local commander = require 'commander'
|
|
||||||
local fs = require 'fs'
|
|
||||||
|
|
||||||
commander.register('cat', function(args, sinks)
|
|
||||||
local exit = 0
|
|
||||||
|
|
||||||
if #args == 0 then
|
|
||||||
sinks.out:writeln [[
|
|
||||||
usage: cat [file]...]]
|
|
||||||
end
|
|
||||||
|
|
||||||
local chunkSize = 2^13 -- 8K buffer size
|
|
||||||
|
|
||||||
for _, fName in ipairs(args) do
|
|
||||||
local f = io.open(fName)
|
|
||||||
if f == nil then
|
|
||||||
exit = 1
|
|
||||||
sinks.out:writeln(string.format('cat: %s: no such file or directory', fName))
|
|
||||||
goto continue
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
local block = f:read(chunkSize)
|
|
||||||
if not block then break end
|
|
||||||
sinks.out:write(block)
|
|
||||||
end
|
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
io.flush()
|
|
||||||
return exit
|
|
||||||
end)
|
|
|
@ -1,28 +0,0 @@
|
||||||
local bait = require 'bait'
|
|
||||||
local commander = require 'commander'
|
|
||||||
local fs = require 'fs'
|
|
||||||
local dirs = require 'nature.dirs'
|
|
||||||
|
|
||||||
dirs.old = hilbish.cwd()
|
|
||||||
commander.register('cd', function (args, sinks)
|
|
||||||
if #args > 1 then
|
|
||||||
sinks.out:writeln("cd: too many arguments")
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local path = args[1] and args[1] or hilbish.home
|
|
||||||
if path == '-' then
|
|
||||||
path = dirs.old
|
|
||||||
sinks.out:writeln(path)
|
|
||||||
end
|
|
||||||
|
|
||||||
dirs.setOld(hilbish.cwd())
|
|
||||||
dirs.push(path)
|
|
||||||
|
|
||||||
local ok, err = pcall(function() fs.cd(path) end)
|
|
||||||
if not ok then
|
|
||||||
sinks.out:writeln(err)
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
bait.throw('cd', path)
|
|
||||||
end)
|
|