fresh-editor 0.3.5

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/env python3
"""Fresh Remote Agent - bootstrapped via SSH stdin"""
import sys
import os
import json
import base64
import stat
import shutil
import subprocess
import re
import threading
import select
from concurrent.futures import ThreadPoolExecutor

CHUNK = 65536
VERSION = 1

# Active background processes: id -> Popen
procs = {}
# Request IDs marked for cancellation
cancelled = set()
# Lock for thread-safe access to procs/cancelled
lock = threading.Lock()
# Lock for serializing stdout writes (prevents interleaved JSON lines)
write_lock = threading.Lock()


def send(id, **kw):
    """Send a JSON message to stdout (thread-safe)."""
    msg = {"id": id, **kw}
    line = json.dumps(msg, separators=(",", ":")) + "\n"
    with write_lock:
        sys.stdout.write(line)
        sys.stdout.flush()


def b64(data):
    """Encode bytes to base64 string."""
    return base64.b64encode(data).decode("ascii")


def unb64(s):
    """Decode base64 string to bytes."""
    return base64.b64decode(s)


def validate_path(p):
    """Validate and canonicalize a path."""
    if not p:
        raise ValueError("empty path")
    expanded = os.path.expanduser(p)
    if not os.path.isabs(expanded):
        expanded = os.path.abspath(expanded)
    return os.path.realpath(expanded)


# === File Operations ===


def cmd_read(id, p):
    """Read file contents, streaming in chunks for large files."""
    path = validate_path(p["path"])
    off = p.get("off", 0)
    length = p.get("len")

    with open(path, "rb") as f:
        if off:
            f.seek(off)
        total = 0
        while True:
            to_read = min(CHUNK, length - total) if length else CHUNK
            chunk = f.read(to_read)
            if not chunk:
                break
            total += len(chunk)
            send(id, d={"data": b64(chunk)})
            if length and total >= length:
                break
    send(id, r={"size": total})


def cmd_write(id, p):
    """Write file contents atomically."""
    path = validate_path(p["path"])
    data = unb64(p["data"])

    # Atomic write: write to temp, then rename
    tmp = f"{path}.fresh-{os.getpid()}"
    try:
        # Preserve permissions if file exists
        mode = None
        if os.path.exists(path):
            mode = os.stat(path).st_mode

        with open(tmp, "wb") as f:
            f.write(data)
            f.flush()
            os.fsync(f.fileno())

        if mode is not None:
            os.chmod(tmp, mode)

        os.replace(tmp, path)  # replace() works cross-platform, rename() fails on Windows if dest exists
    finally:
        if os.path.exists(tmp):
            try:
                os.unlink(tmp)
            except OSError:
                pass

    send(id, r={"size": len(data)})


def cmd_sudo_write(id, p):
    """Write file contents using sudo (for root-owned files).

    Uses sudo tee to write the file. Preserves original permissions and ownership.
    """
    path = validate_path(p["path"])
    data = unb64(p["data"])

    # Get original metadata to preserve permissions
    mode = p.get("mode")
    uid = p.get("uid")
    gid = p.get("gid")

    # Use sudo tee to write the file
    proc = subprocess.Popen(
        ["sudo", "tee", path],
        stdin=subprocess.PIPE,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.PIPE,
    )
    _, stderr = proc.communicate(data)

    if proc.returncode != 0:
        raise RuntimeError(f"sudo tee failed: {stderr.decode().strip()}")

    # Restore permissions and ownership if provided
    if mode is not None:
        subprocess.run(["sudo", "chmod", f"{mode:o}", path], check=True,
                       capture_output=True)
    if uid is not None and gid is not None:
        subprocess.run(["sudo", "chown", f"{uid}:{gid}", path], check=True,
                       capture_output=True)

    send(id, r={"size": len(data)})


def cmd_stat(id, p):
    """Get file/directory metadata."""
    path = validate_path(p["path"])
    follow = p.get("link", True)

    st = os.stat(path, follow_symlinks=follow)
    is_link = stat.S_ISLNK(os.lstat(path).st_mode) if follow else False

    send(
        id,
        r={
            "size": st.st_size,
            "mtime": int(st.st_mtime),
            "mode": st.st_mode,
            "uid": st.st_uid,
            "gid": st.st_gid,
            "dir": stat.S_ISDIR(st.st_mode),
            "file": stat.S_ISREG(st.st_mode),
            "link": is_link,
        },
    )


