cursor-helper 0.2.2

CLI helper for Cursor IDE operations not exposed in the UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! List command - Show all Cursor projects

use anyhow::{Context, Result};
use comfy_table::{presets::UTF8_FULL_CONDENSED, Cell, ContentArrangement, Table};
use percent_encoding::percent_decode_str;
use std::fs;
use std::path::PathBuf;
use std::time::SystemTime;
use url::Url;

use super::utils;
use crate::config;

/// Remote connection type for vscode-remote:// URLs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RemoteType {
    Tunnel,
    SshRemote,
    DevContainer,
    Wsl,
    Unknown(String),
}

impl RemoteType {
    fn parse(s: &str) -> Self {
        match s {
            "tunnel" => Self::Tunnel,
            "ssh-remote" => Self::SshRemote,
            "dev-container" => Self::DevContainer,
            "wsl" => Self::Wsl,
            other => Self::Unknown(other.to_string()),
        }
    }
}

impl std::fmt::Display for RemoteType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Tunnel => write!(f, "tunnel"),
            Self::SshRemote => write!(f, "ssh"),
            Self::DevContainer => write!(f, "container"),
            Self::Wsl => write!(f, "wsl"),
            Self::Unknown(s) => write!(f, "{}", s),
        }
    }
}

/// Remote connection info for vscode-remote:// URLs
#[derive(Debug, Clone)]
pub struct RemoteInfo {
    /// Remote type
    pub remote_type: RemoteType,
    /// Remote host/name
    pub name: String,
}

/// A Cursor project discovered from workspace storage
#[derive(Debug)]
pub struct Project {
    /// The workspace storage folder ID (hash) used by Cursor
    pub folder_id: String,

    /// The project path as stored in workspace.json
    pub path: PathBuf,

    /// Remote connection info (None for local projects)
    pub remote: Option<RemoteInfo>,

    /// When the project was last modified
    pub last_modified: Option<SystemTime>,

    /// Number of chat sessions found
    pub chat_count: usize,
}

/// List all Cursor projects
pub fn list(workspace_storage_dir: PathBuf) -> Result<Vec<Project>> {
    let mut projects = Vec::new();

    if !workspace_storage_dir.exists() {
        return Ok(projects);
    }

    // Read all entries in workspace storage
    let entries = fs::read_dir(&workspace_storage_dir)
        .with_context(|| format!("Failed to read: {}", workspace_storage_dir.display()))?;

    for entry in entries.flatten() {
        // Skip non-directory entries
        if !entry.file_type()?.is_dir() {
            continue;
        }

        let folder_id = entry.file_name().to_string_lossy().to_string();
        let project_dir = entry.path();

        // Try to read workspace.json
        let workspace_json_path = project_dir.join("workspace.json");
        if !workspace_json_path.exists() {
            continue;
        }

        let content = fs::read_to_string(&workspace_json_path)
            .with_context(|| format!("Failed to read: {}", workspace_json_path.display()))?;

        // Parse workspace.json
        let workspace: serde_json::Value = serde_json::from_str(&content)
            .with_context(|| format!("Failed to parse: {}", workspace_json_path.display()))?;

        // workspace.json can have either:
        // - "folder": single-folder project
        // - "workspace": multi-root .code-workspace file
        let folder_url = match workspace.get("folder").and_then(|v| v.as_str()) {
            Some(url) => url,
            None => {
                // Multi-root workspace - skip for now (would need to parse .code-workspace)
                // Could also use workspace.get("workspace") to show the workspace file
                continue;
            }
        };

        // Parse folder URL
        let parsed = match parse_folder_url(folder_url) {
            Some(p) => p,
            None => {
                eprintln!(
                    "Warning: Invalid folder URL in {}: {}",
                    workspace_json_path.display(),
                    folder_url
                );
                continue;
            }
        };

        // Get last modified time
        let last_modified = entry.metadata()?.modified().ok();

        // Count chat sessions (default to 0 on error to avoid one bad project
        // breaking the entire list - the project path is still shown)
        let chat_count = utils::count_chat_sessions(&project_dir).unwrap_or(0);

        projects.push(Project {
            folder_id,
            path: parsed.path,
            remote: parsed.remote,
            last_modified,
            chat_count,
        });
    }

    // Sort by last modified (most recent first)
    projects.sort_by(|a, b| {
        b.last_modified
            .cmp(&a.last_modified)
            .then_with(|| a.path.cmp(&b.path))
    });

    Ok(projects)
}

/// Options for the list command
pub struct ListOptions {
    /// Show workspace ID for each project
    pub with_id: bool,
    /// Sort by: name, modified, chats
    pub sort: String,
    /// Reverse sort order
    pub reverse: bool,
    /// Filter: local, remote, or pattern to match path
    pub filter: Option<String>,
    /// Limit number of results
    pub limit: Option<usize>,
}

