命令行代码片段管理工具。感谢 knqyf263
制作的原 Pet 工具. 原工具仓库
增加了 Markdown 功能,图片功能。
整个程序只有一个 snippet.toml 文件,一个 config.toml 文件(默认生成在 /home/$USER/.config/pet/)需要保存(直接开一个 github 私有仓库同步即可)。Shell 里面增加一个 alias,alias whatever="pushd ~/.config/pet >/dev/null;git pull;git add *;git commit -m 'new snippet';git push;popd >/dev/null"
。
保存命令或者 Markdown 文字,增加关键字;需要使用的时候,通过关键字搜索(fzf)即可。
目的在于提高效率,无需离开命令行,配合 tmux,无缝获取需要的命令或者信息。
Pet 需要依赖 glow
来在命令行显示 Markdown。需要 feh
来显示图片,并需要 fzf
做模糊搜索。
sudo apt install fzf feh
go install github.com/charmbracelet/glow@latest
详情见安装章节。
从源码编译,或者下载编译好的文件。
-
安装依赖
sudo apt install fzf feh proxychains4 go install github.com/charmbracelet/glow@latest
-
克隆仓库到 GOPATH;
git clone https://github.com/0prrr/pet.git
-
编译
cd pet go build
-
将
pet
二进制移到/usr/local/bin/
mv pet /usr/local/bin
-
查看下文使用方式(增加 Alias),粘贴适用的代码到 shell
.rc
文件;
-
安装依赖
brew install fzf feh xorg-server xinit go install github.com/charmbracelet/glow@latest // Optional brew install proxychains-ng
-
克隆仓库到 GOPATH;
git clone https://github.com/0prrr/pet.git
-
编译
cd pet go build
-
将
pet
二进制移到/usr/local/bin/
mv pet /usr/local/bin
-
查看下文使用方式(增加 Alias),粘贴适用的代码到 shell
.rc
文件;
下载相应平台最新版本(v0.9),解压到 /usr/local/bin/
即可。
Linux, 粘贴下面的代码到 shell 的 .rc
文件。
pets () {
# proxychains 是因为我使用 Imgur 保存我的图片
# 更多关于图片的支持看下文的图片支持章节
# ++G 选项默认打开 less 滑动到文末,不需要可以移除
cmd=`proxychains -q /usr/local/bin/pet search`
if [[ "$cmd" == "#"* ]]
then
echo "$cmd" | glow -s dark - | less -r ++G
echo "$cmd" | glow -s dark -
else
echo "$cmd"
echo "$cmd" | tr -d '\n' | xclip -sel c
fi
}
Windows WSL, 粘贴下面的代码到 shell 的 .rc
文件。
pets () {
# proxychains 是因为我使用 Imgur 保存我的图片
# 更多关于图片的支持看下文的图片支持章节
# ++G 选项默认打开 less 滑动到文末,不需要可以移除
cmd=`proxychains -q /usr/local/bin/pet search`
if [[ "$cmd" == "#"* ]]
then
echo "$cmd" | glow -s dark - | less -r ++G
echo "$cmd" | glow -s dark -
else
echo "$cmd"
echo "$cmd" | tr -d '\n' | clip.exe
fi
}
macOS, 粘贴下面的代码到 shell 的 .rc
文件。
export DISPLAY=:0.0
if ! pgrep -x "xquartz" >/dev/null; then
xquartz &
fi
pets () {
# proxychains 是因为我使用 Imgur 保存我的图片
# 更多关于图片的支持看下文的图片支持章节
# ++G 选项默认打开 less 滑动到文末,不需要可以移除
cmd=`proxychains -q /usr/local/bin/pet search`
if [[ "$cmd" == "#"* ]]
then
echo "$cmd" | glow -s dark - | less -r ++G
echo "$cmd" | glow -s dark -
else
echo "$cmd"
echo "$cmd" | tr -d '\n' | pbcopy
fi
}
运行 source xxx.rc
, 然后使用 pets
运行。
n
代表 normal
模式.
运行 pets
, 输入关键字,回车。
注意 <>
是参数, pet 会要求你填入,填入参数后回车。
命令显示在命令行,并已经复制到剪切板,粘贴运行即可。
在文件中输入 Markdown 内容,保存。
使用 pet new /path/to/file
保存文件中的内容。优点是随时修改。
运行 pets
, 输入关键字,回车。
glow
会将代码片段显示在命令行
截图软件都带上传插件。上传截图到图片服务(如 Imgur),然后以如下格式保存图片链接:
img::https://example.com/5Zhdgesz.png
如下图
然后保存该 Markdown 文件。
可以添加任意数量的图片,一张一行即可。
img::https://exmaple.com/img1.png
img::https://exmaple.com/img2.png
-
使用简单的 Markdown(如下例子),否则
glow
可能会显示不正常。以下 Markdown 语法已经过测试。Header 1: # Title Inline code: `command` Horizontal Sep: ---------- Code block: ``` code here ``` Table: |Col1|Col2| |----|----| |val1|val2|
Markdown 示例:
# This Is the Title ----------- Steps to download file from a URL in command line. 1. Open cmd; 2. Issue the following command: ``` powershell wget https://example.com/fancy.png -o fancy.png ```
Markdown 代码片段都以
#
开头(第一行第一个字符)。 -
单行命令中,尖括 (
<
and>
) 会被解析成参数,pet 会弹出输入框。这对于 PHP 单行命令并不友好。目前的解决方式是,保存 PHP 单行命令成 Markdown,或者在保存单行命令的时候,省略右括号(>
),在使用的时候添上。 -
在参数输入框页面,避免粘贴过长的参数。整个库有些问题,过长的文字格式会出错。建议在粘贴命令之后自行添加。
-
由于 feh 依赖 X11,所以在 mac 必须使用 xquartz 来解决 feh 的运行问题。目前还没有计划使用其他的方式代替 feh。Again, bear with me。
欢迎 issue。
MIT
感谢 Teppei Fukuda (knqyf263) 制作的 Pet Snippet manager. 原仓库
Yet another Pet (or I just call it Pet-Enhanced). Since I haven't changed any of the original code, I will not change the name of the tool.
Much appreciation to knqyf263
who made Pet. Original Pet repo
It's a little command snippet management tool which can be used as a quick reference to concepts and commands in case you forgot. Pretty handy.
All we need to save is two toml files located in ~/.config/pet
. So, open a github repo, make an alias, alias whatever="pushd ~/.config/pet >/dev/null;git pull;git add *;git commit -m 'new snippet';git push;popd >/dev/null"
will suffice.
I added markdown support to the tool and personally use it with glow
, a CLI markdown rendering tool to better format the output. Like this:
I haven't tested all the markdown syntax, just header
, inline code block
and table
. If markdown went wrong, please checkout the glow doc.
NOTE: Please refer to known issues for caveats.
Pet
depends on glow
to handle markdown syntax. And feh
to handle images. Install them first. The default search program is fzf
.
sudo apt install fzf feh
go install github.com/charmbracelet/glow@latest
You can either build pet from source, or download the pre-compiled binary from release.
-
Install dependencies
sudo apt install fzf feh go install github.com/charmbracelet/glow@latest
-
Clone the repo to
GOPATH
on your systemgit clone https://github.com/0prrr/pet.git
-
Compile
go build
-
Move
pet
to/usr/local/bin/pet
mv pet /usr/local/bin
-
Refer to Shell Function section next and add appropriate code to shell's
.rc
file;
-
Install dependencies
brew install fzf feh xorg-server xinit go install github.com/charmbracelet/glow@latest // Optional brew install proxychains-ng
-
Clone the repo to
GOPATH
on your systemgit clone https://github.com/0prrr/pet.git
-
Compile
cd pet go build
-
Move
pet
to/usr/local/bin/
mv pet /usr/local/bin
-
Refer to Shell Function section next and add appropriate code to shell's
.rc
file;
Download the pre-compiled binary and extract to /usr/local/bin/
;
For Linux, put the following scirpt in whatever shell's .rc
file.
pets () {
# proxychains for whatever reason
# refer to image support down below
# ++G is to open less and scroll to bottom, remove if not neede
cmd=`proxychains -q /usr/local/bin/pet search`
if [[ "$cmd" == "#"* ]]
then
echo "$cmd" | glow -s dark - | less -r ++G
echo "$cmd" | glow -s dark -
else
echo "$cmd"
echo "$cmd" | tr -d '\n' | xclip -sel c
fi
}
For Windows WSL, put the following scirpt in whatever shell's .rc
file.
pets () {
# proxychains can be removed according to the image service you are using
# refer to image support in the below section
# ++G is to open less and scroll to bottom, remove if not neede
cmd=`proxychains -q /usr/local/bin/pet search`
if [[ "$cmd" == "#"* ]]
then
echo "$cmd" | glow -s dark - | less -r ++G
echo "$cmd" | glow -s dark -
else
echo "$cmd"
echo "$cmd" | tr -d '\n' | clip.exe
fi
}
For macOS, put the following scirpt in whatever shell's .rc
file.
export DISPLAY=:0.0
if ! pgrep -x "xquartz" >/dev/null; then
xquartz &
fi
pets () {
# proxychains can be removed according to the image service you are using
# refer to image support in the below section
# ++G is to open less and scroll to bottom, remove if not neede
cmd=`proxychains -q /usr/local/bin/pet search`
if [[ "$cmd" == "#"* ]]
then
echo "$cmd" | glow -s dark - | less -r ++G
echo "$cmd" | glow -s dark -
else
echo "$cmd"
echo "$cmd" | tr -d '\n' | pbcopy
fi
}
Run source xxx.rc
, then fire up pets
to run the executable.
n
is for normal
mode.
Issue command pets
, enter keywords to search for your snippet, locate the target snippet, then press Enter
.
Note that texts in <>
are arguments, pet will prompt you for input. Fill in each argument, then press Enter
.
Now, the command is in your clipboard, you can paste the command for execution.
Put your markdown in a file, save it.
Use pet new /path/to/file
to read and add Markdown content from the file.
Issue command pets
, enter keywords to search for your snippet, locate the target snippet, then press Enter
.
The snippet will be displayed by glow
in terminal.
Upload your image to your image service, then copy the URL to the image, save it in pet using the following format:
img::https://example.com/5Zhdgesz.png
just like in the screenshot below.
Then save the markdown file with image.
You can add more than one image to a file, one on each line:
img::https://exmaple.com/img1.png
img::https://exmaple.com/img2.png
Image support added. I personally upload image to imgur, and add in the snippet the keyword img::
followed by the image link, eg:
img::https://i.imgur.com/V3vsK.png
- REQUIREMENTS: Install
feh
sudo apt install feh
And if you have feh
installed, when you search for that piece of snippet, then feh
will automatically display the image, which makes my wiki tools more powerful.
Ability to read markdown content from file is added on Jan, 12th, 2022. Now, for simple markdown snippets, you can fire up pet and input line by line. But for longer snippets with bunch of codes, just put the whole markdown in a file, and issue command
pet new /path/to/file
to add the content to pet.
Enjoy!
-
Keep Markdown as simple as possible, otherwise
glow
will not display them correctly. The following markdowns have been tested:Header 1: # Title Inline code: `command` Horizontal Sep: ---------- Code block: ``` code here ``` Table: |Col1|Col2| |----|----| |val1|val2|
Example Markdown:
# This Is the Title ----------- Steps to download file from a URL in command line. 1. Open cmd; 2. Issue the following command: ``` powershell wget https://example.com/fancy.png -o fancy.png ```
Remember, markdown mode snippets always starts with
#
as first character. -
When saving single-line command, angle brackets (
<
and>
) will be interpreted as arguments needed to command, so pet will prompt you for input. This is not cool for saving PHP oneliner. So, either save PHP oneliner as markdown, or just LEAVE OUT the>
at the end, and add that after pasting the command. -
When prompted for argument, try not to paste in very long text, the text will get messed up somehow. You can try for yourself. This is not going to be fixed in a while, so, bear with me.
-
feh (I'm old) replies on X11 to function. So on macOS, it must be solved with xquartz. feh won't be replaced in anticipated future. Again, bear with me.
Feel free to open issue.
MIT
Again, thanks to Teppei Fukuda (knqyf263) who made Pet Snippet manager. Original Pet repo