def cmd_ls(id, p):
    """List directory contents with metadata."""
    path = validate_path(p["path"])
    entries = []

    for entry in os.scandir(path):
        try:
            st = entry.stat(follow_symlinks=False)
            is_link = entry.is_symlink()

            # For symlinks, check target type
            target_is_dir = False
            if is_link:
                try:
                    target_is_dir = entry.is_dir(follow_symlinks=True)
                except OSError:
                    pass

            entries.append(
                {
                    "name": entry.name,
                    "path": os.path.join(path, entry.name),
                    "dir": entry.is_dir(follow_symlinks=True),
                    "file": entry.is_file(follow_symlinks=True),
                    "link": is_link,
                    "link_dir": target_is_dir,
                    "size": st.st_size,
                    "mtime": int(st.st_mtime),
                    "mode": st.st_mode,
                }
            )
        except OSError:
            # Skip entries we can't stat
            pass

    send(id, r={"entries": entries})


def cmd_rm(id, p):
    """Remove a file."""
    os.unlink(validate_path(p["path"]))
    send(id, r={})


def cmd_rmdir(id, p):
    """Remove an empty directory."""
    os.rmdir(validate_path(p["path"]))
    send(id, r={})


def cmd_mkdir(id, p):
    """Create a directory."""
    path = validate_path(p["path"])
    if p.get("parents"):
        os.makedirs(path, exist_ok=True)
    else:
        os.mkdir(path)
    send(id, r={})


def cmd_mv(id, p):
    """Move/rename a file or directory.

    Uses shutil.move() to handle cross-device moves (e.g., /tmp to /etc).
    """
    shutil.move(validate_path(p["from"]), validate_path(p["to"]))
    send(id, r={})


def cmd_cp(id, p):
    """Copy a file."""
    dst = validate_path(p["to"])
    shutil.copy2(validate_path(p["from"]), dst)
    send(id, r={"size": os.path.getsize(dst)})


def cmd_realpath(id, p):
    """Get canonical absolute path."""
    send(id, r={"path": validate_path(p["path"])})


def cmd_chmod(id, p):
    """Change file permissions."""
    os.chmod(validate_path(p["path"]), p["mode"])
    send(id, r={})


def cmd_append(id, p):
    """Append data to a file (creates if doesn't exist)."""
    path = validate_path(p["path"])
    data = unb64(p["data"])
    with open(path, "ab") as f:
        f.write(data)
        f.flush()
        os.fsync(f.fileno())
    send(id, r={"size": len(data)})


def cmd_truncate(id, p):
    """Truncate or extend a file to a specified length."""
    path = validate_path(p["path"])
    os.truncate(path, p["len"])
    send(id, r={})


def cmd_patch(id, p):
    """Apply a patch recipe to create a new file from original + edits.

    Recipe format:
    - {"copy": {"off": offset, "len": length}} - copy from original file
    - {"insert": {"data": base64_data}} - insert new content

    This allows saving edits without transferring unchanged portions of the file.
    """
    src = validate_path(p["src"])  # Original file to read from
    dst = validate_path(p.get("dst", src))  # Destination (defaults to same file)
    ops = p["ops"]

    # Get original file's metadata to preserve permissions
    mode = None
    if os.path.exists(dst):
        mode = os.stat(dst).st_mode

    tmp = f"{dst}.fresh-{os.getpid()}"
    try:
        with open(src, "rb") as orig, open(tmp, "wb") as out:
            for op in ops:
                if "copy" in op:
                    off = op["copy"]["off"]
                    length = op["copy"]["len"]
                    orig.seek(off)
                    data = orig.read(length)
                    # Validate we read the expected number of bytes
                    if len(data) != length:
                        raise IOError(
                            f"cmd_patch copy: expected {length} bytes at offset {off}, "
                            f"got {len(data)} (src: {src})"
                        )
                    out.write(data)
                elif "insert" in op:
                    out.write(unb64(op["insert"]["data"]))

            out.flush()
            os.fsync(out.fileno())

        if mode is not None:
            os.chmod(tmp, mode)

        os.replace(tmp, dst)
    finally:
        if os.path.exists(tmp):
            try:
                os.unlink(tmp)
            except OSError:
                pass

    send(id, r={})


def cmd_count_lf(id, p):
    """Count newline (0x0A) bytes in a file range without returning the data."""
    path = validate_path(p["path"])
    off = p.get("off", 0)
    length = p["len"]

    count = 0
    with open(path, "rb") as f:
        if off:
            f.seek(off)
        remaining = length
        while remaining > 0:
            to_read = min(CHUNK, remaining)
            chunk = f.read(to_read)
            if not chunk:
                break
            count += chunk.count(b"\n")
            remaining -= len(chunk)
    send(id, r={"count": count})


def cmd_exists(id, p):
    """Check if path exists."""
    try:
        path = validate_path(p["path"])
        send(id, r={"exists": os.path.exists(path)})
    except (ValueError, OSError):
        send(id, r={"exists": False})


