Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10b142c071 | ||
|
|
ac6ff63d25 | ||
|
|
54a0e76823 | ||
|
|
93eff79f79 | ||
|
|
11625262f1 | ||
|
|
0ed1d53946 |
33
Cargo.lock
generated
33
Cargo.lock
generated
@@ -860,6 +860,12 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "endian-type"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.2.8"
|
||||
@@ -1302,7 +1308,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "iamb"
|
||||
version = "0.0.4"
|
||||
version = "0.0.6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"chrono",
|
||||
@@ -1876,9 +1882,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "modalkit"
|
||||
version = "0.0.11"
|
||||
version = "0.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd7bd7d02d65842dab4cea53016cf29c16cde197131dd6d9eea95662deb77778"
|
||||
checksum = "038bb42efcd659fb123708bf8e4ea12ca9023f07d44514f9358b9334ea7ba80f"
|
||||
dependencies = [
|
||||
"anymap2",
|
||||
"arboard",
|
||||
@@ -1888,10 +1894,12 @@ dependencies = [
|
||||
"intervaltree",
|
||||
"libc",
|
||||
"nom",
|
||||
"radix_trie",
|
||||
"regex",
|
||||
"ropey",
|
||||
"thiserror",
|
||||
"tui",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1900,6 +1908,15 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
|
||||
|
||||
[[package]]
|
||||
name = "nibble_vec"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43"
|
||||
dependencies = [
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.24.3"
|
||||
@@ -2344,6 +2361,16 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "radix_trie"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd"
|
||||
dependencies = [
|
||||
"endian-type",
|
||||
"nibble_vec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.7.3"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "iamb"
|
||||
version = "0.0.5"
|
||||
version = "0.0.6"
|
||||
edition = "2018"
|
||||
authors = ["Ulyssa <git@ulyssa.dev>"]
|
||||
repository = "https://github.com/ulyssa/iamb"
|
||||
@@ -39,7 +39,7 @@ unicode-width = "0.1.10"
|
||||
url = {version = "^2.2.2", features = ["serde"]}
|
||||
|
||||
[dependencies.modalkit]
|
||||
version = "0.0.11"
|
||||
version = "0.0.13"
|
||||
|
||||
[dependencies.matrix-sdk]
|
||||
version = "0.6"
|
||||
|
||||
17
README.md
17
README.md
@@ -11,6 +11,8 @@
|
||||
This project is a work-in-progress, and there's still a lot to be implemented,
|
||||
but much of the basic client functionality is already present.
|
||||
|
||||

|
||||
|
||||
## Documentation
|
||||
|
||||
You can find documentation for installing, configuring, and using iamb on its
|
||||
@@ -75,7 +77,7 @@ two other TUI clients and Element Web:
|
||||
| VOIP | ❌ | ❌ | ❌ | ✔️ |
|
||||
| Reactions | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Message editing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Room upgrades | ❌ | ✔️ | ❌ | ✔️ |
|
||||
| Room upgrades | ❌ ([#41]) | ✔️ | ❌ | ✔️ |
|
||||
| Localisations | ❌ | 1 | ❌ | 44 |
|
||||
| SSO Support | ❌ | ✔️ | ✔️ | ✔️ |
|
||||
|
||||
@@ -88,18 +90,7 @@ iamb is released under the [Apache License, Version 2.0].
|
||||
[iamb.chat]: https://iamb.chat
|
||||
[gomuks]: https://github.com/tulir/gomuks
|
||||
[weechat-matrix]: https://github.com/poljar/weechat-matrix
|
||||
[#2]: https://github.com/ulyssa/iamb/issues/2
|
||||
[#3]: https://github.com/ulyssa/iamb/issues/3
|
||||
[#4]: https://github.com/ulyssa/iamb/issues/4
|
||||
[#5]: https://github.com/ulyssa/iamb/issues/5
|
||||
[#6]: https://github.com/ulyssa/iamb/issues/6
|
||||
[#7]: https://github.com/ulyssa/iamb/issues/7
|
||||
[#8]: https://github.com/ulyssa/iamb/issues/8
|
||||
[#9]: https://github.com/ulyssa/iamb/issues/9
|
||||
[#10]: https://github.com/ulyssa/iamb/issues/10
|
||||
[#11]: https://github.com/ulyssa/iamb/issues/11
|
||||
[#12]: https://github.com/ulyssa/iamb/issues/12
|
||||
[#13]: https://github.com/ulyssa/iamb/issues/13
|
||||
[#14]: https://github.com/ulyssa/iamb/issues/14
|
||||
[#15]: https://github.com/ulyssa/iamb/issues/15
|
||||
[#16]: https://github.com/ulyssa/iamb/issues/16
|
||||
[#41]: https://github.com/ulyssa/iamb/issues/41
|
||||
|
||||
270
src/base.rs
270
src/base.rs
@@ -1,8 +1,11 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::hash::Hash;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use emojis::Emoji;
|
||||
use tokio::sync::Mutex as AsyncMutex;
|
||||
use tracing::warn;
|
||||
|
||||
@@ -23,6 +26,7 @@ use matrix_sdk::{
|
||||
AnyMessageLikeEvent,
|
||||
MessageLikeEvent,
|
||||
},
|
||||
presence::PresenceState,
|
||||
EventId,
|
||||
OwnedEventId,
|
||||
OwnedRoomId,
|
||||
@@ -42,11 +46,15 @@ use modalkit::{
|
||||
ApplicationStore,
|
||||
ApplicationWindowId,
|
||||
},
|
||||
base::{CommandType, WordStyle},
|
||||
completion::{complete_path, CompletionMap},
|
||||
context::EditContext,
|
||||
cursor::Cursor,
|
||||
rope::EditRope,
|
||||
store::Store,
|
||||
},
|
||||
env::vim::{
|
||||
command::{CommandContext, VimCommand, VimCommandMachine},
|
||||
command::{CommandContext, CommandDescription, VimCommand, VimCommandMachine},
|
||||
keybindings::VimMachine,
|
||||
VimContext,
|
||||
},
|
||||
@@ -66,6 +74,20 @@ use crate::{
|
||||
ApplicationSettings,
|
||||
};
|
||||
|
||||
pub const MATRIX_ID_WORD: WordStyle = WordStyle::CharSet(is_mxid_char);
|
||||
|
||||
/// Find the boundaries for a Matrix username, room alias, or room ID.
|
||||
///
|
||||
/// Technically "[" and "]" should be here since IPv6 addresses are allowed
|
||||
/// in the server name, but in practice that should be uncommon, and people
|
||||
/// can just use `gf` and friends in Visual mode instead.
|
||||
fn is_mxid_char(c: char) -> bool {
|
||||
return c >= 'a' && c <= 'z' ||
|
||||
c >= 'A' && c <= 'Z' ||
|
||||
c >= '0' && c <= '9' ||
|
||||
":-./@_#!".contains(c);
|
||||
}
|
||||
|
||||
const ROOM_FETCH_DEBOUNCE: Duration = Duration::from_secs(2);
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -109,6 +131,30 @@ pub enum MessageAction {
|
||||
Unreact(Option<String>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum CreateRoomType {
|
||||
/// A direct message room.
|
||||
Direct(OwnedUserId),
|
||||
|
||||
/// A standard chat room.
|
||||
Room,
|
||||
|
||||
/// A Matrix space.
|
||||
Space,
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
pub struct CreateRoomFlags: u32 {
|
||||
const NONE = 0b00000000;
|
||||
|
||||
/// Make the room public.
|
||||
const PUBLIC = 0b00000001;
|
||||
|
||||
/// Encrypt this room.
|
||||
const ENCRYPTED = 0b00000010;
|
||||
}
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
pub struct DownloadFlags: u32 {
|
||||
const NONE = 0b00000000;
|
||||
@@ -144,8 +190,14 @@ pub enum SendAction {
|
||||
Upload(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum HomeserverAction {
|
||||
CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum IambAction {
|
||||
Homeserver(HomeserverAction),
|
||||
Message(MessageAction),
|
||||
Room(RoomAction),
|
||||
Send(SendAction),
|
||||
@@ -154,6 +206,12 @@ pub enum IambAction {
|
||||
ToggleScrollbackFocus,
|
||||
}
|
||||
|
||||
impl From<HomeserverAction> for IambAction {
|
||||
fn from(act: HomeserverAction) -> Self {
|
||||
IambAction::Homeserver(act)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MessageAction> for IambAction {
|
||||
fn from(act: MessageAction) -> Self {
|
||||
IambAction::Message(act)
|
||||
@@ -175,6 +233,7 @@ impl From<SendAction> for IambAction {
|
||||
impl ApplicationAction for IambAction {
|
||||
fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => SequenceStatus::Break,
|
||||
IambAction::Message(..) => SequenceStatus::Break,
|
||||
IambAction::Room(..) => SequenceStatus::Break,
|
||||
IambAction::Send(..) => SequenceStatus::Break,
|
||||
@@ -186,6 +245,7 @@ impl ApplicationAction for IambAction {
|
||||
|
||||
fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => SequenceStatus::Atom,
|
||||
IambAction::Message(..) => SequenceStatus::Atom,
|
||||
IambAction::Room(..) => SequenceStatus::Atom,
|
||||
IambAction::Send(..) => SequenceStatus::Atom,
|
||||
@@ -197,6 +257,7 @@ impl ApplicationAction for IambAction {
|
||||
|
||||
fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => SequenceStatus::Ignore,
|
||||
IambAction::Message(..) => SequenceStatus::Ignore,
|
||||
IambAction::Room(..) => SequenceStatus::Ignore,
|
||||
IambAction::Send(..) => SequenceStatus::Ignore,
|
||||
@@ -208,6 +269,7 @@ impl ApplicationAction for IambAction {
|
||||
|
||||
fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
|
||||
match self {
|
||||
IambAction::Homeserver(..) => false,
|
||||
IambAction::Message(..) => false,
|
||||
IambAction::Room(..) => false,
|
||||
IambAction::Send(..) => false,
|
||||
@@ -431,6 +493,8 @@ impl RoomInfo {
|
||||
return;
|
||||
},
|
||||
}
|
||||
|
||||
msg.html = msg.event.html();
|
||||
}
|
||||
|
||||
pub fn insert_message(&mut self, msg: RoomMessageEvent) {
|
||||
@@ -528,13 +592,28 @@ impl RoomInfo {
|
||||
}
|
||||
}
|
||||
|
||||
fn emoji_map() -> CompletionMap<String, &'static Emoji> {
|
||||
let mut emojis = CompletionMap::default();
|
||||
|
||||
for emoji in emojis::iter() {
|
||||
for shortcode in emoji.shortcodes() {
|
||||
emojis.insert(shortcode.to_string(), emoji);
|
||||
}
|
||||
}
|
||||
|
||||
return emojis;
|
||||
}
|
||||
|
||||
pub struct ChatStore {
|
||||
pub cmds: ProgramCommands,
|
||||
pub worker: Requester,
|
||||
pub rooms: HashMap<OwnedRoomId, RoomInfo>,
|
||||
pub names: HashMap<String, OwnedRoomId>,
|
||||
pub rooms: CompletionMap<OwnedRoomId, RoomInfo>,
|
||||
pub names: CompletionMap<String, OwnedRoomId>,
|
||||
pub presences: CompletionMap<OwnedUserId, PresenceState>,
|
||||
pub verifications: HashMap<String, SasVerification>,
|
||||
pub settings: ApplicationSettings,
|
||||
pub need_load: HashSet<OwnedRoomId>,
|
||||
pub emojis: CompletionMap<String, &'static Emoji>,
|
||||
}
|
||||
|
||||
impl ChatStore {
|
||||
@@ -543,10 +622,13 @@ impl ChatStore {
|
||||
worker,
|
||||
settings,
|
||||
|
||||
cmds: crate::commands::setup_commands(),
|
||||
names: Default::default(),
|
||||
rooms: Default::default(),
|
||||
presences: Default::default(),
|
||||
verifications: Default::default(),
|
||||
need_load: Default::default(),
|
||||
emojis: emoji_map(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -587,10 +669,10 @@ impl ChatStore {
|
||||
}
|
||||
|
||||
pub fn load_older(&mut self, limit: u32) {
|
||||
let ChatStore { need_load, rooms, worker, .. } = self;
|
||||
let ChatStore { need_load, presences, rooms, worker, .. } = self;
|
||||
|
||||
for room_id in std::mem::take(need_load).into_iter() {
|
||||
let info = rooms.entry(room_id.clone()).or_default();
|
||||
let info = rooms.get_or_default(room_id.clone());
|
||||
|
||||
if info.recently_fetched() {
|
||||
need_load.insert(room_id);
|
||||
@@ -610,6 +692,9 @@ impl ChatStore {
|
||||
match res {
|
||||
Ok((fetch_id, msgs)) => {
|
||||
for msg in msgs.into_iter() {
|
||||
let sender = msg.sender().to_owned();
|
||||
let _ = presences.get_or_default(sender);
|
||||
|
||||
match msg {
|
||||
AnyMessageLikeEvent::RoomMessage(msg) => {
|
||||
info.insert(msg);
|
||||
@@ -639,11 +724,11 @@ impl ChatStore {
|
||||
}
|
||||
|
||||
pub fn get_room_info(&mut self, room_id: OwnedRoomId) -> &mut RoomInfo {
|
||||
self.rooms.entry(room_id).or_default()
|
||||
self.rooms.get_or_default(room_id)
|
||||
}
|
||||
|
||||
pub fn set_room_name(&mut self, room_id: &RoomId, name: &str) {
|
||||
self.rooms.entry(room_id.to_owned()).or_default().name = name.to_string().into();
|
||||
self.rooms.get_or_default(room_id.to_owned()).name = name.to_string().into();
|
||||
}
|
||||
|
||||
pub fn insert_sas(&mut self, sas: SasVerification) {
|
||||
@@ -686,7 +771,7 @@ impl RoomFocus {
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
pub enum IambBufferId {
|
||||
Command,
|
||||
Command(CommandType),
|
||||
Room(OwnedRoomId, RoomFocus),
|
||||
DirectList,
|
||||
MemberList(OwnedRoomId),
|
||||
@@ -699,7 +784,7 @@ pub enum IambBufferId {
|
||||
impl IambBufferId {
|
||||
pub fn to_window(&self) -> Option<IambId> {
|
||||
match self {
|
||||
IambBufferId::Command => None,
|
||||
IambBufferId::Command(_) => None,
|
||||
IambBufferId::Room(room, _) => Some(IambId::Room(room.clone())),
|
||||
IambBufferId::DirectList => Some(IambId::DirectList),
|
||||
IambBufferId::MemberList(room) => Some(IambId::MemberList(room.clone())),
|
||||
@@ -719,6 +804,133 @@ impl ApplicationInfo for IambInfo {
|
||||
type Action = IambAction;
|
||||
type WindowId = IambId;
|
||||
type ContentId = IambBufferId;
|
||||
|
||||
fn complete(
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
content: &IambBufferId,
|
||||
store: &mut ProgramStore,
|
||||
) -> Vec<String> {
|
||||
match content {
|
||||
IambBufferId::Command(CommandType::Command) => complete_cmdbar(text, cursor, store),
|
||||
IambBufferId::Command(CommandType::Search) => vec![],
|
||||
|
||||
IambBufferId::Room(_, RoomFocus::MessageBar) => {
|
||||
complete_matrix_names(text, cursor, store)
|
||||
},
|
||||
IambBufferId::Room(_, RoomFocus::Scrollback) => vec![],
|
||||
|
||||
IambBufferId::DirectList => vec![],
|
||||
IambBufferId::MemberList(_) => vec![],
|
||||
IambBufferId::RoomList => vec![],
|
||||
IambBufferId::SpaceList => vec![],
|
||||
IambBufferId::VerifyList => vec![],
|
||||
IambBufferId::Welcome => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn content_of_command(ct: CommandType) -> IambBufferId {
|
||||
IambBufferId::Command(ct)
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let id = text
|
||||
.get_prefix_word_mut(cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
let id = Cow::from(&id);
|
||||
|
||||
store
|
||||
.application
|
||||
.presences
|
||||
.complete(id.as_ref())
|
||||
.into_iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn complete_matrix_names(
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
store: &ProgramStore,
|
||||
) -> Vec<String> {
|
||||
let id = text
|
||||
.get_prefix_word_mut(cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
let id = Cow::from(&id);
|
||||
|
||||
let list = store.application.names.complete(id.as_ref());
|
||||
if !list.is_empty() {
|
||||
return list;
|
||||
}
|
||||
|
||||
let list = store.application.presences.complete(id.as_ref());
|
||||
if !list.is_empty() {
|
||||
return list.into_iter().map(|i| i.to_string()).collect();
|
||||
}
|
||||
|
||||
store
|
||||
.application
|
||||
.rooms
|
||||
.complete(id.as_ref())
|
||||
.into_iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let sc = text.get_prefix_word_mut(cursor, &WordStyle::Little);
|
||||
let sc = sc.unwrap_or_else(EditRope::empty);
|
||||
let sc = Cow::from(&sc);
|
||||
|
||||
store.application.emojis.complete(sc.as_ref())
|
||||
}
|
||||
|
||||
fn complete_cmdarg(
|
||||
desc: CommandDescription,
|
||||
text: &EditRope,
|
||||
cursor: &mut Cursor,
|
||||
store: &ProgramStore,
|
||||
) -> Vec<String> {
|
||||
let cmd = match store.application.cmds.get(desc.command.as_str()) {
|
||||
Ok(cmd) => cmd,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
match cmd.name.as_str() {
|
||||
"cancel" | "dms" | "edit" | "redact" | "reply" => vec![],
|
||||
"members" | "rooms" | "spaces" | "welcome" => vec![],
|
||||
"download" | "open" | "upload" => complete_path(text, cursor),
|
||||
"react" | "unreact" => complete_emoji(text, cursor, store),
|
||||
|
||||
"invite" => complete_users(text, cursor, store),
|
||||
"join" => complete_matrix_names(text, cursor, store),
|
||||
"room" => vec![],
|
||||
"verify" => vec![],
|
||||
_ => panic!("unknown command {}", cmd.name.as_str()),
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
|
||||
let eo = text.cursor_to_offset(cursor);
|
||||
let slice = text.slice(0.into(), eo, false);
|
||||
let cow = Cow::from(&slice);
|
||||
|
||||
match CommandDescription::from_str(cow.as_ref()) {
|
||||
Ok(desc) => {
|
||||
if desc.arg.untrimmed.is_empty() {
|
||||
// Complete command name and set cursor position.
|
||||
let _ = text.get_prefix_word_mut(cursor, &WordStyle::Little);
|
||||
store.application.cmds.complete_name(desc.command.as_str())
|
||||
} else {
|
||||
// Complete command argument.
|
||||
complete_cmdarg(desc, text, cursor, store)
|
||||
}
|
||||
},
|
||||
|
||||
// Can't parse command text, so return zero completions.
|
||||
Err(_) => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -804,4 +1016,44 @@ pub mod tests {
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_complete_cmdbar() {
|
||||
let store = mock_store().await;
|
||||
|
||||
let text = EditRope::from("invite ");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let id = text
|
||||
.get_prefix_word_mut(&mut cursor, &MATRIX_ID_WORD)
|
||||
.unwrap_or_else(EditRope::empty);
|
||||
assert_eq!(id.to_string(), "");
|
||||
assert_eq!(cursor, Cursor::new(0, 7));
|
||||
|
||||
let text = EditRope::from("invite ");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec![
|
||||
"@user1:example.com",
|
||||
"@user2:example.com",
|
||||
"@user3:example.com",
|
||||
"@user4:example.com",
|
||||
"@user5:example.com"
|
||||
]);
|
||||
|
||||
let text = EditRope::from("invite ignored");
|
||||
let mut cursor = Cursor::new(0, 7);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec![
|
||||
"@user1:example.com",
|
||||
"@user2:example.com",
|
||||
"@user3:example.com",
|
||||
"@user4:example.com",
|
||||
"@user5:example.com"
|
||||
]);
|
||||
|
||||
let text = EditRope::from("invite @user1ignored");
|
||||
let mut cursor = Cursor::new(0, 13);
|
||||
let res = complete_cmdbar(&text, &mut cursor, &store);
|
||||
assert_eq!(res, vec!["@user1:example.com"]);
|
||||
}
|
||||
}
|
||||
|
||||
145
src/commands.rs
145
src/commands.rs
@@ -4,13 +4,16 @@ use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
|
||||
|
||||
use modalkit::{
|
||||
editing::base::OpenTarget,
|
||||
env::vim::command::{CommandContext, CommandDescription},
|
||||
env::vim::command::{CommandContext, CommandDescription, OptionType},
|
||||
input::commands::{CommandError, CommandResult, CommandStep},
|
||||
input::InputContext,
|
||||
};
|
||||
|
||||
use crate::base::{
|
||||
CreateRoomFlags,
|
||||
CreateRoomType,
|
||||
DownloadFlags,
|
||||
HomeserverAction,
|
||||
IambAction,
|
||||
IambId,
|
||||
MessageAction,
|
||||
@@ -297,6 +300,53 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_create(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let args = desc.arg.options()?;
|
||||
let mut flags = CreateRoomFlags::NONE;
|
||||
let mut alias = None;
|
||||
let mut ct = CreateRoomType::Room;
|
||||
|
||||
for arg in args {
|
||||
match arg {
|
||||
OptionType::Flag(name, Some(arg)) => {
|
||||
match name.as_str() {
|
||||
"alias" => {
|
||||
if alias.is_some() {
|
||||
let msg = "Multiple ++alias arguments are not allowed";
|
||||
let err = CommandError::Error(msg.into());
|
||||
|
||||
return Err(err);
|
||||
} else {
|
||||
alias = Some(arg);
|
||||
}
|
||||
},
|
||||
_ => return Err(CommandError::InvalidArgument),
|
||||
}
|
||||
},
|
||||
OptionType::Flag(name, None) => {
|
||||
match name.as_str() {
|
||||
"public" => flags |= CreateRoomFlags::PUBLIC,
|
||||
"space" => ct = CreateRoomType::Space,
|
||||
"enc" | "encrypted" => flags |= CreateRoomFlags::ENCRYPTED,
|
||||
_ => return Err(CommandError::InvalidArgument),
|
||||
}
|
||||
},
|
||||
OptionType::Positional(_) => {
|
||||
let msg = ":create doesn't take any positional arguments";
|
||||
let err = CommandError::Error(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let hact = HomeserverAction::CreateRoom(alias, ct, flags);
|
||||
let iact = IambAction::from(hact);
|
||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
@@ -395,24 +445,81 @@ fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
}
|
||||
|
||||
fn add_iamb_commands(cmds: &mut ProgramCommands) {
|
||||
cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel });
|
||||
cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
|
||||
cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
|
||||
cmds.add_command(ProgramCommand { names: vec!["open".into()], f: iamb_open });
|
||||
cmds.add_command(ProgramCommand { names: vec!["edit".into()], f: iamb_edit });
|
||||
cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
|
||||
cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join });
|
||||
cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members });
|
||||
cmds.add_command(ProgramCommand { names: vec!["react".into()], f: iamb_react });
|
||||
cmds.add_command(ProgramCommand { names: vec!["redact".into()], f: iamb_redact });
|
||||
cmds.add_command(ProgramCommand { names: vec!["reply".into()], f: iamb_reply });
|
||||
cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms });
|
||||
cmds.add_command(ProgramCommand { names: vec!["room".into()], f: iamb_room });
|
||||
cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
|
||||
cmds.add_command(ProgramCommand { names: vec!["unreact".into()], f: iamb_unreact });
|
||||
cmds.add_command(ProgramCommand { names: vec!["upload".into()], f: iamb_upload });
|
||||
cmds.add_command(ProgramCommand { names: vec!["verify".into()], f: iamb_verify });
|
||||
cmds.add_command(ProgramCommand { names: vec!["welcome".into()], f: iamb_welcome });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "cancel".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_cancel,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "create".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_create,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "download".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_download,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "open".into(), aliases: vec![], f: iamb_open });
|
||||
cmds.add_command(ProgramCommand { name: "edit".into(), aliases: vec![], f: iamb_edit });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "invite".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_invite,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "join".into(), aliases: vec![], f: iamb_join });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "members".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_members,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "react".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_react,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "redact".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_redact,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "reply".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_reply,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "rooms".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_rooms,
|
||||
});
|
||||
cmds.add_command(ProgramCommand { name: "room".into(), aliases: vec![], f: iamb_room });
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "spaces".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_spaces,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "unreact".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_unreact,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "upload".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_upload,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "verify".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_verify,
|
||||
});
|
||||
cmds.add_command(ProgramCommand {
|
||||
name: "welcome".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_welcome,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn setup_commands() -> ProgramCommands {
|
||||
|
||||
@@ -1,34 +1,21 @@
|
||||
use modalkit::{
|
||||
editing::action::WindowAction,
|
||||
editing::base::WordStyle,
|
||||
env::vim::keybindings::{InputStep, VimBindings},
|
||||
env::vim::VimMode,
|
||||
input::bindings::{EdgeEvent, EdgeRepeat, InputBindings},
|
||||
input::key::TerminalKey,
|
||||
};
|
||||
|
||||
use crate::base::{IambAction, IambInfo, Keybindings};
|
||||
use crate::base::{IambAction, IambInfo, Keybindings, MATRIX_ID_WORD};
|
||||
|
||||
type IambStep = InputStep<IambInfo>;
|
||||
|
||||
/// Find the boundaries for a Matrix username, room alias, or room ID.
|
||||
///
|
||||
/// Technically "[" and "]" should be here since IPv6 addresses are allowed
|
||||
/// in the server name, but in practice that should be uncommon, and people
|
||||
/// can just use `gf` and friends in Visual mode instead.
|
||||
fn is_mxid_char(c: char) -> bool {
|
||||
return c >= 'a' && c <= 'z' ||
|
||||
c >= 'A' && c <= 'Z' ||
|
||||
c >= '0' && c <= '9' ||
|
||||
":-./@_#!".contains(c);
|
||||
}
|
||||
|
||||
pub fn setup_keybindings() -> Keybindings {
|
||||
let mut ism = Keybindings::empty();
|
||||
|
||||
let vim = VimBindings::default()
|
||||
.submit_on_enter()
|
||||
.cursor_open(WordStyle::CharSet(is_mxid_char));
|
||||
.cursor_open(MATRIX_ID_WORD.clone());
|
||||
|
||||
vim.setup(&mut ism);
|
||||
|
||||
|
||||
39
src/main.rs
39
src/main.rs
@@ -53,20 +53,19 @@ use crate::{
|
||||
base::{
|
||||
AsyncProgramStore,
|
||||
ChatStore,
|
||||
HomeserverAction,
|
||||
IambAction,
|
||||
IambBufferId,
|
||||
IambError,
|
||||
IambId,
|
||||
IambInfo,
|
||||
IambResult,
|
||||
ProgramAction,
|
||||
ProgramCommands,
|
||||
ProgramContext,
|
||||
ProgramStore,
|
||||
},
|
||||
config::{ApplicationSettings, Iamb},
|
||||
windows::IambWindow,
|
||||
worker::{ClientWorker, LoginStyle, Requester},
|
||||
worker::{create_room, ClientWorker, LoginStyle, Requester},
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
@@ -116,7 +115,6 @@ struct Application {
|
||||
terminal: Terminal<CrosstermBackend<Stdout>>,
|
||||
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
||||
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
||||
cmds: ProgramCommands,
|
||||
screen: ScreenState<IambWindow, IambInfo>,
|
||||
}
|
||||
|
||||
@@ -138,7 +136,6 @@ impl Application {
|
||||
|
||||
let bindings = crate::keybindings::setup_keybindings();
|
||||
let bindings = KeyManager::new(bindings);
|
||||
let cmds = crate::commands::setup_commands();
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
|
||||
@@ -149,7 +146,7 @@ impl Application {
|
||||
.or_else(|| IambWindow::open(IambId::Welcome, locked.deref_mut()).ok())
|
||||
.unwrap();
|
||||
|
||||
let cmd = CommandBarState::new(IambBufferId::Command, locked.deref_mut());
|
||||
let cmd = CommandBarState::new(locked.deref_mut());
|
||||
let screen = ScreenState::new(win, cmd);
|
||||
|
||||
let worker = locked.application.worker.clone();
|
||||
@@ -163,7 +160,6 @@ impl Application {
|
||||
terminal,
|
||||
bindings,
|
||||
actstack,
|
||||
cmds,
|
||||
screen,
|
||||
})
|
||||
}
|
||||
@@ -321,7 +317,7 @@ impl Application {
|
||||
None
|
||||
},
|
||||
Action::Command(act) => {
|
||||
let acts = self.cmds.command(&act, &ctx)?;
|
||||
let acts = store.application.cmds.command(&act, &ctx)?;
|
||||
self.action_prepend(acts);
|
||||
|
||||
None
|
||||
@@ -360,6 +356,12 @@ impl Application {
|
||||
None
|
||||
},
|
||||
|
||||
IambAction::Homeserver(act) => {
|
||||
let acts = self.homeserver_command(act, ctx, store).await?;
|
||||
self.action_prepend(acts);
|
||||
|
||||
None
|
||||
},
|
||||
IambAction::Message(act) => {
|
||||
self.screen.current_window_mut()?.message_command(act, ctx, store).await?
|
||||
},
|
||||
@@ -392,6 +394,25 @@ impl Application {
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
async fn homeserver_command(
|
||||
&mut self,
|
||||
action: HomeserverAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
|
||||
match action {
|
||||
HomeserverAction::CreateRoom(alias, vis, flags) => {
|
||||
let client = &store.application.worker.client;
|
||||
let room_id = create_room(client, alias.as_deref(), vis, flags).await?;
|
||||
let room = IambId::Room(room_id);
|
||||
let target = OpenTarget::Application(room);
|
||||
let action = WindowAction::Switch(target);
|
||||
|
||||
Ok(vec![(action.into(), ctx)])
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<(), std::io::Error> {
|
||||
self.terminal.clear()?;
|
||||
|
||||
@@ -523,7 +544,7 @@ fn main() -> IambResult<()> {
|
||||
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
.with_writer(appender)
|
||||
.with_max_level(Level::TRACE)
|
||||
.with_max_level(Level::INFO)
|
||||
.finish();
|
||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//! The Matrix specification recommends limiting rendered tags and attributes to a safe subset of
|
||||
//! HTML. You can read more in section 11.2.1.1, "m.room.message msgtypes":
|
||||
//!
|
||||
//! https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes
|
||||
//! <https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes>
|
||||
//!
|
||||
//! This isn't as important for iamb, since it isn't a browser environment, but we do still map
|
||||
//! input onto an enum of the safe list of tags to keep it easy to understand and process.
|
||||
@@ -271,10 +271,12 @@ impl StyleTreeNode {
|
||||
},
|
||||
StyleTreeNode::Header(child, level) => {
|
||||
let style = style.add_modifier(StyleModifier::BOLD);
|
||||
let mut hashes = "#".repeat(*level);
|
||||
hashes.push(' ');
|
||||
|
||||
printer.push_str(hashes, style);
|
||||
for _ in 0..*level {
|
||||
printer.push_str("#", style);
|
||||
}
|
||||
|
||||
printer.push_str(" ", style);
|
||||
child.print(printer, style);
|
||||
},
|
||||
StyleTreeNode::Image(None) => {},
|
||||
@@ -320,7 +322,9 @@ impl StyleTreeNode {
|
||||
printer.commit();
|
||||
},
|
||||
StyleTreeNode::Ruler => {
|
||||
printer.push_str(line::HORIZONTAL.repeat(width), style);
|
||||
for _ in 0..width {
|
||||
printer.push_str(line::HORIZONTAL, style);
|
||||
}
|
||||
},
|
||||
StyleTreeNode::Table(table) => {
|
||||
let text = table.to_text(width, style);
|
||||
@@ -636,8 +640,11 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("# ", bold),
|
||||
Span::styled("Header 1", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("1", bold),
|
||||
space_span(10, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -645,8 +652,12 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("## ", bold),
|
||||
Span::styled("Header 2", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("2", bold),
|
||||
space_span(9, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -654,8 +665,13 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("### ", bold),
|
||||
Span::styled("Header 3", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("3", bold),
|
||||
space_span(8, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -663,8 +679,14 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("#### ", bold),
|
||||
Span::styled("Header 4", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("4", bold),
|
||||
space_span(7, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -672,8 +694,15 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("##### ", bold),
|
||||
Span::styled("Header 5", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("5", bold),
|
||||
space_span(6, Style::default())
|
||||
])]);
|
||||
|
||||
@@ -681,8 +710,16 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("###### ", bold),
|
||||
Span::styled("Header 6", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled("#", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("Header", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("6", bold),
|
||||
space_span(5, Style::default())
|
||||
])]);
|
||||
}
|
||||
@@ -700,7 +737,8 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Bold!", bold),
|
||||
Span::styled("Bold", bold),
|
||||
Span::styled("!", bold),
|
||||
space_span(15, def)
|
||||
])]);
|
||||
|
||||
@@ -708,7 +746,8 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Bold!", bold),
|
||||
Span::styled("Bold", bold),
|
||||
Span::styled("!", bold),
|
||||
space_span(15, def)
|
||||
])]);
|
||||
|
||||
@@ -716,7 +755,8 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Italic!", italic),
|
||||
Span::styled("Italic", italic),
|
||||
Span::styled("!", italic),
|
||||
space_span(13, def)
|
||||
])]);
|
||||
|
||||
@@ -724,7 +764,8 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Italic!", italic),
|
||||
Span::styled("Italic", italic),
|
||||
Span::styled("!", italic),
|
||||
space_span(13, def)
|
||||
])]);
|
||||
|
||||
@@ -732,7 +773,8 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Strikethrough!", strike),
|
||||
Span::styled("Strikethrough", strike),
|
||||
Span::styled("!", strike),
|
||||
space_span(6, def)
|
||||
])]);
|
||||
|
||||
@@ -740,7 +782,8 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Strikethrough!", strike),
|
||||
Span::styled("Strikethrough", strike),
|
||||
Span::styled("!", strike),
|
||||
space_span(6, def)
|
||||
])]);
|
||||
|
||||
@@ -748,19 +791,28 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Underline!", underl),
|
||||
Span::styled("Underline", underl),
|
||||
Span::styled("!", underl),
|
||||
space_span(10, def)
|
||||
])]);
|
||||
|
||||
let s = "<font color=\"#ff0000\">Red!</u>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Red", red),
|
||||
Span::styled("!", red),
|
||||
space_span(16, def)
|
||||
])]);
|
||||
|
||||
let s = "<font color=\"red\">Red!</u>";
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(20, Style::default(), false);
|
||||
assert_eq!(text.lines, vec![Spans(vec![Span::styled("Red!", red), space_span(16, def)])]);
|
||||
assert_eq!(text.lines, vec![Spans(vec![
|
||||
Span::styled("Red", red),
|
||||
Span::styled("!", red),
|
||||
space_span(16, def)
|
||||
])]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -769,13 +821,25 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 7);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("Hello worl")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("d!"), Span::raw(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![Span::raw("Hello"), Span::raw(" "), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw("world"), Span::raw("!"), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw(" ")]));
|
||||
assert_eq!(text.lines[3], Spans(vec![Span::raw("Content"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[4], Spans(vec![Span::raw(" ")]));
|
||||
assert_eq!(text.lines[5], Spans(vec![Span::raw("Goodbye wo")]));
|
||||
assert_eq!(text.lines[6], Spans(vec![Span::raw("rld!"), Span::raw(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[5],
|
||||
Spans(vec![Span::raw("Goodbye"), Span::raw(" "), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[6],
|
||||
Spans(vec![Span::raw("world"), Span::raw("!"), Span::raw(" ")])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -784,8 +848,14 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 2);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw(" "), Span::raw("Hello ")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("world!")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![Span::raw(" "), Span::raw("Hello"), Span::raw(" ")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw(" "), Span::raw("world"), Span::raw("!")])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -794,12 +864,60 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(8, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 6);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("- "), Span::raw("List I")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("- "), Span::raw("List I")]));
|
||||
assert_eq!(text.lines[3], Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[4], Spans(vec![Span::raw("- "), Span::raw("List I")]));
|
||||
assert_eq!(text.lines[5], Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("- "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("1")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw("- "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("2")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[4],
|
||||
Spans(vec![
|
||||
Span::raw("- "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[5],
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("3")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -808,20 +926,59 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(9, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 6);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("1. "), Span::raw("List I")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("1. "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 1"), Span::raw(" ")])
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("1")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw("2. "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("2. "), Span::raw("List I")]));
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 2"), Span::raw(" ")])
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("2")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[4],
|
||||
Spans(vec![
|
||||
Span::raw("3. "),
|
||||
Span::raw("List"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(text.lines[4], Spans(vec![Span::raw("3. "), Span::raw("List I")]));
|
||||
assert_eq!(
|
||||
text.lines[5],
|
||||
Spans(vec![Span::raw(" "), Span::raw("tem 3"), Span::raw(" ")])
|
||||
Spans(vec![
|
||||
Span::raw(" "),
|
||||
Span::raw("Item"),
|
||||
Span::raw(" "),
|
||||
Span::raw("3")
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -854,9 +1011,13 @@ pub mod tests {
|
||||
]);
|
||||
assert_eq!(text.lines[2].0, vec![
|
||||
Span::raw("│"),
|
||||
Span::styled("mn 1", bold),
|
||||
Span::styled("mn", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("1", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("mn 2", bold),
|
||||
Span::styled("mn", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("2", bold),
|
||||
Span::raw("│"),
|
||||
Span::styled("umn", bold),
|
||||
Span::raw("│")
|
||||
@@ -867,7 +1028,8 @@ pub mod tests {
|
||||
Span::raw("│"),
|
||||
Span::raw(" "),
|
||||
Span::raw("│"),
|
||||
Span::styled(" 3", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::styled("3", bold),
|
||||
Span::styled(" ", bold),
|
||||
Span::raw("│")
|
||||
]);
|
||||
@@ -928,15 +1090,61 @@ pub mod tests {
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), false);
|
||||
assert_eq!(text.lines.len(), 4);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("This was r")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("eplied to"), Span::raw(" ")]));
|
||||
assert_eq!(text.lines[2], Spans(vec![Span::raw("This is th")]));
|
||||
assert_eq!(text.lines[3], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("This"),
|
||||
Span::raw(" "),
|
||||
Span::raw("was"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![Span::raw("replied"), Span::raw(" "), Span::raw("to")])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[2],
|
||||
Spans(vec![
|
||||
Span::raw("This"),
|
||||
Span::raw(" "),
|
||||
Span::raw("is"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[3],
|
||||
Spans(vec![
|
||||
Span::raw("the"),
|
||||
Span::raw(" "),
|
||||
Span::raw("reply"),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
|
||||
let tree = parse_matrix_html(s);
|
||||
let text = tree.to_text(10, Style::default(), true);
|
||||
assert_eq!(text.lines.len(), 2);
|
||||
assert_eq!(text.lines[0], Spans(vec![Span::raw("This is th")]));
|
||||
assert_eq!(text.lines[1], Spans(vec![Span::raw("e reply"), Span::raw(" ")]));
|
||||
assert_eq!(
|
||||
text.lines[0],
|
||||
Spans(vec![
|
||||
Span::raw("This"),
|
||||
Span::raw(" "),
|
||||
Span::raw("is"),
|
||||
Span::raw(" "),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
text.lines[1],
|
||||
Spans(vec![
|
||||
Span::raw("the"),
|
||||
Span::raw(" "),
|
||||
Span::raw("reply"),
|
||||
Span::raw(" ")
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -710,7 +710,11 @@ impl Message {
|
||||
key
|
||||
};
|
||||
|
||||
emojis.push_str(format!("[{name} {count}]"), style);
|
||||
emojis.push_str("[", style);
|
||||
emojis.push_str(name, style);
|
||||
emojis.push_str(" ", style);
|
||||
emojis.push_span_nobreak(Span::styled(count.to_string(), style));
|
||||
emojis.push_str("]", style);
|
||||
|
||||
reactions += 1;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::borrow::Cow;
|
||||
use modalkit::tui::layout::Alignment;
|
||||
use modalkit::tui::style::Style;
|
||||
use modalkit::tui::text::{Span, Spans, Text};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::util::{space_span, take_width};
|
||||
@@ -107,7 +108,7 @@ impl<'a> TextPrinter<'a> {
|
||||
self.push();
|
||||
}
|
||||
|
||||
pub fn push_str<T>(&mut self, s: T, style: Style)
|
||||
fn push_str_wrapped<T>(&mut self, s: T, style: Style)
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
@@ -140,6 +141,55 @@ impl<'a> TextPrinter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_span_nobreak(&mut self, span: Span<'a>) {
|
||||
let sw = UnicodeWidthStr::width(span.content.as_ref());
|
||||
|
||||
if self.curr_width + sw > self.width {
|
||||
// Span doesn't fit on this line, so start a new one.
|
||||
self.commit();
|
||||
}
|
||||
|
||||
self.curr_spans.push(span);
|
||||
self.curr_width += sw;
|
||||
}
|
||||
|
||||
pub fn push_str(&mut self, s: &'a str, style: Style) {
|
||||
let style = self.base_style.patch(style);
|
||||
|
||||
for word in UnicodeSegmentation::split_word_bounds(s) {
|
||||
if self.width == 0 && word.chars().all(char::is_whitespace) {
|
||||
// Drop leading whitespace.
|
||||
continue;
|
||||
}
|
||||
|
||||
let sw = UnicodeWidthStr::width(word);
|
||||
|
||||
if sw > self.width {
|
||||
self.push_str_wrapped(word, style);
|
||||
continue;
|
||||
}
|
||||
|
||||
if self.curr_width + sw > self.width {
|
||||
// Word doesn't fit on this line, so start a new one.
|
||||
self.commit();
|
||||
|
||||
if word.chars().all(char::is_whitespace) {
|
||||
// Drop leading whitespace.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let span = Span::styled(word, style);
|
||||
self.curr_spans.push(span);
|
||||
self.curr_width += sw;
|
||||
}
|
||||
|
||||
if self.curr_width == self.width {
|
||||
// If the last bit fills the full line, start a new one.
|
||||
self.push();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_line(&mut self, spans: Spans<'a>) {
|
||||
self.commit();
|
||||
self.text.lines.push(spans);
|
||||
|
||||
@@ -208,6 +208,14 @@ pub async fn mock_store() -> ProgramStore {
|
||||
let worker = Requester { tx, client };
|
||||
|
||||
let mut store = ChatStore::new(worker, mock_settings());
|
||||
|
||||
// Add presence information.
|
||||
store.presences.get_or_default(TEST_USER1.clone());
|
||||
store.presences.get_or_default(TEST_USER2.clone());
|
||||
store.presences.get_or_default(TEST_USER3.clone());
|
||||
store.presences.get_or_default(TEST_USER4.clone());
|
||||
store.presences.get_or_default(TEST_USER5.clone());
|
||||
|
||||
let room_id = TEST_ROOM1_ID.clone();
|
||||
let info = mock_room();
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
use matrix_sdk::{
|
||||
encryption::verification::{format_emojis, SasVerification},
|
||||
@@ -45,7 +44,9 @@ use modalkit::{
|
||||
ScrollStyle,
|
||||
ViewportContext,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
completion::CompletionList,
|
||||
},
|
||||
widgets::{
|
||||
list::{List, ListCursor, ListItem, ListState},
|
||||
@@ -469,6 +470,19 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
delegate!(self, w => w.close(flags, store))
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
path: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
delegate!(self, w => w.write(path, flags, store))
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
delegate!(self, w => w.get_completions())
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
delegate!(self, w => w.get_cursor_word(style))
|
||||
}
|
||||
@@ -575,21 +589,18 @@ impl Window<IambInfo> for IambWindow {
|
||||
fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> {
|
||||
let ChatStore { names, worker, .. } = &mut store.application;
|
||||
|
||||
match names.entry(name) {
|
||||
Entry::Vacant(v) => {
|
||||
let room_id = worker.join_room(v.key().to_string())?;
|
||||
v.insert(room_id.clone());
|
||||
if let Some(room) = names.get_mut(&name) {
|
||||
let id = IambId::Room(room.clone());
|
||||
|
||||
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, tags, store);
|
||||
IambWindow::open(id, store)
|
||||
} else {
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
|
||||
Ok(room.into())
|
||||
},
|
||||
Entry::Occupied(o) => {
|
||||
let id = IambId::Room(o.get().clone());
|
||||
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, tags, store);
|
||||
|
||||
IambWindow::open(id, store)
|
||||
},
|
||||
Ok(room.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -620,11 +631,16 @@ impl RoomItem {
|
||||
store: &mut ProgramStore,
|
||||
) -> Self {
|
||||
let name = name.to_string();
|
||||
let room_id = room.room_id();
|
||||
|
||||
let info = store.application.get_room_info(room.room_id().to_owned());
|
||||
let info = store.application.get_room_info(room_id.to_owned());
|
||||
info.name = name.clone().into();
|
||||
info.tags = tags.clone();
|
||||
|
||||
if let Some(alias) = room.canonical_alias() {
|
||||
store.application.names.insert(alias.to_string(), room_id.to_owned());
|
||||
}
|
||||
|
||||
RoomItem { room, tags, name }
|
||||
}
|
||||
}
|
||||
@@ -772,8 +788,13 @@ pub struct SpaceItem {
|
||||
impl SpaceItem {
|
||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||
let name = name.to_string();
|
||||
let room_id = room.room_id();
|
||||
|
||||
store.application.set_room_name(room.room_id(), name.as_str());
|
||||
store.application.set_room_name(room_id, name.as_str());
|
||||
|
||||
if let Some(alias) = room.canonical_alias() {
|
||||
store.application.names.insert(alias.to_string(), room_id.to_owned());
|
||||
}
|
||||
|
||||
SpaceItem { room, name }
|
||||
}
|
||||
|
||||
@@ -52,7 +52,8 @@ use modalkit::editing::{
|
||||
Scrollable,
|
||||
UIError,
|
||||
},
|
||||
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle},
|
||||
base::{CloseFlags, Count, MoveDir1D, PositionList, ScrollStyle, WordStyle, WriteFlags},
|
||||
completion::CompletionList,
|
||||
context::Resolve,
|
||||
history::{self, HistoryList},
|
||||
rope::EditRope,
|
||||
@@ -154,7 +155,7 @@ impl ChatState {
|
||||
let client = &store.application.worker.client;
|
||||
|
||||
let settings = &store.application.settings;
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
|
||||
let msg = self
|
||||
.scrollback
|
||||
@@ -389,7 +390,7 @@ impl ChatState {
|
||||
.client
|
||||
.get_joined_room(self.id())
|
||||
.ok_or(IambError::NotJoined)?;
|
||||
let info = store.application.rooms.entry(self.id().to_owned()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.id().to_owned());
|
||||
let mut show_echo = true;
|
||||
|
||||
let (event_id, msg) = match act {
|
||||
@@ -550,6 +551,21 @@ impl WindowOps<IambInfo> for ChatState {
|
||||
true
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
_: Option<&str>,
|
||||
_: WriteFlags,
|
||||
_: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
// XXX: what's the right writing behaviour for a room?
|
||||
// Should write send a message?
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
delegate!(self, w => w.get_completions())
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
delegate!(self, w => w.get_cursor_word(style))
|
||||
}
|
||||
|
||||
@@ -40,7 +40,9 @@ use modalkit::{
|
||||
PositionList,
|
||||
ScrollStyle,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
editing::completion::CompletionList,
|
||||
input::InputContext,
|
||||
widgets::{TermOffset, TerminalCursor, WindowOps},
|
||||
};
|
||||
@@ -183,8 +185,16 @@ impl RoomState {
|
||||
match act {
|
||||
RoomAction::InviteAccept => {
|
||||
if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
|
||||
let details = room.invite_details().await.map_err(IambError::from)?;
|
||||
let details = details.invitee.event().original_content();
|
||||
let is_direct = details.and_then(|ev| ev.is_direct).unwrap_or_default();
|
||||
|
||||
room.accept_invitation().await.map_err(IambError::from)?;
|
||||
|
||||
if is_direct {
|
||||
room.set_is_direct(true).await.map_err(IambError::from)?;
|
||||
}
|
||||
|
||||
Ok(vec![])
|
||||
} else {
|
||||
Err(IambError::NotInvited.into())
|
||||
@@ -383,10 +393,30 @@ impl WindowOps<IambInfo> for RoomState {
|
||||
}
|
||||
}
|
||||
|
||||
fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
|
||||
// XXX: what's the right closing behaviour for a room?
|
||||
// Should write send a message?
|
||||
true
|
||||
fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.close(flags, store),
|
||||
RoomState::Space(space) => space.close(flags, store),
|
||||
}
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
path: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.write(path, flags, store),
|
||||
RoomState::Space(space) => space.write(path, flags, store),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.get_completions(),
|
||||
RoomState::Space(space) => space.get_completions(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
|
||||
@@ -32,6 +32,9 @@ use modalkit::editing::{
|
||||
base::{
|
||||
Axis,
|
||||
CloseFlags,
|
||||
CompletionDisplay,
|
||||
CompletionSelection,
|
||||
CompletionType,
|
||||
Count,
|
||||
EditRange,
|
||||
EditTarget,
|
||||
@@ -51,7 +54,9 @@ use modalkit::editing::{
|
||||
TargetShape,
|
||||
ViewportContext,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
completion::CompletionList,
|
||||
context::{EditContext, Resolve},
|
||||
cursor::{CursorGroup, CursorState},
|
||||
history::HistoryList,
|
||||
@@ -60,7 +65,7 @@ use modalkit::editing::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
base::{IambBufferId, IambInfo, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||
base::{IambBufferId, IambInfo, IambResult, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||
config::ApplicationSettings,
|
||||
message::{Message, MessageCursor, MessageKey, Messages},
|
||||
};
|
||||
@@ -515,6 +520,23 @@ impl WindowOps<IambInfo> for ScrollbackState {
|
||||
true
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
_: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
_: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
if flags.contains(WriteFlags::FORCE) {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(EditError::ReadOnly.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
None
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, _: &WordStyle) -> Option<String> {
|
||||
None
|
||||
}
|
||||
@@ -532,7 +554,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
ctx: &ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let key = if let Some(k) = self.cursor.to_key(info) {
|
||||
k.clone()
|
||||
} else {
|
||||
@@ -762,6 +784,17 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
}
|
||||
}
|
||||
|
||||
fn complete(
|
||||
&mut self,
|
||||
_: &CompletionType,
|
||||
_: &CompletionSelection,
|
||||
_: &CompletionDisplay,
|
||||
_: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
Err(EditError::ReadOnly)
|
||||
}
|
||||
|
||||
fn insert_text(
|
||||
&mut self,
|
||||
_: &InsertTextAction,
|
||||
@@ -867,9 +900,9 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
EditorAction::Mark(name) => self.mark(ctx.resolve(name), ctx, store),
|
||||
EditorAction::Selection(act) => self.selection_command(act, ctx, store),
|
||||
|
||||
EditorAction::Complete(_, _) => {
|
||||
let msg = "";
|
||||
let err = EditError::Unimplemented(msg.into());
|
||||
EditorAction::Complete(_, _, _) => {
|
||||
let msg = "Nothing to complete in message scrollback";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
Err(err)
|
||||
},
|
||||
@@ -991,7 +1024,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
ctx: &ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let settings = &store.application.settings;
|
||||
let mut corner = self.viewctx.corner.clone();
|
||||
|
||||
@@ -1105,7 +1138,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
Err(err)
|
||||
},
|
||||
Axis::Vertical => {
|
||||
let info = store.application.rooms.entry(self.room_id.clone()).or_default();
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let settings = &store.application.settings;
|
||||
|
||||
if let Some(key) = self.cursor.to_key(info).cloned() {
|
||||
@@ -1193,7 +1226,7 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
||||
type State = ScrollbackState;
|
||||
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||
let info = self.store.application.rooms.entry(state.room_id.clone()).or_default();
|
||||
let info = self.store.application.rooms.get_or_default(state.room_id.clone());
|
||||
let settings = &self.store.application.settings;
|
||||
let area = info.render_typing(area, buf, &self.store.application.settings);
|
||||
|
||||
|
||||
@@ -8,9 +8,11 @@ use modalkit::{
|
||||
widgets::{TermOffset, TerminalCursor},
|
||||
};
|
||||
|
||||
use modalkit::editing::base::{CloseFlags, WordStyle};
|
||||
use modalkit::editing::action::EditInfo;
|
||||
use modalkit::editing::base::{CloseFlags, WordStyle, WriteFlags};
|
||||
use modalkit::editing::completion::CompletionList;
|
||||
|
||||
use crate::base::{IambBufferId, IambInfo, ProgramStore};
|
||||
use crate::base::{IambBufferId, IambInfo, IambResult, ProgramStore};
|
||||
|
||||
const WELCOME_TEXT: &str = include_str!("welcome.md");
|
||||
|
||||
@@ -63,6 +65,19 @@ impl WindowOps<IambInfo> for WelcomeState {
|
||||
self.tbox.close(flags, store)
|
||||
}
|
||||
|
||||
fn write(
|
||||
&mut self,
|
||||
path: Option<&str>,
|
||||
flags: WriteFlags,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
self.tbox.write(path, flags, store)
|
||||
}
|
||||
|
||||
fn get_completions(&self) -> Option<CompletionList> {
|
||||
self.tbox.get_completions()
|
||||
}
|
||||
|
||||
fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
|
||||
self.tbox.get_cursor_word(style)
|
||||
}
|
||||
|
||||
130
src/worker.rs
130
src/worker.rs
@@ -20,10 +20,11 @@ use matrix_sdk::{
|
||||
room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
|
||||
ruma::{
|
||||
api::client::{
|
||||
room::create_room::v3::{Request as CreateRoomRequest, RoomPreset},
|
||||
room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
|
||||
room::Visibility,
|
||||
space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
|
||||
},
|
||||
assign,
|
||||
events::{
|
||||
key::verification::{
|
||||
done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent},
|
||||
@@ -32,18 +33,26 @@ use matrix_sdk::{
|
||||
start::{OriginalSyncKeyVerificationStartEvent, ToDeviceKeyVerificationStartEvent},
|
||||
VerificationMethod,
|
||||
},
|
||||
presence::PresenceEvent,
|
||||
reaction::ReactionEventContent,
|
||||
room::{
|
||||
encryption::RoomEncryptionEventContent,
|
||||
message::{MessageType, RoomMessageEventContent},
|
||||
name::RoomNameEventContent,
|
||||
redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
|
||||
},
|
||||
tag::Tags,
|
||||
typing::SyncTypingEvent,
|
||||
AnyInitialStateEvent,
|
||||
AnyTimelineEvent,
|
||||
EmptyStateKey,
|
||||
InitialStateEvent,
|
||||
SyncMessageLikeEvent,
|
||||
SyncStateEvent,
|
||||
},
|
||||
room::RoomType,
|
||||
serde::Raw,
|
||||
EventEncryptionAlgorithm,
|
||||
OwnedRoomId,
|
||||
OwnedRoomOrAliasId,
|
||||
OwnedUserId,
|
||||
@@ -57,7 +66,16 @@ use matrix_sdk::{
|
||||
use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
|
||||
|
||||
use crate::{
|
||||
base::{AsyncProgramStore, EventLocation, IambError, IambResult, Receipts, VerifyAction},
|
||||
base::{
|
||||
AsyncProgramStore,
|
||||
CreateRoomFlags,
|
||||
CreateRoomType,
|
||||
EventLocation,
|
||||
IambError,
|
||||
IambResult,
|
||||
Receipts,
|
||||
VerifyAction,
|
||||
},
|
||||
message::MessageFetchResult,
|
||||
ApplicationSettings,
|
||||
};
|
||||
@@ -70,6 +88,77 @@ fn initial_devname() -> String {
|
||||
format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
|
||||
}
|
||||
|
||||
pub async fn create_room(
|
||||
client: &Client,
|
||||
room_alias_name: Option<&str>,
|
||||
rt: CreateRoomType,
|
||||
flags: CreateRoomFlags,
|
||||
) -> IambResult<OwnedRoomId> {
|
||||
let mut creation_content = None;
|
||||
let mut initial_state = vec![];
|
||||
let mut is_direct = false;
|
||||
let mut preset = None;
|
||||
let mut invite = vec![];
|
||||
|
||||
let visibility = if flags.contains(CreateRoomFlags::PUBLIC) {
|
||||
Visibility::Public
|
||||
} else {
|
||||
Visibility::Private
|
||||
};
|
||||
|
||||
match rt {
|
||||
CreateRoomType::Direct(user) => {
|
||||
invite.push(user);
|
||||
is_direct = true;
|
||||
preset = Some(RoomPreset::TrustedPrivateChat);
|
||||
},
|
||||
CreateRoomType::Space => {
|
||||
let mut cc = CreationContent::new();
|
||||
cc.room_type = Some(RoomType::Space);
|
||||
|
||||
let raw_cc = Raw::new(&cc).map_err(IambError::from)?;
|
||||
creation_content = Some(raw_cc);
|
||||
},
|
||||
CreateRoomType::Room => {},
|
||||
}
|
||||
|
||||
// Set up encryption.
|
||||
if flags.contains(CreateRoomFlags::ENCRYPTED) {
|
||||
// XXX: Once matrix-sdk uses ruma 0.8, then this can skip the cast.
|
||||
let algo = EventEncryptionAlgorithm::MegolmV1AesSha2;
|
||||
let content = RoomEncryptionEventContent::new(algo);
|
||||
let encr = InitialStateEvent { content, state_key: EmptyStateKey };
|
||||
let encr_raw = Raw::new(&encr).map_err(IambError::from)?;
|
||||
let encr_raw = encr_raw.cast::<AnyInitialStateEvent>();
|
||||
initial_state.push(encr_raw);
|
||||
}
|
||||
|
||||
let request = assign!(CreateRoomRequest::new(), {
|
||||
room_alias_name,
|
||||
creation_content,
|
||||
initial_state: initial_state.as_slice(),
|
||||
invite: invite.as_slice(),
|
||||
is_direct,
|
||||
visibility,
|
||||
preset,
|
||||
});
|
||||
|
||||
let resp = client.create_room(request).await.map_err(IambError::from)?;
|
||||
|
||||
if is_direct {
|
||||
if let Some(room) = client.get_room(&resp.room_id) {
|
||||
room.set_is_direct(true).await.map_err(IambError::from)?;
|
||||
} else {
|
||||
error!(
|
||||
room_id = resp.room_id.as_str(),
|
||||
"Couldn't set is_direct for new direct message room"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(resp.room_id);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LoginStyle {
|
||||
SessionRestore(Session),
|
||||
@@ -503,6 +592,15 @@ impl ClientWorker {
|
||||
},
|
||||
);
|
||||
|
||||
let _ =
|
||||
self.client
|
||||
.add_event_handler(|ev: PresenceEvent, store: Ctx<AsyncProgramStore>| {
|
||||
async move {
|
||||
let mut locked = store.lock().await;
|
||||
locked.application.presences.insert(ev.sender, ev.content.presence);
|
||||
}
|
||||
});
|
||||
|
||||
let _ = self.client.add_event_handler(
|
||||
|ev: SyncStateEvent<RoomNameEventContent>,
|
||||
room: MatrixRoom,
|
||||
@@ -513,8 +611,7 @@ impl ClientWorker {
|
||||
let room_id = room.room_id().to_owned();
|
||||
let room_name = Some(room_name.to_string());
|
||||
let mut locked = store.lock().await;
|
||||
let mut info =
|
||||
locked.application.rooms.entry(room_id.to_owned()).or_default();
|
||||
let mut info = locked.application.rooms.get_or_default(room_id.clone());
|
||||
info.name = room_name;
|
||||
}
|
||||
}
|
||||
@@ -529,8 +626,6 @@ impl ClientWorker {
|
||||
store: Ctx<AsyncProgramStore>| {
|
||||
async move {
|
||||
let room_id = room.room_id();
|
||||
let room_name = room.display_name().await.ok();
|
||||
let room_name = room_name.as_ref().map(ToString::to_string);
|
||||
|
||||
if let Some(msg) = ev.as_original() {
|
||||
if let MessageType::VerificationRequest(_) = msg.content.msgtype {
|
||||
@@ -545,8 +640,11 @@ impl ClientWorker {
|
||||
}
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
let mut info = locked.application.get_room_info(room_id.to_owned());
|
||||
info.name = room_name;
|
||||
|
||||
let sender = ev.sender().to_owned();
|
||||
let _ = locked.application.presences.get_or_default(sender);
|
||||
|
||||
let info = locked.application.get_room_info(room_id.to_owned());
|
||||
info.insert(ev.into_full_event(room_id.to_owned()));
|
||||
}
|
||||
},
|
||||
@@ -560,6 +658,10 @@ impl ClientWorker {
|
||||
let room_id = room.room_id();
|
||||
|
||||
let mut locked = store.lock().await;
|
||||
|
||||
let sender = ev.sender().to_owned();
|
||||
let _ = locked.application.presences.get_or_default(sender);
|
||||
|
||||
let info = locked.application.get_room_info(room_id.to_owned());
|
||||
info.insert_reaction(ev.into_full_event(room_id.to_owned()));
|
||||
}
|
||||
@@ -774,15 +876,11 @@ impl ClientWorker {
|
||||
}
|
||||
}
|
||||
|
||||
let mut request = CreateRoomRequest::new();
|
||||
let invite = [user.clone()];
|
||||
request.is_direct = true;
|
||||
request.invite = &invite;
|
||||
request.visibility = Visibility::Private;
|
||||
request.preset = Some(RoomPreset::PrivateChat);
|
||||
let rt = CreateRoomType::Direct(user.clone());
|
||||
let flags = CreateRoomFlags::ENCRYPTED;
|
||||
|
||||
match self.client.create_room(request).await {
|
||||
Ok(resp) => self.get_room(resp.room_id).await,
|
||||
match create_room(&self.client, None, rt, flags).await {
|
||||
Ok(room_id) => self.get_room(room_id).await,
|
||||
Err(e) => {
|
||||
error!(
|
||||
user_id = user.as_str(),
|
||||
|
||||
Reference in New Issue
Block a user