Nativefier命令补全脚本:Bash/Zsh/Fish实现

Nativefier命令补全脚本:Bash/Zsh/Fish实现

【免费下载链接】nativefier Make any web page a desktop application 【免费下载链接】nativefier 项目地址: https://gitcode.com/gh_mirrors/na/nativefier

你是否还在为记不住Nativefier的众多命令选项而烦恼?每次使用都要反复查阅帮助文档?本文将为你提供一套完整的命令补全方案,支持Bash、Zsh和Fish三大主流Shell,让你只需按下Tab键就能轻松完成命令输入,大幅提升工作效率。

为什么需要命令补全

Nativefier作为一款强大的网页转桌面应用工具,提供了超过50个命令行选项,涵盖应用创建、窗口设置、浏览器配置等多个方面。手动输入这些选项不仅效率低下,还容易出错。通过命令补全,你可以:

  • 节省记忆成本,无需记住所有选项
  • 减少输入错误,提高命令准确性
  • 加快操作速度,提升工作效率

补全脚本实现原理

命令补全的核心是解析Nativefier的命令行选项并生成对应Shell的补全规则。我们通过分析src/cli.ts文件中的Yargs配置,提取出所有可用选项及其说明,然后转换为各Shell支持的补全格式。

选项分类概览

Nativefier的命令选项可分为多个功能组,以下是主要分类及其代表选项:

选项组代表选项功能描述
App Creation--name, -n应用基本信息配置
App Window--width, --height窗口尺寸和位置设置
Internal Browser--user-agent, -u浏览器行为控制
URL Handling--internal-urls内部链接规则定义
Platform-Specific--counter (macOS), --win32metadata (Windows)平台特有功能

Bash补全脚本

Bash的命令补全通过complete命令实现,我们需要创建一个函数来生成补全选项。

#!/usr/bin/env bash

_nativefier_completions() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    
    # 基础选项
    opts="--name -n --icon -i --platform -p --arch -a --electron-version -e \
--upgrade --portable --widevine --always-on-top --width --height \
--min-width --min-height --max-width --max-height --x --y --zoom \
--user-agent -u --inject --internal-urls --proxy-rules --verbose --help"
    
    # 平台特定选项
    case "$(uname -s)" in
        Darwin)
            opts+=" --counter --bounce --fast-quit -f --title-bar-style"
            ;;
        Linux)
            opts+=" --disable-gpu --enable-es3-apis"
            ;;
        CYGWIN*|MINGW32*|MSYS*|MINGW*)
            opts+=" --win32metadata"
            ;;
    esac

    # 补全逻辑
    if [[ ${cur} == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
    
    # 文件路径补全(针对--icon和--inject等选项)
    case "${prev}" in
        --icon|-i|--inject|--upgrade|--global-shortcuts|--bookmarks-menu)
            COMPREPLY=( $(compgen -f -- ${cur}) )
            return 0
            ;;
    esac
    
    # 默认补全URL或目录
    COMPREPLY=( $(compgen -f -- ${cur}) )
}

complete -F _nativefier_completions nativefier

安装方法

  1. 将上述脚本保存为nativefier-completion.bash
  2. 复制到Bash补全目录:sudo cp nativefier-completion.bash /etc/bash_completion.d/
  3. 立即生效:source /etc/bash_completion.d/nativefier-completion.bash

Zsh补全脚本

Zsh的补全系统更为强大,使用_arguments函数可以定义更复杂的补全规则。

#compdef nativefier

local context state line
typeset -A opt_args

# 定义选项分组
local app_creation=(
    '--name[设置应用名称]:应用名称' \
    '-n[设置应用名称]:应用名称' \
    '--icon[指定应用图标路径]:图标文件:_files' \
    '-i[指定应用图标路径]:图标文件:_files' \
    '--platform[目标平台]:平台:(darwin linux win32)' \
    '-p[目标平台]:平台:(darwin linux win32)' \
    '--arch[CPU架构]:架构:(x64 ia32 armv7l arm64)' \
    '-a[CPU架构]:架构:(x64 ia32 armv7l arm64)' \
    '--electron-version[Electron版本]:版本' \
    '-e[Electron版本]:版本' \
    '--upgrade[升级现有应用]:应用路径:_files' \
    '--portable[便携模式]' \
    '--widevine[启用Widevine支持]'
)

local window_options=(
    '--always-on-top[窗口置顶]' \
    '--width[窗口宽度(像素)]:宽度' \
    '--height[窗口高度(像素)]:高度' \
    '--min-width[最小宽度]:宽度' \
    '--min-height[最小高度]:高度' \
    '--max-width[最大宽度]:宽度' \
    '--max-height[最大高度]:高度' \
    '--x[窗口X坐标]:坐标' \
    '--y[窗口Y坐标]:坐标' \
    '--zoom[缩放比例]:比例'
)

# 平台特定选项
local platform_specific=()
case "$(uname -s)" in
    Darwin)
        platform_specific=(
            '--counter[显示 Dock 计数器]' \
            '--bounce[窗口通知时弹跳 Dock 图标]' \
            '--fast-quit[关闭窗口时退出应用]' \
            '-f[关闭窗口时退出应用]' \
            '--title-bar-style[标题栏样式]:样式:(hidden hiddenInset)'
        )
        ;;
    Linux)
        platform_specific=(
            '--disable-gpu[禁用GPU加速]' \
            '--enable-es3-apis[启用WebGL 2.0]'
        )
        ;;
    CYGWIN*|MINGW32*|MSYS*|MINGW*)
        platform_specific=(
            '--win32metadata[Windows元数据JSON]:JSON字符串'
        )
        ;;
esac