def cmd_info(id, p):
    """Get system info (home directory, cwd, temp directory, etc.)."""
    import tempfile
    send(id, r={
        "home": os.path.expanduser("~"),
        "cwd": os.getcwd(),
        "temp_dir": tempfile.gettempdir(),
    })


def cmd_search_file(id, p):
    """Search for a pattern in a file, returning matches with line/column info."""
    path = validate_path(p["path"])
    pattern = p["pattern"]
    fixed_string = p.get("fixed_string", False)
    case_sensitive = p.get("case_sensitive", True)
    whole_word = p.get("whole_word", False)
    max_matches = p.get("max_matches", 100)
    offset = p.get("offset", 0)
    running_line = p.get("running_line", 1)
    end_offset = p.get("end_offset")  # None = search to EOF

    file_size = os.path.getsize(path)
    effective_end = min(end_offset, file_size) if end_offset is not None else file_size

    # Binary detection: check first 8192 bytes for null byte (only for full-file searches)
    if offset == 0 and end_offset is None:
        with open(path, "rb") as f:
            header = f.read(8192)
            if b"\x00" in header:
                send(id, r={"matches": [], "next_offset": file_size,
                             "running_line": running_line, "done": True})
                return

    # Build regex pattern (bytes)
    pat = pattern
    if fixed_string:
        pat = re.escape(pat)
    if whole_word:
        pat = r"\b" + pat + r"\b"
    pat_bytes = pat.encode("utf-8")

    flags = 0
    if not case_sensitive:
        flags |= re.IGNORECASE
    regex = re.compile(pat_bytes, flags)

    # Read one chunk
    CHUNK_SIZE = 1048576  # 1MB
    overlap = max(len(pattern), 256)
    read_start = max(0, offset - overlap)
    read_end = min(read_start + CHUNK_SIZE, effective_end)

    with open(path, "rb") as f:
        f.seek(read_start)
        chunk = f.read(read_end - read_start)

    overlap_len = offset - read_start

    # Count newlines in the overlap region to adjust running_line
    line_at = running_line - chunk[:overlap_len].count(b"\n")

    # Scan for matches
    matches = []
    last_pos = 0
    for m in regex.finditer(chunk):
        if m.end() <= overlap_len:
            continue
        if len(matches) >= max_matches:
            break

        # Count newlines from last_pos to match start to track line number
        line_at += chunk[last_pos:m.start()].count(b"\n")
        last_pos = m.start()

        # Find line boundaries for context
        line_start = chunk.rfind(b"\n", 0, m.start())
        line_start = line_start + 1 if line_start != -1 else 0
        line_end = chunk.find(b"\n", m.end())
        if line_end == -1:
            line_end = len(chunk)

        context = chunk[line_start:line_end].decode("utf-8", errors="replace")
        col = m.start() - line_start + 1
        byte_offset = read_start + m.start()

        matches.append({
            "byte_offset": byte_offset,
            "length": m.end() - m.start(),
            "line": line_at,
            "column": col,
            "context": context,
        })

    # Update running_line by counting newlines in new data
    new_running_line = running_line + chunk[overlap_len:].count(b"\n")

    send(id, r={
        "matches": matches,
        "next_offset": read_end,
        "running_line": new_running_line,
        "done": read_end >= effective_end,
    })


# === Process Operations ===


def cmd_exec(id, p):
    """Execute a process with streaming output."""
    cwd = validate_path(p["cwd"]) if p.get("cwd") else None
    cmd = p["cmd"]
    args = p.get("args", [])

    try:
        proc = subprocess.Popen(
            [cmd] + args,
            cwd=cwd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )
    except FileNotFoundError:
        send(id, e=f"command not found: {cmd}")
        return
    except PermissionError:
        send(id, e=f"permission denied: {cmd}")
        return

    with lock:
        procs[id] = proc

    def stream_output():
        """Stream process output in a background thread."""
        try:
            while proc.poll() is None:
                # Check for cancellation
                if id in cancelled:
                    proc.terminate()
                    try:
                        proc.wait(timeout=2)
                    except subprocess.TimeoutExpired:
                        proc.kill()
                    send(id, e="cancelled")
                    return

                # Non-blocking read from stdout and stderr
                readable, _, _ = select.select(
                    [proc.stdout, proc.stderr], [], [], 0.05
                )
                for fd in readable:
                    data = fd.read(4096)
                    if data:
                        key = "out" if fd == proc.stdout else "err"
                        send(id, d={key: b64(data)})

            # Drain any remaining output
            out, err = proc.communicate(timeout=5)
            if out:
                send(id, d={"out": b64(out)})
            if err:
                send(id, d={"err": b64(err)})

            send(id, r={"code": proc.returncode})
        except Exception as e:
            send(id, e=str(e))
        finally:
            with lock:
                procs.pop(id, None)
                cancelled.discard(id)

    threading.Thread(target=stream_output, daemon=True).start()


