writ 0.12.0

A hybrid markdown editor combining raw text editing with live inline rendering
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
//! writd - GhostText daemon for writ
//!
//! Listens on port 4001 for GhostText browser extension connections.
//! When a connection arrives, spawns a writ instance to edit the textarea content.

use std::net::SocketAddr;
use std::process::Stdio;

use anyhow::Result;
use futures::{SinkExt, StreamExt};
use notify::{EventKind, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message;

use writ::config::GITHUB_TOKEN_ENV;

const DEFAULT_PORT: u16 = 4001;

/// GhostText protocol handshake response
#[derive(Serialize)]
struct HandshakeResponse {
    #[serde(rename = "ProtocolVersion")]
    protocol_version: u32,
    #[serde(rename = "WebSocketPort")]
    websocket_port: u16,
}

/// Message from browser to editor
#[derive(Deserialize, Debug)]
struct ClientMessage {
    title: Option<String>,
    url: Option<String>,
    text: String,
    #[allow(dead_code)]
    selections: Option<Vec<Selection>>,
}

/// Extract owner/repo from a GitHub URL.
/// Handles URLs like:
/// - https://github.com/owner/repo/issues/123
/// - https://github.com/owner/repo/pull/456
/// - https://github.com/owner/repo
fn parse_github_repo_from_url(url: &str) -> Option<String> {
    let url = url
        .strip_prefix("https://github.com/")
        .or_else(|| url.strip_prefix("http://github.com/"))?;

    let mut parts = url.split('/');
    let owner = parts.next()?;
    let repo = parts.next()?;

    if owner.is_empty() || repo.is_empty() {
        return None;
    }

    Some(format!("{}/{}", owner, repo))
}

/// Extract owner/repo from a GitHub page title.
/// Handles titles like:
/// - "Issue title · Issue #123 · owner/repo"
/// - "PR title · Pull Request #456 · owner/repo"
fn parse_github_repo_from_title(title: &str) -> Option<String> {
    // GitHub titles end with " · owner/repo" or " · owner/repo · GitHub"
    // Split by " · " and look for owner/repo pattern (search from end)
    let parts: Vec<&str> = title.split(" · ").collect();
    for part in parts.iter().rev() {
        let part = part.trim();
        // Skip "GitHub" suffix
        if part == "GitHub" {
            continue;
        }
        // Check if it looks like owner/repo
        if let Some((owner, repo)) = part.split_once('/')
            && !owner.is_empty()
            && !repo.is_empty()
            && owner.chars().all(|c| c.is_alphanumeric() || c == '-')
            && repo
                .chars()
                .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '.')
        {
            return Some(format!("{}/{}", owner, repo));
        }
    }
    None
}

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

    #[test]
    fn test_parse_github_repo_from_url() {
        // Issue URL
        assert_eq!(
            parse_github_repo_from_url("https://github.com/owner/repo/issues/123"),
            Some("owner/repo".to_string())
        );

        // PR URL
        assert_eq!(
            parse_github_repo_from_url("https://github.com/owner/repo/pull/456"),
            Some("owner/repo".to_string())
        );

        // Repo root
        assert_eq!(
            parse_github_repo_from_url("https://github.com/owner/repo"),
            Some("owner/repo".to_string())
        );

        // Non-GitHub URL
        assert_eq!(
            parse_github_repo_from_url("https://gitlab.com/owner/repo"),
            None
        );

        // Invalid URL
        assert_eq!(parse_github_repo_from_url("not a url"), None);
    }

    #[test]
    fn test_parse_github_repo_from_title() {
        // PR title format
        assert_eq!(
            parse_github_repo_from_title(
                "`wit-parser`: Add validation hooks · Pull Request #2419 · bytecodealliance/wasm-tools"
            ),
            Some("bytecodealliance/wasm-tools".to_string())
        );

        // Issue title format
        assert_eq!(
            parse_github_repo_from_title("Bug report · Issue #123 · rust-lang/rust"),
            Some("rust-lang/rust".to_string())
        );

        // With GitHub suffix
        assert_eq!(
            parse_github_repo_from_title("Some title · owner/repo · GitHub"),
            Some("owner/repo".to_string())
        );

        // Repo with dots and underscores
        assert_eq!(
            parse_github_repo_from_title("Title · Issue #1 · owner/repo.name_here"),
            Some("owner/repo.name_here".to_string())
        );

        // No repo info
        assert_eq!(parse_github_repo_from_title("Just a title"), None);

        // Not a valid owner/repo pattern
        assert_eq!(
            parse_github_repo_from_title("Title · not a repo · something"),
            None
        );
    }
}

/// Message from editor to browser
#[derive(Serialize)]
struct ServerMessage {
    text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    selections: Option<Vec<Selection>>,
}

/// Selection/cursor position (UTF-16 code units)
#[derive(Deserialize, Serialize, Clone, Debug)]
struct Selection {
    start: usize,
    end: usize,
}

#[tokio::main]
async fn main() -> Result<()> {
    let port = DEFAULT_PORT;
    let addr: SocketAddr = ([127, 0, 0, 1], port).into();

    let listener = TcpListener::bind(addr).await?;
    println!("writd listening on http://{}", addr);

    loop {
        let (stream, peer_addr) = listener.accept().await?;

        tokio::spawn(async move {
            if let Err(e) = handle_connection(stream, peer_addr, port).await {
                eprintln!("Connection error from {}: {}", peer_addr, e);
            }
        });
    }
}

