Skip to main content

vtcode_core/instructions/
labels.rs

1use std::path::Path;
2
3use super::{CLAUDE_FILENAME, InstructionScope, InstructionSource, InstructionSourceKind};
4
5pub fn instruction_scope_label(scope: &InstructionScope) -> &'static str {
6    match scope {
7        InstructionScope::User => "user",
8        InstructionScope::Workspace => "workspace",
9        InstructionScope::Custom => "custom",
10    }
11}
12
13pub fn instruction_source_label(source: &InstructionSource) -> String {
14    match source.kind {
15        InstructionSourceKind::Agents => {
16            format!("{} {}", instruction_scope_label(&source.scope), instruction_file_label(&source.path),)
17        }
18        InstructionSourceKind::Extra => {
19            format!("{} extra instructions", instruction_scope_label(&source.scope))
20        }
21        InstructionSourceKind::Rule if source.matched => {
22            format!("{} matched rule", instruction_scope_label(&source.scope))
23        }
24        InstructionSourceKind::Rule => {
25            format!("{} rule", instruction_scope_label(&source.scope))
26        }
27    }
28}
29
30pub(super) fn instruction_file_label(path: &Path) -> &'static str {
31    if path
32        .file_name()
33        .and_then(|name| name.to_str())
34        .is_some_and(|name| name.eq_ignore_ascii_case(CLAUDE_FILENAME))
35    {
36        "CLAUDE"
37    } else {
38        "AGENTS"
39    }
40}
41
42pub fn format_instruction_path(path: &Path, project_root: &Path, home_dir: Option<&Path>) -> String {
43    if let Ok(relative) = path.strip_prefix(project_root) {
44        let display = relative.display().to_string();
45        if !display.is_empty() {
46            return display;
47        }
48
49        if let Some(name) = path.file_name().and_then(|value| value.to_str()) {
50            return name.to_string();
51        }
52    }
53
54    if let Some(home) = home_dir
55        && let Ok(relative) = path.strip_prefix(home)
56    {
57        let display = relative.display().to_string();
58        if display.is_empty() {
59            return "~".to_string();
60        }
61
62        return format!("~/{display}");
63    }
64
65    path.display().to_string()
66}