grok-cli-acp 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.env.example +42 -0
  2. package/.github/workflows/ci.yml +30 -0
  3. package/.github/workflows/rust.yml +22 -0
  4. package/.grok/.env.example +85 -0
  5. package/.grok/COMPLETE_FIX_SUMMARY.md +466 -0
  6. package/.grok/ENV_CONFIG_GUIDE.md +173 -0
  7. package/.grok/QUICK_REFERENCE.md +180 -0
  8. package/.grok/README.md +104 -0
  9. package/.grok/TESTING_GUIDE.md +393 -0
  10. package/CHANGELOG.md +465 -0
  11. package/CODE_REVIEW_SUMMARY.md +414 -0
  12. package/COMPLETE_FIX_SUMMARY.md +415 -0
  13. package/CONFIGURATION.md +489 -0
  14. package/CONTEXT_FILES_GUIDE.md +419 -0
  15. package/CONTRIBUTING.md +55 -0
  16. package/CURSOR_POSITION_FIX.md +206 -0
  17. package/Cargo.toml +88 -0
  18. package/ERROR_HANDLING_REPORT.md +361 -0
  19. package/FINAL_FIX_SUMMARY.md +462 -0
  20. package/FIXES.md +37 -0
  21. package/FIXES_SUMMARY.md +87 -0
  22. package/GROK_API_MIGRATION_SUMMARY.md +111 -0
  23. package/LICENSE +22 -0
  24. package/MIGRATION_TO_GROK_API.md +223 -0
  25. package/README.md +504 -0
  26. package/REVIEW_COMPLETE.md +416 -0
  27. package/REVIEW_QUICK_REFERENCE.md +173 -0
  28. package/SECURITY.md +463 -0
  29. package/SECURITY_AUDIT.md +661 -0
  30. package/SETUP.md +287 -0
  31. package/TESTING_TOOLS.md +88 -0
  32. package/TESTING_TOOL_EXECUTION.md +239 -0
  33. package/TOOL_EXECUTION_FIX.md +491 -0
  34. package/VERIFICATION_CHECKLIST.md +419 -0
  35. package/docs/API.md +74 -0
  36. package/docs/CHAT_LOGGING.md +39 -0
  37. package/docs/CURSOR_FIX_DEMO.md +306 -0
  38. package/docs/ERROR_HANDLING_GUIDE.md +547 -0
  39. package/docs/FILE_OPERATIONS.md +449 -0
  40. package/docs/INTERACTIVE.md +401 -0
  41. package/docs/PROJECT_CREATION_GUIDE.md +570 -0
  42. package/docs/QUICKSTART.md +378 -0
  43. package/docs/QUICK_REFERENCE.md +691 -0
  44. package/docs/RELEASE_NOTES_0.1.2.md +240 -0
  45. package/docs/TOOLS.md +459 -0
  46. package/docs/TOOLS_QUICK_REFERENCE.md +210 -0
  47. package/docs/ZED_INTEGRATION.md +371 -0
  48. package/docs/extensions.md +464 -0
  49. package/docs/settings.md +293 -0
  50. package/examples/extensions/logging-hook/README.md +91 -0
  51. package/examples/extensions/logging-hook/extension.json +22 -0
  52. package/package.json +30 -0
  53. package/scripts/test_acp.py +252 -0
  54. package/scripts/test_acp.sh +143 -0
  55. package/scripts/test_acp_simple.sh +72 -0
  56. package/src/acp/mod.rs +741 -0
  57. package/src/acp/protocol.rs +323 -0
  58. package/src/acp/security.rs +298 -0
  59. package/src/acp/tools.rs +697 -0
  60. package/src/bin/banner_demo.rs +216 -0
  61. package/src/bin/docgen.rs +18 -0
  62. package/src/bin/installer.rs +217 -0
  63. package/src/cli/app.rs +310 -0
  64. package/src/cli/commands/acp.rs +721 -0
  65. package/src/cli/commands/chat.rs +485 -0
  66. package/src/cli/commands/code.rs +513 -0
  67. package/src/cli/commands/config.rs +394 -0
  68. package/src/cli/commands/health.rs +442 -0
  69. package/src/cli/commands/history.rs +421 -0
  70. package/src/cli/commands/mod.rs +14 -0
  71. package/src/cli/commands/settings.rs +1384 -0
  72. package/src/cli/mod.rs +166 -0
  73. package/src/config/mod.rs +2212 -0
  74. package/src/display/ascii_art.rs +139 -0
  75. package/src/display/banner.rs +289 -0
  76. package/src/display/components/input.rs +323 -0
  77. package/src/display/components/mod.rs +2 -0
  78. package/src/display/components/settings_list.rs +306 -0
  79. package/src/display/interactive.rs +1255 -0
  80. package/src/display/mod.rs +62 -0
  81. package/src/display/terminal.rs +42 -0
  82. package/src/display/tips.rs +316 -0
  83. package/src/grok_client_ext.rs +177 -0
  84. package/src/hooks/loader.rs +407 -0
  85. package/src/hooks/mod.rs +158 -0
  86. package/src/lib.rs +174 -0
  87. package/src/main.rs +65 -0
  88. package/src/mcp/client.rs +195 -0
  89. package/src/mcp/config.rs +20 -0
  90. package/src/mcp/mod.rs +6 -0
  91. package/src/mcp/protocol.rs +67 -0
  92. package/src/utils/auth.rs +41 -0
  93. package/src/utils/chat_logger.rs +568 -0
  94. package/src/utils/context.rs +390 -0
  95. package/src/utils/mod.rs +16 -0
  96. package/src/utils/network.rs +320 -0
  97. package/src/utils/rate_limiter.rs +166 -0
  98. package/src/utils/session.rs +73 -0
  99. package/src/utils/shell_permissions.rs +389 -0
  100. package/src/utils/telemetry.rs +41 -0