/// Execute the list command and return formatted output
pub fn execute(options: ListOptions) -> Result<String> {
    let workspace_storage_dir = config::workspace_storage_dir()
        .context("Failed to determine workspace storage directory")?;

    let mut projects = list(workspace_storage_dir)?;

    // Apply filter
    if let Some(ref filter_str) = options.filter {
        projects.retain(|p| {
            let path_str = p.path.to_string_lossy();
            match filter_str.as_str() {
                "local" => p.remote.is_none(),
                "remote" => p.remote.is_some(),
                pattern => path_str.contains(pattern),
            }
        });
    }

    // Apply sorting
    match options.sort.as_str() {
        "name" => {
            projects.sort_by(|a, b| a.path.cmp(&b.path));
        }
        "chats" => {
            projects.sort_by(|a, b| b.chat_count.cmp(&a.chat_count));
        }
        _ => {
            // Default (including "modified"): already sorted by modified in list()
        }
    }

    // Reverse if requested
    if options.reverse {
        projects.reverse();
    }

    // Apply limit
    let total_count = projects.len();
    if let Some(n) = options.limit {
        projects.truncate(n);
    }

    // Build table
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL_CONDENSED)
        .set_content_arrangement(ContentArrangement::Dynamic);

    // Build header
    let mut header = vec![];
    if options.with_id {
        header.push(Cell::new("ID"));
    }
    header.push(Cell::new("Remote"));
    header.push(Cell::new("Path"));
    header.push(Cell::new("Chats"));
    header.push(Cell::new("Modified"));
    table.set_header(header);

    for project in &projects {
        let path_str = project.path.to_string_lossy().to_string();
        let chat_str = project.chat_count.to_string();
        let remote_str = match &project.remote {
            Some(r) => format!("{}:{}", r.remote_type, r.name),
            None => "-".to_string(),
        };
        let modified_str = project
            .last_modified
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| {
                let secs = d.as_secs();
                let dt = chrono::DateTime::from_timestamp(secs as i64, 0).unwrap_or_default();
                dt.format("%Y-%m-%d %H:%M").to_string()
            })
            .unwrap_or_else(|| "-".to_string());

        let mut row = vec![];
        if options.with_id {
            row.push(Cell::new(&project.folder_id));
        }
        row.push(Cell::new(remote_str));
        row.push(Cell::new(path_str));
        row.push(Cell::new(chat_str));
        row.push(Cell::new(modified_str));
        table.add_row(row);
    }

    // Build output
    let mut output = table.to_string();
    if projects.len() < total_count {
        output.push_str(&format!(
            "\n\nShowing {} of {} projects",
            projects.len(),
            total_count
        ));
    } else {
        output.push_str(&format!("\n\n{} projects found", total_count));
    }

    Ok(output)
}

/// Parsed URL result containing path and optional remote info
struct ParsedUrl {
    path: PathBuf,
    remote: Option<RemoteInfo>,
}

