Newshot

scriptura:你的脚本的温馨角落

Translation(model translation, not the source's own text)

scriptura:为你的脚本打造的温馨角落

我创建了一个新的小工具,用来帮助在文件夹树中运行和管理脚本,叫做 scriptura。它基于 sd 背后的理念,是为 fish 用户打造的。

给定一个这样的脚本文件夹:


        $ set -x SCRIPTURA_ROOT ~/scripts
$ tree ~/scripts
~/scripts
├── os
│   └── update
│       ├── linde
│       └── r5 -> linde
└── scriptura
    └── tree
      

如果我输入 scriptura run os update linde,它会运行 ~/scripts/os/update/linde 处的脚本,同时为每个元素提供 Tab 补全。在脚本被解析后按下 Tab,会先添加 --,以清晰地标示命令行中脚本部分的结束,然后使用默认的 shell 文件名补全。


        $ scriptura run os update linde -- <tab>
bin/        flake.nix  LICENSES/  private/    scripts/  system/
flake.lock  lib/       packages/  REUSE.toml  secrets/  user/
      

用法信息提供了关于可以做什么的更多细节。


        $ scriptura
Usage: scriptura <subcommand> <script_name>
SCRIPTURA_ROOT: ~/scripts

Subcommands:

run          Run a script
  help         Display help information
  new          Create new script
  edit         Edit existing script
  which        Display script path
  cat          Display script content
  cmd          Execute shell command from target directory

User Subcommands:

tree         print a tree of files under $SCRIPTURA_ROOT

Scripts:

os ...       -- os commands
      

输入快捷方式

为了减少所需的输入量,我设置了以下别名。


        alias sfm="scriptura"
alias sf="scriptura run"
      

fish 的补全系统能很好地处理这些:sf <tab> 会向我显示要运行的脚本,而 sfm 会向我显示该工具的子命令。

为脚本编写文档

为了辅助文档,scriptura 会查找几个位置:

  • 一个带有 .help 扩展名的相邻文件
  • 脚本内部的注释(目前只支持一种注释语法 #)此外,可以通过添加一个名为 help 的文件来为文件夹编写文档。这些信息随后会在 Tab 补全、通过 help 子命令或在列出脚本时提供(如果命令行未解析到脚本,则是默认操作)。

语言支持

虽然 scriptura 是用 fish 编写的,但脚本不一定非得如此;它只是默认值。scriptura new 会创建带有 fish shebang 的脚本,可以自由编辑。我还计划添加一个命令行选项来设置不同的解释器。

与 sd 的变化

sd 的博客文章版本和发布到 Github 的版本已经有所不同,博客文章版本使用像 sd new 这样的子命令,而 Github 版本对不涉及运行脚本的操作使用标志(例如 sd --new)。我更喜欢子命令风格,至少在有 run 子命令的短别名时是这样。

此外,我还添加了一些新功能。

新功能

new 之后的 Tab 补全

new 子命令在给定脚本名后会调用底层命令的 Tab 补全:


        $ scriptura new mac svc restart -- launchctl k<tab>
kickstart  (Forces an existing service to start)
kill    (Sends a signal to the service instance)
      

在脚本根目录运行临时命令


        $ scriptura cmd tree
      

会以 $SCRIPTURA_ROOT 作为工作目录运行 tree 命令。

自定义子命令

我认为会有用的一个东西是 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
      

动机

最近,我发现了 sd,它旨在帮助在特定目录树下运行和管理脚本。

我不直接使用这个工具有两个原因:

  • 它叫 sd,而这个名称在我的系统上已经被另一个工具占用了:sd(sed 的替代品)。
  • 它没有附带 fish shell 的补全。曾经有一个包含这些补全的 gist,但似乎已经消失了。在我看来,补全是它最大的特性。

另外,我原本期望 fish 的实现会更容易读写,也不需要那么多外部命令调用。

在介绍性的博客文章中,Ian 写道:它并不是一个真正的东西。它更像是一个想法。如果你愿意,你可以一口气自己实现它。

我花了好几口气才完成,但我很享受这个过程。

试一试

访问仓库获取安装说明,其中包括一个 home-manager 模块:

  • git.alin.ovh

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