Rust终端提示符定制库starship的使用,starship提供跨平台、高度可定化的Shell提示符增强功能

Rust终端提示符定制库starship的使用

Starship是一个跨平台、高度可定制的Shell提示符增强工具,具有以下特点:

  • 极速:运行速度极快
  • 高度可定制:可以配置提示符的每个细节
  • 通用性:支持任何Shell和任何操作系统
  • 智能显示:即时显示相关信息
  • 功能丰富:支持所有常用工具
  • 简单易用:几分钟即可安装使用

Starship演示

安装步骤

前提条件

  • 需要安装并启用Nerd Font(如FiraCode Nerd Font)

1. 安装Starship

根据不同操作系统选择安装方式:

Linux/macOS

curl -sS https://starship.rs/install.sh | sh

Windows

下载MSI安装包或使用包管理器:

choco install starship  # Chocolatey
scoop install starship  # Scoop
winget install Starship.Starship  # winget

其他方式

cargo install starship --locked  # 从crates.io安装

2. 配置Shell使用Starship

根据你的Shell添加相应配置:

Bash

echo 'eval "$(starship init bash)"' >> ~/.bashrc

Zsh

echo 'eval "$(starship init zsh)"' >> ~/.zshrc

Fish

echo 'starship init fish | source' >> ~/.config/fish/config.fish

PowerShell

Add-Content -Path $PROFILE -Value "Invoke-Expression (&starship init powershell)"

3. 配置Starship

启动新的Shell实例即可看到新提示符。如需进一步定制:

  • 查看配置文档
  • 参考预设配置

示例配置

创建一个~/.config/starship.toml文件进行自定义配置:

# 添加命令执行时间显示
[cmd_duration]
min_time = 1000  # 只显示超过1秒的命令
format = " took [$duration]($style) "

# 定制git分支显示
[git_branch]
symbol = "🌱 "
style = "bold purple"

# 定制目录显示
[directory]
truncation_length = 3  # 只显示最后3级目录
truncate_to_repo = false

# 添加自定义模块
[custom.hello]
command = "echo Hello, 世界!"
when = true
format = "[$output]($style bold green)"

这个配置会:

  1. 显示执行时间超过1秒的命令
  2. 用🌱图标显示git分支
  3. 简化目录显示
  4. 添加自定义问候语模块

高级功能

Starship支持许多模块,可以显示:

  • 编程语言版本(Rust、Python、Go等)
  • 容器/Docker信息
  • 电池状态
  • 系统负载
  • 天气预报
# 显示Rust版本
[rust]
symbol = "🦀 "

# 显示Python版本
[python]
symbol = "🐍 "

# 显示Docker上下文
[docker_context]
symbol = "🐳 "
format = "via [$symbol$context]($style) "

完整示例配置

# 基本配置
format = """
$username\
$hostname\
$directory\
$git_branch\
$git_status\
$cmd_duration\
$line_break\
$character"""

# 用户名配置
[username]
style_user = "green bold"
style_root = "red bold"
format = "[$user]($style) "

# 主机名配置
[hostname]
ssh_only = false
format = "on [$hostname]($style) "
style = "blue bold"

# 目录配置
[directory]
truncation_length = 3
truncation_symbol = "…/"
style = "bold cyan"

# Git分支配置
[git_branch]
symbol = "🌱 "
style = "bold purple"

# Git状态配置
[git_status]
style = "bold green"
format = "([$all_status$ahead_behind]($style) )"

# 命令执行时间
[cmd_duration]
min_time = 1000
format = " took [$duration]($style) "
style = "yellow"

# Rust模块配置
[rust]
symbol = "🦀 "
format = "via [$symbol($version)]($style) "
style = "red bold"

# Python模块配置
[python]
symbol = "🐍 "
format = "via [$symbol($version)]($style) "
style = "yellow bold"

# Node.js模块配置
[nodejs]
symbol = "⬢ "
format = "via [$symbol($version)]($style) "
style = "green bold"

# 自定义时间模块
[custom.time]
command = "date +%T"
when = true
format = "[🕒 $output]($style bold blue) "

这个完整配置包含:

  1. 基本提示符布局
  2. 用户名和主机名显示
  3. 目录路径显示
  4. Git分支和状态信息
  5. 命令执行时间
  6. 多种编程语言版本显示
  7. 自定义时间模块

Starship通过其丰富的模块系统和灵活的配置选项,让终端提示符既美观又实用,是提升开发体验的利器。


1 回复

Rust终端提示符定制库starship使用指南

介绍

Starship是一个用Rust编写的轻量级、快速且高度可定制的跨平台shell提示符工具。它支持多种主流shell(Bash、Zsh、Fish、PowerShell等)和操作系统(Linux、macOS、Windows),可以显示各种有用的信息,如Git状态、编程语言版本、执行时间等。

主要特性

  • 跨平台支持
  • 极快的启动速度(Rust编写)
  • 高度可定制
  • 丰富的模块系统
  • 自动上下文感知(只在相关目录显示相关信息)
  • 美观的默认配置

安装方法

使用包管理器安装

# macOS (Homebrew)
brew install starship

# Linux (Snap)
sudo snap install starship

# Windows (Scoop)
scoop install starship

手动安装

# 下载并安装starship
curl -sS https://starship.rs/install.sh | sh

配置shell

