From e16fef2b14daa27c979c07c75b863fc3b645545c Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 22 Feb 2025 11:22:14 -0500 Subject: [PATCH] Write a rudimentary editor --- Cargo.lock | 43 +++++++++++++++++++++++++---- l10n-db/Cargo.toml | 1 + l10n-db/i18n/OpenSandbox.toml | 6 ++-- l10n-db/src/bin/main.rs | 29 ++++++++++++++++--- l10n-db/src/editor.rs | 52 +++++++++++++++++++++++++++++++++++ l10n-db/src/lib.rs | 3 ++ l10n-db/src/types.rs | 4 +++ 7 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 l10n-db/src/editor.rs diff --git a/Cargo.lock b/Cargo.lock index 8f28b30..372fc49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1497,6 +1497,18 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.13.3+wasi-0.2.2", + "windows-targets 0.52.6", +] + [[package]] name = "gif" version = "0.11.4" @@ -2405,6 +2417,7 @@ dependencies = [ "clap", "icu_locid", "serde 1.0.218", + "tempfile", "toml", ] @@ -3502,7 +3515,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom", + "getrandom 0.2.15", ] [[package]] @@ -4404,13 +4417,13 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.15.0" +version = "3.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704" +checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" dependencies = [ "cfg-if", "fastrand", - "getrandom", + "getrandom 0.3.1", "once_cell", "rustix", "windows-sys 0.59.0", @@ -4968,7 +4981,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" dependencies = [ - "getrandom", + "getrandom 0.2.15", "serde 1.0.218", ] @@ -4978,7 +4991,7 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "744018581f9a3454a9e15beb8a33b017183f1e7c0cd170232a2d1453b23a51c4" dependencies = [ - "getrandom", + "getrandom 0.2.15", ] [[package]] @@ -5070,6 +5083,15 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "wasite" version = "0.1.0" @@ -5389,6 +5411,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags 2.8.0", +] + [[package]] name = "write16" version = "1.0.0" diff --git a/l10n-db/Cargo.toml b/l10n-db/Cargo.toml index d50c181..8dda9ff 100644 --- a/l10n-db/Cargo.toml +++ b/l10n-db/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" clap = { version = "4.5.30", features = ["derive"] } icu_locid = { version = "1.5.0", features = ["serde"] } serde = { version = "1.0.218", features = ["derive"] } +tempfile = "3.17.1" toml = "0.8.20" # [lib] diff --git a/l10n-db/i18n/OpenSandbox.toml b/l10n-db/i18n/OpenSandbox.toml index 7df243d..1f24302 100644 --- a/l10n-db/i18n/OpenSandbox.toml +++ b/l10n-db/i18n/OpenSandbox.toml @@ -1,7 +1,7 @@ key = "OpenSandbox" -description = "Open a sandbox" +description = "a basic description without any content" [variants.en-US] locale = "en-US" -content = "Open a sandbox vault" -modified = 1740198524 +content = "" +modified = 1740241312 diff --git a/l10n-db/src/bin/main.rs b/l10n-db/src/bin/main.rs index 39602cb..a51d432 100644 --- a/l10n-db/src/bin/main.rs +++ b/l10n-db/src/bin/main.rs @@ -1,9 +1,9 @@ -use std::path::PathBuf; +use std::{io::{BufReader, Read, Write}, path::PathBuf, process::Command}; use clap::{Parser, Subcommand}; use icu_locid::{langid, LanguageIdentifier}; -use l10n_db::{self, Bundle}; +use l10n_db::{self, Bundle, Editor}; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -29,14 +29,35 @@ enum Commands { }, } -fn edit_key(bundle: &mut Bundle, key: String, editor: &str) { +fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor: &str) { let message = bundle.message(key); + Editor::edit(message, locale, editor); + + println!("final version"); + println!("{}", toml::to_string(&message).unwrap()); + + /* + let mut editor_file = tempfile::NamedTempFile::new().unwrap(); + let _ = editor_file.write(toml::to_string(message).unwrap().as_bytes()); + let _ = editor_file.flush(); + let mut cmd = Command::new(editor).args([editor_file.path()]).spawn().unwrap(); + cmd.wait().unwrap(); + let _ = editor_file.flush(); + let mut reader = BufReader::new(editor_file); + let mut content = Vec::new(); + let _ = reader.read_to_end(&mut content); + + println!("content"); + println!("{}", String::from_utf8(content).unwrap()); + + println!("message: {:?}", message); message.set_description("Open a sandbox".to_owned()); let variant = message.variant_mut(langid!("en-US")); variant.set_content("Open a sandbox vault".to_owned()); + */ bundle.save(); } @@ -50,7 +71,7 @@ fn main() { let mut bundle = Bundle::load_from_disk(PathBuf::from(&db_path)); match &cli.command { - Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), &editor), + Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), langid!("en-US"), &editor), Some(Commands::ListKeys) => todo!(), Some(Commands::Export{..}) => todo!(), None => {}, diff --git a/l10n-db/src/editor.rs b/l10n-db/src/editor.rs new file mode 100644 index 0000000..a650b39 --- /dev/null +++ b/l10n-db/src/editor.rs @@ -0,0 +1,52 @@ +use std::{io::{BufReader, Read, Write}, path::Path, process::Command}; + +use icu_locid::LanguageIdentifier; +use serde::{Deserialize, Serialize}; + +use crate::{Message, Variant}; + + +#[derive(Serialize, Deserialize, Debug, Clone)] +struct EditorMessage { + description: String, + content: String, +} + +impl EditorMessage { + fn from_variant(description: String, variant: &Variant) -> Self { + Self { + description, + content: variant.content().to_string() + } + } +} + +pub struct Editor { +} + +impl Editor { + pub fn edit(msg: &mut Message, locale: LanguageIdentifier, editor: &str) { + let description = msg.description().to_owned(); + let variant = msg.variant_mut(locale); + let editable_content = EditorMessage::from_variant(description, &variant); + + let mut file = tempfile::NamedTempFile::new().unwrap(); + let _ = file.write(toml::to_string(&editable_content).unwrap().as_bytes()); + let _ = file.flush(); + let mut cmd = Command::new(editor).args([file.path()]).spawn().unwrap(); + cmd.wait().unwrap(); + let file = file.reopen().unwrap(); + let mut reader = BufReader::new(file); + let mut content = Vec::new(); + let _ = reader.read_to_end(&mut content); + + println!("content"); + println!("{}", String::from_utf8(content.clone()).unwrap()); + + let new_variant: EditorMessage = toml::from_str(&String::from_utf8(content).unwrap()).unwrap(); + + variant.set_content(new_variant.content); + msg.set_description(new_variant.description); + } +} + diff --git a/l10n-db/src/lib.rs b/l10n-db/src/lib.rs index 1dd4cc3..9dd0718 100644 --- a/l10n-db/src/lib.rs +++ b/l10n-db/src/lib.rs @@ -1,6 +1,9 @@ mod bundle; pub use bundle::Bundle; +mod editor; +pub use editor::Editor; + mod types; pub use types::{Message, Variant}; diff --git a/l10n-db/src/types.rs b/l10n-db/src/types.rs index c6ca0d1..c6ee3e5 100644 --- a/l10n-db/src/types.rs +++ b/l10n-db/src/types.rs @@ -45,6 +45,10 @@ pub struct Variant { } impl Variant { + pub fn content(&self) -> &str { + &self.content + } + pub fn set_content(&mut self, content: String) { self.content = content; self.modified = SystemTime::now();