scriptura: a cozy corner for your scripts
I’ve created a new little tool to help run and manage scripts in a folder tree, called scriptura. It is based on the idea behind sd and is made for fish users.
我创建了一个名为 scriptura 的小工具,旨在帮助运行和管理文件夹树中的脚本。它基于 sd 的理念,专为 fish shell 用户打造。
Given a script folder like so: $ set -x SCRIPTURA_ROOT ~/scripts $ tree ~/scripts ~/scripts ├── os │ └── update │ ├── linde │ └── r5 -> linde └── scriptura └── tree
假设有一个如下所示的脚本文件夹: $ set -x SCRIPTURA_ROOT ~/scripts $ tree ~/scripts ~/scripts ├── os │ └── update │ ├── linde │ └── r5 -> linde └── scriptura └── tree
If I type in scriptura run os update linde, it will run the script at ~/scripts/os/update/linde, whilst providing tab completion for each element. Pressing tab after a script has been resolved will first add — to clearly show the end of the script part of the command line, then use the default shell filename completion.
如果我输入 scriptura run os update linde,它将运行 ~/scripts/os/update/linde 处的脚本,同时为每个元素提供 Tab 补全。在脚本路径解析完成后按下 Tab 键,会先添加 — 以明确显示命令行中脚本部分的结束,随后使用默认的 shell 文件名补全。
$ scriptura run os update linde —
$ scriptura run os update linde —
The usage information gives more detail on what can be done.
$ scriptura
Usage: scriptura
使用说明提供了更多关于功能细节的信息。 $ scriptura 用法:scriptura <子命令> <脚本名称> SCRIPTURA_ROOT: ~/scripts 子命令: run 运行脚本 help 显示帮助信息 new 创建新脚本 edit 编辑现有脚本 which 显示脚本路径 cat 显示脚本内容 cmd 在目标目录下执行 shell 命令 用户子命令: tree 打印 $SCRIPTURA_ROOT 下的文件树 脚本: os … — os 命令
Typing shortcuts
To reduce the amount of typing required, I have set the following aliases.
alias sfm=“scriptura”
alias sf=“scriptura run”
The fish completion system handles these well: sf
输入快捷方式
为了减少输入量,我设置了以下别名。
alias sfm=“scriptura”
alias sf=“scriptura run”
fish 的补全系统可以很好地处理这些别名:sf
Documenting scripts To aid documentation, scriptura will look in a couple of places: an adjacent file with the .help extension comments inside the script (currently only supports one comment syntax #) Additionally, folders can be documented by adding a file called help. This information is then provided during tab completion, via the help subcommand or when listing scripts (the default action if a the command line does not resolve to a script).
脚本说明文档 为了辅助文档编写,scriptura 会在以下几个地方查找说明: 带有 .help 扩展名的同名文件 脚本内部的注释(目前仅支持 # 这一种注释语法) 此外,可以通过添加名为 help 的文件来为文件夹编写说明。这些信息会在 Tab 补全时、通过 help 子命令调用时,或在列出脚本时(如果命令行未解析为具体脚本时的默认操作)显示。
Language support Whilst scriptura is written in fish, scripts don’t have to be; it’s just the default. scriptura new will create scripts with a fish shebang, which can be freely edited. I also plan to add a command-line option to set a different interpreter.
语言支持 虽然 scriptura 本身是用 fish 编写的,但脚本并不一定要用 fish;那只是默认设置。scriptura new 会创建带有 fish shebang 的脚本,用户可以自由编辑。我还计划添加一个命令行选项来设置不同的解释器。
Changes from sd The blog post version of sd and the version published to Github already differ in that the blog post version uses subcommands like sd new, whereas the Github version uses flags for actions that don’t involve running a script (e.g. sd —new). I prefer the subcommand style, at least when there is a short alias to the run subcommand. Additionally, I’ve added some new features.
与 sd 的区别 博客文章版本的 sd 与发布在 Github 上的版本已经有所不同:博客版本使用像 sd new 这样的子命令,而 Github 版本则对不涉及运行脚本的操作使用标志(例如 sd —new)。我更喜欢子命令风格,至少在有 run 子命令的简短别名时是这样。此外,我还添加了一些新功能。
New features
Tab completion after new
The new subcommand invokes the underlying command’s tab-completion after a script name is given:
$ scriptura new mac svc restart — launchctl k
新功能
new 后的 Tab 补全
new 子命令在给定脚本名称后会调用底层命令的 Tab 补全:
$ scriptura new mac svc restart — launchctl k
Run ad-hoc commands in the script root $ scriptura cmd tree will run the tree command with the working directory set to $SCRIPTURA_ROOT.
在脚本根目录下运行临时命令 $ scriptura cmd tree 将在工作目录设置为 $SCRIPTURA_ROOT 的情况下运行 tree 命令。
Custom subcommands One thing that I thought would be useful is a tree subcommand. However, I don’t want to force that on any other potential users. Whilst I could have settled with the cmd subcommand mentioned in the previous section, I decided I wanted it to be even easier. Scripts under the special scriptura subdirectory are exposed as subcommands, which pass flags to the underlying command and interpret positional arguments as script path components:
自定义子命令 我认为 tree 子命令会很有用,但我不想强加给其他潜在用户。虽然我可以使用上一节提到的 cmd 子命令,但我决定让它变得更简单。位于特殊 scriptura 子目录下的脚本会被暴露为子命令,它们会将标志传递给底层命令,并将位置参数解释为脚本路径组件:
create a new script at ~/scripts/scriptura/tree which just calls tree
$ scriptura new scriptura tree — tree $ scriptura tree . ├── os │ └── update │ ├── linde │ └── r5 -> linde └── scriptura └── tree 4 directories, 3 files $ scriptura tree -L1 os os └── update 2 directories, 0 files
在 ~/scripts/scriptura/tree 创建一个仅调用 tree 的新脚本
$ scriptura new scriptura tree — tree $ scriptura tree . ├── os │ └── update │ ├── linde │ └── r5 -> linde └── scriptura └── tree 4 个目录,3 个文件 $ scriptura tree -L1 os os └── update 2 个目录,0 个文件
Motivation Recently, I found sd, which is designed to help run and manage scripts under a specific directory tree. There are two reasons I’m not using this tool directly: It’s called sd, which is a name that’s already taken on my system by another tool: sd (sed alternative). It doesn’t ship with completions for the fish shell. There was a gist with these, but it seems to have disappeared. The completions are, in my opinion, the biggest feature. Also, I expected a fish implementation to be easier to read and write, as well as not requiring so many external command calls. In the introductory blog post, Ian writes It’s not really a thing. It’s more of an idea. You can implement it yourself in one sitting, if you want.. It took me more than one sitting, but I enjoyed it.
动机 最近,我发现了 sd,它旨在帮助运行和管理特定目录树下的脚本。我不直接使用该工具的原因有两个: 它叫 sd,这个名字在我的系统上已经被另一个工具占用了:sd(sed 的替代品)。 它没有附带 fish shell 的补全功能。曾经有一个 gist 包含这些补全,但似乎已经消失了。 在我看来,补全功能是最大的亮点。此外,我期望用 fish 实现会更易于阅读和编写,且不需要调用那么多外部命令。在介绍性的博客文章中,Ian 写道:“它其实算不上什么东西,更多是一个想法。如果你愿意,可以在一次坐下来就实现它。”我花的时间不止一次,但我很享受这个过程。
Try it out Visit the repository for installation instructions, including a home-manager module: git.alin.ovh codeberg sr.ht Tags: #shell
尝试一下 访问仓库获取安装说明,包括一个 home-manager 模块:git.alin.ovh codeberg sr.ht 标签:#shell