Skip to main content

ปรับแต่ง Status Line

กำหนดค่า status bar แบบกำหนดเองเพื่อตรวจสอบ context window usage, costs และ git status ใน Claude Code

Status line คือแถบที่ปรับแต่งได้ที่อยู่ด้านล่างของ Claude Code ซึ่งรัน shell script ใด ๆ ที่คุณกำหนดค่า มันรับข้อมูล JSON session ผ่าน stdin และแสดงสิ่งที่ script ของคุณพิมพ์ออกมา ทำให้คุณมีมุมมองแบบ at-a-glance ของ context usage, costs, git status หรืออื่น ๆ ที่ต้องการติดตาม

Status lines มีประโยชน์เมื่อ:

  • ต้องการตรวจสอบ context window usage ขณะทำงาน
  • ต้องการติดตาม session costs
  • ทำงานข้ามหลาย sessions และต้องการแยกแยะระหว่างกัน
  • ต้องการเห็น git branch และ status ตลอดเวลา

ตั้งค่า Status Line

ใช้คำสั่ง /statusline เพื่อให้ Claude Code สร้าง script ให้คุณ หรือสร้าง script เองและเพิ่มลงใน settings

ใช้คำสั่ง /statusline

คำสั่ง /statusline รับคำสั่งในภาษาธรรมชาติที่อธิบายสิ่งที่ต้องการแสดง Claude Code สร้างไฟล์ script ใน ~/.claude/ และอัปเดต settings โดยอัตโนมัติ:

/statusline show model name and context percentage with a progress bar

กำหนดค่า Status Line ด้วยตนเอง

เพิ่ม field statusLine ใน user settings (~/.claude/settings.json) หรือ project settings ตั้ง type เป็น "command" และชี้ command ไปยัง script path หรือ inline shell command:

{
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.sh",
"padding": 2
}
}

field command รันใน shell ดังนั้นคุณสามารถใช้ inline commands แทน script file ตัวอย่างนี้ใช้ jq เพื่อ parse JSON input และแสดง model name และ context percentage:

{
"statusLine": {
"type": "command",
"command": "jq -r '\"[\\(.model.display_name)] \\(.context_window.used_percentage // 0)% context\"'"
}
}

Field ที่ optional อื่น ๆ:

  • padding: เพิ่ม horizontal spacing (ตัวอักษร)
  • refreshInterval: re-run command ทุก N วินาที (ขั้นต่ำ 1)
  • hideVimModeIndicator: ซ่อน -- INSERT -- ที่ built-in

สร้าง Status Line แบบ Step by Step

ตัวอย่างนี้สร้าง status line ที่แสดง current model, working directory และ context window usage percentage

ขั้นที่ 1: สร้าง script

บันทึกไปที่ ~/.claude/statusline.sh:

#!/bin/bash
# Read JSON data that Claude Code sends to stdin
input=$(cat)

# Extract fields using jq
MODEL=$(echo "$input" | jq -r '.model.display_name')
DIR=$(echo "$input" | jq -r '.workspace.current_dir')
# The "// 0" provides a fallback if the field is null
PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d. -f1)

# Output the status line - ${DIR##*/} extracts just the folder name
echo "[$MODEL] 📁 ${DIR##*/} | ${PCT}% context"

ขั้นที่ 2: ทำให้ executable

chmod +x ~/.claude/statusline.sh

ขั้นที่ 3: เพิ่มใน settings

{
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.sh"
}
}

วิธีการทำงานของ Status Lines

Claude Code รัน script และ pipe ข้อมูล JSON session ให้ผ่าน stdin Script ของคุณอ่าน JSON, ดึงข้อมูลที่ต้องการ และพิมพ์ข้อความออก stdin Claude Code แสดงสิ่งที่ script พิมพ์

เมื่อ update: Script รันหลัง assistant message ใหม่ หลัง /compact เสร็จ เมื่อ permission mode เปลี่ยน หรือเมื่อ vim mode toggle