# 组合所有选项
local all_options=(
    $app_creation
    $window_options
    '--user-agent[用户代理字符串]:UA字符串' \
    '-u[用户代理字符串]:UA字符串' \
    '--inject[注入CSS/JS文件]:文件路径:_files' \
    '--internal-urls[内部URL正则]:正则表达式' \
    '--proxy-rules[代理规则]:规则' \
    '--verbose[详细日志]' \
    '--help[显示帮助信息]' \
    $platform_specific
)

_arguments -s \
    ":目标URL:_urls" \
    ":输出目录:_directories" \
    $all_options

安装方法

  1. 将脚本保存为_nativefier
  2. 复制到Zsh函数目录:cp _nativefier ~/.zsh/functions/
  3. .zshrc中添加:fpath=($HOME/.zsh/functions $fpath)
  4. 刷新补全缓存:autoload -Uz compinit && compinit

Fish补全脚本

Fish的补全脚本采用简单的结构化格式,易于编写和维护。

# Nativefier命令补全脚本 for Fish Shell
# 保存为 ~/.config/fish/completions/nativefier.fish

# 基础命令结构
complete -c nativefier -x -a '(__fish_complete_urls)' -d '目标URL'
complete -c nativefier -x -a '(__fish_complete_directories)' -d '输出目录'

# App Creation Options
complete -c nativefier -s n -l name -r -d '设置应用名称'
complete -c nativefier -s i -l icon -r -f -d '指定应用图标路径'
complete -c nativefier -s p -l platform -r -d '目标平台' -a 'darwin linux win32'
complete -c nativefier -s a -l arch -r -d 'CPU架构' -a 'x64 ia32 armv7l arm64'
complete -c nativefier -s e -l electron-version -r -d 'Electron版本'
complete -c nativefier -l upgrade -r -f -d '升级现有应用'
complete -c nativefier -l portable -d '便携模式'
complete -c nativefier -l widevine -d '启用Widevine支持'

# App Window Options
complete -c nativefier -l always-on-top -d '窗口置顶'
complete -c nativefier -l width -r -d '窗口宽度(像素)' -a '1280'
complete -c nativefier -l height -r -d '窗口高度(像素)' -a '800'
complete -c nativefier -l min-width -r -d '最小宽度'
complete -c nativefier -l min-height -r -d '最小高度'
complete -c nativefier -l max-width -r -d '最大宽度'
complete -c nativefier -l max-height -r -d '最大高度'
complete -c nativefier -l x -r -d '窗口X坐标'
complete -c nativefier -l y -r -d '窗口Y坐标'
complete -c nativefier -l zoom -r -d '缩放比例' -a '1.0'

# 平台特定选项
switch (uname)
    case Darwin
        complete -c nativefier -l counter -d '显示 Dock 计数器'
        complete -c nativefier -l bounce -d '窗口通知时弹跳 Dock 图标'
        complete -c nativefier -s f -l fast-quit -d '关闭窗口时退出应用'
        complete -c nativefier -l title-bar-style -r -d '标题栏样式' -a 'hidden hiddenInset'
    case Linux
        complete -c nativefier -l disable-gpu -d '禁用GPU加速'
        complete -c nativefier -l enable-es3-apis -d '启用WebGL 2.0'
    case CYGWIN* MINGW32* MSYS* MINGW*
        complete -c nativefier -l win32metadata -r -d 'Windows元数据JSON'
end

# 其他常用选项
complete -c nativefier -s u -l user-agent -r -d '用户代理字符串'
complete -c nativefier -l inject -r -f -d '注入CSS/JS文件'
complete -c nativefier -l internal-urls -r -d '内部URL正则'
complete -c nativefier -l verbose -d '详细日志'
complete -c nativefier -l help -d '显示帮助信息'

安装方法

  1. 将脚本保存到Fish补全目录:~/.config/fish/completions/nativefier.fish
  2. 无需额外配置,Fish会自动加载补全脚本

使用效果展示

以Zsh为例,当输入nativefier https://example.com --并按下Tab键时,会显示所有可用选项:

--always-on-top     --height           --no-overwrite      --tray             
--arch              --help             --platform          --upgrade          
--background-color  --hide-window-frame --portable          --user-agent       
--basic-auth-password --icon             --proxy-rules       --user-agent-honest
--basic-auth-username --inject           --quiet             --verbose          
--bookmarks-menu    --internal-urls     --single-instance   --version          
--browserwindow-options --lang             --strict-internal-urls --video            
--clear-cache       --max-height        --title-bar-style   --widevine         
--counter           --max-width         --tray              --width            
--crash-reporter    --maximize         --tray              --win32metadata    
--disable-context-menu --min-height        --upgrade           --x                
--disable-dev-tools --min-width         --user-agent        --y                
--disable-gpu       --name              --user-agent-honest --zoom             

当输入nativefier --title-bar-style并按下Tab键时,会显示可用的标题栏样式:

hidden       hiddenInset

总结与扩展

本文介绍了Nativefier在Bash、Zsh和Fish三大Shell中的命令补全实现方法。这些脚本覆盖了大部分常用选项,并根据不同操作系统提供了平台特定选项的补全。

你可以根据自己的需求扩展这些脚本,例如:

  1. 添加更多选项的补全参数(如--browserwindow-options的JSON格式提示)
  2. 增加对自定义配置文件的补全支持
  3. 实现更智能的版本号补全(如Electron版本)

完整的补全脚本代码可在Nativefier项目的contrib/completions目录中找到(假设存在),欢迎贡献和改进。

通过使用这些补全脚本,你将能够更高效地使用Nativefier的强大功能,减少记忆负担,提高工作效率。现在就选择适合你Shell的脚本进行安装,体验命令补全带来的便利吧!

【免费下载链接】nativefier Make any web page a desktop application 【免费下载链接】nativefier 项目地址: https://gitcode.com/gh_mirrors/na/nativefier

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值