From 860061c552ff4ce2306b9fef7791e2c9acf7fe31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 27 Jul 2024 18:53:22 +0200 Subject: [PATCH] Support `Shift+Enter` keybind to create a newline --- Cargo.lock | 134 ++++++++++++++++++------------------- Cargo.toml | 2 +- src/data/chat.rs | 25 ++++++- src/screen/conversation.rs | 35 +++++++--- 4 files changed, 114 insertions(+), 82 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9cde973..d770a61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,9 +116,9 @@ dependencies = [ [[package]] name = "arrayref" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" +checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a" [[package]] name = "arrayvec" @@ -247,7 +247,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -282,7 +282,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -420,7 +420,7 @@ checksum = "1ee891b04274a59bd38b412188e24b849617b2e45a0fd8d057deb63e7403761b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -984,7 +984,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -1165,7 +1165,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -1259,7 +1259,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -1705,7 +1705,7 @@ dependencies = [ [[package]] name = "iced" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "iced_core", "iced_futures", @@ -1719,7 +1719,7 @@ dependencies = [ [[package]] name = "iced_core" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "bitflags 2.6.0", "bytes", @@ -1738,7 +1738,7 @@ dependencies = [ [[package]] name = "iced_futures" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "futures", "iced_core", @@ -1752,7 +1752,7 @@ dependencies = [ [[package]] name = "iced_graphics" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "bitflags 2.6.0", "bytemuck", @@ -1771,7 +1771,7 @@ dependencies = [ [[package]] name = "iced_highlighter" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "iced_core", "once_cell", @@ -1781,7 +1781,7 @@ dependencies = [ [[package]] name = "iced_renderer" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "iced_graphics", "iced_tiny_skia", @@ -1793,7 +1793,7 @@ dependencies = [ [[package]] name = "iced_runtime" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "bytes", "iced_core", @@ -1805,7 +1805,7 @@ dependencies = [ [[package]] name = "iced_tiny_skia" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "bytemuck", "cosmic-text", @@ -1821,7 +1821,7 @@ dependencies = [ [[package]] name = "iced_wgpu" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "bitflags 2.6.0", "bytemuck", @@ -1841,7 +1841,7 @@ dependencies = [ [[package]] name = "iced_widget" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "iced_highlighter", "iced_renderer", @@ -1858,7 +1858,7 @@ dependencies = [ [[package]] name = "iced_winit" version = "0.13.0-dev" -source = "git+https://github.com/iced-rs/iced.git?rev=f18f08bd617edbf58787ec4f379aa52c0ef292e2#f18f08bd617edbf58787ec4f379aa52c0ef292e2" +source = "git+https://github.com/iced-rs/iced.git?rev=23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7#23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" dependencies = [ "iced_futures", "iced_graphics", @@ -1974,9 +1974,9 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" [[package]] name = "jobserver" -version = "0.1.31" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e" +checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" dependencies = [ "libc", ] @@ -2192,13 +2192,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.11" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" dependencies = [ + "hermit-abi 0.3.9", "libc", "wasi", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -2342,7 +2343,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2589,9 +2590,9 @@ dependencies = [ [[package]] name = "object" -version = "0.36.1" +version = "0.36.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" +checksum = "3f203fa8daa7bb185f760ae12bd8e097f63d17041dcdcaf675ac54cdf863170e" dependencies = [ "memchr", ] @@ -2637,9 +2638,9 @@ dependencies = [ [[package]] name = "openssl" -version = "0.10.65" +version = "0.10.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2823eb4c6453ed64055057ea8bd416eda38c71018723869dd043a3b1186115e" +checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" dependencies = [ "bitflags 2.6.0", "cfg-if", @@ -2658,7 +2659,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2738,7 +2739,7 @@ dependencies = [ "by_address", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2843,7 +2844,7 @@ dependencies = [ "phf_shared", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2878,7 +2879,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3305,9 +3306,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.11" +version = "0.23.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4828ea528154ae444e5a642dbb7d5623354030dc9822b83fd9bb79683c7399d0" +checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" dependencies = [ "once_cell", "rustls-pki-types", @@ -3334,9 +3335,9 @@ checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" [[package]] name = "rustls-webpki" -version = "0.102.5" +version = "0.102.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a6fccd794a42c2c105b513a2f62bc3fd8f3ba57a4593677ceb0bd035164d78" +checksum = "8e6b52d4fda176fd835fdc55a835d4a89b8499cad995885a21149d5ad62f852e" dependencies = [ "ring", "rustls-pki-types", @@ -3455,7 +3456,7 @@ checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3477,7 +3478,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3762,9 +3763,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.71" +version = "2.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" dependencies = [ "proc-macro2", "quote", @@ -3882,7 +3883,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3972,19 +3973,18 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.38.1" +version = "1.39.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb2caba9f80616f438e09748d5acda951967e1ea58508ef53d9c6402485a46df" +checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" dependencies = [ "backtrace", "bytes", "libc", "mio", - "num_cpus", "pin-project-lite", "signal-hook-registry", "socket2", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -4034,9 +4034,9 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.6.6" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" +checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db" [[package]] name = "toml_edit" @@ -4095,7 +4095,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -4294,9 +4294,9 @@ checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] name = "version_check" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "walkdir" @@ -4344,7 +4344,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "wasm-bindgen-shared", ] @@ -4378,7 +4378,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -5121,9 +5121,9 @@ checksum = "c94451ac9513335b5e23d7a8a2b61a7102398b8cca5160829d313e84c9d98be1" [[package]] name = "zbus" -version = "4.3.1" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "851238c133804e0aa888edf4a0229481c753544ca12a60fd1c3230c8a500fe40" +checksum = "bb97012beadd29e654708a0fdb4c84bc046f537aecfde2c3ee0a9e4b4d48c725" dependencies = [ "async-broadcast", "async-executor", @@ -5159,14 +5159,14 @@ dependencies = [ [[package]] name = "zbus_macros" -version = "4.3.1" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d5a3f12c20bd473be3194af6b49d50d7bb804ef3192dc70eddedb26b85d9da7" +checksum = "267db9407081e90bbfa46d841d3cbc60f59c0351838c4bc65199ecd79ab1983e" dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "zvariant_utils", ] @@ -5204,7 +5204,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -5215,9 +5215,9 @@ checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" [[package]] name = "zvariant" -version = "4.1.2" +version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1724a2b330760dc7d2a8402d841119dc869ef120b139d29862d6980e9c75bfc9" +checksum = "2084290ab9a1c471c38fc524945837734fbf124487e105daec2bb57fd48c81fe" dependencies = [ "endi", "enumflags2", @@ -5228,24 +5228,24 @@ dependencies = [ [[package]] name = "zvariant_derive" -version = "4.1.2" +version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55025a7a518ad14518fb243559c058a2e5b848b015e31f1d90414f36e3317859" +checksum = "73e2ba546bda683a90652bac4a279bc146adad1386f25379cf73200d2002c449" dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "zvariant_utils", ] [[package]] name = "zvariant_utils" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc242db087efc22bd9ade7aa7809e4ba828132edc312871584a6b4391bdf8786" +checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] diff --git a/Cargo.toml b/Cargo.toml index d614146..c24b555 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] iced.git = "https://github.com/iced-rs/iced.git" -iced.rev = "f18f08bd617edbf58787ec4f379aa52c0ef292e2" +iced.rev = "23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7" iced.features = ["tokio", "svg", "markdown", "highlighter", "system", "web-colors", "debug"] tokio.version = "1.38" diff --git a/src/data/chat.rs b/src/data/chat.rs index 8f0f1e7..20b97ed 100644 --- a/src/data/chat.rs +++ b/src/data/chat.rs @@ -135,16 +135,35 @@ pub enum Event { TitleChanged(String), } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Content(String); + +impl Content { + pub fn parse(content: &str) -> Option { + let content = content.trim(); + + if content.is_empty() { + return None; + } + + Some(Self(content.to_owned())) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + pub fn send( assistant: &Assistant, - history: &[assistant::Message], - message: &str, + history: &[Message], + message: Content, ) -> impl Stream> { const SYSTEM_PROMPT: &str = "You are a helpful assistant."; let assistant = assistant.clone(); let mut messages = history.to_vec(); - let message = message.to_owned(); + let message = message.as_str().to_owned(); iced::stream::try_channel(1, |mut sender| async move { messages.push(Message::User(message.clone())); diff --git a/src/screen/conversation.rs b/src/screen/conversation.rs index 78b3c92..dee7c70 100644 --- a/src/screen/conversation.rs +++ b/src/screen/conversation.rs @@ -48,6 +48,7 @@ pub enum Message { Booting(Result), Tick(Instant), InputChanged(text_editor::Action), + Submit, Chatting(Result), Copy(assistant::Message), Created(Result), @@ -172,11 +173,17 @@ impl Conversation { Action::None } - Message::InputChanged(action) => match action { - text_editor::Action::Edit(text_editor::Edit::Enter) => { - if let State::Running { assistant, sending } = &mut self.state { + Message::InputChanged(action) => { + self.input.perform(action); + self.error = None; + + Action::None + } + Message::Submit => { + if let State::Running { assistant, sending } = &mut self.state { + if let Some(message) = chat::Content::parse(&self.input.text()) { let (send, handle) = Task::run( - chat::send(assistant, &self.history, &self.input.text()), + chat::send(assistant, &self.history, message), Message::Chatting, ) .abortable(); @@ -187,14 +194,10 @@ impl Conversation { } else { Action::None } - } - _ => { - self.input.perform(action); - self.error = None; - + } else { Action::None } - }, + } Message::Chatting(Ok(event)) if !self.can_send() => match event { chat::Event::TitleChanged(title) => { self.title = Some(title); @@ -509,7 +512,17 @@ impl Conversation { let input = text_editor(&self.input) .placeholder("Type your message here...") .on_action(Message::InputChanged) - .padding(10); + .padding(10) + .key_binding(|key_press| { + let modifiers = key_press.modifiers; + + match text_editor::Binding::from_key_press(key_press) { + Some(text_editor::Binding::Enter) if !modifiers.shift() => { + Some(text_editor::Binding::Custom(Message::Submit)) + } + binding => binding, + } + }); let chat = column![header, messages, input].spacing(10).align_x(Center);