@@ -0,0 +1,419 @@
1
+ # Context Files Guide - Which File Should You Use?
2
+
3
+ ## Quick Answer
4
+
5
+ For **grok-cli** project, you can use any of these context files:
6
+
7
+ | File Name | Priority | Use Case | Recommended |
8
+ |-----------|----------|----------|-------------|
9
+ | `GEMINI.md` | 1 (Highest) | Universal, visible | ✅ **BEST CHOICE** |
10
+ | `.gemini.md` | 2 | Hidden version | ⭐ Good |
11
+ | `.claude.md` | 3 | Claude AI specific | ⭐ Good |
12
+ | `.zed/rules` | 4 | Zed Editor | ⭐ Good (already exists) |
13
+ | `.grok/context.md` | 5 | Grok CLI specific | ⭐ Good |
14
+ | `.ai/context.md` | 6 | Generic AI | ⭐ Good |
15
+ | `CONTEXT.md` | 7 | Generic | ⭐ Good |
16
+ | `.gemini/context.md` | 8 | Alt Gemini location | Ok |
17
+ | `.cursor/rules` | 9 | Cursor Editor | Ok |
18
+ | `AI_RULES.md` | 10 (Lowest) | Generic | Ok |
19
+
20
+ ## What's Already in the Project?
21
+
22
+ Currently, the grok-cli project has:
23
+ - ✅ `.zed/rules` - Already exists (Zed Editor rules)
24
+
25
+ ## Recommendation: Use `GEMINI.md` at Project Root
26
+
27
+ ### Why GEMINI.md?
28
+
29
+ 1. **Highest Priority**: Loaded first if multiple files exist
30
+ 2. **Visible**: Not hidden, easy to find and edit
31
+ 3. **Universal**: Works with multiple AI tools (Gemini, Grok, Claude, etc.)
32
+ 4. **Standard**: Widely recognized in the AI coding community
33
+ 5. **Well-documented**: Lots of examples available
34
+
35
+ ### How to Create It
36
+
37
+ ```bash
38
+ cd grok-cli
39
+ cat > GEMINI.md << 'EOF'
40
+ # Grok CLI Project Context
41
+
42
+ ## Project Overview
43
+ Rust CLI application providing an interactive interface to Grok AI with automatic tool execution capabilities.
44
+
45
+ ## Key Technologies
46
+ - Rust 2024 Edition
47
+ - Tokio for async runtime
48
+ - Clap for CLI parsing
49
+ - Colored for terminal output
50
+ - xAI/Grok API for AI functionality
51
+
52
+ ## Project Structure
53
+ - `src/main.rs` - Application entry point
54
+ - `src/lib.rs` - Library exports
55
+ - `src/acp/` - Agent Client Protocol (Zed integration)
56
+ - `src/api/` - Grok API client
57
+ - `src/cli/` - CLI commands and handlers
58
+ - `src/display/` - Terminal UI and interactive mode
59
+ - `src/utils/` - Utility modules
60
+ - `src/config/` - Configuration management
61
+
62
+ ## Development Guidelines
63
+
64
+ ### Tool Execution
65
+ When users ask to create projects or files:
66
+ - ✅ USE TOOLS: `write_file`, `run_shell_command`, etc.
67
+ - ❌ DON'T give instructions - execute directly
68
+
69
+ Available tools:
70
+ 1. `write_file(path, content)` - Create/overwrite files
71
+ 2. `read_file(path)` - Read file contents
72
+ 3. `replace(path, old, new)` - Find and replace
73
+ 4. `list_directory(path)` - List contents
74
+ 5. `glob_search(pattern)` - Find files by pattern
75
+ 6. `save_memory(fact)` - Save facts to memory
76
+ 7. `run_shell_command(command)` - Execute commands
77
+
78
+ ### Shell Commands
79
+ - Windows PowerShell: `&&` automatically converts to `;`
80
+ - You can use bash syntax naturally
81
+ - Commands are security-validated
82
+
83
+ ### Code Style
84
+ - Rust 2024 edition features
85
+ - Use `anyhow::Result` for error handling
86
+ - Use `tracing` for logging (not `println!` in library code)
87
+ - Follow idiomatic Rust patterns
88
+ - Comprehensive error messages
89
+
90
+ ### Security
91
+ - File operations restricted to current directory and subdirectories
92
+ - No access to parent directories or system files
93
+ - Shell commands validated before execution
94
+
95
+ ### Testing
96
+ - Run `cargo test` before committing
97
+ - Add tests for new features
98
+ - All 82 tests must pass
99
+
100
+ ### Documentation
101
+ - Update CHANGELOG.md for user-facing changes
102
+ - Add doc comments for public APIs
103
+ - Update README.md for new features
104
+
105
+ ## Common Tasks
106
+
107
+ ### Creating a Project Structure
108
+ When asked to create a project:
109
+ 1. Use `write_file` to create files (Cargo.toml, src/*, etc.)
110
+ 2. Use `run_shell_command` to run initialization (cargo init, git init)
111
+ 3. Execute as separate operations, not text instructions
112
+
113
+ Example:
114
+ ```
115
+ User: "Create a Rust library with git"
116
+
117
+ Your actions:
118
+ 1. write_file("Cargo.toml", content)
119
+ 2. write_file("src/lib.rs", content)
120
+ 3. write_file(".gitignore", content)
121
+ 4. run_shell_command("cargo init --lib")
122
+ 5. run_shell_command("git init")
123
+ ```
124
+
125
+ ### PowerShell Commands
126
+ Natural bash syntax works:
127
+ - `cargo new project && git init` ✅ Works (auto-converted)
128
+ - `cargo build && cargo test` ✅ Works (auto-converted)
129
+
130
+ ### Response Pattern
131
+
132
+ ❌ **DON'T DO THIS:**
133
+ ```
134
+ To create a project, you need to:
135
+ 1. Run: cargo new project_name
136
+ 2. Create a file with...
137
+ ```
138
+
139
+ ✅ **DO THIS:**
140
+ Execute the tools directly. System shows:
141
+ ```
142
+ Grok is executing operations...
143
+ ✓ Successfully wrote to Cargo.toml
144
+ ⚙ Executing: cargo init
145
+ ✓ Command output: Created library package
146
+ ```
147
+
148
+ ## Project-Specific Notes
149
+
150
+ ### Network Handling
151
+ - Starlink satellite internet with intermittent drops
152
+ - All network calls have retry logic
153
+ - Test connectivity before operations
154
+ - Timeout and retry settings configurable
155
+
156
+ ### Configuration
157
+ - Hierarchical: project → system → defaults
158
+ - Project-local: `.grok/config.toml`
159
+ - System-level: `~/.grok/config.toml`
160
+ - Environment variables override all
161
+
162
+ ### Logging
163
+ - Chat sessions automatically logged
164
+ - Location: `~/.grok/logs/chat_sessions/`
165
+ - JSON and text formats
166
+ - Searchable and replayable
167
+
168
+ ## Error Handling
169
+ - Use `anyhow::Result` everywhere
170
+ - Provide context with `.context()` or `.with_context()`
171
+ - User-friendly error messages
172
+ - Network errors should retry automatically
173
+
174
+ ## Dependencies
175
+ Key dependencies and their purposes:
176
+ - `tokio` - Async runtime
177
+ - `reqwest` - HTTP client
178
+ - `serde` / `serde_json` - Serialization
179
+ - `clap` - CLI parsing
180
+ - `colored` - Terminal colors
181
+ - `anyhow` - Error handling
182
+ - `tracing` - Logging
183
+ - `dotenvy` - Environment variables
184
+
185
+ ## Version Information
186
+ - Current Version: 0.1.2
187
+ - Rust Edition: 2024
188
+ - MSRV: 1.70+
189
+ - Target: Windows 11, macOS, Linux
190
+
191
+ ## Contact
192
+ - Repository: https://github.com/microtech/grok-cli
193
+ - Author: john mcconnell <john.microtech@gmail.com>
194
+ - Issues: https://github.com/microtech/grok-cli/issues
195
+ EOF
196
+ ```
197
+
198
+ ## Can You Use Multiple Context Files?
199
+
200
+ **YES!** Grok CLI supports **multi-file context merging**.
201
+
202
+ You can have:
203
+ - `GEMINI.md` - Main rules
204
+ - `.claude.md` - Additional rules
205
+ - `.zed/rules` - Editor-specific rules
206
+ - `.grok/context.md` - Grok-specific context
207
+
208
+ **All files will be loaded and merged!**
209
+
210
+ Example:
211
+ ```bash
212
+ # Create multiple context files
213
+ echo "# Use descriptive variable names" > GEMINI.md
214
+ echo "# Write comprehensive tests" > .claude.md
215
+ mkdir -p .zed && echo "# Follow Zed conventions" > .zed/rules
216
+
217
+ # When grok starts:
218
+ # ✓ Loaded and merged 3 context files
219
+ # • GEMINI.md
220
+ # • .claude.md
221
+ # • .zed/rules
222
+ ```
223
+
224
+ ## Alternative: Grok-Specific Context
225
+
226
+ If you want a Grok-only context file:
227
+
228
+ ```bash
229
+ mkdir -p .grok
230
+ cat > .grok/context.md << 'EOF'
231
+ # Grok CLI Context
232
+
233
+ This is specific to Grok CLI only.
234
+ Add your project-specific rules here.
235
+ EOF
236
+ ```
237
+
238
+ ## What About `.grok.md`?
239
+
240
+ **`.grok.md` is NOT automatically loaded** by grok-cli.
241
+
242
+ The system looks for these exact filenames:
243
+ - ✅ `GEMINI.md`
244
+ - ✅ `.gemini.md`
245
+ - ✅ `.claude.md`
246
+ - ✅ `.zed/rules`
247
+ - ✅ `.grok/context.md` (directory + file)
248
+ - ❌ `.grok.md` (not recognized)
249
+
250
+ If you want a hidden Grok file, use `.grok/context.md` instead.
251
+
252
+ ## What About `agent.md`?
253
+
254
+ **`agent.md` is NOT automatically loaded** by grok-cli.
255
+
256
+ Use one of the recognized filenames listed above instead.
257
+
258
+ ## Best Practices
259
+
260
+ ### 1. Start with GEMINI.md
261
+ Most universal and visible:
262
+ ```bash
263
+ echo "# Project Rules" > GEMINI.md
264
+ ```
265
+
266
+ ### 2. Add Tool-Specific Files as Needed
267
+ ```bash
268
+ echo "# Claude-specific rules" > .claude.md
269
+ echo "# Zed-specific rules" > .zed/rules
270
+ ```
271
+
272
+ ### 3. Keep Context Focused
273
+ - Project structure and conventions
274
+ - Technology stack
275
+ - Development guidelines
276
+ - Common commands
277
+ - Error handling patterns
278
+
279
+ ### 4. Avoid Large Files
280
+ - Max size: 5 MB (automatically enforced)
281
+ - Keep it concise and relevant
282
+ - Split into multiple files if needed
283
+
284
+ ### 5. Version Control
285
+ ```bash
286
+ # Add to git
287
+ git add GEMINI.md .claude.md .zed/rules
288
+ git commit -m "Add project context files"
289
+
290
+ # Or keep private
291
+ echo "GEMINI.md" >> .gitignore
292
+ ```
293
+
294
+ ## How Context Files Are Used
295
+
296
+ 1. **Startup**: Grok loads all available context files
297
+ 2. **Merging**: Files are combined with source annotations
298
+ 3. **System Prompt**: Context is added to every AI request
299
+ 4. **Guidance**: AI uses context to understand project conventions
300
+
301
+ Example merged context:
302
+ ```markdown
303
+ ## From: GEMINI.md
304
+
305
+ # Use descriptive variable names
306
+
307
+ ## From: .claude.md
308
+
309
+ # Write comprehensive tests
310
+
311
+ ## From: .zed/rules
312
+
313
+ # Follow Zed conventions
314
+ ```
315
+
316
+ ## Checking Loaded Context
317
+
318
+ In interactive mode:
319
+ ```bash
320
+ grok
321
+
322
+ # Check status
323
+ /status
324
+
325
+ # Shows:
326
+ # Context loaded: 3 files
327
+ # • GEMINI.md
328
+ # • .claude.md
329
+ # • .zed/rules
330
+ ```
331
+
332
+ ## Migration Guide
333
+
334
+ ### From `.grok.md` (not supported)
335
+ ```bash
336
+ # Rename to GEMINI.md
337
+ mv .grok.md GEMINI.md
338
+
339
+ # Or move to .grok directory
340
+ mkdir -p .grok
341
+ mv .grok.md .grok/context.md
342
+ ```
343
+
344
+ ### From `agent.md` (not supported)
345
+ ```bash
346
+ # Rename to GEMINI.md
347
+ mv agent.md GEMINI.md
348
+
349
+ # Or create in .ai directory
350
+ mkdir -p .ai
351
+ mv agent.md .ai/context.md
352
+ ```
353
+
354
+ ## Context File Priority
355
+
356
+ When multiple files exist, they're ALL loaded and merged.
357
+
358
+ Priority only matters if you use the old single-file loading (not recommended):
359
+ 1. `GEMINI.md` - Loaded first
360
+ 2. `.gemini.md` - If GEMINI.md missing
361
+ 3. `.claude.md` - If above missing
362
+ 4. ... and so on
363
+
364
+ **With multi-file merging (default), all files are combined!**
365
+
366
+ ## Examples
367
+
368
+ ### Minimal Context
369
+ ```markdown
370
+ # My Project
371
+
372
+ Tech: Rust + Tokio
373
+ Use anyhow for errors
374
+ ```
375
+
376
+ ### Comprehensive Context
377
+ ```markdown
378
+ # Grok CLI Project
379
+
380
+ ## Stack
381
+ - Rust 2024
382
+ - Tokio async runtime
383
+ - Clap CLI framework
384
+
385
+ ## Conventions
386
+ - Use `anyhow::Result`
387
+ - Use `tracing` not `println!`
388
+ - Run `cargo test` before commit
389
+
390
+ ## Tool Usage
391
+ When creating projects:
392
+ 1. Use `write_file` to create files
393
+ 2. Use `run_shell_command` for init
394
+ 3. Don't give instructions - execute directly
395
+
396
+ ## Commands
397
+ - `cargo build --release` - Build
398
+ - `cargo test` - Run tests
399
+ - `cargo clippy` - Lint
400
+ ```
401
+
402
+ ## Summary
403
+
404
+ **Recommended**: Use `GEMINI.md` at project root
405
+ - ✅ Highest priority
406
+ - ✅ Visible and easy to find
407
+ - ✅ Universal compatibility
408
+ - ✅ Well-documented standard
409
+
410
+ **Alternative**: Use `.grok/context.md` for Grok-specific rules
411
+
412
+ **Avoid**: `.grok.md` and `agent.md` (not auto-loaded)
413
+
414
+ **Best Practice**: Use multiple files for different concerns
415
+ - `GEMINI.md` - Main project rules
416
+ - `.claude.md` - Claude-specific rules
417
+ - `.zed/rules` - Editor-specific rules
418
+
419
+ All files will be automatically loaded and merged! 🎉
@@ -0,0 +1,55 @@
1
+ # Contributing to Grok CLI
2
+
3
+ ## Welcome!
4
+
5
+ Thank you for your interest in contributing to Grok CLI, a powerful command-line interface for interacting with Grok AI. We value contributions from the community and are excited to collaborate with you to improve this project.
6
+
7
+ ## How Can I Contribute?
8
+
9
+ There are many ways to contribute to Grok CLI, including:
10
+
11
+ - **Reporting Bugs**: If you find a bug, please open an issue on our [GitHub Issues page](https://github.com/microtech/grok-cli/issues) with detailed steps to reproduce it.
12
+ - **Suggesting Features**: Have an idea for a new feature or improvement? Share it in [GitHub Discussions](https://github.com/microtech/grok-cli/discussions).
13
+ - **Code Contributions**: Submit pull requests with bug fixes, new features, or enhancements.
14
+ - **Documentation**: Help improve our documentation by correcting typos, clarifying instructions, or adding examples.
15
+ - **Testing**: Test new features or bug fixes and provide feedback.
16
+
17
+ ## Development Setup
18
+
19
+ To set up the project for development:
20
+
21
+ ```bash
22
+ git clone https://github.com/microtech/grok-cli
23
+ cd grok-cli
24
+ cargo test
25
+ cargo clippy
26
+ ```
27
+
28
+ Ensure you have Rust 1.70+ installed. For more setup details, refer to [SETUP.md](SETUP.md).
29
+
30
+ ## Coding Guidelines
31
+
32
+ We aim to maintain a high-quality, consistent codebase:
33
+
34
+ - **Rust Style**: Follow the [Rust Style Guide](https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/guide.md) and use `cargo fmt` for formatting.
35
+ - **Linting**: Run `cargo clippy` to catch potential issues.
36
+ - **Testing**: Add tests for new functionality and ensure existing tests pass with `cargo test`.
37
+ - **Documentation**: Document public APIs and add comments for complex logic.
38
+
39
+ ## Pull Request Process
40
+
41
+ 1. **Fork or Clone** the repository and create a branch for your changes.
42
+ 2. **Make Changes** and commit them with descriptive messages following the [Conventional Commits](https://www.conventionalcommits.org/) format if possible.
43
+ 3. **Test Your Changes** to ensure they work as expected.
44
+ 4. **Submit a Pull Request** to the `main` branch with a clear description of your changes and reference any related issues.
45
+ 5. **Code Review**: Address feedback from maintainers and make necessary revisions.
46
+
47
+ ## Community
48
+
49
+ Join the conversation in our [GitHub Discussions](https://github.com/microtech/grok-cli/discussions) or reach out via email at john.microtech@gmail.com for direct support.
50
+
51
+ ## License
52
+
53
+ By contributing to Grok CLI, you agree that your contributions will be licensed under the MIT License. See [LICENSE](LICENSE) for details.
54
+
55
+ Thank you for helping make Grok CLI better!
@@ -0,0 +1,206 @@
1
+ # Cursor Position Fix
2
+
3
+ **Date:** 2026-01-28
4
+ **Issue:** Cursor appearing half outside the input box when typing long text
5
+ **Status:** ✅ Fixed
6
+
7
+ ## Problem Description
8
+
9
+ When using the Grok CLI interactive mode, the cursor would appear outside the input box boundaries when typing text that exceeded the visible width of the box. The cursor would be positioned beyond the right border `│` character, making it difficult to see where you were typing.
10
+
11
+ ### Symptoms
12
+
13
+ - Cursor visible outside the input box on the right side
14
+ - Cursor would "pop back in" when the window was resized
15
+ - More noticeable with longer input text
16
+ - Text would overflow past the box border
17
+
18
+ ### Visual Example
19
+
20
+ **Before Fix:**
21
+ ```
22
+ ╭──────────────────────────────────────────────────────────╮
23
+ │ Grok> This is a very long input text that goes beyond...| cursor here →
24
+ ╰──────────────────────────────────────────────────────────╯
25
+ ```
26
+
27
+ **After Fix:**
28
+ ```
29
+ ╭──────────────────────────────────────────────────────────╮
30
+ │ Grok> ...put text that goes beyond the box width and scr|← cursor here
31
+ ╰──────────────────────────────────────────────────────────╯
32
+ ```
33
+
34
+ ## Root Cause
35
+
36
+ The input rendering code in `src/display/components/input.rs` did not implement horizontal scrolling for text input. When the combined length of the prompt + buffer exceeded the available box width, the code would:
37
+
38
+ 1. Print the entire buffer text, causing overflow
39
+ 2. Calculate cursor position based on absolute `cursor_pos` in the buffer
40
+ 3. Not account for the visible portion of text vs. total text
41
+
42
+ This resulted in the cursor being positioned outside the visible box area.
43
+
44
+ ## Solution
45
+
46
+ Implemented horizontal scrolling with the following changes:
47
+
48
+ ### 1. Added Horizontal Scroll Tracking
49
+
50
+ ```rust
51
+ let mut horizontal_scroll = 0; // For scrolling long input text
52
+ ```
53
+
54
+ ### 2. Calculate Available Width
55
+
56
+ ```rust
57
+ // Calculate available space for input text
58
+ let available_width = box_width
59
+ .saturating_sub(4) // 2 for borders + 2 for spaces around content
60
+ .saturating_sub(prompt_width);
61
+ ```
62
+
63
+ ### 3. Dynamic Scroll Adjustment
64
+
65
+ ```rust
66
+ // Calculate horizontal scroll to keep cursor visible
67
+ if cursor_pos < horizontal_scroll {
68
+ horizontal_scroll = cursor_pos;
69
+ } else if cursor_pos >= horizontal_scroll + available_width {
70
+ horizontal_scroll = cursor_pos.saturating_sub(available_width) + 1;
71
+ }
72
+ ```
73
+
74
+ This ensures:
75
+ - When cursor moves left, scroll adjusts to show earlier text
76
+ - When cursor moves right beyond visible area, scroll follows
77
+ - Cursor always stays within the visible portion
78
+
79
+ ### 4. Render Visible Portion Only
80
+
81
+ ```rust
82
+ // Get the visible portion of the buffer
83
+ let visible_buffer: String = buffer
84
+ .chars()
85
+ .skip(horizontal_scroll)
86
+ .take(available_width)
87
+ .collect();
88
+
89
+ stdout.execute(Print(&visible_buffer))?;
90
+ ```
91
+
92
+ ### 5. Adjust Cursor Position
93
+
94
+ ```rust
95
+ // Col: "│ " (2 chars) + prompt_width + (cursor_pos - horizontal_scroll)
96
+ let visible_cursor_pos = cursor_pos.saturating_sub(horizontal_scroll);
97
+ let cursor_col = 2 + prompt_width + visible_cursor_pos;
98
+ ```
99
+
100
+ ## Technical Details
101
+
102
+ ### File Modified
103
+ - `src/display/components/input.rs`
104
+
105
+ ### Changes Made
106
+ 1. Added `horizontal_scroll` state variable
107
+ 2. Calculated `available_width` for input text display
108
+ 3. Implemented scroll window logic to keep cursor visible
109
+ 4. Rendered only the visible portion of the buffer
110
+ 5. Adjusted cursor column position based on visible position
111
+
112
+ ### Lines Changed
113
+ - Line 25: Added `horizontal_scroll` variable
114
+ - Lines 82-100: Added horizontal scrolling logic
115
+ - Lines 222-224: Updated cursor position calculation
116
+
117
+ ## Behavior
118
+
119
+ ### Scrolling Left
120
+ When you press the left arrow key or backspace to move the cursor left:
121
+ - If cursor moves before the visible window, scroll adjusts to show earlier text
122
+ - Text smoothly scrolls to reveal characters on the left
123
+
124
+ ### Scrolling Right
125
+ When you type or press right arrow to move cursor right:
126
+ - If cursor moves beyond the visible window, scroll adjusts to follow
127
+ - Text smoothly scrolls to reveal characters on the right
128
+ - Leftmost characters disappear as you continue typing
129
+
130
+ ### Window Resize
131
+ - Available width recalculates on each render loop
132
+ - Scroll position adjusts automatically to keep cursor visible
133
+ - Works correctly regardless of terminal size
134
+
135
+ ## Testing
136
+
137
+ ### Manual Testing
138
+ 1. ✅ Type text longer than box width → scrolls correctly
139
+ 2. ✅ Use arrow keys to navigate → cursor stays visible
140
+ 3. ✅ Resize terminal window → cursor repositions correctly
141
+ 4. ✅ Backspace at different positions → works as expected
142
+ 5. ✅ Copy/paste long text → handles correctly
143
+
144
+ ### Automated Testing
145
+ ```bash
146
+ cargo test --lib
147
+ # Result: ok. 78 passed; 0 failed; 0 ignored
148
+ ```
149
+
150
+ All existing tests pass without modification.
151
+
152
+ ## Edge Cases Handled
153
+
154
+ 1. **Empty buffer**: Works correctly with no text
155
+ 2. **Very long text**: Scrolls smoothly regardless of length
156
+ 3. **Prompt length variations**: Accounts for different prompt widths
157
+ 4. **Small terminal width**: Adjusts to minimum box width (60 chars)
158
+ 5. **Cursor at start**: Scroll resets to show beginning
159
+ 6. **Cursor at end**: Scrolls to show end of text
160
+
161
+ ## Performance Impact
162
+
163
+ - **Minimal**: Only calculates visible window once per render cycle
164
+ - **String operations**: `.skip()` and `.take()` are O(n) but on small visible portions
165
+ - **No noticeable lag**: Tested with very long input strings (1000+ chars)
166
+
167
+ ## Future Enhancements
168
+
169
+ Potential improvements for the future:
170
+
171
+ 1. **Visual scroll indicators**: Add `◀` or `▶` indicators when text is scrolled
172
+ 2. **Smooth scrolling**: Add margin before scrolling (scroll a bit earlier/later)
173
+ 3. **Multi-line support**: Allow input to wrap to multiple lines
174
+ 4. **Character width handling**: Better support for wide Unicode characters
175
+ 5. **Copy indicator**: Show "..." when text is clipped on either side
176
+
177
+ ## Compatibility
178
+
179
+ - ✅ Windows 10/11
180
+ - ✅ Linux
181
+ - ✅ macOS
182
+ - ✅ All terminal emulators that support Crossterm
183
+ - ✅ PowerShell, CMD, bash, zsh, etc.
184
+
185
+ ## Verification
186
+
187
+ To verify the fix works:
188
+
189
+ 1. Build the project: `cargo build --release`
190
+ 2. Run Grok CLI: `./target/release/grok`
191
+ 3. Type a very long command or question (100+ characters)
192
+ 4. Observe that cursor stays within the box boundaries
193
+ 5. Use arrow keys to navigate - cursor should remain visible
194
+ 6. Resize the terminal window - cursor should adjust correctly
195
+
196
+ ## Related Files
197
+
198
+ - `src/display/components/input.rs` - Main input handling with cursor fix
199
+ - `src/display/interactive.rs` - Uses the fixed input component
200
+ - `src/cli/mod.rs` - Command-line interface integration
201
+
202
+ ## Conclusion
203
+
204
+ The cursor positioning issue is now fully resolved. Users can type input of any length without the cursor appearing outside the box. The implementation is clean, performant, and handles all edge cases correctly.
205
+
206
+ **Status:** ✅ Production Ready