Skip to content

Commit

Permalink
feat: Use jumprope for multi-editing
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 12, 2024
1 parent 1d32587 commit 5287edf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ clap = "4.5.4"
dunce = "1.0.4"
ignore = "0.4.22"
java-properties = "2.0.0"
jumprope = "1.1.2"
path-slash = "0.2.1"
rpassword = "7.3.1"
serde = { version = "1.0.203", features = ["derive"] }
Expand Down
23 changes: 14 additions & 9 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
use crate::chat::*;
use crate::user::*;
use crate::util::*;
use jumprope::{JumpRope, JumpRopeBuf};
use std::collections::HashMap;
use std::io::Write;

#[derive(Debug)]
pub struct File {
path: String, // absolute path
chat: Chat, // messages in this file
view: Option<String>, // the file view
path: String, // absolute path
chat: Chat, // messages in this file
view: Option<JumpRopeBuf>, // the file view
}

impl File {
Expand All @@ -48,19 +50,20 @@ impl File {
if !self.view.is_none() {
return;
}
self.view = Some(read_to_string(&self.path));
let content = read_to_string(&self.path);
self.view = Some(JumpRopeBuf::from(content));
}

pub fn update(&mut self, add_or_delete: &String, beg: u64, end: u64, content: &String) {
pub fn update(&mut self, add_or_delete: &String, beg: usize, end: usize, contents: &String) {
self.load_file(); // ensure read
let view = self.view.as_mut().unwrap();

match add_or_delete.clone().as_str() {
"add" => {
// TODO: ..
//self.view.insert(content, beg);
view.insert(beg, &contents);
}
"delete" => {
// TODO: ..
view.remove(beg..end);
}
_ => {
unreachable!()
Expand All @@ -70,6 +73,8 @@ impl File {

/// Write the content to file.
pub fn save(&self) {
let _ = std::fs::write(&self.path, &self.view.clone().unwrap());
let view = self.view.clone().unwrap();
let contents = &view.to_string();
let _ = std::fs::write(&self.path, contents);
}
}
12 changes: 6 additions & 6 deletions src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub mod update {

let path = data_str(json, "path").unwrap();
let add_or_delete = data_str(json, "add_or_delete").unwrap();
let beg = data_u64(json, "beg").unwrap();
let end = data_u64(json, "end").unwrap();
let content = data_str(json, "content").unwrap();
let beg = data_usize(json, "beg").unwrap();
let end = data_usize(json, "end").unwrap();
let contents = data_str(json, "contents").unwrap();

let path = to_room_path(&addr, &room, &path);
let file = room.get_file_mut(&path);
Expand All @@ -48,7 +48,7 @@ pub mod update {

let file = file.unwrap();

file.update(&add_or_delete, beg, end, &content);
file.update(&add_or_delete, beg, end, &contents);
}
}

Expand Down Expand Up @@ -120,13 +120,13 @@ pub mod sync {
return;
}

let content = read_to_string(&local_path);
let contents = read_to_string(&local_path);

channel
.send_json(&serde_json::json!({
"method": METHOD,
"file": file_path, // send it back directly
"content": content,
"contents": contents,
"status": "success",
}))
.await;
Expand Down
15 changes: 14 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn data_str(json: &Value, key: &str) -> Option<String> {
Some(json[key].as_str().unwrap().to_string())
}

/// Get data as string.
/// Get data as u64.
///
/// # Arguments
///
Expand All @@ -46,6 +46,19 @@ pub fn data_u64(json: &Value, key: &str) -> Option<u64> {
Some(json[key].as_u64().unwrap())
}

/// Get data as usize.
///
/// # Arguments
///
/// * `json` - JSON object.
/// * `key` - Key to the data.
pub fn data_usize(json: &Value, key: &str) -> Option<usize> {
if json[key].is_null() {
return None;
}
Some(json[key].to_string().parse::<usize>().unwrap())
}

/// Parse data to u64.
///
/// # Arguments
Expand Down

0 comments on commit 5287edf

Please sign in to comment.