/// Convert a file:// or vscode-remote:// URL to a PathBuf with optional remote info
fn parse_folder_url(url_str: &str) -> Option<ParsedUrl> {
    let url = Url::parse(url_str).ok()?;

    match url.scheme() {
        "file" => {
            // file:// URL - local project
            let path = url.to_file_path().ok()?;
            Some(ParsedUrl { path, remote: None })
        }
        "vscode-remote" => {
            // vscode-remote://[type]+[name]/path
            // Complex format: dev-container+{config}@ssh-remote+host/path
            //   - username = dev-container+{config}
            //   - host = ssh-remote+host

            // Check for dev-container (appears as username in URL)
            let username = percent_decode_str(url.username()).decode_utf8_lossy();
            let host_encoded = url.host_str()?;
            let host = percent_decode_str(host_encoded).decode_utf8_lossy();

            let remote = if username.starts_with("dev-container+") {
                // Dev container on underlying remote - use host for the underlying name
                let name = host.split('+').nth(1).unwrap_or("container").to_string();
                Some(RemoteInfo {
                    remote_type: RemoteType::DevContainer,
                    name,
                })
            } else if let Some(plus_pos) = host.find('+') {
                // Simple remote: tunnel+name, ssh-remote+host, etc.
                let remote_type_str = &host[..plus_pos];
                let name = host[plus_pos + 1..].to_string();
                Some(RemoteInfo {
                    remote_type: RemoteType::parse(remote_type_str),
                    name,
                })
            } else {
                None
            };

            let path = PathBuf::from(url.path());
            Some(ParsedUrl { path, remote })
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(not(windows))]
    #[test]
    fn test_parse_local_url() {
        let parsed = parse_folder_url("file:///Users/me/projects/myapp").unwrap();
        assert_eq!(parsed.path, PathBuf::from("/Users/me/projects/myapp"));
        assert!(parsed.remote.is_none());
    }

    #[cfg(not(windows))]
    #[test]
    fn test_parse_local_url_with_spaces() {
        let parsed = parse_folder_url("file:///Users/me/my%20project").unwrap();
        assert_eq!(parsed.path, PathBuf::from("/Users/me/my project"));
        assert!(parsed.remote.is_none());
    }

    #[cfg(windows)]
    #[test]
    fn test_parse_local_url_windows() {
        let parsed = parse_folder_url("file:///C:/Users/me/projects/myapp").unwrap();
        assert_eq!(parsed.path, PathBuf::from("C:\\Users\\me\\projects\\myapp"));
        assert!(parsed.remote.is_none());
    }

    #[cfg(windows)]
    #[test]
    fn test_parse_local_url_with_spaces_windows() {
        let parsed = parse_folder_url("file:///C:/Users/me/my%20project").unwrap();
        assert_eq!(parsed.path, PathBuf::from("C:\\Users\\me\\my project"));
        assert!(parsed.remote.is_none());
    }

    #[test]
    fn test_parse_tunnel_url() {
        let parsed =
            parse_folder_url("vscode-remote://tunnel+myserver/home/user/data/project").unwrap();
        assert_eq!(parsed.path, PathBuf::from("/home/user/data/project"));
        let remote = parsed.remote.unwrap();
        assert_eq!(remote.remote_type, RemoteType::Tunnel);
        assert_eq!(remote.name, "myserver");
    }

    #[test]
    fn test_parse_ssh_remote_url() {
        let parsed =
            parse_folder_url("vscode-remote://ssh-remote+myhost/home/user/project").unwrap();
        assert_eq!(parsed.path, PathBuf::from("/home/user/project"));
        let remote = parsed.remote.unwrap();
        assert_eq!(remote.remote_type, RemoteType::SshRemote);
        assert_eq!(remote.name, "myhost");
    }

    #[test]
    fn test_parse_percent_encoded_tunnel_url() {
        // Real-world format: + is encoded as %2B
        let parsed =
            parse_folder_url("vscode-remote://tunnel%2Bdev-server/home/user/.config/myapp")
                .unwrap();
        assert_eq!(parsed.path, PathBuf::from("/home/user/.config/myapp"));
        let remote = parsed.remote.unwrap();
        assert_eq!(remote.remote_type, RemoteType::Tunnel);
        assert_eq!(remote.name, "dev-server");
    }

    #[test]
    fn test_parse_dev_container_on_ssh() {
        // Dev container running on SSH remote: dev-container+{config}@ssh-remote+host
        let parsed = parse_folder_url(
            "vscode-remote://dev-container%2Bconfig@ssh-remote%2Bwin11-wsl/workspaces/project",
        )
        .unwrap();
        assert_eq!(parsed.path, PathBuf::from("/workspaces/project"));
        let remote = parsed.remote.unwrap();
        assert_eq!(remote.remote_type, RemoteType::DevContainer);
        assert_eq!(remote.name, "win11-wsl");
    }

    #[test]
    fn test_parse_wsl_url() {
        let parsed = parse_folder_url("vscode-remote://wsl+Ubuntu/home/user/project").unwrap();
        assert_eq!(parsed.path, PathBuf::from("/home/user/project"));
        let remote = parsed.remote.unwrap();
        assert_eq!(remote.remote_type, RemoteType::Wsl);
        assert_eq!(remote.name, "Ubuntu");
    }

    #[test]
    fn test_parse_invalid_scheme() {
        // Unknown scheme should return None
        assert!(parse_folder_url("http://example.com/path").is_none());
        assert!(parse_folder_url("ftp://server/path").is_none());
    }

    #[test]
    fn test_parse_invalid_url() {
        // Malformed URL should return None
        assert!(parse_folder_url("not a url at all").is_none());
    }

    #[test]
    fn test_remote_type_parse() {
        assert_eq!(RemoteType::parse("tunnel"), RemoteType::Tunnel);
        assert_eq!(RemoteType::parse("ssh-remote"), RemoteType::SshRemote);
        assert_eq!(RemoteType::parse("dev-container"), RemoteType::DevContainer);
        assert_eq!(RemoteType::parse("wsl"), RemoteType::Wsl);
        assert_eq!(
            RemoteType::parse("unknown-type"),
            RemoteType::Unknown("unknown-type".to_string())
        );
    }

    #[test]
    fn test_remote_type_display() {
        assert_eq!(format!("{}", RemoteType::Tunnel), "tunnel");
        assert_eq!(format!("{}", RemoteType::SshRemote), "ssh");
        assert_eq!(format!("{}", RemoteType::DevContainer), "container");
        assert_eq!(format!("{}", RemoteType::Wsl), "wsl");
        assert_eq!(
            format!("{}", RemoteType::Unknown("custom".to_string())),
            "custom"
        );
    }

    #[test]
    fn test_project_struct_fields() {
        // Verify Project struct can be constructed with all fields
        let project = Project {
            folder_id: "abc123".to_string(),
            path: PathBuf::from("/test/path"),
            remote: Some(RemoteInfo {
                remote_type: RemoteType::Tunnel,
                name: "myserver".to_string(),
            }),
            last_modified: None,
            chat_count: 5,
        };
        assert_eq!(project.folder_id, "abc123");
        assert_eq!(project.chat_count, 5);
        assert!(project.remote.is_some());
    }
}