สิ่งที่ script สามารถ output:

  • หลายบรรทัด: แต่ละ echo แสดงเป็นแถวแยก
  • สี: ใช้ ANSI escape codes เช่น \033[32m สำหรับสีเขียว
  • Links: ใช้ OSC 8 escape sequences เพื่อทำให้ข้อความคลิกได้

ขนาด: อ่าน environment variables COLUMNS และ LINES (Claude Code ตั้งค่าไว้ก่อนรัน script)

ข้อมูลที่ใช้ได้

Claude Code ส่ง JSON fields เหล่านี้ไปยัง script ผ่าน stdin:

Fieldคำอธิบาย
model.id, model.display_nameModel identifier และ display name ปัจจุบัน
cwd, workspace.current_dirCurrent working directory
workspace.project_dirDirectory ที่เปิด Claude Code
workspace.added_dirsDirectories เพิ่มเติมจาก /add-dir
workspace.git_worktreeGit worktree name เมื่ออยู่ใน linked worktree
workspace.repo.host, .owner, .nameRepository identity
cost.total_cost_usdค่าใช้จ่าย session โดยประมาณเป็น USD
cost.total_duration_msเวลาทั้งหมดตั้งแต่ session เริ่ม
cost.total_lines_added, cost.total_lines_removedบรรทัดโค้ดที่เปลี่ยน
context_window.total_input_tokensToken counts ใน context window
context_window.context_window_sizeขนาด context window สูงสุด
context_window.used_percentageเปอร์เซ็นต์ที่ใช้แล้ว
context_window.remaining_percentageเปอร์เซ็นต์ที่เหลือ
effort.levelReasoning effort ปัจจุบัน
thinking.enabledเปิดใช้ extended thinking หรือไม่
rate_limits.five_hour.used_percentageเปอร์เซ็นต์ rate limit 5 ชั่วโมง
rate_limits.seven_day.used_percentageเปอร์เซ็นต์ rate limit 7 วัน
session_idSession identifier
session_nameCustom session name
vim.modeVim mode ปัจจุบัน (NORMAL, INSERT, VISUAL)
pr.number, pr.urlPull request ที่เปิดอยู่สำหรับ branch ปัจจุบัน
pr.review_stateสถานะ review: approved, pending, changes_requested, draft
worktree.name, .path, .branchข้อมูล worktree

Context Window Fields

context_window.current_usage มี fields:

  • input_tokens, output_tokens
  • cache_creation_input_tokens, cache_read_input_tokens

ตัวอย่าง

แสดงหลายบรรทัด

#!/bin/bash
input=$(cat)
MODEL=$(echo "$input" | jq -r '.model.display_name')
DIR=$(echo "$input" | jq -r '.workspace.current_dir')
BRANCH=$(cd "$DIR" && git branch --show-current 2>/dev/null || echo "no-git")
PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d. -f1)
COST=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')

# บรรทัดที่ 1: model และ git info
echo "[$MODEL] 📁 ${DIR##*/} | branch: $BRANCH"

# บรรทัดที่ 2: context bar
echo "${PCT}% context | cost: \$$(printf '%.4f' $COST)"

Git Status พร้อมสี

#!/bin/bash
input=$(cat)
DIR=$(echo "$input" | jq -r '.workspace.current_dir')
BRANCH=$(cd "$DIR" && git branch --show-current 2>/dev/null)

if [ -n "$BRANCH" ]; then
# สีเขียวสำหรับ branch ที่ clean
CHANGES=$(cd "$DIR" && git status --porcelain 2>/dev/null | wc -l)
if [ "$CHANGES" -gt 0 ]; then
echo -e "\033[33m[$BRANCH]\033[0m $CHANGES files changed"
else
echo -e "\033[32m[$BRANCH]\033[0m clean"
fi
fi

Windows Configuration

บน Windows ใช้ PowerShell script:

# ~/.claude/statusline.ps1
$input = $input | ConvertFrom-Json
$model = $input.model.display_name
$pct = [int]($input.context_window.used_percentage ?? 0)

Write-Output "[$model] $pct% context"

จากนั้น settings.json:

{
"statusLine": {
"type": "command",
"command": "pwsh -File ~/.claude/statusline.ps1"
}
}

ลบ Status Line

รัน /statusline และขอให้ลบ เช่น /statusline delete หรือลบ field statusLine จาก settings.json ด้วยตนเอง

แหล่งข้อมูลที่เกี่ยวข้อง