Add terminal client, memory, model fallback, version and install improvements
- codecraft: full-screen terminal client (ratatui) – local tools and files, models via the CodeCraft server gateway or own providers (standalone with a guided setup), slash commands, @files, permission dialogs, resume, update - Static Linux CLI builds (x86_64/aarch64, cargo-zigbuild) served by the server: /cli/install.sh and /cli/download/<target> - Project memory (remember tool, .codecraft/memory.md) and personal memory - Model fallback when a host is unreachable or a budget is used up - Version/commit in UI and health, daily release check, "What's new" dialog - PWA: install button, maskable icon, manifest id and screenshots - Case-insensitive detection of connection errors for retries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aa1f8cf016
commit
6dffcb501a
52 files changed
+6477
-765
No files matched your search
@@ -0,0 +1,219 @@
|
||||
//! Multi-line input field with history.
|
||||
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Editor {
|
||||
pub lines: Vec<String>,
|
||||
/// Cursor: line index and char index.
|
||||
pub row: usize,
|
||||
pub col: usize,
|
||||
history: Vec<String>,
|
||||
hist_pos: Option<usize>,
|
||||
}
|
||||
|
||||
fn byte_idx(s: &str, ch: usize) -> usize {
|
||||
s.char_indices().nth(ch).map(|(i, _)| i).unwrap_or(s.len())
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn new() -> Self {
|
||||
let history = std::fs::read_to_string(crate::conf::data_dir().join("history.txt"))
|
||||
.map(|t| t.lines().map(|l| l.replace("\\n", "\n")).collect())
|
||||
.unwrap_or_default();
|
||||
Self { lines: vec![String::new()], history, ..Default::default() }
|
||||
}
|
||||
|
||||
pub fn text(&self) -> String {
|
||||
self.lines.join("\n")
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.lines.iter().all(|l| l.is_empty())
|
||||
}
|
||||
|
||||
pub fn set(&mut self, text: &str) {
|
||||
self.lines = text.split('\n').map(String::from).collect();
|
||||
if self.lines.is_empty() {
|
||||
self.lines.push(String::new());
|
||||
}
|
||||
self.row = self.lines.len() - 1;
|
||||
self.col = self.lines[self.row].chars().count();
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.lines = vec![String::new()];
|
||||
self.row = 0;
|
||||
self.col = 0;
|
||||
self.hist_pos = None;
|
||||
}
|
||||
|
||||
/// Take the text (and remember it in the history).
|
||||
pub fn take(&mut self) -> String {
|
||||
let t = self.text();
|
||||
if !t.trim().is_empty() && self.history.last() != Some(&t) {
|
||||
self.history.push(t.clone());
|
||||
if self.history.len() > 500 {
|
||||
self.history.remove(0);
|
||||
}
|
||||
let _ = std::fs::create_dir_all(crate::conf::data_dir());
|
||||
let _ = std::fs::write(crate::conf::data_dir().join("history.txt"), self.history.iter().map(|h| h.replace('\n', "\\n")).collect::<Vec<_>>().join("\n"));
|
||||
}
|
||||
self.clear();
|
||||
t
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, s: &str) {
|
||||
for (i, part) in s.split('\n').enumerate() {
|
||||
if i > 0 {
|
||||
self.newline();
|
||||
}
|
||||
let part = part.replace('\r', "").replace('\t', " ");
|
||||
let line = &mut self.lines[self.row];
|
||||
let at = byte_idx(line, self.col);
|
||||
line.insert_str(at, &part);
|
||||
self.col += part.chars().count();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn newline(&mut self) {
|
||||
let line = &mut self.lines[self.row];
|
||||
let at = byte_idx(line, self.col);
|
||||
let rest = line.split_off(at);
|
||||
self.lines.insert(self.row + 1, rest);
|
||||
self.row += 1;
|
||||
self.col = 0;
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self) {
|
||||
if self.col > 0 {
|
||||
let line = &mut self.lines[self.row];
|
||||
let a = byte_idx(line, self.col - 1);
|
||||
let b = byte_idx(line, self.col);
|
||||
line.replace_range(a..b, "");
|
||||
self.col -= 1;
|
||||
} else if self.row > 0 {
|
||||
let line = self.lines.remove(self.row);
|
||||
self.row -= 1;
|
||||
self.col = self.lines[self.row].chars().count();
|
||||
self.lines[self.row].push_str(&line);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&mut self) {
|
||||
let len = self.lines[self.row].chars().count();
|
||||
if self.col < len {
|
||||
let line = &mut self.lines[self.row];
|
||||
let a = byte_idx(line, self.col);
|
||||
let b = byte_idx(line, self.col + 1);
|
||||
line.replace_range(a..b, "");
|
||||
} else if self.row + 1 < self.lines.len() {
|
||||
let next = self.lines.remove(self.row + 1);
|
||||
self.lines[self.row].push_str(&next);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_word(&mut self) {
|
||||
let chars: Vec<char> = self.lines[self.row].chars().collect();
|
||||
let mut i = self.col;
|
||||
while i > 0 && chars[i - 1].is_whitespace() {
|
||||
i -= 1;
|
||||
}
|
||||
while i > 0 && !chars[i - 1].is_whitespace() {
|
||||
i -= 1;
|
||||
}
|
||||
let line = &mut self.lines[self.row];
|
||||
let a = byte_idx(line, i);
|
||||
let b = byte_idx(line, self.col);
|
||||
line.replace_range(a..b, "");
|
||||
self.col = i;
|
||||
}
|
||||
|
||||
pub fn left(&mut self) {
|
||||
if self.col > 0 {
|
||||
self.col -= 1;
|
||||
} else if self.row > 0 {
|
||||
self.row -= 1;
|
||||
self.col = self.lines[self.row].chars().count();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn right(&mut self) {
|
||||
if self.col < self.lines[self.row].chars().count() {
|
||||
self.col += 1;
|
||||
} else if self.row + 1 < self.lines.len() {
|
||||
self.row += 1;
|
||||
self.col = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn home(&mut self) {
|
||||
self.col = 0;
|
||||
}
|
||||
|
||||
pub fn end(&mut self) {
|
||||
self.col = self.lines[self.row].chars().count();
|
||||
}
|
||||
|
||||
/// Up: previous line, or older history entry on the first line.
|
||||
pub fn up(&mut self) {
|
||||
if self.row > 0 {
|
||||
self.row -= 1;
|
||||
self.col = self.col.min(self.lines[self.row].chars().count());
|
||||
return;
|
||||
}
|
||||
if self.history.is_empty() {
|
||||
return;
|
||||
}
|
||||
let pos = match self.hist_pos {
|
||||
None if !self.is_empty() => return,
|
||||
None => self.history.len() - 1,
|
||||
Some(0) => 0,
|
||||
Some(p) => p - 1,
|
||||
};
|
||||
self.hist_pos = Some(pos);
|
||||
let h = self.history[pos].clone();
|
||||
self.set(&h);
|
||||
}
|
||||
|
||||
pub fn down(&mut self) {
|
||||
if self.row + 1 < self.lines.len() {
|
||||
self.row += 1;
|
||||
self.col = self.col.min(self.lines[self.row].chars().count());
|
||||
return;
|
||||
}
|
||||
if let Some(p) = self.hist_pos {
|
||||
if p + 1 < self.history.len() {
|
||||
self.hist_pos = Some(p + 1);
|
||||
let h = self.history[p + 1].clone();
|
||||
self.set(&h);
|
||||
} else {
|
||||
self.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The word the cursor is in (for @file and /command completion): (start char, word).
|
||||
pub fn current_word(&self) -> (usize, String) {
|
||||
let chars: Vec<char> = self.lines[self.row].chars().collect();
|
||||
let mut i = self.col.min(chars.len());
|
||||
while i > 0 && !chars[i - 1].is_whitespace() {
|
||||
i -= 1;
|
||||
}
|
||||
(i, chars[i..self.col.min(chars.len())].iter().collect())
|
||||
}
|
||||
|
||||
pub fn replace_word(&mut self, start: usize, with: &str) {
|
||||
let line = &mut self.lines[self.row];
|
||||
let a = byte_idx(line, start);
|
||||
let b = byte_idx(line, self.col);
|
||||
line.replace_range(a..b, with);
|
||||
self.col = start + with.chars().count();
|
||||
}
|
||||
|
||||
/// Cursor position in display columns (for the terminal cursor).
|
||||
pub fn cursor_x(&self) -> usize {
|
||||
let line = &self.lines[self.row];
|
||||
UnicodeWidthStr::width(&line[..byte_idx(line, self.col)])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user