def cmd_kill(id, p):
    """Kill a background process."""
    target_id = p["id"]
    with lock:
        proc = procs.get(target_id)

    if proc:
        proc.terminate()
        try:
            proc.wait(timeout=2)
        except subprocess.TimeoutExpired:
            proc.kill()
        send(id, r={})
    else:
        send(id, e="process not found")


def cmd_cancel(id, p):
    """Cancel an in-flight request."""
    target_id = p["id"]
    cancelled.add(target_id)

    with lock:
        proc = procs.get(target_id)
    if proc:
        proc.terminate()

    send(id, r={})


def cmd_walk_files(id, p):
    """Recursively walk a directory, streaming file paths in batches.

    Skips hidden entries (dot-prefixed) and directories in skip_dirs.
    Sends batches of relative paths as streaming data messages.
    Stops after max_files (default 50000).
    """
    root = validate_path(p["path"])
    skip_dirs = set(p.get("skip_dirs", []))
    max_files = p.get("max_files", 50000)
    batch_size = 500

    count = 0
    batch = []
    stack = [root]

    while stack:
        if id in cancelled:
            send(id, r={"count": count, "cancelled": True})
            return

        d = stack.pop()
        try:
            entries = os.scandir(d)
        except OSError:
            continue

        for entry in entries:
            if id in cancelled:
                send(id, r={"count": count, "cancelled": True})
                return

            if entry.name.startswith("."):
                continue

            try:
                if entry.is_file(follow_symlinks=False):
                    rel = os.path.relpath(entry.path, root)
                    batch.append(rel)
                    count += 1
                    if len(batch) >= batch_size:
                        send(id, d={"files": batch})
                        batch = []
                    if count >= max_files:
                        if batch:
                            send(id, d={"files": batch})
                        send(id, r={"count": count})
                        return
                elif entry.is_dir(follow_symlinks=False):
                    if entry.name not in skip_dirs:
                        stack.append(entry.path)
            except OSError:
                continue

    if batch:
        send(id, d={"files": batch})
    send(id, r={"count": count})


# === Method dispatch ===

METHODS = {
    "read": cmd_read,
    "write": cmd_write,
    "sudo_write": cmd_sudo_write,
    "stat": cmd_stat,
    "ls": cmd_ls,
    "rm": cmd_rm,
    "rmdir": cmd_rmdir,
    "mkdir": cmd_mkdir,
    "mv": cmd_mv,
    "cp": cmd_cp,
    "realpath": cmd_realpath,
    "chmod": cmd_chmod,
    "append": cmd_append,
    "truncate": cmd_truncate,
    "patch": cmd_patch,
    "count_lf": cmd_count_lf,
    "exists": cmd_exists,
    "info": cmd_info,
    "search_file": cmd_search_file,
    "exec": cmd_exec,
    "kill": cmd_kill,
    "cancel": cmd_cancel,
    "walk_files": cmd_walk_files,
}


def handle_request(line):
    """Parse and handle a single request."""
    try:
        req = json.loads(line)
    except json.JSONDecodeError as e:
        send(0, e=f"parse error: {e}")
        return

    id = req.get("id", 0)
    method = req.get("m")
    params = req.get("p", {})

    if method not in METHODS:
        send(id, e=f"unknown method: {method}")
        return

    try:
        METHODS[method](id, params)
    except PermissionError as e:
        send(id, e=f"permission denied: {e}")
    except FileNotFoundError as e:
        send(id, e=f"not found: {e}")
    except IsADirectoryError as e:
        send(id, e=f"is a directory: {e}")
    except NotADirectoryError as e:
        send(id, e=f"not a directory: {e}")
    except OSError as e:
        send(id, e=f"os error: {e}")
    except Exception as e:
        send(id, e=str(e))


def main():
    """Main entry point."""
    # Send ready message
    send(0, ok=True, v=VERSION)

    # Thread pool for concurrent request handling.
    # File I/O requests (read, count_lf, stat, etc.) can execute in parallel,
    # which is important when the editor pipelines many count_lf requests
    # during line-feed scanning of large files.
    pool = ThreadPoolExecutor(max_workers=64)

    # Process requests from stdin
    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue
        pool.submit(handle_request, line)

    pool.shutdown(wait=True)


if __name__ == "__main__":
    main()