async fn handle_connection(mut stream: TcpStream, peer_addr: SocketAddr, port: u16) -> Result<()> {
    let mut buf = [0u8; 1024];
    let n = stream.peek(&mut buf).await?;
    let request = String::from_utf8_lossy(&buf[..n]);

    if request.contains("Upgrade: websocket") || request.contains("upgrade: websocket") {
        let ws_stream = tokio_tungstenite::accept_async(stream).await?;
        handle_websocket(ws_stream, peer_addr).await?;
    } else {
        let mut request_buf = vec![0u8; 1024];
        let _ = stream.read(&mut request_buf).await?;

        let response = HandshakeResponse {
            protocol_version: 1,
            websocket_port: port,
        };
        let json = serde_json::to_string(&response)?;

        let http_response = format!(
            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
            json.len(),
            json
        );

        stream.write_all(http_response.as_bytes()).await?;
    }

    Ok(())
}

async fn handle_websocket(
    ws_stream: tokio_tungstenite::WebSocketStream<TcpStream>,
    peer_addr: SocketAddr,
) -> Result<()> {
    let (mut write, mut read) = ws_stream.split();

    let initial_msg = match read.next().await {
        Some(Ok(Message::Text(text))) => text,
        Some(Ok(msg)) => {
            eprintln!("Unexpected message type: {:?}", msg);
            return Ok(());
        }
        Some(Err(e)) => return Err(e.into()),
        None => return Ok(()),
    };

    let client_msg: ClientMessage = serde_json::from_str(&initial_msg)?;
    println!(
        "GhostText message: title={:?}, url={:?}, text_len={}",
        client_msg.title,
        client_msg.url,
        client_msg.text.len()
    );
    let title = client_msg
        .title
        .clone()
        .unwrap_or_else(|| "ghosttext".to_string());

    let temp_dir = std::env::temp_dir();
    let sanitized_title: String = title
        .chars()
        .map(|c| if c.is_alphanumeric() { c } else { '_' })
        .take(50)
        .collect();
    let temp_file = temp_dir.join(format!(
        "ghosttext-{}-{}.md",
        sanitized_title,
        peer_addr.port()
    ));

    std::fs::write(&temp_file, &client_msg.text)?;
    println!("Created temp file: {:?}", temp_file);

    let (file_tx, mut file_rx) = mpsc::channel::<String>(16);
    let watch_path = temp_file.clone();

    let mut watcher = notify::recommended_watcher(move |res: Result<notify::Event, _>| {
        if let Ok(event) = res
            && matches!(event.kind, EventKind::Modify(_) | EventKind::Create(_))
            && let Ok(content) = std::fs::read_to_string(&watch_path)
        {
            let _ = file_tx.blocking_send(content);
        }
    })?;

    watcher.watch(&temp_file, RecursiveMode::NonRecursive)?;

    // Build writ command with optional GitHub context
    let mut cmd = Command::new("writ");
    cmd.arg("--file").arg(&temp_file).arg("--autosave");

    // Pass GitHub repo context if URL is a GitHub page or title contains repo info
    let github_repo = client_msg
        .url
        .as_ref()
        .and_then(|url| parse_github_repo_from_url(url))
        .or_else(|| {
            client_msg
                .title
                .as_ref()
                .and_then(|title| parse_github_repo_from_title(title))
        });

    if let Some(repo) = github_repo {
        cmd.arg("--github-repo").arg(&repo);
        println!("Detected GitHub repo: {}", repo);
    } else {
        println!("Could not detect GitHub repo from URL or title");
    }

    // Pass GitHub token if available in environment
    let has_github_token = if let Ok(token) = std::env::var(GITHUB_TOKEN_ENV) {
        cmd.arg("--github-token").arg(&token);
        println!(
            "Passing {} ({}... {} chars)",
            GITHUB_TOKEN_ENV,
            &token[..token.len().min(8)],
            token.len()
        );
        true
    } else {
        println!("{} not set in environment", GITHUB_TOKEN_ENV);
        false
    };

    // Log the full command being executed
    println!(
        "Starting writ: --file {:?} --autosave{}{}",
        temp_file,
        if client_msg.url.is_some() {
            " --github-repo ..."
        } else {
            ""
        },
        if has_github_token {
            " --github-token ..."
        } else {
            ""
        }
    );

    let mut child = cmd
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .spawn()?;

    println!("Spawned writ process (pid: {:?})", child.id());

    let mut last_content = client_msg.text.clone();

    loop {
        tokio::select! {
            Some(content) = file_rx.recv() => {
                if content != last_content {
                    last_content = content.clone();
                    let msg = ServerMessage {
                        text: content,
                        selections: None,
                    };
                    let json = serde_json::to_string(&msg)?;
                    if write.send(Message::Text(json.into())).await.is_err() {
                        break;
                    }
                }
            }

            Some(msg) = read.next() => {
                match msg {
                    Ok(Message::Text(text)) => {
                        if let Ok(client_msg) = serde_json::from_str::<ClientMessage>(&text)
                            && client_msg.text != last_content
                        {
                            last_content = client_msg.text.clone();
                            std::fs::write(&temp_file, &client_msg.text)?;
                        }
                    }
                    Ok(Message::Close(_)) => break,
                    Err(_) => break,
                    _ => {}
                }
            }

            status = child.wait() => {
                println!("writ process exited with {:?} for {:?}", status, temp_file);
                if let Ok(content) = std::fs::read_to_string(&temp_file)
                    && content != last_content
                {
                    let msg = ServerMessage {
                        text: content,
                        selections: None,
                    };
                    let json = serde_json::to_string(&msg)?;
                    let _ = write.send(Message::Text(json.into())).await;
                }
                break;
            }
        }
    }

    drop(watcher);
    let _ = std::fs::remove_file(&temp_file);
    println!("Cleaned up session for {}", peer_addr);

    Ok(())
}