安装完成后,需要将starship初始化脚本添加到你的shell配置文件:

# Bash (~/.bashrc)
eval "$(starship init bash)"

# Zsh (~/.zshrc)
eval "$(starship init zsh)"

# Fish (~/.config/fish/config.fish)
starship init fish | source

# PowerShell (添加到profile)
Invoke-Expression (&starship init powershell)

基本使用

安装并配置后,重启终端即可看到starship的默认提示符效果。

配置示例

Starship的配置文件位于~/.config/starship.toml。如果不存在可以创建。

简单配置示例

# 在~/.config/starship.toml中添加

# 修改提示符格式
format = """
$username\
$hostname\
$directory\
$git_branch\
$git_state\
$git_status\
$cmd_duration\
$line_break\
$character"""

# 启用并配置特定模块
[directory]
truncation_length = 3
truncate_to_repo = false

[git_branch]
symbol = "🌱 "
truncation_length = 4

[cmd_duration]
min_time = 1000
format = "underwent [$duration]($style)"

高级配置示例

# 自定义提示符格式和颜色
format = """
[┌─────────────────](bold green)\
$all\
$line_break\
[└─](bold green) $character"""

# 自定义模块
[nodejs]
format = "via [🤖 $version](bold green) "

[python]
format = "via [🐍 $version](bold yellow) "

[rust]
format = "via [🦀 $version](bold red) "

# 自定义提示符字符
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✗](bold red)"

常用模块

Starship提供了许多有用的模块,以下是一些常用模块:

  1. directory - 显示当前目录
  2. git_branch - 显示当前Git分支
  3. git_status - 显示Git仓库状态
  4. nodejs - 显示Node.js版本
  5. python - 显示Python版本
  6. rust - 显示Rust版本
  7. cmd_duration - 显示上一条命令执行时间
  8. jobs - 显示后台任务数量
  9. battery - 显示电池状态
  10. time - 显示当前时间

主题设置

Starship内置了一些主题,可以通过配置快速切换:

# 使用内置主题
[palettes]
my_cool_palette = {
  primary = "green"
  secondary = "blue"
  error = "red"
}

[palette]
inherit = "my_cool_palette"

实用技巧

  1. 快速查看配置效果

    starship explain
    
  2. 预览所有模块

    starship module --list
    
  3. 调试配置

    starship debug-config
    
  4. 临时禁用starship

    starship off
    

完整配置示例

# ~/.config/starship.toml 完整配置示例

# 全局提示符格式
format = """
[╭─](bold cyan)$username@$hostname [](bold cyan)$all\
$line_break\
[╰─](bold cyan) $character"""

# 用户名模块配置
[username]
style_user = "bold green"
style_root = "bold red"
format = "[$user]($style)"
disabled = false
show_always = true

# 主机名模块配置
[hostname]
ssh_only = false
format = "[$hostname]($style) "
trim_at = ".company.com"
disabled = false

# 目录模块配置
[directory]
truncation_length = 3
truncation_symbol = "…/"
style = "bold blue"
read_only = " "
read_only_style = "red"

# Git分支模块配置
[git_branch]
symbol = " "
style = "bold purple"
format = "[$symbol$branch]($style) "
truncation_length = 30

# Git状态模块配置
[git_status]
style = "bold green"
format = "([$all_status$ahead_behind]($style) )"
conflicted = "🏳"
ahead = "⇡${count}"
behind = "⇣${count}"
diverged = "⇕⇡${ahead_count}⇣${behind_count}"
untracked = "?${count}"
stashed = "≡"
modified = "!${count}"
staged = "+${count}"
renamed = "»${count}"
deleted = "✘${count}"

# 编程语言版本模块
[nodejs]
format = "via [ $version](bold green) "
detect_extensions = ["js", "mjs", "cjs", "ts"]
detect_files = ["package.json", ".node-version"]
detect_folders = ["node_modules"]

[python]
format = "via [ $version](bold yellow) "
python_binary = ["python", "python3", "python2"]
detect_extensions = ["py"]
detect_files = ["requirements.txt", "Pipfile", "pyproject.toml"]
detect_folders = []

[rust]
format = "via [ $version](bold red) "
detect_extensions = ["rs"]
detect_files = ["Cargo.toml"]
detect_folders = []

# 命令执行时间模块
[cmd_duration]
min_time = 1000
format = "took [$duration]($style) "
style = "bold yellow"

# 后台任务模块
[jobs]
symbol = "✦"
style = "bold blue"
threshold = 1
format = "[$symbol$number]($style) "

# 电池模块
[battery]
full_symbol = "🔋"
charging_symbol = "⚡"
discharging_symbol = "🪫"
format = "[$symbol$percentage]($style) "
display = [
  { threshold = 30, style = "bold red" },
  { threshold = 80, style = "bold yellow" },
  { style = "bold green" }
]

# 时间模块
[time]
disabled = false
format = "🕙 [$time]($style)"
time_format = "%T"
style = "bold dimmed"

# 自定义字符提示符
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✗](bold red)"
vicmd_symbol = "[❮](bold green)"

# 自定义颜色主题
[palette]
inherit = "default"
primary = "cyan"
secondary = "yellow"
error = "red"

这个完整配置示例包含了大部分常用模块的配置,你可以根据自己的需求进行调整和修改。配置完成后,记得重启终端或重新加载shell配置以使更改生效。

回到顶部