Compare commits
81 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89bb107c87 | ||
|
|
ca4c0034d9 | ||
|
|
bb30cecc63 | ||
|
|
7b050f82aa | ||
|
|
b1ccec6732 | ||
|
|
6e8e12b579 | ||
|
|
3da9835a17 | ||
|
|
64891ec68f | ||
|
|
61aba80be1 | ||
|
|
8d4539831f | ||
|
|
7c39e88ba2 | ||
|
|
758397eb5a | ||
|
|
1a0af6df37 | ||
|
|
885b56038f | ||
|
|
430c601ff2 | ||
|
|
0ddefcd7b3 | ||
|
|
2a573b6056 | ||
|
|
a020b860dd | ||
|
|
6c031f589e | ||
|
|
b0256d7120 | ||
|
|
0f870367b3 | ||
|
|
8d22b83d85 | ||
|
|
529073f4d4 | ||
|
|
17c87a617e | ||
|
|
2899d4f45a | ||
|
|
ad8b4a60d2 | ||
|
|
4935899aed | ||
|
|
cc1d2f3bf8 | ||
|
|
5df9fe7960 | ||
|
|
a5c25f2487 | ||
|
|
50023bad40 | ||
|
|
b6a318dfe3 | ||
|
|
ad3b40d538 | ||
|
|
953be6a195 | ||
|
|
463d46b8ab | ||
|
|
274234ce4c | ||
|
|
a2590b6bbb | ||
|
|
725ebb9fd6 | ||
|
|
ca395097e1 | ||
|
|
e98d58a8cc | ||
|
|
e6cdd02f22 | ||
|
|
0bc4ff07b0 | ||
|
|
14fe916d94 | ||
|
|
db35581d07 | ||
|
|
7c1c62897a | ||
|
|
61897ea6f2 | ||
|
|
6a0722795a | ||
|
|
f3bbc6ad9f | ||
|
|
2dd8c0fddf | ||
|
|
a786369b14 | ||
|
|
066f60ad32 | ||
|
|
10b142c071 | ||
|
|
ac6ff63d25 | ||
|
|
54a0e76823 | ||
|
|
93eff79f79 | ||
|
|
11625262f1 | ||
|
|
0ed1d53946 | ||
|
|
e3be8c16cb | ||
|
|
4c5c57e26c | ||
|
|
8eef8787cc | ||
|
|
c9c547acc1 | ||
|
|
3629f15e0d | ||
|
|
fd72cf5c4e | ||
|
|
1d93461183 | ||
|
|
a1574c6b8d | ||
|
|
e8205df21d | ||
|
|
8c010d7e7e | ||
|
|
4337be108b | ||
|
|
b968d8c4a2 | ||
|
|
5683a2e7a8 | ||
|
|
afe892c7fe | ||
|
|
d8713141f2 | ||
|
|
a6888bbc93 | ||
|
|
4f2261e66f | ||
|
|
8966644f6e | ||
|
|
69125e3fc4 | ||
|
|
56ec90523c | ||
|
|
d13d4b9f7f | ||
|
|
54ce042384 | ||
|
|
b6f4b03c12 | ||
|
|
504b520fe1 |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
ko_fi: ulyssam
|
||||
58
.github/workflows/ci.yml
vendored
58
.github/workflows/ci.yml
vendored
@@ -9,25 +9,6 @@ on:
|
||||
name: CI
|
||||
|
||||
jobs:
|
||||
clippy_check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v1
|
||||
with:
|
||||
submodules: true
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
override: true
|
||||
- name: Check Clippy
|
||||
uses: actions-rs/clippy-check@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
toolchain: stable
|
||||
args:
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
@@ -35,26 +16,41 @@ jobs:
|
||||
runs-on: ${{ matrix.platform }}
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
- name: Install Rust (1.66 w/ clippy)
|
||||
uses: dtolnay/rust-toolchain@1.66
|
||||
with:
|
||||
toolchain: nightly
|
||||
override: true
|
||||
components: rustfmt, clippy
|
||||
components: clippy
|
||||
- name: Install Rust (nightly w/ rustfmt)
|
||||
run: rustup toolchain install nightly --component rustfmt
|
||||
- name: Cache cargo registry
|
||||
uses: actions/cache@v1
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.cargo/registry
|
||||
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
||||
- name: Cache cargo build
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: target
|
||||
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
|
||||
- name: Check formatting
|
||||
uses: actions-rs/cargo@v1
|
||||
run: cargo +nightly fmt --all -- --check
|
||||
- name: Check Clippy
|
||||
if: matrix.platform == 'ubuntu-latest'
|
||||
uses: giraffate/clippy-action@v1
|
||||
with:
|
||||
command: fmt
|
||||
args: --all -- --check
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
reporter: 'github-check'
|
||||
- name: Run tests
|
||||
uses: actions-rs/cargo@v1
|
||||
run: cargo test
|
||||
- name: Build artifacts
|
||||
run: cargo build --release
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@master
|
||||
with:
|
||||
command: test
|
||||
name: iamb-${{ matrix.platform }}
|
||||
path: |
|
||||
./target/release/iamb
|
||||
./target/release/iamb.exe
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/target
|
||||
/result
|
||||
/TODO
|
||||
/docs/iamb.[15]
|
||||
|
||||
2322
Cargo.lock
generated
2322
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
47
Cargo.toml
47
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "iamb"
|
||||
version = "0.0.2"
|
||||
version = "0.0.8"
|
||||
edition = "2018"
|
||||
authors = ["Ulyssa <git@ulyssa.dev>"]
|
||||
repository = "https://github.com/ulyssa/iamb"
|
||||
@@ -10,23 +10,41 @@ description = "A Matrix chat client that uses Vim keybindings"
|
||||
license = "Apache-2.0"
|
||||
exclude = [".github", "CONTRIBUTING.md"]
|
||||
keywords = ["matrix", "chat", "tui", "vim"]
|
||||
categories = ["command-line-utilities"]
|
||||
rust-version = "1.66"
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
mandown = "0.1.3"
|
||||
|
||||
[build-dependencies.vergen]
|
||||
version = "8"
|
||||
default-features = false
|
||||
features = ["build", "git", "gitcl",]
|
||||
|
||||
[dependencies]
|
||||
arboard = "3.2.0"
|
||||
bitflags = "1.3.2"
|
||||
chrono = "0.4"
|
||||
clap = {version = "4.0", features = ["derive"]}
|
||||
comrak = {version = "0.18.0", features = ["shortcodes"]}
|
||||
css-color-parser = "0.1.2"
|
||||
dirs = "4.0.0"
|
||||
futures = "0.3.21"
|
||||
emojis = "~0.5.2"
|
||||
futures = "0.3"
|
||||
gethostname = "0.4.1"
|
||||
matrix-sdk = {version = "0.6", default-features = false, features = ["e2e-encryption", "sled", "rustls-tls"]}
|
||||
modalkit = "0.0.9"
|
||||
html5ever = "0.26.0"
|
||||
image = "0.24.5"
|
||||
libc = "0.2"
|
||||
markup5ever_rcdom = "0.2.0"
|
||||
mime = "^0.3.16"
|
||||
mime_guess = "^2.0.4"
|
||||
open = "3.2.0"
|
||||
regex = "^1.5"
|
||||
rpassword = "^7.2"
|
||||
serde = "^1.0"
|
||||
serde_json = "^1.0"
|
||||
sled = "0.34"
|
||||
thiserror = "^1.0.37"
|
||||
tokio = {version = "1.17.0", features = ["full"]}
|
||||
tracing = "~0.1.36"
|
||||
tracing-appender = "~0.2.2"
|
||||
tracing-subscriber = "0.3.16"
|
||||
@@ -34,5 +52,22 @@ unicode-segmentation = "^1.7"
|
||||
unicode-width = "0.1.10"
|
||||
url = {version = "^2.2.2", features = ["serde"]}
|
||||
|
||||
[dependencies.modalkit]
|
||||
version = "0.0.16"
|
||||
|
||||
[dependencies.matrix-sdk]
|
||||
version = "0.6"
|
||||
default-features = false
|
||||
features = ["e2e-encryption", "sled", "rustls-tls"]
|
||||
|
||||
[dependencies.tokio]
|
||||
version = "1.24.1"
|
||||
features = ["macros", "net", "rt-multi-thread", "sync", "time"]
|
||||
|
||||
[dev-dependencies]
|
||||
lazy_static = "1.4.0"
|
||||
pretty_assertions = "1.4.0"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
incremental = false
|
||||
|
||||
111
README.md
111
README.md
@@ -1,5 +1,10 @@
|
||||
# iamb
|
||||
|
||||
[](https://github.com/ulyssa/iamb/actions?query=workflow%3ACI+)
|
||||
[](https://crates.io/crates/iamb)
|
||||
[](https://matrix.to/#/#iamb:0x.badd.cafe)
|
||||
[](https://crates.io/crates/iamb)
|
||||
|
||||
## About
|
||||
|
||||
`iamb` is a Matrix client for the terminal that uses Vim keybindings.
|
||||
@@ -7,6 +12,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
|
||||
@@ -14,10 +21,33 @@ website, [iamb.chat].
|
||||
|
||||
## Installation
|
||||
|
||||
Install Rust and Cargo, and then run:
|
||||
Install Rust (1.66.0 or above) and Cargo, and then run:
|
||||
|
||||
```
|
||||
cargo install iamb
|
||||
cargo install --locked iamb
|
||||
```
|
||||
|
||||
### NetBSD
|
||||
|
||||
On NetBSD a package is available from the official repositories. To install it simply run:
|
||||
|
||||
```
|
||||
pkgin install iamb
|
||||
```
|
||||
|
||||
### Arch Linux
|
||||
|
||||
On Arch Linux a [package](https://aur.archlinux.org/packages/iamb-git) is available in the
|
||||
Arch User Repositories (AUR). To install it simply run with your favorite AUR helper:
|
||||
|
||||
```
|
||||
paru iamb-git
|
||||
```
|
||||
|
||||
### Nix / NixOS (flake)
|
||||
|
||||
```
|
||||
nix profile install "github:ulyssa/iamb"
|
||||
```
|
||||
|
||||
## Configuration
|
||||
@@ -42,38 +72,38 @@ Matrix website's [features comparison table][client-comparison-matrix], showing
|
||||
two other TUI clients and Element Web:
|
||||
|
||||
| | iamb | [gomuks] | [weechat-matrix] | Element Web/Desktop |
|
||||
| --------------------------------------- | :----------------- | :----------------: | :----------------: | :-----------------: |
|
||||
| Room directory | :x: ([#14]) | :x: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Room tag showing | :x: ([#15]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Room tag editing | :x: ([#15]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Search joined rooms | :x: ([#16]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Room user list | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Display Room Description | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Edit Room Description | :heavy_check_mark: | :x: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Highlights | :x: ([#8]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Pushrules | :x: | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Send read markers | :x: ([#11]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Display read markers | :x: ([#11]) | :x: | :x: | :heavy_check_mark: |
|
||||
| Sending Invites | :x: ([#7]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Accepting Invites | :x: ([#7]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Typing Notification | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| E2E | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Replies | :x: ([#3]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Attachment uploading | :x: ([#13]) | :x: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Attachment downloading | :x: ([#13]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Send stickers | :x: | :x: | :x: | :heavy_check_mark: |
|
||||
| Send formatted messages (markdown) | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Rich Text Editor for formatted messages | :x: | :x: | :x: | :heavy_check_mark: |
|
||||
| Display formatted messages | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Redacting | :x: ([#5]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| Multiple Matrix Accounts | :heavy_check_mark: | :x: | :heavy_check_mark: | :x: |
|
||||
| New user registration | :x: | :x: | :x: | :heavy_check_mark: |
|
||||
| VOIP | :x: | :x: | :x: | :heavy_check_mark: |
|
||||
| Reactions | :x: ([#2]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Message editing | :x: ([#4]) | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Room upgrades | :x: | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
||||
| Localisations | :x: | 1 | :x: | 44 |
|
||||
| SSO Support | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
||||
| --------------------------------------- | :---------- | :------: | :--------------: | :-----------------: |
|
||||
| Room directory | ❌ ([#14]) | ❌ | ✔️ | ✔️ |
|
||||
| Room tag showing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Room tag editing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Search joined rooms | ❌ ([#16]) | ✔️ | ❌ | ✔️ |
|
||||
| Room user list | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Display Room Description | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Edit Room Description | ✔️ | ❌ | ✔️ | ✔️ |
|
||||
| Highlights | ❌ ([#8]) | ✔️ | ✔️ | ✔️ |
|
||||
| Pushrules | ❌ | ✔️ | ❌ | ✔️ |
|
||||
| Send read markers | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Display read markers | ✔️ | ❌ | ❌ | ✔️ |
|
||||
| Sending Invites | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Accepting Invites | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Typing Notification | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| E2E | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Replies | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Attachment uploading | ✔️ | ❌ | ✔️ | ✔️ |
|
||||
| Attachment downloading | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Send stickers | ❌ | ❌ | ❌ | ✔️ |
|
||||
| Send formatted messages (markdown) | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Rich Text Editor for formatted messages | ❌ | ❌ | ❌ | ✔️ |
|
||||
| Display formatted messages | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Redacting | ✔️ | ✔️ | ✔️ | ✔️ |
|
||||
| Multiple Matrix Accounts | ✔️ | ❌ | ✔️ | ❌ |
|
||||
| New user registration | ❌ | ❌ | ❌ | ✔️ |
|
||||
| VOIP | ❌ | ❌ | ❌ | ✔️ |
|
||||
| Reactions | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Message editing | ✔️ | ✔️ | ❌ | ✔️ |
|
||||
| Room upgrades | ❌ ([#41]) | ✔️ | ❌ | ✔️ |
|
||||
| Localisations | ❌ | 1 | ❌ | 44 |
|
||||
| SSO Support | ❌ | ✔️ | ✔️ | ✔️ |
|
||||
|
||||
## License
|
||||
|
||||
@@ -84,18 +114,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
|
||||
|
||||
29
build.rs
Normal file
29
build.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::iter::FromIterator;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mandown::convert;
|
||||
use vergen::EmitBuilder;
|
||||
|
||||
const IAMB_1_MD: &str = include_str!("docs/iamb.1.md");
|
||||
const IAMB_5_MD: &str = include_str!("docs/iamb.5.md");
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
EmitBuilder::builder().git_sha(true).emit()?;
|
||||
|
||||
// Build the manual pages.
|
||||
println!("cargo:rerun-if-changed=docs/iamb.1.md");
|
||||
println!("cargo:rerun-if-changed=docs/iamb.5.md");
|
||||
|
||||
let iamb_1 = convert(IAMB_1_MD, "IAMB", 1);
|
||||
let iamb_5 = convert(IAMB_5_MD, "IAMB", 5);
|
||||
|
||||
let out_dir = std::env::var("OUT_DIR");
|
||||
let out_dir = out_dir.as_deref().unwrap_or("docs");
|
||||
|
||||
fs::write(PathBuf::from_iter([out_dir, "iamb.1"]), iamb_1.as_bytes())?;
|
||||
fs::write(PathBuf::from_iter([out_dir, "iamb.5"]), iamb_5.as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
32
docs/example_config.json
Normal file
32
docs/example_config.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"default_profile": "default",
|
||||
"profiles": {
|
||||
"default": {
|
||||
"user_id": "",
|
||||
"url": "https://matrix.org",
|
||||
"settings": {},
|
||||
"dirs": {}
|
||||
}
|
||||
},
|
||||
"settings": {
|
||||
"log_level": "warn",
|
||||
"reaction_display": true,
|
||||
"reaction_shortcode_display": false,
|
||||
"read_receipt_send": true,
|
||||
"read_receipt_display": true,
|
||||
"request_timeout": 10000,
|
||||
"typing_notice_send": true,
|
||||
"typing_notice_display": true,
|
||||
"users": {
|
||||
"@user:matrix.org": {
|
||||
"name": "John Doe",
|
||||
"color": "magenta"
|
||||
}
|
||||
},
|
||||
"default_room": "#iamb-users:0x.badd.cafe"
|
||||
},
|
||||
"dirs": {
|
||||
"cache": "~/.cache/iamb/",
|
||||
"logs": "~/.local/share/iamb/logs/",
|
||||
"downloads": "~/Downloads/"
|
||||
}
|
||||
29
docs/iamb.1.md
Normal file
29
docs/iamb.1.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# NAME
|
||||
|
||||
iamb – a terminal-based client for Matrix for the Vim addict
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
**iamb** [**--profile** _profile_] [**--config-directory** _directory_] [**--help** | **--version**]
|
||||
|
||||
# OPTIONS
|
||||
|
||||
These options are primitives at the top-level of the file.
|
||||
|
||||
**--profile**, **-P**
|
||||
> The profile to start with. Overrides **default_profile** from **iamb(5)**.
|
||||
|
||||
**--config-directory**, **-C**
|
||||
> Path to the directory the configuration file is located in.
|
||||
|
||||
**--help**, **-h**
|
||||
> Show a short help text and quit.
|
||||
|
||||
**--version**, **-V**
|
||||
> Show the iamb version and quit.
|
||||
|
||||
# SEE ALSO
|
||||
|
||||
**iamb(5)**
|
||||
|
||||
Full documentation is available online at \<https://iamb.chat\>
|
||||
134
docs/iamb.5.md
Normal file
134
docs/iamb.5.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# NAME
|
||||
|
||||
config.json – configuration file for iamb
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
Configuration must be placed under _~/.config/iamb/_ and is named config.json.
|
||||
|
||||
Example configuration usually comes bundled with your installation and can
|
||||
typically be found in _/usr/share/iamb_.
|
||||
|
||||
As implied by the filename, the configuration is formatted in JSON. It's
|
||||
structure and fields are described below.
|
||||
|
||||
# BASIC SETTINGS
|
||||
|
||||
These options are primitives at the top-level of the file.
|
||||
|
||||
**default_profile** (type: string)
|
||||
> The default profile to connect to, unless overwritten by a commandline
|
||||
> switch. It has to be defined in the *PROFILES* section.
|
||||
|
||||
# PROFILES
|
||||
|
||||
These options are configured as a map under the profiles name.
|
||||
|
||||
**user_id** (type: string)
|
||||
> The user ID to use when connecting to the server. For example "user" for
|
||||
> "@user:matrix.org".
|
||||
|
||||
**url** (type: string)
|
||||
> The URL of the users server. For example "https://matrix.org" for
|
||||
> "@user:matrix.org".
|
||||
|
||||
**settings** (type: settings object)
|
||||
> Overwrite general settings for this account. The fields are identical to
|
||||
> those in *TUNABLES*.
|
||||
|
||||
**layout** (type: startup layout object)
|
||||
> Overwrite general settings for this account. The fields are identical to
|
||||
> those in *STARTUP LAYOUT*.
|
||||
|
||||
**dirs** (type: XDG overrides object)
|
||||
> Overwrite general settings for this account. The fields are identical to
|
||||
> those in *DIRECTORIES*.
|
||||
|
||||
# TUNABLES
|
||||
|
||||
These options are configured as a map under the *settings* key and can be
|
||||
overridden as described in *PROFILES*.
|
||||
|
||||
**log_level** (type: string)
|
||||
> Specifies the lowest log level that should be shown. Possible values
|
||||
> are: _trace_, _debug_, _info_, _warn_, and _error_.
|
||||
|
||||
**reaction_display** (type: boolean)
|
||||
> Defines whether or not reactions should be shown.
|
||||
|
||||
**reaction_shortcode_display** (type: boolean)
|
||||
> Defines whether or not reactions should be shown as their respective
|
||||
> shortcode.
|
||||
|
||||
**read_receipt_send** (type: boolean)
|
||||
> Defines whether or not read confirmations are sent.
|
||||
|
||||
**read_receipt_display** (type: boolean)
|
||||
> Defines whether or not read confirmations are displayed.
|
||||
|
||||
**request_timeout** (type: uint64)
|
||||
> Defines the maximum time per request in seconds.
|
||||
|
||||
**typing_notice_send** (type: boolean)
|
||||
> Defines whether or not the typing state is sent.
|
||||
|
||||
**typing_notice_display** (type: boolean)
|
||||
> Defines whether or not the typing state is displayed.
|
||||
|
||||
**user** (type: map)
|
||||
> Overrides values for the specified user. See *USER OVERRIDES* for
|
||||
> details on the format.
|
||||
|
||||
**default_room** (type: string)
|
||||
> The room to show by default instead of a welcome-screen.
|
||||
|
||||
## USER OVERRIDES
|
||||
|
||||
Overrides are mapped onto matrix User IDs such as _@user:matrix.org_ and are
|
||||
maps containing the following key value pairs.
|
||||
|
||||
**name** (type: string)
|
||||
> Change the display name of the user.
|
||||
|
||||
**color** (type: string)
|
||||
> Change the color the user is shown as. Possible values are: _black_,
|
||||
> _blue_, _cyan_, _dark-gray_, _gray_, _green_, _light-blue_,
|
||||
> _light-cyan_, _light-green_, _light-magenta_, _light-red_,
|
||||
> _light-yellow_, _magenta_, _none_, _red_, _white_, _yellow_
|
||||
|
||||
# STARTUP LAYOUT
|
||||
|
||||
Specifies what initial set of tabs and windows to show when starting the
|
||||
client. Configured as an object under the key *layout*.
|
||||
|
||||
**style** (type: string)
|
||||
> Specifies what window layout to load when starting. Valid values are
|
||||
> _restore_ to restore the layout from the last time the client was exited,
|
||||
> _new_ to open a single window (uses the value of _default\_room_ if set), or
|
||||
> _config_ to open the layout described under _tabs_.
|
||||
|
||||
**tabs** (type: array of window objects)
|
||||
> If **style** is set to _config_, then this value will be used to open a set
|
||||
> of tabs and windows at startup. Each object can contain either a **window**
|
||||
> key specifying a username, room identifier or room alias to show, or a
|
||||
> **split** key specifying an array of window objects.
|
||||
|
||||
# DIRECTORIES
|
||||
|
||||
Specifies the directories to save data in. Configured as a map under the key
|
||||
*dirs*.
|
||||
|
||||
**cache** (type: string)
|
||||
> Specifies where to store assets and temporary data in.
|
||||
|
||||
**logs** (type: string)
|
||||
> Specifies where to store log files.
|
||||
|
||||
**downloads** (type: string)
|
||||
> Specifies where to store downloaded files.
|
||||
|
||||
# SEE ALSO
|
||||
|
||||
*iamb(1)*
|
||||
|
||||
Full documentation is available online at \<https://iamb.chat\>
|
||||
94
flake.lock
generated
Normal file
94
flake.lock
generated
Normal file
@@ -0,0 +1,94 @@
|
||||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1678901627,
|
||||
"narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils_2": {
|
||||
"locked": {
|
||||
"lastModified": 1659877975,
|
||||
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1679437018,
|
||||
"narHash": "sha256-vOuiDPLHSEo/7NkiWtxpHpHgoXoNmrm+wkXZ6a072Fc=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "19cf008bb18e47b6e3b4e16e32a9a4bdd4b45f7e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs_2": {
|
||||
"locked": {
|
||||
"lastModified": 1665296151,
|
||||
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"rust-overlay": "rust-overlay"
|
||||
}
|
||||
},
|
||||
"rust-overlay": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils_2",
|
||||
"nixpkgs": "nixpkgs_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1679624450,
|
||||
"narHash": "sha256-wiDqUaklmc31E1+wz5sv52sMcWvZKsL1FBeGJCxz628=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "afbdcf305fd6f05f708fe76d52f24d37d066c251",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
40
flake.nix
Normal file
40
flake.nix
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
description = "iamb";
|
||||
nixConfig.bash-prompt = "\[nix-develop\]$ ";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils, rust-overlay, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
# We only need the nightly overlay in the devShell because .rs files are formatted with nightly.
|
||||
overlays = [ (import rust-overlay) ];
|
||||
pkgs = import nixpkgs { inherit system overlays; };
|
||||
rustNightly = pkgs.rust-bin.nightly."2023-03-17".default;
|
||||
in
|
||||
with pkgs;
|
||||
{
|
||||
packages.default = pkgs.rustPlatform.buildRustPackage {
|
||||
pname = "iamb";
|
||||
version = "0.0.7";
|
||||
src = ./.;
|
||||
cargoLock.lockFile = ./Cargo.lock;
|
||||
nativeBuildInputs = [ pkgs.pkgconfig ];
|
||||
buildInputs = [ pkgs.openssl ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin
|
||||
(with pkgs.darwin.apple_sdk.frameworks; [ AppKit Security ]);
|
||||
};
|
||||
devShell = mkShell {
|
||||
buildInputs = [
|
||||
(rustNightly.override { extensions = [ "rust-src" ]; })
|
||||
pkg-config
|
||||
cargo-tarpaulin
|
||||
rust-analyzer
|
||||
rustfmt
|
||||
];
|
||||
};
|
||||
});
|
||||
}
|
||||
955
src/base.rs
955
src/base.rs
File diff suppressed because it is too large
Load Diff
688
src/commands.rs
688
src/commands.rs
@@ -1,24 +1,105 @@
|
||||
use std::convert::TryFrom;
|
||||
|
||||
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,
|
||||
ProgramCommand,
|
||||
ProgramCommands,
|
||||
ProgramContext,
|
||||
RoomAction,
|
||||
SetRoomField,
|
||||
RoomField,
|
||||
SendAction,
|
||||
VerifyAction,
|
||||
};
|
||||
|
||||
type ProgContext = CommandContext<ProgramContext>;
|
||||
type ProgResult = CommandResult<ProgramCommand>;
|
||||
|
||||
/// Convert strings the user types into a tag name.
|
||||
fn tag_name(name: String) -> Result<TagName, CommandError> {
|
||||
let tag = match name.as_str() {
|
||||
"fav" | "favorite" | "favourite" | "m.favourite" => TagName::Favorite,
|
||||
"low" | "lowpriority" | "low_priority" | "low-priority" | "m.lowpriority" => {
|
||||
TagName::LowPriority
|
||||
},
|
||||
"servernotice" | "server_notice" | "server-notice" | "m.server_notice" => {
|
||||
TagName::ServerNotice
|
||||
},
|
||||
_ => {
|
||||
if let Ok(tag) = name.parse() {
|
||||
TagName::User(tag)
|
||||
} else {
|
||||
let msg = format!("Invalid user tag name: {name}");
|
||||
|
||||
return Err(CommandError::Error(msg));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Ok(tag)
|
||||
}
|
||||
|
||||
fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let args = desc.arg.strings()?;
|
||||
|
||||
if args.is_empty() {
|
||||
return Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let ract = match args[0].as_str() {
|
||||
"accept" => {
|
||||
if args.len() != 1 {
|
||||
return Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
RoomAction::InviteAccept
|
||||
},
|
||||
"reject" => {
|
||||
if args.len() != 1 {
|
||||
return Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
RoomAction::InviteReject
|
||||
},
|
||||
"send" => {
|
||||
if args.len() != 2 {
|
||||
return Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
if let Ok(user) = OwnedUserId::try_from(args[1].as_str()) {
|
||||
RoomAction::InviteSend(user)
|
||||
} else {
|
||||
let msg = format!("Invalid user identifier: {}", args[1]);
|
||||
let err = CommandError::Error(msg);
|
||||
|
||||
return Err(err);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return Err(CommandError::InvalidArgument);
|
||||
},
|
||||
};
|
||||
|
||||
let iact = IambAction::from(ract);
|
||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
@@ -80,6 +161,111 @@ fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_leave(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let leave = IambAction::Room(RoomAction::Leave(desc.bang));
|
||||
let step = CommandStep::Continue(leave.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let mact = IambAction::from(MessageAction::Cancel(desc.bang));
|
||||
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_edit(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let mact = IambAction::from(MessageAction::Edit);
|
||||
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_react(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let args = desc.arg.strings()?;
|
||||
|
||||
if args.len() != 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let k = args[0].as_str();
|
||||
|
||||
if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
|
||||
let mact = IambAction::from(MessageAction::React(emoji.to_string()));
|
||||
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
} else {
|
||||
let msg = format!("Invalid Emoji or shortcode: {k}");
|
||||
|
||||
return Result::Err(CommandError::Error(msg));
|
||||
}
|
||||
}
|
||||
|
||||
fn iamb_unreact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
if args.len() > 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let mact = if let Some(k) = args.pop() {
|
||||
let k = k.as_str();
|
||||
|
||||
if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
|
||||
IambAction::from(MessageAction::Unreact(Some(emoji.to_string())))
|
||||
} else {
|
||||
let msg = format!("Invalid Emoji or shortcode: {k}");
|
||||
|
||||
return Result::Err(CommandError::Error(msg));
|
||||
}
|
||||
} else {
|
||||
IambAction::from(MessageAction::Unreact(None))
|
||||
};
|
||||
|
||||
let step = CommandStep::Continue(mact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_redact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let args = desc.arg.strings()?;
|
||||
|
||||
if args.len() > 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let reason = args.into_iter().next();
|
||||
let ract = IambAction::from(MessageAction::Redact(reason, desc.bang));
|
||||
let step = CommandStep::Continue(ract.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_reply(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let ract = IambAction::from(MessageAction::Reply);
|
||||
let step = CommandStep::Continue(ract.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
if !desc.arg.text.is_empty() {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
@@ -126,22 +312,93 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_set(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
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()?;
|
||||
|
||||
if args.len() != 2 {
|
||||
if args.len() < 2 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let field = args.remove(0);
|
||||
let value = args.remove(0);
|
||||
let action = args.remove(0);
|
||||
|
||||
let act: IambAction = match field.as_str() {
|
||||
"room.name" => RoomAction::Set(SetRoomField::Name(value)).into(),
|
||||
"room.topic" => RoomAction::Set(SetRoomField::Topic(value)).into(),
|
||||
_ => {
|
||||
if args.len() > 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
},
|
||||
}
|
||||
|
||||
let act: IambAction = match (field.as_str(), action.as_str(), args.pop()) {
|
||||
// :room name set <room-name>
|
||||
("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
|
||||
("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
||||
|
||||
// :room name unset
|
||||
("name", "unset", None) => RoomAction::Unset(RoomField::Name).into(),
|
||||
("name", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
|
||||
|
||||
// :room topic set <topic>
|
||||
("topic", "set", Some(s)) => RoomAction::Set(RoomField::Topic, s).into(),
|
||||
("topic", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
||||
|
||||
// :room topic unset
|
||||
("topic", "unset", None) => RoomAction::Unset(RoomField::Topic).into(),
|
||||
("topic", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
|
||||
|
||||
// :room tag set <tag-name>
|
||||
("tag", "set", Some(s)) => RoomAction::Set(RoomField::Tag(tag_name(s)?), "".into()).into(),
|
||||
("tag", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
||||
|
||||
// :room tag unset <tag-name>
|
||||
("tag", "unset", Some(s)) => RoomAction::Unset(RoomField::Tag(tag_name(s)?)).into(),
|
||||
("tag", "unset", None) => return Result::Err(CommandError::InvalidArgument),
|
||||
|
||||
_ => return Result::Err(CommandError::InvalidArgument),
|
||||
};
|
||||
|
||||
let step = CommandStep::Continue(act.into(), ctx.context.take());
|
||||
@@ -149,15 +406,137 @@ fn iamb_set(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_upload(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
if args.len() != 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let sact = SendAction::Upload(args.remove(0));
|
||||
let iact = IambAction::from(sact);
|
||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
if args.len() > 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let mut flags = DownloadFlags::NONE;
|
||||
if desc.bang {
|
||||
flags |= DownloadFlags::FORCE;
|
||||
};
|
||||
let mact = MessageAction::Download(args.pop(), flags);
|
||||
let iact = IambAction::from(mact);
|
||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
||||
let mut args = desc.arg.strings()?;
|
||||
|
||||
if args.len() > 1 {
|
||||
return Result::Err(CommandError::InvalidArgument);
|
||||
}
|
||||
|
||||
let mut flags = DownloadFlags::OPEN;
|
||||
if desc.bang {
|
||||
flags |= DownloadFlags::FORCE;
|
||||
};
|
||||
let mact = MessageAction::Download(args.pop(), flags);
|
||||
let iact = IambAction::from(mact);
|
||||
let step = CommandStep::Continue(iact.into(), ctx.context.take());
|
||||
|
||||
return Ok(step);
|
||||
}
|
||||
|
||||
fn add_iamb_commands(cmds: &mut ProgramCommands) {
|
||||
cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
|
||||
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!["rooms".into()], f: iamb_rooms });
|
||||
cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set });
|
||||
cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
|
||||
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: "leave".into(),
|
||||
aliases: vec![],
|
||||
f: iamb_leave,
|
||||
});
|
||||
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 {
|
||||
@@ -171,6 +550,7 @@ pub fn setup_commands() -> ProgramCommands {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use matrix_sdk::ruma::user_id;
|
||||
use modalkit::editing::action::WindowAction;
|
||||
|
||||
#[test]
|
||||
@@ -241,46 +621,288 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_set() {
|
||||
fn test_cmd_room_invalid() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("room", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("room foo", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("room set topic", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_room_topic_set() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds
|
||||
.input_cmd("set room.topic \"Lots of fun discussion!\"", ctx.clone())
|
||||
.input_cmd("room topic set \"Lots of fun discussion!\"", ctx.clone())
|
||||
.unwrap();
|
||||
let act = IambAction::Room(SetRoomField::Topic("Lots of fun discussion!".into()).into());
|
||||
let act = RoomAction::Set(RoomField::Topic, "Lots of fun discussion!".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds
|
||||
.input_cmd("set room.topic The\\ Discussion\\ Room", ctx.clone())
|
||||
.input_cmd("room topic set The\\ Discussion\\ Room", ctx.clone())
|
||||
.unwrap();
|
||||
let act = IambAction::Room(SetRoomField::Topic("The Discussion Room".into()).into());
|
||||
let act = RoomAction::Set(RoomField::Topic, "The Discussion Room".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("set room.topic Development", ctx.clone()).unwrap();
|
||||
let act = IambAction::Room(SetRoomField::Topic("Development".into()).into());
|
||||
let res = cmds.input_cmd("room topic set Development", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Topic, "Development".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("set room.name Development", ctx.clone()).unwrap();
|
||||
let act = IambAction::Room(SetRoomField::Name("Development".into()).into());
|
||||
let res = cmds.input_cmd("room topic", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("room topic set", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("room topic set A B C", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_room_name_invalid() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("room name", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("room name foo", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_room_name_set() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("room name set Development", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Name, "Development".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds
|
||||
.input_cmd("set room.name \"Application Development\"", ctx.clone())
|
||||
.input_cmd("room name set \"Application Development\"", ctx.clone())
|
||||
.unwrap();
|
||||
let act = IambAction::Room(SetRoomField::Name("Application Development".into()).into());
|
||||
let act = RoomAction::Set(RoomField::Name, "Application Development".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("set", ctx.clone());
|
||||
let res = cmds.input_cmd("room name set", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_room_name_unset() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("room name unset", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Name);
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room name unset foo", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_room_tag_set() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("room tag set favourite", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set favorite", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set fav", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set low_priority", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set low-priority", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set low", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set servernotice", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set u.custom-tag", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Set(
|
||||
RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())),
|
||||
"".into(),
|
||||
);
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag set u.irc", ctx.clone()).unwrap();
|
||||
let act =
|
||||
RoomAction::Set(RoomField::Tag(TagName::User("u.irc".parse().unwrap())), "".into());
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("set room.name", ctx.clone());
|
||||
let res = cmds.input_cmd("room tag set", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("set room.topic", ctx.clone());
|
||||
let res = cmds.input_cmd("room tag set unknown", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
|
||||
|
||||
let res = cmds.input_cmd("room tag set needs-leading-u-dot", ctx.clone());
|
||||
assert_eq!(
|
||||
res,
|
||||
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_room_tag_unset() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("room tag unset favourite", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset favorite", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset fav", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset low_priority", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset low-priority", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset low", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset servernotice", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset u.custom-tag", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag unset u.irc", ctx.clone()).unwrap();
|
||||
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.irc".parse().unwrap())));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("room tag", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("set room.topic A B C", ctx.clone());
|
||||
let res = cmds.input_cmd("room tag set", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("room tag unset unknown", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
|
||||
|
||||
let res = cmds.input_cmd("room tag unset needs-leading-u-dot", ctx.clone());
|
||||
assert_eq!(
|
||||
res,
|
||||
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_invite() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("invite accept", ctx.clone()).unwrap();
|
||||
let act = IambAction::Room(RoomAction::InviteAccept);
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("invite reject", ctx.clone()).unwrap();
|
||||
let act = IambAction::Room(RoomAction::InviteReject);
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("invite send @user:example.com", ctx.clone()).unwrap();
|
||||
let act =
|
||||
IambAction::Room(RoomAction::InviteSend(user_id!("@user:example.com").to_owned()));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("invite", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("invite foo", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("invite accept @user:example.com", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("invite reject @user:example.com", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("invite send", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
|
||||
let res = cmds.input_cmd("invite @user:example.com", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cmd_redact() {
|
||||
let mut cmds = setup_commands();
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
let res = cmds.input_cmd("redact", ctx.clone()).unwrap();
|
||||
let act = IambAction::Message(MessageAction::Redact(None, false));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("redact!", ctx.clone()).unwrap();
|
||||
let act = IambAction::Message(MessageAction::Redact(None, true));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("redact Removed", ctx.clone()).unwrap();
|
||||
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("redact \"Removed\"", ctx.clone()).unwrap();
|
||||
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
|
||||
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
||||
|
||||
let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
|
||||
assert_eq!(res, Err(CommandError::InvalidArgument));
|
||||
}
|
||||
}
|
||||
|
||||
508
src/config.rs
508
src/config.rs
@@ -1,14 +1,26 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::io::BufReader;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
|
||||
use clap::Parser;
|
||||
use matrix_sdk::ruma::OwnedUserId;
|
||||
use serde::Deserialize;
|
||||
use matrix_sdk::ruma::{OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId};
|
||||
use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer};
|
||||
use tracing::Level;
|
||||
use url::Url;
|
||||
|
||||
use modalkit::tui::{
|
||||
style::{Color, Modifier as StyleModifier, Style},
|
||||
text::Span,
|
||||
};
|
||||
|
||||
use super::base::{IambId, RoomInfo};
|
||||
|
||||
macro_rules! usage {
|
||||
( $($args: tt)* ) => {
|
||||
println!($($args)*);
|
||||
@@ -16,6 +28,36 @@ macro_rules! usage {
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_REQ_TIMEOUT: u64 = 120;
|
||||
|
||||
const COLORS: [Color; 13] = [
|
||||
Color::Blue,
|
||||
Color::Cyan,
|
||||
Color::Green,
|
||||
Color::LightBlue,
|
||||
Color::LightGreen,
|
||||
Color::LightCyan,
|
||||
Color::LightMagenta,
|
||||
Color::LightRed,
|
||||
Color::LightYellow,
|
||||
Color::Magenta,
|
||||
Color::Red,
|
||||
Color::Reset,
|
||||
Color::Yellow,
|
||||
];
|
||||
|
||||
pub fn user_color(user: &str) -> Color {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
user.hash(&mut hasher);
|
||||
let color = hasher.finish() as usize % COLORS.len();
|
||||
|
||||
COLORS[color]
|
||||
}
|
||||
|
||||
pub fn user_style_from_color(color: Color) -> Style {
|
||||
Style::default().fg(color).add_modifier(StyleModifier::BOLD)
|
||||
}
|
||||
|
||||
fn is_profile_char(c: char) -> bool {
|
||||
c.is_ascii_alphanumeric() || c == '.' || c == '-'
|
||||
}
|
||||
@@ -49,8 +91,13 @@ fn validate_profile_names(names: &HashMap<String, ProfileConfig>) {
|
||||
}
|
||||
}
|
||||
|
||||
const VERSION: &str = match option_env!("VERGEN_GIT_SHA") {
|
||||
None => env!("CARGO_PKG_VERSION"),
|
||||
Some(_) => concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")"),
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(version, about, long_about = None)]
|
||||
#[clap(version = VERSION, about, long_about = None)]
|
||||
#[clap(propagate_version = true)]
|
||||
pub struct Iamb {
|
||||
#[clap(short = 'P', long, value_parser)]
|
||||
@@ -69,30 +116,201 @@ pub enum ConfigError {
|
||||
Invalid(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct LogLevel(pub Level);
|
||||
pub struct LogLevelVisitor;
|
||||
|
||||
impl From<LogLevel> for Level {
|
||||
fn from(level: LogLevel) -> Level {
|
||||
level.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for LogLevelVisitor {
|
||||
type Value = LogLevel;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a valid log level (e.g. \"warn\" or \"debug\")")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: SerdeError,
|
||||
{
|
||||
match value {
|
||||
"info" => Ok(LogLevel(Level::INFO)),
|
||||
"debug" => Ok(LogLevel(Level::DEBUG)),
|
||||
"warn" => Ok(LogLevel(Level::WARN)),
|
||||
"error" => Ok(LogLevel(Level::ERROR)),
|
||||
"trace" => Ok(LogLevel(Level::TRACE)),
|
||||
_ => Err(E::custom("Could not parse log level")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for LogLevel {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(LogLevelVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct UserColor(pub Color);
|
||||
pub struct UserColorVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for UserColorVisitor {
|
||||
type Value = UserColor;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a valid color")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: SerdeError,
|
||||
{
|
||||
match value {
|
||||
"none" => Ok(UserColor(Color::Reset)),
|
||||
"red" => Ok(UserColor(Color::Red)),
|
||||
"black" => Ok(UserColor(Color::Black)),
|
||||
"green" => Ok(UserColor(Color::Green)),
|
||||
"yellow" => Ok(UserColor(Color::Yellow)),
|
||||
"blue" => Ok(UserColor(Color::Blue)),
|
||||
"magenta" => Ok(UserColor(Color::Magenta)),
|
||||
"cyan" => Ok(UserColor(Color::Cyan)),
|
||||
"gray" => Ok(UserColor(Color::Gray)),
|
||||
"dark-gray" => Ok(UserColor(Color::DarkGray)),
|
||||
"light-red" => Ok(UserColor(Color::LightRed)),
|
||||
"light-green" => Ok(UserColor(Color::LightGreen)),
|
||||
"light-yellow" => Ok(UserColor(Color::LightYellow)),
|
||||
"light-blue" => Ok(UserColor(Color::LightBlue)),
|
||||
"light-magenta" => Ok(UserColor(Color::LightMagenta)),
|
||||
"light-cyan" => Ok(UserColor(Color::LightCyan)),
|
||||
"white" => Ok(UserColor(Color::White)),
|
||||
_ => Err(E::custom("Could not parse color")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for UserColor {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(UserColorVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
|
||||
pub struct UserDisplayTunables {
|
||||
pub color: Option<UserColor>,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>;
|
||||
|
||||
fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<UserOverrides> {
|
||||
match (a, b) {
|
||||
(Some(a), None) => Some(a),
|
||||
(None, Some(b)) => Some(b),
|
||||
(Some(mut a), Some(b)) => {
|
||||
for (k, v) in b {
|
||||
a.insert(k, v);
|
||||
}
|
||||
|
||||
Some(a)
|
||||
},
|
||||
(None, None) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum UserDisplayStyle {
|
||||
// The Matrix username for the sender (e.g., "@user:example.com").
|
||||
#[default]
|
||||
Username,
|
||||
|
||||
// The localpart of the Matrix username (e.g., "@user").
|
||||
LocalPart,
|
||||
|
||||
// The display name for the Matrix user, calculated according to the rules from the spec.
|
||||
//
|
||||
// This is usually something like "Ada Lovelace" if the user has configured a display name, but
|
||||
// it can wind up being the Matrix username if there are display name collisions in the room,
|
||||
// in order to avoid any confusion.
|
||||
DisplayName,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TunableValues {
|
||||
pub typing_notice: bool,
|
||||
pub log_level: Level,
|
||||
pub reaction_display: bool,
|
||||
pub reaction_shortcode_display: bool,
|
||||
pub read_receipt_send: bool,
|
||||
pub read_receipt_display: bool,
|
||||
pub request_timeout: u64,
|
||||
pub typing_notice_send: bool,
|
||||
pub typing_notice_display: bool,
|
||||
pub users: UserOverrides,
|
||||
pub username_display: UserDisplayStyle,
|
||||
pub default_room: Option<String>,
|
||||
pub open_command: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
pub struct Tunables {
|
||||
pub typing_notice: Option<bool>,
|
||||
pub log_level: Option<LogLevel>,
|
||||
pub reaction_display: Option<bool>,
|
||||
pub reaction_shortcode_display: Option<bool>,
|
||||
pub read_receipt_send: Option<bool>,
|
||||
pub read_receipt_display: Option<bool>,
|
||||
pub request_timeout: Option<u64>,
|
||||
pub typing_notice_send: Option<bool>,
|
||||
pub typing_notice_display: Option<bool>,
|
||||
pub users: Option<UserOverrides>,
|
||||
pub username_display: Option<UserDisplayStyle>,
|
||||
pub default_room: Option<String>,
|
||||
pub open_command: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl Tunables {
|
||||
fn merge(self, other: Self) -> Self {
|
||||
Tunables {
|
||||
typing_notice: self.typing_notice.or(other.typing_notice),
|
||||
log_level: self.log_level.or(other.log_level),
|
||||
reaction_display: self.reaction_display.or(other.reaction_display),
|
||||
reaction_shortcode_display: self
|
||||
.reaction_shortcode_display
|
||||
.or(other.reaction_shortcode_display),
|
||||
read_receipt_send: self.read_receipt_send.or(other.read_receipt_send),
|
||||
read_receipt_display: self.read_receipt_display.or(other.read_receipt_display),
|
||||
request_timeout: self.request_timeout.or(other.request_timeout),
|
||||
typing_notice_send: self.typing_notice_send.or(other.typing_notice_send),
|
||||
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
|
||||
users: merge_users(self.users, other.users),
|
||||
username_display: self.username_display.or(other.username_display),
|
||||
default_room: self.default_room.or(other.default_room),
|
||||
open_command: self.open_command.or(other.open_command),
|
||||
}
|
||||
}
|
||||
|
||||
fn values(self) -> TunableValues {
|
||||
TunableValues {
|
||||
typing_notice: self.typing_notice.unwrap_or(true),
|
||||
typing_notice_display: self.typing_notice.unwrap_or(true),
|
||||
log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO),
|
||||
reaction_display: self.reaction_display.unwrap_or(true),
|
||||
reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
|
||||
read_receipt_send: self.read_receipt_send.unwrap_or(true),
|
||||
read_receipt_display: self.read_receipt_display.unwrap_or(true),
|
||||
request_timeout: self.request_timeout.unwrap_or(DEFAULT_REQ_TIMEOUT),
|
||||
typing_notice_send: self.typing_notice_send.unwrap_or(true),
|
||||
typing_notice_display: self.typing_notice_display.unwrap_or(true),
|
||||
users: self.users.unwrap_or_default(),
|
||||
username_display: self.username_display.unwrap_or_default(),
|
||||
default_room: self.default_room,
|
||||
open_command: self.open_command,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,7 +319,7 @@ impl Tunables {
|
||||
pub struct DirectoryValues {
|
||||
pub cache: PathBuf,
|
||||
pub logs: PathBuf,
|
||||
pub downloads: PathBuf,
|
||||
pub downloads: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
@@ -123,7 +341,11 @@ impl Directories {
|
||||
fn values(self) -> DirectoryValues {
|
||||
let cache = self
|
||||
.cache
|
||||
.or_else(dirs::cache_dir)
|
||||
.or_else(|| {
|
||||
let mut dir = dirs::cache_dir()?;
|
||||
dir.push("iamb");
|
||||
dir.into()
|
||||
})
|
||||
.expect("no dirs.cache value configured!");
|
||||
|
||||
let logs = self.logs.unwrap_or_else(|| {
|
||||
@@ -132,21 +354,49 @@ impl Directories {
|
||||
dir
|
||||
});
|
||||
|
||||
let downloads = self
|
||||
.downloads
|
||||
.or_else(dirs::download_dir)
|
||||
.expect("no dirs.download value configured!");
|
||||
let downloads = self.downloads.or_else(dirs::download_dir);
|
||||
|
||||
DirectoryValues { cache, logs, downloads }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum WindowPath {
|
||||
AliasId(OwnedRoomAliasId),
|
||||
RoomId(OwnedRoomId),
|
||||
UserId(OwnedUserId),
|
||||
Window(IambId),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
|
||||
#[serde(untagged, deny_unknown_fields)]
|
||||
pub enum WindowLayout {
|
||||
Window { window: WindowPath },
|
||||
Split { split: Vec<WindowLayout> },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "lowercase", tag = "style")]
|
||||
pub enum Layout {
|
||||
/// Restore the layout from the previous session.
|
||||
#[default]
|
||||
Restore,
|
||||
|
||||
/// Open a single window using the `default_room` value.
|
||||
New,
|
||||
|
||||
/// Open the window layouts described under `tabs`.
|
||||
Config { tabs: Vec<WindowLayout> },
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct ProfileConfig {
|
||||
pub user_id: OwnedUserId,
|
||||
pub url: Url,
|
||||
pub settings: Option<Tunables>,
|
||||
pub dirs: Option<Directories>,
|
||||
pub layout: Option<Layout>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
@@ -155,6 +405,7 @@ pub struct IambConfig {
|
||||
pub default_profile: Option<String>,
|
||||
pub settings: Option<Tunables>,
|
||||
pub dirs: Option<Directories>,
|
||||
pub layout: Option<Layout>,
|
||||
}
|
||||
|
||||
impl IambConfig {
|
||||
@@ -178,11 +429,13 @@ impl IambConfig {
|
||||
#[derive(Clone)]
|
||||
pub struct ApplicationSettings {
|
||||
pub matrix_dir: PathBuf,
|
||||
pub layout_json: PathBuf,
|
||||
pub session_json: PathBuf,
|
||||
pub profile_name: String,
|
||||
pub profile: ProfileConfig,
|
||||
pub tunables: TunableValues,
|
||||
pub dirs: DirectoryValues,
|
||||
pub layout: Layout,
|
||||
}
|
||||
|
||||
impl ApplicationSettings {
|
||||
@@ -203,6 +456,7 @@ impl ApplicationSettings {
|
||||
default_profile,
|
||||
dirs,
|
||||
settings: global,
|
||||
layout,
|
||||
} = IambConfig::load(config_json.as_path())?;
|
||||
|
||||
validate_profile_names(&profiles);
|
||||
@@ -226,10 +480,17 @@ impl ApplicationSettings {
|
||||
);
|
||||
};
|
||||
|
||||
let layout = profile.layout.take().or(layout).unwrap_or_default();
|
||||
|
||||
let tunables = global.unwrap_or_default();
|
||||
let tunables = profile.settings.take().unwrap_or_default().merge(tunables);
|
||||
let tunables = tunables.values();
|
||||
|
||||
let dirs = dirs.unwrap_or_default();
|
||||
let dirs = profile.dirs.take().unwrap_or_default().merge(dirs);
|
||||
let dirs = dirs.values();
|
||||
|
||||
// Set up paths that live inside the profile's data directory.
|
||||
let mut profile_dir = config_dir.clone();
|
||||
profile_dir.push("profiles");
|
||||
profile_dir.push(profile_name.as_str());
|
||||
@@ -240,26 +501,98 @@ impl ApplicationSettings {
|
||||
let mut session_json = profile_dir;
|
||||
session_json.push("session.json");
|
||||
|
||||
let dirs = dirs.unwrap_or_default();
|
||||
let dirs = profile.dirs.take().unwrap_or_default().merge(dirs);
|
||||
let dirs = dirs.values();
|
||||
// Set up paths that live inside the profile's cache directory.
|
||||
let mut cache_dir = dirs.cache.clone();
|
||||
cache_dir.push("profiles");
|
||||
cache_dir.push(profile_name.as_str());
|
||||
|
||||
let mut layout_json = cache_dir.clone();
|
||||
layout_json.push("layout.json");
|
||||
|
||||
let settings = ApplicationSettings {
|
||||
matrix_dir,
|
||||
layout_json,
|
||||
session_json,
|
||||
profile_name,
|
||||
profile,
|
||||
tunables,
|
||||
dirs,
|
||||
layout,
|
||||
};
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
pub fn get_user_char_span<'a>(&self, user_id: &'a UserId) -> Span<'a> {
|
||||
let (color, c) = self
|
||||
.tunables
|
||||
.users
|
||||
.get(user_id)
|
||||
.map(|user| {
|
||||
(
|
||||
user.color.as_ref().map(|c| c.0),
|
||||
user.name.as_ref().and_then(|s| s.chars().next()),
|
||||
)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
|
||||
let style = user_style_from_color(color);
|
||||
|
||||
let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' '));
|
||||
|
||||
Span::styled(String::from(c), style)
|
||||
}
|
||||
|
||||
pub fn get_user_overrides(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> (Option<Color>, Option<Cow<'static, str>>) {
|
||||
self.tunables
|
||||
.users
|
||||
.get(user_id)
|
||||
.map(|user| (user.color.as_ref().map(|c| c.0), user.name.clone().map(Cow::Owned)))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn get_user_style(&self, user_id: &UserId) -> Style {
|
||||
let color = self
|
||||
.tunables
|
||||
.users
|
||||
.get(user_id)
|
||||
.and_then(|user| user.color.as_ref().map(|c| c.0))
|
||||
.unwrap_or_else(|| user_color(user_id.as_str()));
|
||||
|
||||
user_style_from_color(color)
|
||||
}
|
||||
|
||||
pub fn get_user_span<'a>(&self, user_id: &'a UserId, info: &'a RoomInfo) -> Span<'a> {
|
||||
let (color, name) = self.get_user_overrides(user_id);
|
||||
|
||||
let color = color.unwrap_or_else(|| user_color(user_id.as_str()));
|
||||
let style = user_style_from_color(color);
|
||||
let name = match (name, &self.tunables.username_display) {
|
||||
(Some(name), _) => name,
|
||||
(None, UserDisplayStyle::Username) => Cow::Borrowed(user_id.as_str()),
|
||||
(None, UserDisplayStyle::LocalPart) => Cow::Borrowed(user_id.localpart()),
|
||||
(None, UserDisplayStyle::DisplayName) => {
|
||||
if let Some(display) = info.display_names.get(user_id) {
|
||||
Cow::Borrowed(display.as_str())
|
||||
} else {
|
||||
Cow::Borrowed(user_id.as_str())
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Span::styled(name, style)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use matrix_sdk::ruma::user_id;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
#[test]
|
||||
fn test_profile_name_invalid() {
|
||||
@@ -283,4 +616,145 @@ mod tests {
|
||||
assert_eq!(validate_profile_name("a.b-c"), true);
|
||||
assert_eq!(validate_profile_name("a.B-c"), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merge_users() {
|
||||
let a = None;
|
||||
let b = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
|
||||
color: Some(UserColor(Color::Red)),
|
||||
name: Some("Hello".into()),
|
||||
})]
|
||||
.into_iter()
|
||||
.collect::<HashMap<_, _>>();
|
||||
let c = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
|
||||
color: Some(UserColor(Color::Green)),
|
||||
name: Some("World".into()),
|
||||
})]
|
||||
.into_iter()
|
||||
.collect::<HashMap<_, _>>();
|
||||
|
||||
let res = merge_users(a.clone(), a.clone());
|
||||
assert_eq!(res, None);
|
||||
|
||||
let res = merge_users(a.clone(), Some(b.clone()));
|
||||
assert_eq!(res, Some(b.clone()));
|
||||
|
||||
let res = merge_users(Some(b.clone()), a.clone());
|
||||
assert_eq!(res, Some(b.clone()));
|
||||
|
||||
let res = merge_users(Some(b.clone()), Some(b.clone()));
|
||||
assert_eq!(res, Some(b.clone()));
|
||||
|
||||
let res = merge_users(Some(b.clone()), Some(c.clone()));
|
||||
assert_eq!(res, Some(c.clone()));
|
||||
|
||||
let res = merge_users(Some(c.clone()), Some(b.clone()));
|
||||
assert_eq!(res, Some(b.clone()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_tunables() {
|
||||
let res: Tunables = serde_json::from_str("{}").unwrap();
|
||||
assert_eq!(res.typing_notice_send, None);
|
||||
assert_eq!(res.typing_notice_display, None);
|
||||
assert_eq!(res.users, None);
|
||||
|
||||
let res: Tunables = serde_json::from_str("{\"typing_notice_send\": true}").unwrap();
|
||||
assert_eq!(res.typing_notice_send, Some(true));
|
||||
assert_eq!(res.typing_notice_display, None);
|
||||
assert_eq!(res.users, None);
|
||||
|
||||
let res: Tunables = serde_json::from_str("{\"typing_notice_send\": false}").unwrap();
|
||||
assert_eq!(res.typing_notice_send, Some(false));
|
||||
assert_eq!(res.typing_notice_display, None);
|
||||
assert_eq!(res.users, None);
|
||||
|
||||
let res: Tunables = serde_json::from_str("{\"users\": {}}").unwrap();
|
||||
assert_eq!(res.typing_notice_send, None);
|
||||
assert_eq!(res.typing_notice_display, None);
|
||||
assert_eq!(res.users, Some(HashMap::new()));
|
||||
|
||||
let res: Tunables = serde_json::from_str(
|
||||
"{\"users\": {\"@a:b.c\": {\"color\": \"black\", \"name\": \"Tim\"}}}",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(res.typing_notice_send, None);
|
||||
assert_eq!(res.typing_notice_display, None);
|
||||
let users = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables {
|
||||
color: Some(UserColor(Color::Black)),
|
||||
name: Some("Tim".into()),
|
||||
})];
|
||||
assert_eq!(res.users, Some(users.into_iter().collect()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_tunables_username_display() {
|
||||
let res: Tunables = serde_json::from_str("{\"username_display\": \"username\"}").unwrap();
|
||||
assert_eq!(res.username_display, Some(UserDisplayStyle::Username));
|
||||
|
||||
let res: Tunables = serde_json::from_str("{\"username_display\": \"localpart\"}").unwrap();
|
||||
assert_eq!(res.username_display, Some(UserDisplayStyle::LocalPart));
|
||||
|
||||
let res: Tunables =
|
||||
serde_json::from_str("{\"username_display\": \"displayname\"}").unwrap();
|
||||
assert_eq!(res.username_display, Some(UserDisplayStyle::DisplayName));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_layout() {
|
||||
let user = WindowPath::UserId(user_id!("@user:example.com").to_owned());
|
||||
let alias = WindowPath::AliasId(OwnedRoomAliasId::try_from("#room:example.com").unwrap());
|
||||
let room = WindowPath::RoomId(OwnedRoomId::try_from("!room:example.com").unwrap());
|
||||
let dms = WindowPath::Window(IambId::DirectList);
|
||||
let welcome = WindowPath::Window(IambId::Welcome);
|
||||
|
||||
let res: Layout = serde_json::from_str("{\"style\": \"restore\"}").unwrap();
|
||||
assert_eq!(res, Layout::Restore);
|
||||
|
||||
let res: Layout = serde_json::from_str("{\"style\": \"new\"}").unwrap();
|
||||
assert_eq!(res, Layout::New);
|
||||
|
||||
let res: Layout = serde_json::from_str(
|
||||
"{\"style\": \"config\", \"tabs\": [{\"window\":\"@user:example.com\"}]}",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(res, Layout::Config {
|
||||
tabs: vec![WindowLayout::Window { window: user.clone() }]
|
||||
});
|
||||
|
||||
let res: Layout = serde_json::from_str(
|
||||
"{\
|
||||
\"style\": \"config\",\
|
||||
\"tabs\": [\
|
||||
{\"split\":[\
|
||||
{\"window\":\"@user:example.com\"},\
|
||||
{\"window\":\"#room:example.com\"}\
|
||||
]},\
|
||||
{\"split\":[\
|
||||
{\"window\":\"!room:example.com\"},\
|
||||
{\"split\":[\
|
||||
{\"window\":\"iamb://dms\"},\
|
||||
{\"window\":\"iamb://welcome\"}\
|
||||
]}\
|
||||
]}\
|
||||
]}",
|
||||
)
|
||||
.unwrap();
|
||||
let split1 = WindowLayout::Split {
|
||||
split: vec![
|
||||
WindowLayout::Window { window: user.clone() },
|
||||
WindowLayout::Window { window: alias },
|
||||
],
|
||||
};
|
||||
let split2 = WindowLayout::Split {
|
||||
split: vec![WindowLayout::Window { window: dms }, WindowLayout::Window {
|
||||
window: welcome,
|
||||
}],
|
||||
};
|
||||
let split3 = WindowLayout::Split {
|
||||
split: vec![WindowLayout::Window { window: room }, split2],
|
||||
};
|
||||
let tabs = vec![split1, split3];
|
||||
assert_eq!(res, Layout::Config { tabs });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +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, Keybindings};
|
||||
use crate::base::{IambAction, IambInfo, Keybindings, MATRIX_ID_WORD};
|
||||
|
||||
/// 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);
|
||||
}
|
||||
type IambStep = InputStep<IambInfo>;
|
||||
|
||||
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);
|
||||
|
||||
@@ -44,19 +33,27 @@ pub fn setup_keybindings() -> Keybindings {
|
||||
(EdgeRepeat::Once, ctrl_w.clone()),
|
||||
(EdgeRepeat::Once, ctrl_z),
|
||||
];
|
||||
let zoom = InputStep::new().actions(vec![WindowAction::ZoomToggle.into()]);
|
||||
let zoom = IambStep::new()
|
||||
.actions(vec![WindowAction::ZoomToggle.into()])
|
||||
.goto(VimMode::Normal);
|
||||
|
||||
ism.add_mapping(VimMode::Normal, &cwz, &zoom);
|
||||
ism.add_mapping(VimMode::Visual, &cwz, &zoom);
|
||||
ism.add_mapping(VimMode::Normal, &cwcz, &zoom);
|
||||
ism.add_mapping(VimMode::Visual, &cwcz, &zoom);
|
||||
|
||||
let cwm = vec![
|
||||
(EdgeRepeat::Once, ctrl_w.clone()),
|
||||
(EdgeRepeat::Once, key_m_lc),
|
||||
];
|
||||
let cwcm = vec![(EdgeRepeat::Once, ctrl_w), (EdgeRepeat::Once, ctrl_m)];
|
||||
let stoggle = InputStep::new().actions(vec![IambAction::ToggleScrollbackFocus.into()]);
|
||||
let stoggle = IambStep::new()
|
||||
.actions(vec![IambAction::ToggleScrollbackFocus.into()])
|
||||
.goto(VimMode::Normal);
|
||||
ism.add_mapping(VimMode::Normal, &cwm, &stoggle);
|
||||
ism.add_mapping(VimMode::Visual, &cwm, &stoggle);
|
||||
ism.add_mapping(VimMode::Normal, &cwcm, &stoggle);
|
||||
ism.add_mapping(VimMode::Visual, &cwcm, &stoggle);
|
||||
|
||||
return ism;
|
||||
}
|
||||
|
||||
415
src/main.rs
415
src/main.rs
@@ -6,23 +6,37 @@ use std::collections::VecDeque;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::Display;
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::io::{stdout, BufReader, Stdout};
|
||||
use std::io::{stdout, BufReader, BufWriter, Stdout};
|
||||
use std::ops::DerefMut;
|
||||
use std::process;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use clap::Parser;
|
||||
use tokio::sync::Mutex as AsyncMutex;
|
||||
use tracing::{self, Level};
|
||||
use tracing_subscriber::FmtSubscriber;
|
||||
|
||||
use matrix_sdk::ruma::OwnedUserId;
|
||||
use matrix_sdk::{
|
||||
config::SyncSettings,
|
||||
ruma::{
|
||||
api::client::filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
|
||||
OwnedUserId,
|
||||
},
|
||||
};
|
||||
|
||||
use modalkit::crossterm::{
|
||||
self,
|
||||
cursor::Show as CursorShow,
|
||||
event::{poll, read, Event},
|
||||
event::{
|
||||
poll,
|
||||
read,
|
||||
DisableBracketedPaste,
|
||||
DisableFocusChange,
|
||||
EnableBracketedPaste,
|
||||
EnableFocusChange,
|
||||
Event,
|
||||
},
|
||||
execute,
|
||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle},
|
||||
};
|
||||
@@ -41,6 +55,7 @@ mod commands;
|
||||
mod config;
|
||||
mod keybindings;
|
||||
mod message;
|
||||
mod util;
|
||||
mod windows;
|
||||
mod worker;
|
||||
|
||||
@@ -51,21 +66,19 @@ use crate::{
|
||||
base::{
|
||||
AsyncProgramStore,
|
||||
ChatStore,
|
||||
HomeserverAction,
|
||||
IambAction,
|
||||
IambBufferId,
|
||||
IambError,
|
||||
IambId,
|
||||
IambInfo,
|
||||
IambResult,
|
||||
ProgramAction,
|
||||
ProgramCommands,
|
||||
ProgramContext,
|
||||
ProgramStore,
|
||||
},
|
||||
config::{ApplicationSettings, Iamb},
|
||||
message::{Message, MessageContent, MessageTimeStamp},
|
||||
windows::IambWindow,
|
||||
worker::{ClientWorker, LoginStyle, Requester},
|
||||
worker::{create_room, ClientWorker, LoginStyle, Requester},
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
@@ -76,37 +89,141 @@ use modalkit::{
|
||||
EditError,
|
||||
EditInfo,
|
||||
Editable,
|
||||
EditorAction,
|
||||
InfoMessage,
|
||||
InsertTextAction,
|
||||
Jumpable,
|
||||
Promptable,
|
||||
Scrollable,
|
||||
TabAction,
|
||||
TabContainer,
|
||||
TabCount,
|
||||
UIError,
|
||||
WindowAction,
|
||||
WindowContainer,
|
||||
},
|
||||
base::{OpenTarget, RepeatType},
|
||||
base::{MoveDir1D, OpenTarget, RepeatType},
|
||||
context::Resolve,
|
||||
key::KeyManager,
|
||||
store::Store,
|
||||
},
|
||||
input::{bindings::BindingMachine, key::TerminalKey},
|
||||
input::{bindings::BindingMachine, dialog::Pager, key::TerminalKey},
|
||||
widgets::{
|
||||
cmdbar::CommandBarState,
|
||||
screen::{Screen, ScreenState},
|
||||
screen::{FocusList, Screen, ScreenState, TabLayoutDescription},
|
||||
windows::WindowLayoutDescription,
|
||||
TerminalCursor,
|
||||
TerminalExtOps,
|
||||
Window,
|
||||
},
|
||||
};
|
||||
|
||||
fn config_tab_to_desc(
|
||||
layout: config::WindowLayout,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<WindowLayoutDescription<IambInfo>> {
|
||||
let desc = match layout {
|
||||
config::WindowLayout::Window { window } => {
|
||||
let ChatStore { names, worker, .. } = &mut store.application;
|
||||
|
||||
let window = match window {
|
||||
config::WindowPath::UserId(user_id) => {
|
||||
let name = user_id.to_string();
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
IambId::Room(room_id)
|
||||
},
|
||||
config::WindowPath::RoomId(room_id) => IambId::Room(room_id),
|
||||
config::WindowPath::AliasId(alias) => {
|
||||
let name = alias.to_string();
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
IambId::Room(room_id)
|
||||
},
|
||||
config::WindowPath::Window(id) => id,
|
||||
};
|
||||
|
||||
WindowLayoutDescription::Window { window, length: None }
|
||||
},
|
||||
config::WindowLayout::Split { split } => {
|
||||
let children = split
|
||||
.into_iter()
|
||||
.map(|child| config_tab_to_desc(child, store))
|
||||
.collect::<IambResult<Vec<_>>>()?;
|
||||
|
||||
WindowLayoutDescription::Split { children, length: None }
|
||||
},
|
||||
};
|
||||
|
||||
Ok(desc)
|
||||
}
|
||||
|
||||
fn setup_screen(
|
||||
settings: ApplicationSettings,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<ScreenState<IambWindow, IambInfo>> {
|
||||
let cmd = CommandBarState::new(store);
|
||||
let dims = crossterm::terminal::size()?;
|
||||
let area = Rect::new(0, 0, dims.0, dims.1);
|
||||
|
||||
match settings.layout {
|
||||
config::Layout::Restore => {
|
||||
if let Ok(layout) = std::fs::read(&settings.layout_json) {
|
||||
let tabs: TabLayoutDescription<IambInfo> =
|
||||
serde_json::from_slice(&layout).map_err(IambError::from)?;
|
||||
let tabs = tabs.to_layout(area.into(), store)?;
|
||||
|
||||
return Ok(ScreenState::from_list(tabs, cmd));
|
||||
}
|
||||
},
|
||||
config::Layout::New => {},
|
||||
config::Layout::Config { tabs } => {
|
||||
let mut list = FocusList::default();
|
||||
|
||||
for tab in tabs.into_iter() {
|
||||
let tab = config_tab_to_desc(tab, store)?;
|
||||
let tab = tab.to_layout(area.into(), store)?;
|
||||
list.push(tab);
|
||||
}
|
||||
|
||||
return Ok(ScreenState::from_list(list, cmd));
|
||||
},
|
||||
}
|
||||
|
||||
let win = settings
|
||||
.tunables
|
||||
.default_room
|
||||
.and_then(|room| IambWindow::find(room, store).ok())
|
||||
.or_else(|| IambWindow::open(IambId::Welcome, store).ok())
|
||||
.unwrap();
|
||||
|
||||
return Ok(ScreenState::new(win, cmd));
|
||||
}
|
||||
|
||||
struct Application {
|
||||
store: AsyncProgramStore,
|
||||
worker: Requester,
|
||||
/// Terminal backend.
|
||||
terminal: Terminal<CrosstermBackend<Stdout>>,
|
||||
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
||||
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
||||
cmds: ProgramCommands,
|
||||
|
||||
/// State for the Matrix client, editing, etc.
|
||||
store: AsyncProgramStore,
|
||||
|
||||
/// UI state (open tabs, command bar, etc.) to use when rendering.
|
||||
screen: ScreenState<IambWindow, IambInfo>,
|
||||
|
||||
/// Handle to communicate synchronously with the Matrix worker task.
|
||||
worker: Requester,
|
||||
|
||||
/// Mapped keybindings.
|
||||
bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
|
||||
|
||||
/// Pending actions to run.
|
||||
actstack: VecDeque<(ProgramAction, ProgramContext)>,
|
||||
|
||||
/// Whether or not the terminal is currently focused.
|
||||
focused: bool,
|
||||
|
||||
/// The tab layout before the last executed [TabAction].
|
||||
last_layout: Option<TabLayoutDescription<IambInfo>>,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
@@ -117,6 +234,8 @@ impl Application {
|
||||
let mut stdout = stdout();
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
crossterm::execute!(stdout, EnterAlternateScreen)?;
|
||||
crossterm::execute!(stdout, EnableBracketedPaste)?;
|
||||
crossterm::execute!(stdout, EnableFocusChange)?;
|
||||
|
||||
let title = format!("iamb ({})", settings.profile.user_id);
|
||||
crossterm::execute!(stdout, SetTitle(title))?;
|
||||
@@ -126,12 +245,9 @@ 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;
|
||||
let win = IambWindow::open(IambId::Welcome, locked.deref_mut()).unwrap();
|
||||
let cmd = CommandBarState::new(IambBufferId::Command, locked.deref_mut());
|
||||
let screen = ScreenState::new(win, cmd);
|
||||
let screen = setup_screen(settings, locked.deref_mut())?;
|
||||
|
||||
let worker = locked.application.worker.clone();
|
||||
drop(locked);
|
||||
@@ -144,14 +260,15 @@ impl Application {
|
||||
terminal,
|
||||
bindings,
|
||||
actstack,
|
||||
cmds,
|
||||
screen,
|
||||
focused: true,
|
||||
last_layout: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn redraw(&mut self, full: bool, store: &mut ProgramStore) -> Result<(), std::io::Error> {
|
||||
let modestr = self.bindings.showmode();
|
||||
let cursor = self.bindings.get_cursor_indicator();
|
||||
let bindings = &mut self.bindings;
|
||||
let focused = self.focused;
|
||||
let sstate = &mut self.screen;
|
||||
let term = &mut self.terminal;
|
||||
|
||||
@@ -162,9 +279,24 @@ impl Application {
|
||||
term.draw(|f| {
|
||||
let area = f.size();
|
||||
|
||||
let screen = Screen::new(store).showmode(modestr).borders(true);
|
||||
let modestr = bindings.show_mode();
|
||||
let cursor = bindings.get_cursor_indicator();
|
||||
let dialogstr = bindings.show_dialog(area.height as usize, area.width as usize);
|
||||
|
||||
// Don't show terminal cursor when we show a dialog.
|
||||
let hide_cursor = !dialogstr.is_empty();
|
||||
|
||||
let screen = Screen::new(store)
|
||||
.show_dialog(dialogstr)
|
||||
.show_mode(modestr)
|
||||
.borders(true)
|
||||
.focus(focused);
|
||||
f.render_stateful_widget(screen, area, sstate);
|
||||
|
||||
if hide_cursor {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some((cx, cy)) = sstate.get_term_cursor() {
|
||||
if let Some(c) = cursor {
|
||||
let style = Style::default().fg(Color::Green);
|
||||
@@ -175,8 +307,6 @@ impl Application {
|
||||
}
|
||||
f.set_cursor(cx, cy);
|
||||
}
|
||||
|
||||
store.application.load_older(area.height as u32);
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
@@ -186,7 +316,8 @@ impl Application {
|
||||
loop {
|
||||
self.redraw(false, self.store.clone().lock().await.deref_mut())?;
|
||||
|
||||
if !poll(Duration::from_millis(500))? {
|
||||
if !poll(Duration::from_secs(1))? {
|
||||
// Redraw in case there's new messages to show.
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -195,14 +326,31 @@ impl Application {
|
||||
Event::Mouse(_) => {
|
||||
// Do nothing for now.
|
||||
},
|
||||
Event::FocusGained | Event::FocusLost => {
|
||||
// Do nothing for now.
|
||||
Event::FocusGained => {
|
||||
self.focused = true;
|
||||
},
|
||||
Event::FocusLost => {
|
||||
self.focused = false;
|
||||
},
|
||||
Event::Resize(_, _) => {
|
||||
// We'll redraw for the new size next time step() is called.
|
||||
},
|
||||
Event::Paste(_) => {
|
||||
// Do nothing for now.
|
||||
Event::Paste(s) => {
|
||||
let act = InsertTextAction::Transcribe(s, MoveDir1D::Previous, 1.into());
|
||||
let act = EditorAction::from(act);
|
||||
let ctx = ProgramContext::default();
|
||||
let mut store = self.store.lock().await;
|
||||
|
||||
match self.screen.editor_command(&act, &ctx, store.deref_mut()) {
|
||||
Ok(None) => {},
|
||||
Ok(Some(info)) => {
|
||||
drop(store);
|
||||
self.handle_info(info);
|
||||
},
|
||||
Err(e) => {
|
||||
self.screen.push_error(e);
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -226,7 +374,7 @@ impl Application {
|
||||
}
|
||||
}
|
||||
|
||||
fn action_run(
|
||||
async fn action_run(
|
||||
&mut self,
|
||||
action: ProgramAction,
|
||||
ctx: ProgramContext,
|
||||
@@ -257,12 +405,11 @@ impl Application {
|
||||
},
|
||||
|
||||
// Simple delegations.
|
||||
Action::Application(act) => self.iamb_run(act, ctx, store)?,
|
||||
Action::Application(act) => self.iamb_run(act, ctx, store).await?,
|
||||
Action::CommandBar(act) => self.screen.command_bar(&act, &ctx)?,
|
||||
Action::Macro(act) => self.bindings.macro_command(&act, &ctx, store)?,
|
||||
Action::Scroll(style) => self.screen.scroll(&style, &ctx, store)?,
|
||||
Action::Suspend => self.terminal.program_suspend()?,
|
||||
Action::Tab(cmd) => self.screen.tab_command(&cmd, &ctx, store)?,
|
||||
Action::ShowInfoMessage(info) => Some(info),
|
||||
Action::Window(cmd) => self.screen.window_command(&cmd, &ctx, store)?,
|
||||
|
||||
Action::Jump(l, dir, count) => {
|
||||
@@ -271,8 +418,20 @@ impl Application {
|
||||
|
||||
None
|
||||
},
|
||||
Action::Suspend => {
|
||||
self.terminal.program_suspend()?;
|
||||
|
||||
None
|
||||
},
|
||||
|
||||
// UI actions.
|
||||
Action::Tab(cmd) => {
|
||||
if let TabAction::Close(_, _) = &cmd {
|
||||
self.last_layout = self.screen.as_description().into();
|
||||
}
|
||||
|
||||
self.screen.tab_command(&cmd, &ctx, store)?
|
||||
},
|
||||
Action::RedrawScreen => {
|
||||
self.screen.clear_message();
|
||||
self.redraw(true, store)?;
|
||||
@@ -288,7 +447,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
|
||||
@@ -314,7 +473,7 @@ impl Application {
|
||||
return Ok(info);
|
||||
}
|
||||
|
||||
fn iamb_run(
|
||||
async fn iamb_run(
|
||||
&mut self,
|
||||
action: IambAction,
|
||||
ctx: ProgramContext,
|
||||
@@ -327,24 +486,25 @@ impl Application {
|
||||
None
|
||||
},
|
||||
|
||||
IambAction::Room(act) => {
|
||||
let acts = self.screen.current_window_mut()?.room_command(act, ctx, store)?;
|
||||
IambAction::Homeserver(act) => {
|
||||
let acts = self.homeserver_command(act, ctx, store).await?;
|
||||
self.action_prepend(acts);
|
||||
|
||||
None
|
||||
},
|
||||
|
||||
IambAction::SendMessage(room_id, msg) => {
|
||||
let (event_id, msg) = self.worker.send_message(room_id.clone(), msg)?;
|
||||
let user = store.application.settings.profile.user_id.clone();
|
||||
let info = store.application.get_room_info(room_id);
|
||||
let key = (MessageTimeStamp::LocalEcho, event_id);
|
||||
let msg = MessageContent::Original(msg.into());
|
||||
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
|
||||
info.messages.insert(key, msg);
|
||||
IambAction::Message(act) => {
|
||||
self.screen.current_window_mut()?.message_command(act, ctx, store).await?
|
||||
},
|
||||
IambAction::Room(act) => {
|
||||
let acts = self.screen.current_window_mut()?.room_command(act, ctx, store).await?;
|
||||
self.action_prepend(acts);
|
||||
|
||||
None
|
||||
},
|
||||
IambAction::Send(act) => {
|
||||
self.screen.current_window_mut()?.send_command(act, ctx, store).await?
|
||||
},
|
||||
|
||||
IambAction::Verify(act, user_dev) => {
|
||||
if let Some(sas) = store.application.verifications.get(&user_dev) {
|
||||
self.worker.verify(act, sas.clone())?
|
||||
@@ -364,6 +524,37 @@ 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)])
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_info(&mut self, info: InfoMessage) {
|
||||
match info {
|
||||
InfoMessage::Message(info) => {
|
||||
self.screen.push_info(info);
|
||||
},
|
||||
InfoMessage::Pager(text) => {
|
||||
let pager = Box::new(Pager::new(text, vec![]));
|
||||
self.bindings.run_dialog(pager);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<(), std::io::Error> {
|
||||
self.terminal.clear()?;
|
||||
|
||||
@@ -378,17 +569,24 @@ impl Application {
|
||||
let mut keyskip = false;
|
||||
|
||||
while let Some((action, ctx)) = self.action_pop(keyskip) {
|
||||
match self.action_run(action, ctx, locked.deref_mut()) {
|
||||
match self.action_run(action, ctx, locked.deref_mut()).await {
|
||||
Ok(None) => {
|
||||
// Continue processing.
|
||||
continue;
|
||||
},
|
||||
Ok(Some(info)) => {
|
||||
self.screen.push_info(info);
|
||||
self.handle_info(info);
|
||||
|
||||
// Continue processing; we'll redraw later.
|
||||
continue;
|
||||
},
|
||||
Err(
|
||||
UIError::NeedConfirm(dialog) |
|
||||
UIError::EditingFailure(EditError::NeedConfirm(dialog)),
|
||||
) => {
|
||||
self.bindings.run_dialog(dialog);
|
||||
continue;
|
||||
},
|
||||
Err(e) => {
|
||||
self.screen.push_error(e);
|
||||
|
||||
@@ -400,6 +598,19 @@ impl Application {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref layout) = self.last_layout {
|
||||
let locked = self.store.lock().await;
|
||||
let path = locked.application.settings.layout_json.as_path();
|
||||
path.parent().map(create_dir_all).transpose()?;
|
||||
|
||||
let file = File::create(path)?;
|
||||
let writer = BufWriter::new(file);
|
||||
|
||||
if let Err(e) = serde_json::to_writer(writer, layout) {
|
||||
tracing::error!("Failed to save window layout while exiting: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||
self.terminal.show_cursor()?;
|
||||
@@ -408,7 +619,7 @@ impl Application {
|
||||
}
|
||||
}
|
||||
|
||||
fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<()> {
|
||||
async fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<()> {
|
||||
println!("Logging in for {}...", settings.profile.user_id);
|
||||
|
||||
if settings.session_json.is_file() {
|
||||
@@ -427,34 +638,88 @@ fn login(worker: Requester, settings: &ApplicationSettings) -> IambResult<()> {
|
||||
match worker.login(LoginStyle::Password(password)) {
|
||||
Ok(info) => {
|
||||
if let Some(msg) = info {
|
||||
println!("{}", msg);
|
||||
println!("{msg}");
|
||||
}
|
||||
|
||||
break;
|
||||
},
|
||||
Err(err) => {
|
||||
println!("Failed to login: {}", err);
|
||||
println!("Failed to login: {err}");
|
||||
continue;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Perform an initial, lazily-loaded sync.
|
||||
let mut room = RoomEventFilter::default();
|
||||
room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
|
||||
|
||||
let mut room_ev = RoomFilter::default();
|
||||
room_ev.state = room;
|
||||
|
||||
let mut filter = FilterDefinition::default();
|
||||
filter.room = room_ev;
|
||||
|
||||
let settings = SyncSettings::new().filter(filter.into());
|
||||
|
||||
worker.client.sync_once(settings).await.map_err(IambError::from)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_exit<T: Display, N>(v: T) -> N {
|
||||
println!("{}", v);
|
||||
println!("{v}");
|
||||
process::exit(2);
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> IambResult<()> {
|
||||
async fn run(settings: ApplicationSettings) -> IambResult<()> {
|
||||
// Set up the async worker thread and global store.
|
||||
let worker = ClientWorker::spawn(settings.clone()).await;
|
||||
let store = ChatStore::new(worker.clone(), settings.clone());
|
||||
let store = Store::new(store);
|
||||
let store = Arc::new(AsyncMutex::new(store));
|
||||
worker.init(store.clone());
|
||||
|
||||
login(worker, &settings).await.unwrap_or_else(print_exit);
|
||||
|
||||
fn restore_tty() {
|
||||
let _ = crossterm::terminal::disable_raw_mode();
|
||||
let _ = crossterm::execute!(stdout(), DisableBracketedPaste);
|
||||
let _ = crossterm::execute!(stdout(), DisableFocusChange);
|
||||
let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
|
||||
let _ = crossterm::execute!(stdout(), CursorShow);
|
||||
}
|
||||
|
||||
// Make sure panics clean up the terminal properly.
|
||||
let orig_hook = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |panic_info| {
|
||||
restore_tty();
|
||||
orig_hook(panic_info);
|
||||
process::exit(1);
|
||||
}));
|
||||
|
||||
let mut application = Application::new(settings, store).await?;
|
||||
|
||||
// We can now run the application.
|
||||
application.run().await?;
|
||||
restore_tty();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> IambResult<()> {
|
||||
// Parse command-line flags.
|
||||
let iamb = Iamb::parse();
|
||||
|
||||
// Load configuration and set up the Matrix SDK.
|
||||
let settings = ApplicationSettings::load(iamb).unwrap_or_else(print_exit);
|
||||
|
||||
// Set umask on Unix platforms so that tokens, keys, etc. are only readable by the user.
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
libc::umask(0o077);
|
||||
};
|
||||
|
||||
// Set up the tracing subscriber so we can log client messages.
|
||||
let log_prefix = format!("iamb-log-{}", settings.profile_name);
|
||||
let log_dir = settings.dirs.logs.as_path();
|
||||
@@ -463,37 +728,27 @@ async fn main() -> IambResult<()> {
|
||||
create_dir_all(log_dir)?;
|
||||
|
||||
let appender = tracing_appender::rolling::daily(log_dir, log_prefix);
|
||||
let (appender, _) = tracing_appender::non_blocking(appender);
|
||||
let (appender, guard) = tracing_appender::non_blocking(appender);
|
||||
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
.with_writer(appender)
|
||||
.with_max_level(Level::WARN)
|
||||
.with_max_level(settings.tunables.log_level)
|
||||
.finish();
|
||||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||
|
||||
// Set up the async worker thread and global store.
|
||||
let worker = ClientWorker::spawn(settings.clone());
|
||||
let store = ChatStore::new(worker.clone(), settings.clone());
|
||||
let store = Store::new(store);
|
||||
let store = Arc::new(AsyncMutex::new(store));
|
||||
worker.init(store.clone());
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.worker_threads(2)
|
||||
.thread_name_fn(|| {
|
||||
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
|
||||
format!("iamb-worker-{id}")
|
||||
})
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
login(worker, &settings).unwrap_or_else(print_exit);
|
||||
|
||||
// Make sure panics clean up the terminal properly.
|
||||
let orig_hook = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |panic_info| {
|
||||
let _ = crossterm::terminal::disable_raw_mode();
|
||||
let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
|
||||
let _ = crossterm::execute!(stdout(), CursorShow);
|
||||
orig_hook(panic_info);
|
||||
process::exit(1);
|
||||
}));
|
||||
|
||||
let mut application = Application::new(settings, store).await?;
|
||||
|
||||
// We can now run the application.
|
||||
application.run().await?;
|
||||
rt.block_on(async move { run(settings).await })?;
|
||||
|
||||
drop(guard);
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
650
src/message.rs
650
src/message.rs
@@ -1,650 +0,0 @@
|
||||
use std::borrow::Cow;
|
||||
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::str::Lines;
|
||||
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use matrix_sdk::ruma::{
|
||||
events::{
|
||||
room::message::{MessageType, RoomMessageEventContent},
|
||||
MessageLikeEvent,
|
||||
},
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
OwnedEventId,
|
||||
OwnedUserId,
|
||||
UInt,
|
||||
};
|
||||
|
||||
use modalkit::tui::{
|
||||
style::{Color, Modifier as StyleModifier, Style},
|
||||
text::{Span, Spans, Text},
|
||||
};
|
||||
|
||||
use modalkit::editing::{base::ViewportContext, cursor::Cursor};
|
||||
|
||||
use crate::base::{IambResult, RoomInfo};
|
||||
|
||||
pub type MessageEvent = MessageLikeEvent<RoomMessageEventContent>;
|
||||
pub type MessageFetchResult = IambResult<(Option<String>, Vec<MessageEvent>)>;
|
||||
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
|
||||
pub type Messages = BTreeMap<MessageKey, Message>;
|
||||
|
||||
const COLORS: [Color; 13] = [
|
||||
Color::Blue,
|
||||
Color::Cyan,
|
||||
Color::Green,
|
||||
Color::LightBlue,
|
||||
Color::LightGreen,
|
||||
Color::LightCyan,
|
||||
Color::LightMagenta,
|
||||
Color::LightRed,
|
||||
Color::LightYellow,
|
||||
Color::Magenta,
|
||||
Color::Red,
|
||||
Color::Reset,
|
||||
Color::Yellow,
|
||||
];
|
||||
|
||||
const USER_GUTTER: usize = 30;
|
||||
const TIME_GUTTER: usize = 12;
|
||||
const MIN_MSG_LEN: usize = 30;
|
||||
|
||||
const USER_GUTTER_EMPTY: &str = " ";
|
||||
const USER_GUTTER_EMPTY_SPAN: Span<'static> = Span {
|
||||
content: Cow::Borrowed(USER_GUTTER_EMPTY),
|
||||
style: Style {
|
||||
fg: None,
|
||||
bg: None,
|
||||
add_modifier: StyleModifier::empty(),
|
||||
sub_modifier: StyleModifier::empty(),
|
||||
},
|
||||
};
|
||||
|
||||
pub(crate) fn user_color(user: &str) -> Color {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
user.hash(&mut hasher);
|
||||
let color = hasher.finish() as usize % COLORS.len();
|
||||
|
||||
COLORS[color]
|
||||
}
|
||||
|
||||
pub(crate) fn user_style(user: &str) -> Style {
|
||||
Style::default().fg(user_color(user)).add_modifier(StyleModifier::BOLD)
|
||||
}
|
||||
|
||||
struct WrappedLinesIterator<'a> {
|
||||
iter: Lines<'a>,
|
||||
curr: Option<&'a str>,
|
||||
width: usize,
|
||||
}
|
||||
|
||||
impl<'a> WrappedLinesIterator<'a> {
|
||||
fn new(input: &'a str, width: usize) -> Self {
|
||||
WrappedLinesIterator { iter: input.lines(), curr: None, width }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for WrappedLinesIterator<'a> {
|
||||
type Item = (&'a str, usize);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.curr.is_none() {
|
||||
self.curr = self.iter.next();
|
||||
}
|
||||
|
||||
if let Some(s) = self.curr.take() {
|
||||
let width = UnicodeWidthStr::width(s);
|
||||
|
||||
if width <= self.width {
|
||||
return Some((s, width));
|
||||
} else {
|
||||
// Find where to split the line.
|
||||
let mut width = 0;
|
||||
let mut idx = 0;
|
||||
|
||||
for (i, g) in UnicodeSegmentation::grapheme_indices(s, true) {
|
||||
let gw = UnicodeWidthStr::width(g);
|
||||
idx = i;
|
||||
|
||||
if width + gw > self.width {
|
||||
break;
|
||||
}
|
||||
|
||||
width += gw;
|
||||
}
|
||||
|
||||
self.curr = Some(&s[idx..]);
|
||||
|
||||
return Some((&s[..idx], width));
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn wrap(input: &str, width: usize) -> WrappedLinesIterator<'_> {
|
||||
WrappedLinesIterator::new(input, width)
|
||||
}
|
||||
|
||||
fn space(width: usize) -> String {
|
||||
" ".repeat(width)
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum TimeStampIntError {
|
||||
#[error("Integer conversion error: {0}")]
|
||||
IntError(#[from] std::num::TryFromIntError),
|
||||
|
||||
#[error("UInt conversion error: {0}")]
|
||||
UIntError(<UInt as TryFrom<u64>>::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum MessageTimeStamp {
|
||||
OriginServer(UInt),
|
||||
LocalEcho,
|
||||
}
|
||||
|
||||
impl MessageTimeStamp {
|
||||
fn show(&self) -> Option<Span> {
|
||||
match self {
|
||||
MessageTimeStamp::OriginServer(ts) => {
|
||||
let time = i64::from(*ts) / 1000;
|
||||
let time = NaiveDateTime::from_timestamp_opt(time, 0)?;
|
||||
let time = DateTime::<Utc>::from_utc(time, Utc);
|
||||
let time = time.format("%T");
|
||||
let time = format!(" [{}]", time);
|
||||
|
||||
Span::raw(time).into()
|
||||
},
|
||||
MessageTimeStamp::LocalEcho => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_local_echo(&self) -> bool {
|
||||
matches!(self, MessageTimeStamp::LocalEcho)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for MessageTimeStamp {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
match (self, other) {
|
||||
(MessageTimeStamp::OriginServer(_), MessageTimeStamp::LocalEcho) => Ordering::Less,
|
||||
(MessageTimeStamp::OriginServer(a), MessageTimeStamp::OriginServer(b)) => a.cmp(b),
|
||||
(MessageTimeStamp::LocalEcho, MessageTimeStamp::OriginServer(_)) => Ordering::Greater,
|
||||
(MessageTimeStamp::LocalEcho, MessageTimeStamp::LocalEcho) => Ordering::Equal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for MessageTimeStamp {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
|
||||
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
|
||||
MessageTimeStamp::OriginServer(millis.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&MessageTimeStamp> for usize {
|
||||
type Error = TimeStampIntError;
|
||||
|
||||
fn try_from(ts: &MessageTimeStamp) -> Result<Self, Self::Error> {
|
||||
let n = match ts {
|
||||
MessageTimeStamp::LocalEcho => 0,
|
||||
MessageTimeStamp::OriginServer(u) => usize::try_from(u64::from(*u))?,
|
||||
};
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<usize> for MessageTimeStamp {
|
||||
type Error = TimeStampIntError;
|
||||
|
||||
fn try_from(u: usize) -> Result<Self, Self::Error> {
|
||||
if u == 0 {
|
||||
Ok(MessageTimeStamp::LocalEcho)
|
||||
} else {
|
||||
let n = u64::try_from(u)?;
|
||||
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
|
||||
|
||||
Ok(MessageTimeStamp::OriginServer(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct MessageCursor {
|
||||
/// When timestamp is None, the corner is determined by moving backwards from
|
||||
/// the most recently received message.
|
||||
pub timestamp: Option<MessageKey>,
|
||||
|
||||
/// A row within the [Text] representation of a [Message].
|
||||
pub text_row: usize,
|
||||
}
|
||||
|
||||
impl MessageCursor {
|
||||
pub fn new(timestamp: MessageKey, text_row: usize) -> Self {
|
||||
MessageCursor { timestamp: Some(timestamp), text_row }
|
||||
}
|
||||
|
||||
/// Get a cursor that refers to the most recent message.
|
||||
pub fn latest() -> Self {
|
||||
MessageCursor::default()
|
||||
}
|
||||
|
||||
pub fn to_key<'a>(&'a self, info: &'a RoomInfo) -> Option<&'a MessageKey> {
|
||||
if let Some(ref key) = self.timestamp {
|
||||
Some(key)
|
||||
} else {
|
||||
Some(info.messages.last_key_value()?.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_cursor(cursor: &Cursor, info: &RoomInfo) -> Option<Self> {
|
||||
let ev_hash = u64::try_from(cursor.get_x()).ok()?;
|
||||
let ev_term = OwnedEventId::try_from("$").ok()?;
|
||||
|
||||
let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?;
|
||||
let start = (ts_start, ev_term);
|
||||
let mut mc = None;
|
||||
|
||||
for ((ts, event_id), _) in info.messages.range(start..) {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
event_id.hash(&mut hasher);
|
||||
|
||||
if hasher.finish() == ev_hash {
|
||||
mc = Self::from((*ts, event_id.clone())).into();
|
||||
break;
|
||||
}
|
||||
|
||||
if mc.is_none() {
|
||||
mc = Self::from((*ts, event_id.clone())).into();
|
||||
}
|
||||
|
||||
if ts > &ts_start {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return mc;
|
||||
}
|
||||
|
||||
pub fn to_cursor(&self, info: &RoomInfo) -> Option<Cursor> {
|
||||
let (ts, event_id) = self.to_key(info)?;
|
||||
|
||||
let y: usize = usize::try_from(ts).ok()?;
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
event_id.hash(&mut hasher);
|
||||
let x = usize::try_from(hasher.finish()).ok()?;
|
||||
|
||||
Cursor::new(y, x).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<MessageKey>> for MessageCursor {
|
||||
fn from(key: Option<MessageKey>) -> Self {
|
||||
MessageCursor { timestamp: key, text_row: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MessageKey> for MessageCursor {
|
||||
fn from(key: MessageKey) -> Self {
|
||||
MessageCursor { timestamp: Some(key), text_row: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for MessageCursor {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
match (&self.timestamp, &other.timestamp) {
|
||||
(None, None) => self.text_row.cmp(&other.text_row),
|
||||
(None, Some(_)) => Ordering::Greater,
|
||||
(Some(_), None) => Ordering::Less,
|
||||
(Some(st), Some(ot)) => {
|
||||
let pcmp = st.cmp(ot);
|
||||
let tcmp = self.text_row.cmp(&other.text_row);
|
||||
|
||||
pcmp.then(tcmp)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for MessageCursor {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum MessageContent {
|
||||
Original(Box<RoomMessageEventContent>),
|
||||
Redacted,
|
||||
}
|
||||
|
||||
impl AsRef<str> for MessageContent {
|
||||
fn as_ref(&self) -> &str {
|
||||
match self {
|
||||
MessageContent::Original(ev) => {
|
||||
match &ev.msgtype {
|
||||
MessageType::Text(content) => {
|
||||
return content.body.as_ref();
|
||||
},
|
||||
MessageType::Emote(content) => {
|
||||
return content.body.as_ref();
|
||||
},
|
||||
MessageType::Notice(content) => {
|
||||
return content.body.as_str();
|
||||
},
|
||||
MessageType::ServerNotice(_) => {
|
||||
// XXX: implement
|
||||
|
||||
return "[server notice]";
|
||||
},
|
||||
MessageType::VerificationRequest(_) => {
|
||||
// XXX: implement
|
||||
|
||||
return "[verification request]";
|
||||
},
|
||||
MessageType::Audio(..) => {
|
||||
return "[audio]";
|
||||
},
|
||||
MessageType::File(..) => {
|
||||
return "[file]";
|
||||
},
|
||||
MessageType::Image(..) => {
|
||||
return "[image]";
|
||||
},
|
||||
MessageType::Video(..) => {
|
||||
return "[video]";
|
||||
},
|
||||
_ => return "[unknown message type]",
|
||||
}
|
||||
},
|
||||
MessageContent::Redacted => "[redacted]",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Message {
|
||||
pub content: MessageContent,
|
||||
pub sender: OwnedUserId,
|
||||
pub timestamp: MessageTimeStamp,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new(content: MessageContent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
|
||||
Message { content, sender, timestamp }
|
||||
}
|
||||
|
||||
pub fn show(&self, selected: bool, vwctx: &ViewportContext<MessageCursor>) -> Text {
|
||||
let width = vwctx.get_width();
|
||||
let msg = self.as_ref();
|
||||
|
||||
let mut lines = vec![];
|
||||
|
||||
let mut style = Style::default();
|
||||
|
||||
if selected {
|
||||
style = style.add_modifier(StyleModifier::REVERSED)
|
||||
}
|
||||
|
||||
if self.timestamp.is_local_echo() {
|
||||
style = style.add_modifier(StyleModifier::ITALIC);
|
||||
}
|
||||
|
||||
if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
|
||||
let lw = width - USER_GUTTER - TIME_GUTTER;
|
||||
|
||||
for (i, (line, w)) in wrap(msg, lw).enumerate() {
|
||||
let line = Span::styled(line, style);
|
||||
let trailing = Span::styled(space(lw.saturating_sub(w)), style);
|
||||
|
||||
if i == 0 {
|
||||
let user = self.show_sender(true);
|
||||
|
||||
if let Some(time) = self.timestamp.show() {
|
||||
lines.push(Spans(vec![user, line, trailing, time]))
|
||||
} else {
|
||||
lines.push(Spans(vec![user, line, trailing]))
|
||||
}
|
||||
} else {
|
||||
let space = USER_GUTTER_EMPTY_SPAN;
|
||||
|
||||
lines.push(Spans(vec![space, line, trailing]))
|
||||
}
|
||||
}
|
||||
} else if USER_GUTTER + MIN_MSG_LEN <= width {
|
||||
let lw = width - USER_GUTTER;
|
||||
|
||||
for (i, (line, w)) in wrap(msg, lw).enumerate() {
|
||||
let line = Span::styled(line, style);
|
||||
let trailing = Span::styled(space(lw.saturating_sub(w)), style);
|
||||
|
||||
let prefix = if i == 0 {
|
||||
self.show_sender(true)
|
||||
} else {
|
||||
USER_GUTTER_EMPTY_SPAN
|
||||
};
|
||||
|
||||
lines.push(Spans(vec![prefix, line, trailing]))
|
||||
}
|
||||
} else {
|
||||
lines.push(Spans::from(self.show_sender(false)));
|
||||
|
||||
for (line, _) in wrap(msg, width.saturating_sub(2)) {
|
||||
let line = format!(" {}", line);
|
||||
let line = Span::styled(line, style);
|
||||
|
||||
lines.push(Spans(vec![line]))
|
||||
}
|
||||
}
|
||||
|
||||
return Text { lines };
|
||||
}
|
||||
|
||||
fn show_sender(&self, align_right: bool) -> Span {
|
||||
let sender = self.sender.to_string();
|
||||
let style = user_style(sender.as_str());
|
||||
|
||||
let sender = if align_right {
|
||||
format!("{: >width$} ", sender, width = 28)
|
||||
} else {
|
||||
format!("{: <width$} ", sender, width = 28)
|
||||
};
|
||||
|
||||
Span::styled(sender, style)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MessageEvent> for Message {
|
||||
fn from(event: MessageEvent) -> Self {
|
||||
match event {
|
||||
MessageLikeEvent::Original(ev) => {
|
||||
let content = MessageContent::Original(ev.content.into());
|
||||
|
||||
Message::new(content, ev.sender, ev.origin_server_ts.into())
|
||||
},
|
||||
MessageLikeEvent::Redacted(ev) => {
|
||||
Message::new(MessageContent::Redacted, ev.sender, ev.origin_server_ts.into())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for Message {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.content.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Message {
|
||||
fn to_string(&self) -> String {
|
||||
self.as_ref().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use crate::tests::*;
|
||||
|
||||
#[test]
|
||||
fn test_wrapped_lines_ascii() {
|
||||
let s = "hello world!\nabcdefghijklmnopqrstuvwxyz\ngoodbye";
|
||||
|
||||
let mut iter = wrap(s, 100);
|
||||
assert_eq!(iter.next(), Some(("hello world!", 12)));
|
||||
assert_eq!(iter.next(), Some(("abcdefghijklmnopqrstuvwxyz", 26)));
|
||||
assert_eq!(iter.next(), Some(("goodbye", 7)));
|
||||
assert_eq!(iter.next(), None);
|
||||
|
||||
let mut iter = wrap(s, 5);
|
||||
assert_eq!(iter.next(), Some(("hello", 5)));
|
||||
assert_eq!(iter.next(), Some((" worl", 5)));
|
||||
assert_eq!(iter.next(), Some(("d!", 2)));
|
||||
assert_eq!(iter.next(), Some(("abcde", 5)));
|
||||
assert_eq!(iter.next(), Some(("fghij", 5)));
|
||||
assert_eq!(iter.next(), Some(("klmno", 5)));
|
||||
assert_eq!(iter.next(), Some(("pqrst", 5)));
|
||||
assert_eq!(iter.next(), Some(("uvwxy", 5)));
|
||||
assert_eq!(iter.next(), Some(("z", 1)));
|
||||
assert_eq!(iter.next(), Some(("goodb", 5)));
|
||||
assert_eq!(iter.next(), Some(("ye", 2)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrapped_lines_unicode() {
|
||||
let s = "CHICKEN";
|
||||
|
||||
let mut iter = wrap(s, 14);
|
||||
assert_eq!(iter.next(), Some((s, 14)));
|
||||
assert_eq!(iter.next(), None);
|
||||
|
||||
let mut iter = wrap(s, 5);
|
||||
assert_eq!(iter.next(), Some(("CH", 4)));
|
||||
assert_eq!(iter.next(), Some(("IC", 4)));
|
||||
assert_eq!(iter.next(), Some(("KE", 4)));
|
||||
assert_eq!(iter.next(), Some(("N", 2)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mc_cmp() {
|
||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
||||
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
||||
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
||||
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
||||
|
||||
// Everything is equal to itself.
|
||||
assert_eq!(mc1.cmp(&mc1), Ordering::Equal);
|
||||
assert_eq!(mc2.cmp(&mc2), Ordering::Equal);
|
||||
assert_eq!(mc3.cmp(&mc3), Ordering::Equal);
|
||||
assert_eq!(mc4.cmp(&mc4), Ordering::Equal);
|
||||
assert_eq!(mc5.cmp(&mc5), Ordering::Equal);
|
||||
|
||||
// Local echo is always greater than an origin server timestamp.
|
||||
assert_eq!(mc1.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc1.cmp(&mc3), Ordering::Greater);
|
||||
assert_eq!(mc1.cmp(&mc4), Ordering::Greater);
|
||||
assert_eq!(mc1.cmp(&mc5), Ordering::Greater);
|
||||
|
||||
// mc2 is the smallest timestamp.
|
||||
assert_eq!(mc2.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc2.cmp(&mc3), Ordering::Less);
|
||||
assert_eq!(mc2.cmp(&mc4), Ordering::Less);
|
||||
assert_eq!(mc2.cmp(&mc5), Ordering::Less);
|
||||
|
||||
// mc3 should be less than mc4 because of its event ID.
|
||||
assert_eq!(mc3.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc3.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc3.cmp(&mc4), Ordering::Less);
|
||||
assert_eq!(mc3.cmp(&mc5), Ordering::Less);
|
||||
|
||||
// mc4 should be greater than mc3 because of its event ID.
|
||||
assert_eq!(mc4.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc4.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc4.cmp(&mc3), Ordering::Greater);
|
||||
assert_eq!(mc4.cmp(&mc5), Ordering::Less);
|
||||
|
||||
// mc5 is the greatest OriginServer timestamp.
|
||||
assert_eq!(mc5.cmp(&mc1), Ordering::Less);
|
||||
assert_eq!(mc5.cmp(&mc2), Ordering::Greater);
|
||||
assert_eq!(mc5.cmp(&mc3), Ordering::Greater);
|
||||
assert_eq!(mc5.cmp(&mc4), Ordering::Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mc_to_key() {
|
||||
let info = mock_room();
|
||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
||||
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
||||
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
||||
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
||||
let mc6 = MessageCursor::latest();
|
||||
|
||||
let k1 = mc1.to_key(&info).unwrap();
|
||||
let k2 = mc2.to_key(&info).unwrap();
|
||||
let k3 = mc3.to_key(&info).unwrap();
|
||||
let k4 = mc4.to_key(&info).unwrap();
|
||||
let k5 = mc5.to_key(&info).unwrap();
|
||||
let k6 = mc6.to_key(&info).unwrap();
|
||||
|
||||
// These should all be equal to their MSGN_KEYs.
|
||||
assert_eq!(k1, &MSG1_KEY.clone());
|
||||
assert_eq!(k2, &MSG2_KEY.clone());
|
||||
assert_eq!(k3, &MSG3_KEY.clone());
|
||||
assert_eq!(k4, &MSG4_KEY.clone());
|
||||
assert_eq!(k5, &MSG5_KEY.clone());
|
||||
|
||||
// MessageCursor::latest() turns into the largest key (our local echo message).
|
||||
assert_eq!(k6, &MSG1_KEY.clone());
|
||||
|
||||
// MessageCursor::latest() fails to convert for a room w/o messages.
|
||||
let info_empty = RoomInfo::default();
|
||||
assert_eq!(mc6.to_key(&info_empty), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mc_to_from_cursor() {
|
||||
let info = mock_room();
|
||||
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
||||
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
||||
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
||||
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
||||
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
||||
let mc6 = MessageCursor::latest();
|
||||
|
||||
let identity = |mc: &MessageCursor| {
|
||||
let c = mc.to_cursor(&info).unwrap();
|
||||
|
||||
MessageCursor::from_cursor(&c, &info).unwrap()
|
||||
};
|
||||
|
||||
// These should all convert to a Cursor and back to the original value.
|
||||
assert_eq!(identity(&mc1), mc1);
|
||||
assert_eq!(identity(&mc2), mc2);
|
||||
assert_eq!(identity(&mc3), mc3);
|
||||
assert_eq!(identity(&mc4), mc4);
|
||||
assert_eq!(identity(&mc5), mc5);
|
||||
|
||||
// MessageCursor::latest() should point at the most recent message after conversion.
|
||||
assert_eq!(identity(&mc6), mc1);
|
||||
}
|
||||
}
|
||||
1284
src/message/html.rs
Normal file
1284
src/message/html.rs
Normal file
File diff suppressed because it is too large
Load Diff
1045
src/message/mod.rs
Normal file
1045
src/message/mod.rs
Normal file
File diff suppressed because it is too large
Load Diff
229
src/message/printer.rs
Normal file
229
src/message/printer.rs
Normal file
@@ -0,0 +1,229 @@
|
||||
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};
|
||||
|
||||
pub struct TextPrinter<'a> {
|
||||
text: Text<'a>,
|
||||
width: usize,
|
||||
base_style: Style,
|
||||
hide_reply: bool,
|
||||
|
||||
alignment: Alignment,
|
||||
curr_spans: Vec<Span<'a>>,
|
||||
curr_width: usize,
|
||||
literal: bool,
|
||||
}
|
||||
|
||||
impl<'a> TextPrinter<'a> {
|
||||
pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
|
||||
TextPrinter {
|
||||
text: Text::default(),
|
||||
width,
|
||||
base_style,
|
||||
hide_reply,
|
||||
|
||||
alignment: Alignment::Left,
|
||||
curr_spans: vec![],
|
||||
curr_width: 0,
|
||||
literal: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn align(mut self, alignment: Alignment) -> Self {
|
||||
self.alignment = alignment;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn literal(mut self, literal: bool) -> Self {
|
||||
self.literal = literal;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn hide_reply(&self) -> bool {
|
||||
self.hide_reply
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn sub(&self, indent: usize) -> Self {
|
||||
TextPrinter {
|
||||
text: Text::default(),
|
||||
width: self.width.saturating_sub(indent),
|
||||
base_style: self.base_style,
|
||||
hide_reply: self.hide_reply,
|
||||
|
||||
alignment: self.alignment,
|
||||
curr_spans: vec![],
|
||||
curr_width: 0,
|
||||
literal: self.literal,
|
||||
}
|
||||
}
|
||||
|
||||
fn remaining(&self) -> usize {
|
||||
self.width - self.curr_width
|
||||
}
|
||||
|
||||
pub fn commit(&mut self) {
|
||||
if self.curr_width > 0 {
|
||||
self.push_break();
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self) {
|
||||
self.curr_width = 0;
|
||||
self.text.lines.push(Spans(std::mem::take(&mut self.curr_spans)));
|
||||
}
|
||||
|
||||
pub fn push_break(&mut self) {
|
||||
if self.curr_width == 0 && self.text.lines.is_empty() {
|
||||
// Disallow leading breaks.
|
||||
return;
|
||||
}
|
||||
|
||||
let remaining = self.remaining();
|
||||
|
||||
if remaining > 0 {
|
||||
match self.alignment {
|
||||
Alignment::Left => {
|
||||
let tspan = space_span(remaining, self.base_style);
|
||||
self.curr_spans.push(tspan);
|
||||
},
|
||||
Alignment::Center => {
|
||||
let trailing = remaining / 2;
|
||||
let leading = remaining - trailing;
|
||||
|
||||
let tspan = space_span(trailing, self.base_style);
|
||||
let lspan = space_span(leading, self.base_style);
|
||||
|
||||
self.curr_spans.push(tspan);
|
||||
self.curr_spans.insert(0, lspan);
|
||||
},
|
||||
Alignment::Right => {
|
||||
let lspan = space_span(remaining, self.base_style);
|
||||
self.curr_spans.insert(0, lspan);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
self.push();
|
||||
}
|
||||
|
||||
fn push_str_wrapped<T>(&mut self, s: T, style: Style)
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
let style = self.base_style.patch(style);
|
||||
let mut cow = s.into();
|
||||
|
||||
loop {
|
||||
let sw = UnicodeWidthStr::width(cow.as_ref());
|
||||
|
||||
if self.curr_width + sw <= self.width {
|
||||
// The text fits within the current line.
|
||||
self.curr_spans.push(Span::styled(cow, style));
|
||||
self.curr_width += sw;
|
||||
break;
|
||||
}
|
||||
|
||||
// Take a leading portion of the text that fits in the line.
|
||||
let ((s0, w), s1) = take_width(cow, self.remaining());
|
||||
cow = s1;
|
||||
|
||||
self.curr_spans.push(Span::styled(s0, style));
|
||||
self.curr_width += w;
|
||||
|
||||
self.commit();
|
||||
}
|
||||
|
||||
if self.curr_width == self.width {
|
||||
// If the last bit fills the full line, start a new one.
|
||||
self.push();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if self.width == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
for mut word in UnicodeSegmentation::split_word_bounds(s) {
|
||||
if let "\n" | "\r\n" = word {
|
||||
if self.literal {
|
||||
self.commit();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Render embedded newlines as spaces.
|
||||
word = " ";
|
||||
}
|
||||
|
||||
if !self.literal && self.curr_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 !self.literal && 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);
|
||||
}
|
||||
|
||||
pub fn push_text(&mut self, text: Text<'a>) {
|
||||
self.commit();
|
||||
self.text.lines.extend(text.lines);
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> Text<'a> {
|
||||
self.commit();
|
||||
self.text
|
||||
}
|
||||
}
|
||||
172
src/tests.rs
172
src/tests.rs
@@ -1,29 +1,41 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use matrix_sdk::ruma::{
|
||||
event_id,
|
||||
events::room::message::RoomMessageEventContent,
|
||||
events::room::message::{OriginalRoomMessageEvent, RoomMessageEventContent},
|
||||
server_name,
|
||||
user_id,
|
||||
EventId,
|
||||
OwnedEventId,
|
||||
OwnedRoomId,
|
||||
OwnedUserId,
|
||||
RoomId,
|
||||
UInt,
|
||||
};
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::mpsc::sync_channel;
|
||||
use lazy_static::lazy_static;
|
||||
use modalkit::tui::style::{Color, Style};
|
||||
use tokio::sync::mpsc::unbounded_channel;
|
||||
use tracing::Level;
|
||||
use url::Url;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::{
|
||||
base::{ChatStore, ProgramStore, RoomFetchStatus, RoomInfo},
|
||||
config::{ApplicationSettings, DirectoryValues, ProfileConfig, TunableValues},
|
||||
base::{ChatStore, EventLocation, ProgramStore, RoomFetchStatus, RoomInfo},
|
||||
config::{
|
||||
user_color,
|
||||
user_style_from_color,
|
||||
ApplicationSettings,
|
||||
DirectoryValues,
|
||||
ProfileConfig,
|
||||
TunableValues,
|
||||
UserColor,
|
||||
UserDisplayStyle,
|
||||
UserDisplayTunables,
|
||||
},
|
||||
message::{
|
||||
Message,
|
||||
MessageContent,
|
||||
MessageEvent,
|
||||
MessageKey,
|
||||
MessageTimeStamp::{LocalEcho, OriginServer},
|
||||
Messages,
|
||||
@@ -31,61 +43,94 @@ use crate::{
|
||||
worker::Requester,
|
||||
};
|
||||
|
||||
const TEST_ROOM1_ALIAS: &str = "#room1:example.com";
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TEST_ROOM1_ID: OwnedRoomId = RoomId::new(server_name!("example.com")).to_owned();
|
||||
pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned();
|
||||
pub static ref TEST_USER2: OwnedUserId = user_id!("@user2:example.com").to_owned();
|
||||
pub static ref TEST_USER3: OwnedUserId = user_id!("@user2:example.com").to_owned();
|
||||
pub static ref TEST_USER4: OwnedUserId = user_id!("@user2:example.com").to_owned();
|
||||
pub static ref TEST_USER5: OwnedUserId = user_id!("@user2:example.com").to_owned();
|
||||
pub static ref MSG1_KEY: MessageKey = (LocalEcho, EventId::new(server_name!("example.com")));
|
||||
pub static ref MSG2_KEY: MessageKey =
|
||||
(OriginServer(UInt::new(1).unwrap()), EventId::new(server_name!("example.com")));
|
||||
pub static ref MSG3_KEY: MessageKey = (
|
||||
OriginServer(UInt::new(2).unwrap()),
|
||||
event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned()
|
||||
);
|
||||
pub static ref MSG4_KEY: MessageKey = (
|
||||
OriginServer(UInt::new(2).unwrap()),
|
||||
event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned()
|
||||
);
|
||||
pub static ref MSG5_KEY: MessageKey =
|
||||
(OriginServer(UInt::new(8).unwrap()), EventId::new(server_name!("example.com")));
|
||||
pub static ref TEST_USER3: OwnedUserId = user_id!("@user3:example.com").to_owned();
|
||||
pub static ref TEST_USER4: OwnedUserId = user_id!("@user4:example.com").to_owned();
|
||||
pub static ref TEST_USER5: OwnedUserId = user_id!("@user5:example.com").to_owned();
|
||||
pub static ref MSG1_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
|
||||
pub static ref MSG2_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
|
||||
pub static ref MSG3_EVID: OwnedEventId =
|
||||
event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned();
|
||||
pub static ref MSG4_EVID: OwnedEventId =
|
||||
event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned();
|
||||
pub static ref MSG5_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
|
||||
pub static ref MSG1_KEY: MessageKey = (LocalEcho, MSG1_EVID.clone());
|
||||
pub static ref MSG2_KEY: MessageKey = (OriginServer(UInt::new(1).unwrap()), MSG2_EVID.clone());
|
||||
pub static ref MSG3_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG3_EVID.clone());
|
||||
pub static ref MSG4_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG4_EVID.clone());
|
||||
pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
|
||||
}
|
||||
|
||||
pub fn user_style(user: &str) -> Style {
|
||||
user_style_from_color(user_color(user))
|
||||
}
|
||||
|
||||
pub fn mock_room1_message(
|
||||
content: RoomMessageEventContent,
|
||||
sender: OwnedUserId,
|
||||
key: MessageKey,
|
||||
) -> Message {
|
||||
let origin_server_ts = key.0.as_millis().unwrap();
|
||||
let event_id = key.1;
|
||||
|
||||
let event = OriginalRoomMessageEvent {
|
||||
content,
|
||||
event_id,
|
||||
sender,
|
||||
origin_server_ts,
|
||||
room_id: TEST_ROOM1_ID.clone(),
|
||||
unsigned: Default::default(),
|
||||
};
|
||||
|
||||
event.into()
|
||||
}
|
||||
|
||||
pub fn mock_message1() -> Message {
|
||||
let content = RoomMessageEventContent::text_plain("writhe");
|
||||
let content = MessageContent::Original(content.into());
|
||||
let content = MessageEvent::Local(MSG1_EVID.clone(), content.into());
|
||||
|
||||
Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
|
||||
}
|
||||
|
||||
pub fn mock_message2() -> Message {
|
||||
let content = RoomMessageEventContent::text_plain("helium");
|
||||
let content = MessageContent::Original(content.into());
|
||||
|
||||
Message::new(content, TEST_USER2.clone(), MSG2_KEY.0)
|
||||
mock_room1_message(content, TEST_USER2.clone(), MSG2_KEY.clone())
|
||||
}
|
||||
|
||||
pub fn mock_message3() -> Message {
|
||||
let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage");
|
||||
let content = MessageContent::Original(content.into());
|
||||
|
||||
Message::new(content, TEST_USER2.clone(), MSG3_KEY.0)
|
||||
mock_room1_message(content, TEST_USER2.clone(), MSG3_KEY.clone())
|
||||
}
|
||||
|
||||
pub fn mock_message4() -> Message {
|
||||
let content = RoomMessageEventContent::text_plain("help");
|
||||
let content = MessageContent::Original(content.into());
|
||||
|
||||
Message::new(content, TEST_USER1.clone(), MSG4_KEY.0)
|
||||
mock_room1_message(content, TEST_USER1.clone(), MSG4_KEY.clone())
|
||||
}
|
||||
|
||||
pub fn mock_message5() -> Message {
|
||||
let content = RoomMessageEventContent::text_plain("character");
|
||||
let content = MessageContent::Original(content.into());
|
||||
|
||||
Message::new(content, TEST_USER2.clone(), MSG5_KEY.0)
|
||||
mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
|
||||
}
|
||||
|
||||
pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> {
|
||||
let mut keys = HashMap::new();
|
||||
|
||||
keys.insert(MSG1_EVID.clone(), EventLocation::Message(MSG1_KEY.clone()));
|
||||
keys.insert(MSG2_EVID.clone(), EventLocation::Message(MSG2_KEY.clone()));
|
||||
keys.insert(MSG3_EVID.clone(), EventLocation::Message(MSG3_KEY.clone()));
|
||||
keys.insert(MSG4_EVID.clone(), EventLocation::Message(MSG4_KEY.clone()));
|
||||
keys.insert(MSG5_EVID.clone(), EventLocation::Message(MSG5_KEY.clone()));
|
||||
|
||||
keys
|
||||
}
|
||||
|
||||
pub fn mock_messages() -> Messages {
|
||||
@@ -103,10 +148,20 @@ pub fn mock_messages() -> Messages {
|
||||
pub fn mock_room() -> RoomInfo {
|
||||
RoomInfo {
|
||||
name: Some("Watercooler Discussion".into()),
|
||||
tags: None,
|
||||
|
||||
keys: mock_keys(),
|
||||
messages: mock_messages(),
|
||||
|
||||
receipts: HashMap::new(),
|
||||
read_till: None,
|
||||
reactions: HashMap::new(),
|
||||
|
||||
fetching: false,
|
||||
fetch_id: RoomFetchStatus::NotStarted,
|
||||
fetch_last: None,
|
||||
users_typing: None,
|
||||
display_names: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,35 +169,72 @@ pub fn mock_dirs() -> DirectoryValues {
|
||||
DirectoryValues {
|
||||
cache: PathBuf::new(),
|
||||
logs: PathBuf::new(),
|
||||
downloads: PathBuf::new(),
|
||||
downloads: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mock_tunables() -> TunableValues {
|
||||
TunableValues {
|
||||
default_room: None,
|
||||
log_level: Level::INFO,
|
||||
reaction_display: true,
|
||||
reaction_shortcode_display: false,
|
||||
read_receipt_send: true,
|
||||
read_receipt_display: true,
|
||||
request_timeout: 120,
|
||||
typing_notice_send: true,
|
||||
typing_notice_display: true,
|
||||
users: vec![(TEST_USER5.clone(), UserDisplayTunables {
|
||||
color: Some(UserColor(Color::Black)),
|
||||
name: Some("USER 5".into()),
|
||||
})]
|
||||
.into_iter()
|
||||
.collect::<HashMap<_, _>>(),
|
||||
open_command: None,
|
||||
username_display: UserDisplayStyle::Username,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mock_settings() -> ApplicationSettings {
|
||||
ApplicationSettings {
|
||||
matrix_dir: PathBuf::new(),
|
||||
layout_json: PathBuf::new(),
|
||||
session_json: PathBuf::new(),
|
||||
|
||||
profile_name: "test".into(),
|
||||
profile: ProfileConfig {
|
||||
user_id: user_id!("@user:example.com").to_owned(),
|
||||
url: Url::parse("https://example.com").unwrap(),
|
||||
settings: None,
|
||||
dirs: None,
|
||||
layout: None,
|
||||
},
|
||||
tunables: TunableValues { typing_notice: true, typing_notice_display: true },
|
||||
tunables: mock_tunables(),
|
||||
dirs: mock_dirs(),
|
||||
layout: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mock_store() -> ProgramStore {
|
||||
let (tx, _) = sync_channel(5);
|
||||
let worker = Requester { tx };
|
||||
pub async fn mock_store() -> ProgramStore {
|
||||
let (tx, _) = unbounded_channel();
|
||||
let homeserver = Url::parse("https://localhost").unwrap();
|
||||
let client = matrix_sdk::Client::new(homeserver).await.unwrap();
|
||||
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();
|
||||
|
||||
store.rooms.insert(room_id, info);
|
||||
store.rooms.insert(room_id.clone(), info);
|
||||
store.names.insert(TEST_ROOM1_ALIAS.to_string(), room_id);
|
||||
|
||||
ProgramStore::new(store)
|
||||
}
|
||||
|
||||
191
src/util.rs
Normal file
191
src/util.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use modalkit::tui::style::Style;
|
||||
use modalkit::tui::text::{Span, Spans, Text};
|
||||
|
||||
pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>) {
|
||||
match cow {
|
||||
Cow::Borrowed(s) => {
|
||||
let s1 = Cow::Borrowed(&s[idx..]);
|
||||
let s0 = Cow::Borrowed(&s[..idx]);
|
||||
|
||||
(s0, s1)
|
||||
},
|
||||
Cow::Owned(mut s) => {
|
||||
let s1 = Cow::Owned(s.split_off(idx));
|
||||
let s0 = Cow::Owned(s);
|
||||
|
||||
(s0, s1)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
|
||||
// Find where to split the line.
|
||||
let mut idx = 0;
|
||||
let mut w = 0;
|
||||
|
||||
for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) {
|
||||
let gw = UnicodeWidthStr::width(g);
|
||||
idx = i;
|
||||
|
||||
if w + gw > width {
|
||||
break;
|
||||
}
|
||||
|
||||
w += gw;
|
||||
}
|
||||
|
||||
let (s0, s1) = split_cow(s, idx);
|
||||
|
||||
((s0, w), s1)
|
||||
}
|
||||
|
||||
pub struct WrappedLinesIterator<'a> {
|
||||
iter: std::vec::IntoIter<Cow<'a, str>>,
|
||||
curr: Option<Cow<'a, str>>,
|
||||
width: usize,
|
||||
}
|
||||
|
||||
impl<'a> WrappedLinesIterator<'a> {
|
||||
fn new<T>(input: T, width: usize) -> Self
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
let width = width.max(2);
|
||||
|
||||
let cows: Vec<Cow<'a, str>> = match input.into() {
|
||||
Cow::Borrowed(s) => s.lines().map(Cow::Borrowed).collect(),
|
||||
Cow::Owned(s) => s.lines().map(ToOwned::to_owned).map(Cow::Owned).collect(),
|
||||
};
|
||||
|
||||
WrappedLinesIterator { iter: cows.into_iter(), curr: None, width }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for WrappedLinesIterator<'a> {
|
||||
type Item = (Cow<'a, str>, usize);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.curr.is_none() {
|
||||
self.curr = self.iter.next();
|
||||
}
|
||||
|
||||
if let Some(s) = self.curr.take() {
|
||||
let width = UnicodeWidthStr::width(s.as_ref());
|
||||
|
||||
if width <= self.width {
|
||||
return Some((s, width));
|
||||
} else {
|
||||
let (prefix, s1) = take_width(s, self.width);
|
||||
self.curr = Some(s1);
|
||||
return Some(prefix);
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wrap<'a, T>(input: T, width: usize) -> WrappedLinesIterator<'a>
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
WrappedLinesIterator::new(input, width)
|
||||
}
|
||||
|
||||
pub fn wrapped_text<'a, T>(s: T, width: usize, style: Style) -> Text<'a>
|
||||
where
|
||||
T: Into<Cow<'a, str>>,
|
||||
{
|
||||
let mut text = Text::default();
|
||||
|
||||
for (line, w) in wrap(s, width) {
|
||||
let space = space_span(width.saturating_sub(w), style);
|
||||
let spans = Spans(vec![Span::styled(line, style), space]);
|
||||
|
||||
text.lines.push(spans);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
pub fn space(width: usize) -> String {
|
||||
" ".repeat(width)
|
||||
}
|
||||
|
||||
pub fn space_span(width: usize, style: Style) -> Span<'static> {
|
||||
Span::styled(space(width), style)
|
||||
}
|
||||
|
||||
pub fn space_text(width: usize, style: Style) -> Text<'static> {
|
||||
space_span(width, style).into()
|
||||
}
|
||||
|
||||
pub fn join_cell_text<'a>(texts: Vec<(Text<'a>, usize)>, join: Span<'a>, style: Style) -> Text<'a> {
|
||||
let height = texts.iter().map(|t| t.0.height()).max().unwrap_or(0);
|
||||
let mut text = Text { lines: vec![Spans(vec![join.clone()]); height] };
|
||||
|
||||
for (mut t, w) in texts.into_iter() {
|
||||
for i in 0..height {
|
||||
if let Some(spans) = t.lines.get_mut(i) {
|
||||
text.lines[i].0.append(&mut spans.0);
|
||||
} else {
|
||||
text.lines[i].0.push(space_span(w, style));
|
||||
}
|
||||
|
||||
text.lines[i].0.push(join.clone());
|
||||
}
|
||||
}
|
||||
|
||||
text
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_wrapped_lines_ascii() {
|
||||
let s = "hello world!\nabcdefghijklmnopqrstuvwxyz\ngoodbye";
|
||||
|
||||
let mut iter = wrap(s, 100);
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("hello world!"), 12)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("abcdefghijklmnopqrstuvwxyz"), 26)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("goodbye"), 7)));
|
||||
assert_eq!(iter.next(), None);
|
||||
|
||||
let mut iter = wrap(s, 5);
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("hello"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed(" worl"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("d!"), 2)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("abcde"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("fghij"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("klmno"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("pqrst"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("uvwxy"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("z"), 1)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("goodb"), 5)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("ye"), 2)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrapped_lines_unicode() {
|
||||
let s = "CHICKEN";
|
||||
|
||||
let mut iter = wrap(s, 14);
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed(s), 14)));
|
||||
assert_eq!(iter.next(), None);
|
||||
|
||||
let mut iter = wrap(s, 5);
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("CH"), 4)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("IC"), 4)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("KE"), 4)));
|
||||
assert_eq!(iter.next(), Some((Cow::Borrowed("N"), 2)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use matrix_sdk::{
|
||||
encryption::verification::{format_emojis, SasVerification},
|
||||
room::{Room as MatrixRoom, RoomMember},
|
||||
ruma::{events::room::member::MembershipState, OwnedRoomId, RoomId},
|
||||
DisplayName,
|
||||
ruma::{
|
||||
events::room::member::MembershipState,
|
||||
events::tag::{TagName, Tags},
|
||||
OwnedRoomId,
|
||||
RoomId,
|
||||
},
|
||||
};
|
||||
|
||||
use modalkit::tui::{
|
||||
@@ -40,7 +46,9 @@ use modalkit::{
|
||||
ScrollStyle,
|
||||
ViewportContext,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
completion::CompletionList,
|
||||
},
|
||||
widgets::{
|
||||
list::{List, ListCursor, ListItem, ListState},
|
||||
@@ -51,19 +59,19 @@ use modalkit::{
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
base::{
|
||||
use crate::base::{
|
||||
ChatStore,
|
||||
IambBufferId,
|
||||
IambError,
|
||||
IambId,
|
||||
IambInfo,
|
||||
IambResult,
|
||||
MessageAction,
|
||||
ProgramAction,
|
||||
ProgramContext,
|
||||
ProgramStore,
|
||||
RoomAction,
|
||||
},
|
||||
message::user_style,
|
||||
SendAction,
|
||||
};
|
||||
|
||||
use self::{room::RoomState, welcome::WelcomeState};
|
||||
@@ -71,6 +79,10 @@ use self::{room::RoomState, welcome::WelcomeState};
|
||||
pub mod room;
|
||||
pub mod welcome;
|
||||
|
||||
type MatrixRoomInfo = Arc<(MatrixRoom, Option<Tags>)>;
|
||||
|
||||
const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);
|
||||
|
||||
#[inline]
|
||||
fn bold_style() -> Style {
|
||||
Style::default().add_modifier(StyleModifier::BOLD)
|
||||
@@ -105,6 +117,71 @@ fn selected_text(s: &str, selected: bool) -> Text {
|
||||
Text::from(selected_span(s, selected))
|
||||
}
|
||||
|
||||
fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering {
|
||||
let ca1 = a.canonical_alias();
|
||||
let ca2 = b.canonical_alias();
|
||||
|
||||
let ord = match (ca1, ca2) {
|
||||
(None, None) => Ordering::Equal,
|
||||
(None, Some(_)) => Ordering::Greater,
|
||||
(Some(_), None) => Ordering::Less,
|
||||
(Some(ca1), Some(ca2)) => ca1.cmp(&ca2),
|
||||
};
|
||||
|
||||
ord.then_with(|| a.room_id().cmp(b.room_id()))
|
||||
}
|
||||
|
||||
fn tag_cmp(a: &Option<Tags>, b: &Option<Tags>) -> Ordering {
|
||||
let (fava, lowa) = a
|
||||
.as_ref()
|
||||
.map(|tags| {
|
||||
(tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
|
||||
})
|
||||
.unwrap_or((false, false));
|
||||
|
||||
let (favb, lowb) = b
|
||||
.as_ref()
|
||||
.map(|tags| {
|
||||
(tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
|
||||
})
|
||||
.unwrap_or((false, false));
|
||||
|
||||
// If a has Favorite and b doesn't, it should sort earlier in room list.
|
||||
let cmpf = favb.cmp(&fava);
|
||||
|
||||
// If a has LowPriority and b doesn't, it should sort later in room list.
|
||||
let cmpl = lowa.cmp(&lowb);
|
||||
|
||||
cmpl.then(cmpf)
|
||||
}
|
||||
|
||||
fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) {
|
||||
if tags.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
spans.push(Span::styled(" (", style));
|
||||
|
||||
for (i, tag) in tags.keys().enumerate() {
|
||||
if i > 0 {
|
||||
spans.push(Span::styled(", ", style));
|
||||
}
|
||||
|
||||
match tag {
|
||||
TagName::Favorite => spans.push(Span::styled("Favorite", style)),
|
||||
TagName::LowPriority => spans.push(Span::styled("Low Priority", style)),
|
||||
TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)),
|
||||
TagName::User(tag) => {
|
||||
spans.push(Span::styled("User Tag: ", style));
|
||||
spans.push(Span::styled(tag.as_ref(), style));
|
||||
},
|
||||
tag => spans.push(Span::styled(format!("{tag:?}"), style)),
|
||||
}
|
||||
}
|
||||
|
||||
spans.push(Span::styled(")", style));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room_prompt(
|
||||
room_id: &RoomId,
|
||||
@@ -125,7 +202,7 @@ fn room_prompt(
|
||||
|
||||
Err(err)
|
||||
},
|
||||
PromptAction::Recall(_, _) => {
|
||||
PromptAction::Recall(..) => {
|
||||
let msg = "Cannot recall history inside a list";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
@@ -140,7 +217,7 @@ macro_rules! delegate {
|
||||
match $s {
|
||||
IambWindow::Room($id) => $e,
|
||||
IambWindow::DirectList($id) => $e,
|
||||
IambWindow::MemberList($id, _) => $e,
|
||||
IambWindow::MemberList($id, _, _) => $e,
|
||||
IambWindow::RoomList($id) => $e,
|
||||
IambWindow::SpaceList($id) => $e,
|
||||
IambWindow::VerifyList($id) => $e,
|
||||
@@ -151,7 +228,7 @@ macro_rules! delegate {
|
||||
|
||||
pub enum IambWindow {
|
||||
DirectList(DirectListState),
|
||||
MemberList(MemberListState, OwnedRoomId),
|
||||
MemberList(MemberListState, OwnedRoomId, Option<Instant>),
|
||||
Room(RoomState),
|
||||
VerifyList(VerifyListState),
|
||||
RoomList(RoomListState),
|
||||
@@ -168,19 +245,42 @@ impl IambWindow {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn room_command(
|
||||
pub async fn message_command(
|
||||
&mut self,
|
||||
act: MessageAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
if let IambWindow::Room(w) = self {
|
||||
w.message_command(act, ctx, store).await
|
||||
} else {
|
||||
return Err(IambError::NoSelectedRoom.into());
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn room_command(
|
||||
&mut self,
|
||||
act: RoomAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
|
||||
if let IambWindow::Room(w) = self {
|
||||
w.room_command(act, ctx, store)
|
||||
w.room_command(act, ctx, store).await
|
||||
} else {
|
||||
let msg = "No room currently focused!";
|
||||
let err = UIError::Failure(msg.into());
|
||||
return Err(IambError::NoSelectedRoomOrSpace.into());
|
||||
}
|
||||
}
|
||||
|
||||
return Err(err);
|
||||
pub async fn send_command(
|
||||
&mut self,
|
||||
act: SendAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
if let IambWindow::Room(w) = self {
|
||||
w.send_command(act, ctx, store).await
|
||||
} else {
|
||||
return Err(IambError::NoSelectedRoom.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -283,9 +383,17 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
match self {
|
||||
IambWindow::Room(state) => state.draw(area, buf, focused, store),
|
||||
IambWindow::DirectList(state) => {
|
||||
let dms = store.application.worker.direct_messages();
|
||||
let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store));
|
||||
state.set(items.collect());
|
||||
let mut items = store
|
||||
.application
|
||||
.sync_info
|
||||
.dms
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|room_info| DirectItem::new(room_info, store))
|
||||
.collect::<Vec<_>>();
|
||||
items.sort();
|
||||
|
||||
state.set(items);
|
||||
|
||||
List::new(store)
|
||||
.empty_message("No direct messages yet!")
|
||||
@@ -293,10 +401,18 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
.focus(focused)
|
||||
.render(area, buf, state);
|
||||
},
|
||||
IambWindow::MemberList(state, room_id) => {
|
||||
IambWindow::MemberList(state, room_id, last_fetch) => {
|
||||
let need_fetch = match last_fetch {
|
||||
Some(i) => i.elapsed() >= MEMBER_FETCH_DEBOUNCE,
|
||||
None => true,
|
||||
};
|
||||
|
||||
if need_fetch {
|
||||
if let Ok(mems) = store.application.worker.members(room_id.clone()) {
|
||||
let items = mems.into_iter().map(MemberItem::new);
|
||||
let items = mems.into_iter().map(|m| MemberItem::new(m, room_id.clone()));
|
||||
state.set(items.collect());
|
||||
*last_fetch = Some(Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
List::new(store)
|
||||
@@ -306,9 +422,17 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
.render(area, buf, state);
|
||||
},
|
||||
IambWindow::RoomList(state) => {
|
||||
let joined = store.application.worker.joined_rooms();
|
||||
let items = joined.into_iter().map(|(id, name)| RoomItem::new(id, name, store));
|
||||
state.set(items.collect());
|
||||
let mut items = store
|
||||
.application
|
||||
.sync_info
|
||||
.rooms
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|room_info| RoomItem::new(room_info, store))
|
||||
.collect::<Vec<_>>();
|
||||
items.sort();
|
||||
|
||||
state.set(items);
|
||||
|
||||
List::new(store)
|
||||
.empty_message("You haven't joined any rooms yet")
|
||||
@@ -317,9 +441,13 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
.render(area, buf, state);
|
||||
},
|
||||
IambWindow::SpaceList(state) => {
|
||||
let spaces = store.application.worker.spaces();
|
||||
let items =
|
||||
spaces.into_iter().map(|(room, name)| SpaceItem::new(room, name, store));
|
||||
let items = store
|
||||
.application
|
||||
.sync_info
|
||||
.spaces
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|room| SpaceItem::new(room, store));
|
||||
state.set(items.collect());
|
||||
state.draw(area, buf, focused, store);
|
||||
|
||||
@@ -352,8 +480,8 @@ impl WindowOps<IambInfo> for IambWindow {
|
||||
match self {
|
||||
IambWindow::Room(w) => w.dup(store).into(),
|
||||
IambWindow::DirectList(w) => w.dup(store).into(),
|
||||
IambWindow::MemberList(w, room_id) => {
|
||||
IambWindow::MemberList(w.dup(store), room_id.clone())
|
||||
IambWindow::MemberList(w, room_id, last_fetch) => {
|
||||
IambWindow::MemberList(w.dup(store), room_id.clone(), *last_fetch)
|
||||
},
|
||||
IambWindow::RoomList(w) => w.dup(store).into(),
|
||||
IambWindow::SpaceList(w) => w.dup(store).into(),
|
||||
@@ -366,6 +494,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))
|
||||
}
|
||||
@@ -380,7 +521,7 @@ impl Window<IambInfo> for IambWindow {
|
||||
match self {
|
||||
IambWindow::Room(room) => IambId::Room(room.id().to_owned()),
|
||||
IambWindow::DirectList(_) => IambId::DirectList,
|
||||
IambWindow::MemberList(_, room_id) => IambId::MemberList(room_id.clone()),
|
||||
IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()),
|
||||
IambWindow::RoomList(_) => IambId::RoomList,
|
||||
IambWindow::SpaceList(_) => IambId::SpaceList,
|
||||
IambWindow::VerifyList(_) => IambId::VerifyList,
|
||||
@@ -401,10 +542,15 @@ impl Window<IambInfo> for IambWindow {
|
||||
|
||||
Spans::from(title)
|
||||
},
|
||||
IambWindow::MemberList(_, room_id) => {
|
||||
IambWindow::MemberList(state, room_id, _) => {
|
||||
let title = store.application.get_room_title(room_id.as_ref());
|
||||
|
||||
Spans(vec![bold_span("Room Members: "), title.into()])
|
||||
let n = state.len();
|
||||
let v = vec![
|
||||
bold_span("Room Members "),
|
||||
Span::styled(format!("({n}): "), bold_style()),
|
||||
title.into(),
|
||||
];
|
||||
Spans(v)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -418,10 +564,15 @@ impl Window<IambInfo> for IambWindow {
|
||||
IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
|
||||
|
||||
IambWindow::Room(w) => w.get_title(store),
|
||||
IambWindow::MemberList(_, room_id) => {
|
||||
IambWindow::MemberList(state, room_id, _) => {
|
||||
let title = store.application.get_room_title(room_id.as_ref());
|
||||
|
||||
Spans(vec![bold_span("Room Members: "), title.into()])
|
||||
let n = state.len();
|
||||
let v = vec![
|
||||
bold_span("Room Members "),
|
||||
Span::styled(format!("({n}): "), bold_style()),
|
||||
title.into(),
|
||||
];
|
||||
Spans(v)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -429,8 +580,8 @@ impl Window<IambInfo> for IambWindow {
|
||||
fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> {
|
||||
match id {
|
||||
IambId::Room(room_id) => {
|
||||
let (room, name) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, store);
|
||||
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, tags, store);
|
||||
|
||||
return Ok(room.into());
|
||||
},
|
||||
@@ -442,7 +593,7 @@ impl Window<IambInfo> for IambWindow {
|
||||
IambId::MemberList(room_id) => {
|
||||
let id = IambBufferId::MemberList(room_id.clone());
|
||||
let list = MemberListState::new(id, vec![]);
|
||||
let win = IambWindow::MemberList(list, room_id);
|
||||
let win = IambWindow::MemberList(list, room_id, None);
|
||||
|
||||
return Ok(win);
|
||||
},
|
||||
@@ -472,26 +623,23 @@ 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());
|
||||
|
||||
let (room, name) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, store);
|
||||
|
||||
Ok(room.into())
|
||||
},
|
||||
Entry::Occupied(o) => {
|
||||
let id = IambId::Room(o.get().clone());
|
||||
if let Some(room) = names.get_mut(&name) {
|
||||
let id = IambId::Room(room.clone());
|
||||
|
||||
IambWindow::open(id, store)
|
||||
},
|
||||
} else {
|
||||
let room_id = worker.join_room(name.clone())?;
|
||||
names.insert(name, room_id.clone());
|
||||
|
||||
let (room, name, tags) = store.application.worker.get_room(room_id)?;
|
||||
let room = RoomState::new(room, name, tags, store);
|
||||
|
||||
Ok(room.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn posn(index: usize, _: &mut ProgramStore) -> IambResult<Self> {
|
||||
let msg = format!("Cannot find indexed buffer (index = {})", index);
|
||||
let msg = format!("Cannot find indexed buffer (index = {index})");
|
||||
let err = UIError::Unimplemented(msg);
|
||||
|
||||
Err(err)
|
||||
@@ -504,17 +652,59 @@ impl Window<IambInfo> for IambWindow {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RoomItem {
|
||||
room: MatrixRoom,
|
||||
room_info: MatrixRoomInfo,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl RoomItem {
|
||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||
let name = name.to_string();
|
||||
fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
|
||||
let room = &room_info.deref().0;
|
||||
let room_id = room.room_id();
|
||||
|
||||
store.application.set_room_name(room.room_id(), name.as_str());
|
||||
let info = store.application.get_room_info(room_id.to_owned());
|
||||
let name = info.name.clone().unwrap_or_default();
|
||||
info.tags = room_info.deref().1.clone();
|
||||
|
||||
RoomItem { room, name }
|
||||
if let Some(alias) = room.canonical_alias() {
|
||||
store.application.names.insert(alias.to_string(), room_id.to_owned());
|
||||
}
|
||||
|
||||
RoomItem { room_info, name }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room(&self) -> &MatrixRoom {
|
||||
&self.room_info.deref().0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room_id(&self) -> &RoomId {
|
||||
self.room().room_id()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn tags(&self) -> &Option<Tags> {
|
||||
&self.room_info.deref().1
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for RoomItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.room_id() == other.room_id()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for RoomItem {}
|
||||
|
||||
impl Ord for RoomItem {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room()))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for RoomItem {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,11 +716,20 @@ impl ToString for RoomItem {
|
||||
|
||||
impl ListItem<IambInfo> for RoomItem {
|
||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||
if let Some(tags) = &self.tags() {
|
||||
let style = selected_style(selected);
|
||||
let mut spans = vec![Span::styled(self.name.as_str(), style)];
|
||||
|
||||
append_tags(tags, &mut spans, style);
|
||||
|
||||
Text::from(Spans(spans))
|
||||
} else {
|
||||
selected_text(self.name.as_str(), selected)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_word(&self) -> Option<String> {
|
||||
self.room.room_id().to_string().into()
|
||||
self.room_id().to_string().into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,23 +740,37 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
room_prompt(self.room.room_id(), act, ctx)
|
||||
room_prompt(self.room_id(), act, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DirectItem {
|
||||
room: MatrixRoom,
|
||||
room_info: MatrixRoomInfo,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl DirectItem {
|
||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||
let name = name.to_string();
|
||||
fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
|
||||
let room_id = room_info.deref().0.room_id().to_owned();
|
||||
let name = store.application.get_room_info(room_id).name.clone().unwrap_or_default();
|
||||
|
||||
store.application.set_room_name(room.room_id(), name.as_str());
|
||||
DirectItem { room_info, name }
|
||||
}
|
||||
|
||||
DirectItem { room, name }
|
||||
#[inline]
|
||||
fn room(&self) -> &MatrixRoom {
|
||||
&self.room_info.deref().0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn room_id(&self) -> &RoomId {
|
||||
self.room().room_id()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn tags(&self) -> &Option<Tags> {
|
||||
&self.room_info.deref().1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -569,11 +782,40 @@ impl ToString for DirectItem {
|
||||
|
||||
impl ListItem<IambInfo> for DirectItem {
|
||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||
if let Some(tags) = &self.tags() {
|
||||
let style = selected_style(selected);
|
||||
let mut spans = vec![Span::styled(self.name.as_str(), style)];
|
||||
|
||||
append_tags(tags, &mut spans, style);
|
||||
|
||||
Text::from(Spans(spans))
|
||||
} else {
|
||||
selected_text(self.name.as_str(), selected)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_word(&self) -> Option<String> {
|
||||
self.room.room_id().to_string().into()
|
||||
self.room_id().to_string().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for DirectItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.room_id() == other.room_id()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for DirectItem {}
|
||||
|
||||
impl Ord for DirectItem {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room()))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for DirectItem {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -584,7 +826,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
room_prompt(self.room.room_id(), act, ctx)
|
||||
room_prompt(self.room_id(), act, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -595,15 +837,43 @@ pub struct SpaceItem {
|
||||
}
|
||||
|
||||
impl SpaceItem {
|
||||
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||
let name = name.to_string();
|
||||
fn new(room: MatrixRoom, store: &mut ProgramStore) -> Self {
|
||||
let room_id = room.room_id();
|
||||
let name = store
|
||||
.application
|
||||
.get_room_info(room_id.to_owned())
|
||||
.name
|
||||
.clone()
|
||||
.unwrap_or_default();
|
||||
|
||||
store.application.set_room_name(room.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 }
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for SpaceItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.room.room_id() == other.room.room_id()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for SpaceItem {}
|
||||
|
||||
impl Ord for SpaceItem {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
room_cmp(&self.room, &other.room)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for SpaceItem {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.cmp(other).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for SpaceItem {
|
||||
fn to_string(&self) -> String {
|
||||
return self.room.room_id().to_string();
|
||||
@@ -657,7 +927,7 @@ impl VerifyItem {
|
||||
let device = self.sasv1.other_device();
|
||||
|
||||
if let Some(display_name) = device.display_name() {
|
||||
format!("Device verification with {} ({})", display_name, state)
|
||||
format!("Device verification with {display_name} ({state})")
|
||||
} else {
|
||||
format!("Device verification with device {} ({})", device.device_id(), state)
|
||||
}
|
||||
@@ -763,7 +1033,7 @@ impl ListItem<IambInfo> for VerifyItem {
|
||||
lines.push(Spans::from(""));
|
||||
|
||||
for line in format_emojis(emoji).lines() {
|
||||
lines.push(Spans::from(format!(" {}", line)));
|
||||
lines.push(Spans::from(format!(" {line}")));
|
||||
}
|
||||
|
||||
lines.push(Spans::from(""));
|
||||
@@ -816,7 +1086,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for VerifyItem {
|
||||
|
||||
Err(err)
|
||||
},
|
||||
PromptAction::Recall(_, _) => {
|
||||
PromptAction::Recall(..) => {
|
||||
let msg = "Cannot recall history inside a list";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
@@ -830,11 +1100,12 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for VerifyItem {
|
||||
#[derive(Clone)]
|
||||
pub struct MemberItem {
|
||||
member: RoomMember,
|
||||
room_id: OwnedRoomId,
|
||||
}
|
||||
|
||||
impl MemberItem {
|
||||
fn new(member: RoomMember) -> Self {
|
||||
Self { member }
|
||||
fn new(member: RoomMember, room_id: OwnedRoomId) -> Self {
|
||||
Self { member, room_id }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -845,14 +1116,37 @@ impl ToString for MemberItem {
|
||||
}
|
||||
|
||||
impl ListItem<IambInfo> for MemberItem {
|
||||
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
|
||||
let mut style = user_style(self.member.user_id().as_str());
|
||||
fn show(
|
||||
&self,
|
||||
selected: bool,
|
||||
_: &ViewportContext<ListCursor>,
|
||||
store: &mut ProgramStore,
|
||||
) -> Text {
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let user_id = self.member.user_id();
|
||||
|
||||
let (color, name) = store.application.settings.get_user_overrides(self.member.user_id());
|
||||
let color = color.unwrap_or_else(|| super::config::user_color(user_id.as_str()));
|
||||
let mut style = super::config::user_style_from_color(color);
|
||||
|
||||
if selected {
|
||||
style = style.add_modifier(StyleModifier::REVERSED);
|
||||
}
|
||||
|
||||
let user = Span::styled(self.to_string(), style);
|
||||
let mut spans = vec![];
|
||||
let mut parens = false;
|
||||
|
||||
if let Some(name) = name {
|
||||
spans.push(Span::styled(name, style));
|
||||
parens = true;
|
||||
} else if let Some(display) = info.display_names.get(user_id) {
|
||||
spans.push(Span::styled(display.clone(), style));
|
||||
parens = true;
|
||||
}
|
||||
|
||||
spans.extend(parens.then_some(Span::styled(" (", style)));
|
||||
spans.push(Span::styled(user_id.as_str(), style));
|
||||
spans.extend(parens.then_some(Span::styled(")", style)));
|
||||
|
||||
let state = match self.member.membership() {
|
||||
MembershipState::Ban => Span::raw(" (banned)").into(),
|
||||
@@ -863,11 +1157,9 @@ impl ListItem<IambInfo> for MemberItem {
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(state) = state {
|
||||
Spans(vec![user, state]).into()
|
||||
} else {
|
||||
user.into()
|
||||
}
|
||||
spans.extend(state);
|
||||
|
||||
return Spans(spans).into();
|
||||
}
|
||||
|
||||
fn get_word(&self) -> Option<String> {
|
||||
@@ -890,7 +1182,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MemberItem {
|
||||
|
||||
Err(err)
|
||||
},
|
||||
PromptAction::Recall(_, _) => {
|
||||
PromptAction::Recall(..) => {
|
||||
let msg = "Cannot recall history inside a list";
|
||||
let err = EditError::Failure(msg.into());
|
||||
|
||||
|
||||
@@ -1,11 +1,41 @@
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs;
|
||||
use std::ops::Deref;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use modalkit::editing::store::RegisterError;
|
||||
use std::process::Command;
|
||||
use tokio;
|
||||
|
||||
use matrix_sdk::{
|
||||
room::Room as MatrixRoom,
|
||||
ruma::{OwnedRoomId, RoomId},
|
||||
attachment::AttachmentConfig,
|
||||
media::{MediaFormat, MediaRequest},
|
||||
room::{Joined, Room as MatrixRoom},
|
||||
ruma::{
|
||||
events::reaction::{ReactionEventContent, Relation as Reaction},
|
||||
events::room::message::{
|
||||
MessageType,
|
||||
OriginalRoomMessageEvent,
|
||||
Relation,
|
||||
Replacement,
|
||||
RoomMessageEventContent,
|
||||
TextMessageEventContent,
|
||||
},
|
||||
EventId,
|
||||
OwnedRoomId,
|
||||
RoomId,
|
||||
},
|
||||
};
|
||||
|
||||
use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
|
||||
|
||||
use modalkit::{
|
||||
input::dialog::PromptYesNo,
|
||||
tui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
text::{Span, Spans},
|
||||
widgets::{Paragraph, StatefulWidget, Widget},
|
||||
},
|
||||
widgets::textbox::{TextBox, TextBoxState},
|
||||
widgets::TerminalCursor,
|
||||
widgets::{PromptActions, WindowOps},
|
||||
@@ -13,33 +43,45 @@ use modalkit::{
|
||||
|
||||
use modalkit::editing::{
|
||||
action::{
|
||||
Action,
|
||||
EditError,
|
||||
EditInfo,
|
||||
EditResult,
|
||||
Editable,
|
||||
EditorAction,
|
||||
InfoMessage,
|
||||
Jumpable,
|
||||
PromptAction,
|
||||
Promptable,
|
||||
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,
|
||||
};
|
||||
|
||||
use crate::base::{
|
||||
DownloadFlags,
|
||||
IambAction,
|
||||
IambBufferId,
|
||||
IambError,
|
||||
IambInfo,
|
||||
IambResult,
|
||||
MessageAction,
|
||||
ProgramAction,
|
||||
ProgramContext,
|
||||
ProgramStore,
|
||||
RoomFocus,
|
||||
RoomInfo,
|
||||
SendAction,
|
||||
};
|
||||
|
||||
use crate::message::{text_to_message, Message, MessageEvent, MessageKey, MessageTimeStamp};
|
||||
use crate::worker::Requester;
|
||||
|
||||
use super::scrollback::{Scrollback, ScrollbackState};
|
||||
|
||||
pub struct ChatState {
|
||||
@@ -52,6 +94,9 @@ pub struct ChatState {
|
||||
|
||||
scrollback: ScrollbackState,
|
||||
focus: RoomFocus,
|
||||
|
||||
reply_to: Option<MessageKey>,
|
||||
editing: Option<MessageKey>,
|
||||
}
|
||||
|
||||
impl ChatState {
|
||||
@@ -72,9 +117,418 @@ impl ChatState {
|
||||
|
||||
scrollback,
|
||||
focus: RoomFocus::MessageBar,
|
||||
|
||||
reply_to: None,
|
||||
editing: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_joined(&self, worker: &Requester) -> Result<Joined, IambError> {
|
||||
worker.client.get_joined_room(self.id()).ok_or(IambError::NotJoined)
|
||||
}
|
||||
|
||||
fn get_reply_to<'a>(&self, info: &'a RoomInfo) -> Option<&'a OriginalRoomMessageEvent> {
|
||||
let key = self.reply_to.as_ref()?;
|
||||
let msg = info.messages.get(key)?;
|
||||
|
||||
if let MessageEvent::Original(ev) = &msg.event {
|
||||
Some(ev)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> EditRope {
|
||||
self.reply_to = None;
|
||||
self.editing = None;
|
||||
self.tbox.reset()
|
||||
}
|
||||
|
||||
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
|
||||
if let Some(room) = store.application.worker.client.get_room(self.id()) {
|
||||
self.room = room;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn message_command(
|
||||
&mut self,
|
||||
act: MessageAction,
|
||||
_: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
let client = &store.application.worker.client;
|
||||
|
||||
let settings = &store.application.settings;
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
|
||||
let msg = self
|
||||
.scrollback
|
||||
.get_mut(&mut info.messages)
|
||||
.ok_or(IambError::NoSelectedMessage)?;
|
||||
|
||||
match act {
|
||||
MessageAction::Cancel(skip_confirm) => {
|
||||
self.reply_to = None;
|
||||
self.editing = None;
|
||||
|
||||
if skip_confirm {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let msg = "Would you like to clear the message bar?";
|
||||
let act = PromptAction::Abort(false);
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(UIError::NeedConfirm(prompt))
|
||||
},
|
||||
MessageAction::Download(filename, flags) => {
|
||||
if let MessageEvent::Original(ev) = &msg.event {
|
||||
let media = client.media();
|
||||
|
||||
let mut filename = match (filename, &settings.dirs.downloads) {
|
||||
(Some(f), _) => PathBuf::from(f),
|
||||
(None, Some(downloads)) => downloads.clone(),
|
||||
(None, None) => return Err(IambError::NoDownloadDir.into()),
|
||||
};
|
||||
|
||||
let (source, msg_filename) = match &ev.content.msgtype {
|
||||
MessageType::Audio(c) => (c.source.clone(), c.body.as_str()),
|
||||
MessageType::File(c) => {
|
||||
(c.source.clone(), c.filename.as_deref().unwrap_or(c.body.as_str()))
|
||||
},
|
||||
MessageType::Image(c) => (c.source.clone(), c.body.as_str()),
|
||||
MessageType::Video(c) => (c.source.clone(), c.body.as_str()),
|
||||
_ => {
|
||||
return Err(IambError::NoAttachment.into());
|
||||
},
|
||||
};
|
||||
|
||||
if filename.is_dir() {
|
||||
filename.push(msg_filename);
|
||||
}
|
||||
|
||||
if filename.exists() && !flags.contains(DownloadFlags::FORCE) {
|
||||
// Find an incrementally suffixed filename, e.g. image-2.jpg -> image-3.jpg
|
||||
if let Some(stem) = filename.file_stem().and_then(OsStr::to_str) {
|
||||
let ext = filename.extension();
|
||||
let mut filename_incr = filename.clone();
|
||||
for n in 1..=1000 {
|
||||
if let Some(ext) = ext.and_then(OsStr::to_str) {
|
||||
filename_incr.set_file_name(format!("{}-{}.{}", stem, n, ext));
|
||||
} else {
|
||||
filename_incr.set_file_name(format!("{}-{}", stem, n));
|
||||
}
|
||||
|
||||
if !filename_incr.exists() {
|
||||
filename = filename_incr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !filename.exists() || flags.contains(DownloadFlags::FORCE) {
|
||||
let req = MediaRequest { source, format: MediaFormat::File };
|
||||
|
||||
let bytes =
|
||||
media.get_media_content(&req, true).await.map_err(IambError::from)?;
|
||||
|
||||
fs::write(filename.as_path(), bytes.as_slice())?;
|
||||
|
||||
msg.downloaded = true;
|
||||
} else if !flags.contains(DownloadFlags::OPEN) {
|
||||
let msg = format!(
|
||||
"The file {} already exists; add ! to end of command to overwrite it.",
|
||||
filename.display()
|
||||
);
|
||||
let err = UIError::Failure(msg);
|
||||
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let info = if flags.contains(DownloadFlags::OPEN) {
|
||||
let target = filename.clone().into_os_string();
|
||||
match open_command(
|
||||
store.application.settings.tunables.open_command.as_ref(),
|
||||
target,
|
||||
) {
|
||||
Ok(_) => {
|
||||
InfoMessage::from(format!(
|
||||
"Attachment downloaded to {} and opened",
|
||||
filename.display()
|
||||
))
|
||||
},
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
},
|
||||
}
|
||||
} else {
|
||||
InfoMessage::from(format!(
|
||||
"Attachment downloaded to {}",
|
||||
filename.display()
|
||||
))
|
||||
};
|
||||
|
||||
return Ok(info.into());
|
||||
}
|
||||
|
||||
Err(IambError::NoAttachment.into())
|
||||
},
|
||||
MessageAction::Edit => {
|
||||
if msg.sender != settings.profile.user_id {
|
||||
let msg = "Cannot edit messages sent by someone else";
|
||||
let err = UIError::Failure(msg.into());
|
||||
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let ev = match &msg.event {
|
||||
MessageEvent::Original(ev) => &ev.content,
|
||||
MessageEvent::Local(_, ev) => ev.deref(),
|
||||
_ => {
|
||||
let msg = "Cannot edit a redacted message";
|
||||
let err = UIError::Failure(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
};
|
||||
|
||||
let text = match &ev.msgtype {
|
||||
MessageType::Text(msg) => msg.body.as_str(),
|
||||
_ => {
|
||||
let msg = "Cannot edit a non-text message";
|
||||
let err = UIError::Failure(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
};
|
||||
|
||||
self.tbox.set_text(text);
|
||||
self.reply_to = msg.reply_to().and_then(|id| info.get_message_key(&id)).cloned();
|
||||
self.editing = self.scrollback.get_key(info);
|
||||
self.focus = RoomFocus::MessageBar;
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
MessageAction::React(emoji) => {
|
||||
let room = self.get_joined(&store.application.worker)?;
|
||||
let event_id = match &msg.event {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Original(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Local(event_id, _) => event_id.clone(),
|
||||
MessageEvent::Redacted(_) => {
|
||||
let msg = "Cannot react to a redacted message";
|
||||
let err = UIError::Failure(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
};
|
||||
|
||||
let reaction = Reaction::new(event_id, emoji);
|
||||
let msg = ReactionEventContent::new(reaction);
|
||||
let _ = room.send(msg, None).await.map_err(IambError::from)?;
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
MessageAction::Redact(reason, skip_confirm) => {
|
||||
if !skip_confirm {
|
||||
let msg = "Are you sure you want to redact this message?";
|
||||
let act = IambAction::Message(MessageAction::Redact(reason, true));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
return Err(UIError::NeedConfirm(prompt));
|
||||
}
|
||||
|
||||
let room = self.get_joined(&store.application.worker)?;
|
||||
let event_id = match &msg.event {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Original(ev) => ev.event_id.clone(),
|
||||
MessageEvent::Local(event_id, _) => event_id.clone(),
|
||||
MessageEvent::Redacted(_) => {
|
||||
let msg = "Cannot redact already redacted message";
|
||||
let err = UIError::Failure(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
};
|
||||
|
||||
let event_id = event_id.as_ref();
|
||||
let reason = reason.as_deref();
|
||||
let _ = room.redact(event_id, reason, None).await.map_err(IambError::from)?;
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
MessageAction::Reply => {
|
||||
self.reply_to = self.scrollback.get_key(info);
|
||||
self.focus = RoomFocus::MessageBar;
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
MessageAction::Unreact(emoji) => {
|
||||
let room = self.get_joined(&store.application.worker)?;
|
||||
let event_id: &EventId = match &msg.event {
|
||||
MessageEvent::EncryptedOriginal(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::EncryptedRedacted(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::Original(ev) => ev.event_id.as_ref(),
|
||||
MessageEvent::Local(event_id, _) => event_id.as_ref(),
|
||||
MessageEvent::Redacted(_) => {
|
||||
let msg = "Cannot unreact to a redacted message";
|
||||
let err = UIError::Failure(msg.into());
|
||||
|
||||
return Err(err);
|
||||
},
|
||||
};
|
||||
|
||||
let reactions = match info.reactions.get(event_id) {
|
||||
Some(r) => r,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let reactions = reactions.iter().filter_map(|(event_id, (reaction, user_id))| {
|
||||
if user_id != &settings.profile.user_id {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(emoji) = &emoji {
|
||||
if emoji == reaction {
|
||||
return Some(event_id);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
return Some(event_id);
|
||||
}
|
||||
});
|
||||
|
||||
for reaction in reactions {
|
||||
let _ = room.redact(reaction, None, None).await.map_err(IambError::from)?;
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_command(
|
||||
&mut self,
|
||||
act: SendAction,
|
||||
_: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
let room = store
|
||||
.application
|
||||
.worker
|
||||
.client
|
||||
.get_joined_room(self.id())
|
||||
.ok_or(IambError::NotJoined)?;
|
||||
let info = store.application.rooms.get_or_default(self.id().to_owned());
|
||||
let mut show_echo = true;
|
||||
|
||||
let (event_id, msg) = match act {
|
||||
SendAction::Submit => {
|
||||
let msg = self.tbox.get();
|
||||
|
||||
if msg.is_blank() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mut msg = text_to_message(msg.trim_end().to_string());
|
||||
|
||||
if let Some((_, event_id)) = &self.editing {
|
||||
msg.relates_to = Some(Relation::Replacement(Replacement::new(
|
||||
event_id.clone(),
|
||||
Box::new(msg.clone()),
|
||||
)));
|
||||
|
||||
show_echo = false;
|
||||
} else if let Some(m) = self.get_reply_to(info) {
|
||||
// XXX: Switch to RoomMessageEventContent::reply() once it's stable?
|
||||
msg = msg.make_reply_to(m);
|
||||
}
|
||||
|
||||
// XXX: second parameter can be a locally unique transaction id.
|
||||
// Useful for doing retries.
|
||||
let resp = room.send(msg.clone(), None).await.map_err(IambError::from)?;
|
||||
let event_id = resp.event_id;
|
||||
|
||||
// Reset message bar state now that it's been sent.
|
||||
self.reset();
|
||||
|
||||
(event_id, msg)
|
||||
},
|
||||
SendAction::Upload(file) => {
|
||||
let path = Path::new(file.as_str());
|
||||
let mime = mime_guess::from_path(path).first_or(mime::APPLICATION_OCTET_STREAM);
|
||||
|
||||
let bytes = fs::read(path)?;
|
||||
let name = path
|
||||
.file_name()
|
||||
.map(OsStr::to_string_lossy)
|
||||
.unwrap_or_else(|| Cow::from("Attachment"));
|
||||
let config = AttachmentConfig::new();
|
||||
|
||||
let resp = room
|
||||
.send_attachment(name.as_ref(), &mime, bytes.as_ref(), config)
|
||||
.await
|
||||
.map_err(IambError::from)?;
|
||||
|
||||
// Mock up the local echo message for the scrollback.
|
||||
let msg = TextMessageEventContent::plain(format!("[Attached File: {name}]"));
|
||||
let msg = MessageType::Text(msg);
|
||||
let msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
(resp.event_id, msg)
|
||||
},
|
||||
SendAction::UploadImage(width, height, bytes) => {
|
||||
// Convert to png because arboard does not give us the mime type.
|
||||
let bytes =
|
||||
image::ImageBuffer::from_raw(width as _, height as _, bytes.into_owned())
|
||||
.ok_or(IambError::Clipboard)
|
||||
.and_then(|imagebuf| {
|
||||
let dynimage = image::DynamicImage::ImageRgba8(imagebuf);
|
||||
let bytes = Vec::<u8>::new();
|
||||
let mut buff = std::io::Cursor::new(bytes);
|
||||
dynimage.write_to(&mut buff, image::ImageOutputFormat::Png)?;
|
||||
Ok(buff.into_inner())
|
||||
})
|
||||
.map_err(IambError::from)?;
|
||||
let mime = mime::IMAGE_PNG;
|
||||
|
||||
let name = "Clipboard.png";
|
||||
let config = AttachmentConfig::new();
|
||||
|
||||
let resp = room
|
||||
.send_attachment(name.as_ref(), &mime, bytes.as_ref(), config)
|
||||
.await
|
||||
.map_err(IambError::from)?;
|
||||
|
||||
// Mock up the local echo message for the scrollback.
|
||||
let msg = TextMessageEventContent::plain(format!("[Attached File: {name}]"));
|
||||
let msg = MessageType::Text(msg);
|
||||
let msg = RoomMessageEventContent::new(msg);
|
||||
|
||||
(resp.event_id, msg)
|
||||
},
|
||||
};
|
||||
|
||||
if show_echo {
|
||||
let user = store.application.settings.profile.user_id.clone();
|
||||
let key = (MessageTimeStamp::LocalEcho, event_id.clone());
|
||||
let msg = MessageEvent::Local(event_id, msg.into());
|
||||
let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho);
|
||||
info.messages.insert(key, msg);
|
||||
}
|
||||
|
||||
// Jump to the end of the scrollback to show the message.
|
||||
self.scrollback.goto_latest();
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn focus_toggle(&mut self) {
|
||||
self.focus = match self.focus {
|
||||
RoomFocus::Scrollback => RoomFocus::MessageBar,
|
||||
@@ -100,7 +554,7 @@ impl ChatState {
|
||||
return;
|
||||
}
|
||||
|
||||
if !store.application.settings.tunables.typing_notice {
|
||||
if !store.application.settings.tunables.typing_notice_send {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -147,6 +601,9 @@ impl WindowOps<IambInfo> for ChatState {
|
||||
|
||||
scrollback: self.scrollback.dup(store),
|
||||
focus: self.focus,
|
||||
|
||||
reply_to: None,
|
||||
editing: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,6 +613,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))
|
||||
}
|
||||
@@ -185,6 +657,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
// Run command again.
|
||||
delegate!(self, w => w.editor_command(act, ctx, store))
|
||||
},
|
||||
Err(EditError::Register(RegisterError::ClipboardImage(data))) => {
|
||||
let msg = "Do you really want to upload the image from your system clipboard?";
|
||||
let send =
|
||||
IambAction::Send(SendAction::UploadImage(data.width, data.height, data.bytes));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(send)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(EditError::NeedConfirm(prompt))
|
||||
},
|
||||
res @ Err(_) => res,
|
||||
}
|
||||
}
|
||||
@@ -229,17 +710,9 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
let txt = self.tbox.reset_text();
|
||||
let act = SendAction::Submit;
|
||||
|
||||
let act = if txt.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
let act = IambAction::SendMessage(self.room_id.clone(), txt).into();
|
||||
|
||||
vec![(act, ctx.clone())]
|
||||
};
|
||||
|
||||
Ok(act)
|
||||
Ok(vec![(IambAction::from(act).into(), ctx.clone())])
|
||||
}
|
||||
|
||||
fn abort(
|
||||
@@ -254,7 +727,7 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let text = self.tbox.reset().trim();
|
||||
let text = self.reset().trim();
|
||||
|
||||
if text.is_empty() {
|
||||
let _ = self.sent.end();
|
||||
@@ -269,13 +742,14 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
&mut self,
|
||||
dir: &MoveDir1D,
|
||||
count: &Count,
|
||||
prefixed: bool,
|
||||
ctx: &ProgramContext,
|
||||
_: &mut ProgramStore,
|
||||
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
|
||||
let count = ctx.resolve(count);
|
||||
let rope = self.tbox.get();
|
||||
|
||||
let text = self.sent.recall(&rope, &mut self.sent_scrollback, *dir, count);
|
||||
let text = self.sent.recall(&rope, &mut self.sent_scrollback, *dir, prefixed, count);
|
||||
|
||||
if let Some(text) = text {
|
||||
self.tbox.set_text(text);
|
||||
@@ -299,7 +773,9 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ChatState {
|
||||
match act {
|
||||
PromptAction::Submit => self.submit(ctx, store),
|
||||
PromptAction::Abort(empty) => self.abort(*empty, ctx, store),
|
||||
PromptAction::Recall(dir, count) => self.recall(dir, count, ctx, store),
|
||||
PromptAction::Recall(dir, count, prefixed) => {
|
||||
self.recall(dir, count, *prefixed, ctx, store)
|
||||
},
|
||||
_ => Err(EditError::Unimplemented("unknown prompt action".to_string())),
|
||||
}
|
||||
}
|
||||
@@ -325,21 +801,81 @@ impl<'a> StatefulWidget for Chat<'a> {
|
||||
type State = ChatState;
|
||||
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||
// Determine whether we have a description to show for the message bar.
|
||||
let desc_spans = match (&state.editing, &state.reply_to) {
|
||||
(None, None) => None,
|
||||
(Some(_), None) => Some(Spans::from("Editing message")),
|
||||
(editing, Some(_)) => {
|
||||
state.reply_to.as_ref().and_then(|k| {
|
||||
let room = self.store.application.rooms.get(state.id())?;
|
||||
let msg = room.messages.get(k)?;
|
||||
let user =
|
||||
self.store.application.settings.get_user_span(msg.sender.as_ref(), room);
|
||||
let prefix = if editing.is_some() {
|
||||
Span::from("Editing reply to ")
|
||||
} else {
|
||||
Span::from("Replying to ")
|
||||
};
|
||||
let spans = Spans(vec![prefix, user]);
|
||||
|
||||
spans.into()
|
||||
})
|
||||
},
|
||||
};
|
||||
|
||||
// Determine the region to show each UI element.
|
||||
let lines = state.tbox.has_lines(5).max(1) as u16;
|
||||
let drawh = area.height;
|
||||
let texth = lines.min(drawh).clamp(1, 5);
|
||||
let scrollh = drawh.saturating_sub(texth);
|
||||
let desch = if desc_spans.is_some() {
|
||||
drawh.saturating_sub(texth).min(1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let scrollh = drawh.saturating_sub(texth).saturating_sub(desch);
|
||||
|
||||
let scrollarea = Rect::new(area.x, area.y, area.width, scrollh);
|
||||
let textarea = Rect::new(scrollarea.x, scrollarea.y + scrollh, scrollarea.width, texth);
|
||||
let descarea = Rect::new(area.x, scrollarea.y + scrollh, area.width, desch);
|
||||
let textarea = Rect::new(area.x, descarea.y + desch, area.width, texth);
|
||||
|
||||
let scrollback_focused = state.focus.is_scrollback() && self.focused;
|
||||
let scrollback = Scrollback::new(self.store).focus(scrollback_focused);
|
||||
scrollback.render(scrollarea, buf, &mut state.scrollback);
|
||||
// Render the message bar and any description for it.
|
||||
if let Some(desc_spans) = desc_spans {
|
||||
Paragraph::new(desc_spans).render(descarea, buf);
|
||||
}
|
||||
|
||||
let prompt = if self.focused { "> " } else { " " };
|
||||
|
||||
let tbox = TextBox::new().prompt(prompt);
|
||||
tbox.render(textarea, buf, &mut state.tbox);
|
||||
|
||||
// Render the message scrollback.
|
||||
let scrollback_focused = state.focus.is_scrollback() && self.focused;
|
||||
let scrollback = Scrollback::new(self.store)
|
||||
.focus(scrollback_focused)
|
||||
.room_focus(self.focused);
|
||||
scrollback.render(scrollarea, buf, &mut state.scrollback);
|
||||
}
|
||||
}
|
||||
|
||||
fn open_command(open_command: Option<&Vec<String>>, target: OsString) -> IambResult<()> {
|
||||
if let Some(mut cmd) = open_command.and_then(cmd) {
|
||||
cmd.arg(target);
|
||||
cmd.spawn()?;
|
||||
return Ok(());
|
||||
} else {
|
||||
// open::that may not return until the spawned program closes.
|
||||
tokio::task::spawn_blocking(move || {
|
||||
return open::that(target);
|
||||
});
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd(open_command: &Vec<String>) -> Option<Command> {
|
||||
if let [program, args @ ..] = open_command.as_slice() {
|
||||
let mut cmd = Command::new(program);
|
||||
cmd.args(args);
|
||||
return Some(cmd);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
use matrix_sdk::room::Room as MatrixRoom;
|
||||
use matrix_sdk::ruma::RoomId;
|
||||
use matrix_sdk::DisplayName;
|
||||
use matrix_sdk::{
|
||||
room::{Invited, Room as MatrixRoom},
|
||||
ruma::{
|
||||
events::{
|
||||
room::{name::RoomNameEventContent, topic::RoomTopicEventContent},
|
||||
tag::{TagInfo, Tags},
|
||||
},
|
||||
RoomId,
|
||||
},
|
||||
DisplayName,
|
||||
};
|
||||
|
||||
use modalkit::tui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
layout::{Alignment, Rect},
|
||||
style::{Modifier as StyleModifier, Style},
|
||||
text::{Span, Spans},
|
||||
widgets::StatefulWidget,
|
||||
text::{Span, Spans, Text},
|
||||
widgets::{Paragraph, StatefulWidget, Widget},
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
@@ -21,6 +29,7 @@ use modalkit::{
|
||||
PromptAction,
|
||||
Promptable,
|
||||
Scrollable,
|
||||
UIError,
|
||||
},
|
||||
editing::base::{
|
||||
Axis,
|
||||
@@ -31,19 +40,27 @@ use modalkit::{
|
||||
PositionList,
|
||||
ScrollStyle,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
editing::completion::CompletionList,
|
||||
input::dialog::PromptYesNo,
|
||||
input::InputContext,
|
||||
widgets::{TermOffset, TerminalCursor, WindowOps},
|
||||
};
|
||||
|
||||
use crate::base::{
|
||||
IambAction,
|
||||
IambError,
|
||||
IambId,
|
||||
IambInfo,
|
||||
IambResult,
|
||||
MessageAction,
|
||||
ProgramAction,
|
||||
ProgramContext,
|
||||
ProgramStore,
|
||||
RoomAction,
|
||||
RoomField,
|
||||
SendAction,
|
||||
};
|
||||
|
||||
use self::chat::ChatState;
|
||||
@@ -80,10 +97,16 @@ impl From<SpaceState> for RoomState {
|
||||
}
|
||||
|
||||
impl RoomState {
|
||||
pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
|
||||
pub fn new(
|
||||
room: MatrixRoom,
|
||||
name: DisplayName,
|
||||
tags: Option<Tags>,
|
||||
store: &mut ProgramStore,
|
||||
) -> Self {
|
||||
let room_id = room.room_id().to_owned();
|
||||
let info = store.application.get_room_info(room_id);
|
||||
info.name = name.to_string().into();
|
||||
info.tags = tags;
|
||||
|
||||
if room.is_space() {
|
||||
SpaceState::new(room).into()
|
||||
@@ -92,13 +115,130 @@ impl RoomState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn room_command(
|
||||
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.refresh_room(store),
|
||||
RoomState::Space(space) => space.refresh_room(store),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_invite(
|
||||
&self,
|
||||
invited: Invited,
|
||||
area: Rect,
|
||||
buf: &mut Buffer,
|
||||
store: &mut ProgramStore,
|
||||
) {
|
||||
let inviter = store.application.worker.get_inviter(invited.clone());
|
||||
|
||||
let name = match invited.canonical_alias() {
|
||||
Some(alias) => alias.to_string(),
|
||||
None => format!("{:?}", store.application.get_room_title(self.id())),
|
||||
};
|
||||
|
||||
let mut invited = vec![Span::from(format!("You have been invited to join {name}"))];
|
||||
|
||||
if let Ok(Some(inviter)) = &inviter {
|
||||
let info = store.application.rooms.get_or_default(self.id().to_owned());
|
||||
invited.push(Span::from(" by "));
|
||||
invited.push(store.application.settings.get_user_span(inviter.user_id(), info));
|
||||
}
|
||||
|
||||
let l1 = Spans(invited);
|
||||
let l2 = Spans::from(
|
||||
"You can run `:invite accept` or `:invite reject` to accept or reject this invitation.",
|
||||
);
|
||||
let text = Text { lines: vec![l1, l2] };
|
||||
|
||||
Paragraph::new(text).alignment(Alignment::Center).render(area, buf);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
pub async fn message_command(
|
||||
&mut self,
|
||||
act: MessageAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.message_command(act, ctx, store).await,
|
||||
RoomState::Space(_) => Err(IambError::NoSelectedMessage.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_command(
|
||||
&mut self,
|
||||
act: SendAction,
|
||||
ctx: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<EditInfo> {
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.send_command(act, ctx, store).await,
|
||||
RoomState::Space(_) => Err(IambError::NoSelectedRoom.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn room_command(
|
||||
&mut self,
|
||||
act: RoomAction,
|
||||
_: ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
|
||||
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())
|
||||
}
|
||||
},
|
||||
RoomAction::InviteReject => {
|
||||
if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
|
||||
room.reject_invitation().await.map_err(IambError::from)?;
|
||||
|
||||
Ok(vec![])
|
||||
} else {
|
||||
Err(IambError::NotInvited.into())
|
||||
}
|
||||
},
|
||||
RoomAction::InviteSend(user) => {
|
||||
if let Some(room) = store.application.worker.client.get_joined_room(self.id()) {
|
||||
room.invite_user_by_id(user.as_ref()).await.map_err(IambError::from)?;
|
||||
|
||||
Ok(vec![])
|
||||
} else {
|
||||
Err(IambError::NotJoined.into())
|
||||
}
|
||||
},
|
||||
RoomAction::Leave(skip_confirm) => {
|
||||
if let Some(room) = store.application.worker.client.get_joined_room(self.id()) {
|
||||
if skip_confirm {
|
||||
room.leave().await.map_err(IambError::from)?;
|
||||
|
||||
Ok(vec![])
|
||||
} else {
|
||||
let msg = "Do you really want to leave this room?";
|
||||
let leave = IambAction::Room(RoomAction::Leave(true));
|
||||
let prompt = PromptYesNo::new(msg, vec![Action::from(leave)]);
|
||||
let prompt = Box::new(prompt);
|
||||
|
||||
Err(UIError::NeedConfirm(prompt))
|
||||
}
|
||||
} else {
|
||||
Err(IambError::NotJoined.into())
|
||||
}
|
||||
},
|
||||
RoomAction::Members(mut cmd) => {
|
||||
let width = Count::Exact(30);
|
||||
let act =
|
||||
@@ -109,8 +249,50 @@ impl RoomState {
|
||||
|
||||
Ok(vec![(act, cmd.context.take())])
|
||||
},
|
||||
RoomAction::Set(field) => {
|
||||
store.application.worker.set_room(self.id().to_owned(), field)?;
|
||||
RoomAction::Set(field, value) => {
|
||||
let room = store
|
||||
.application
|
||||
.get_joined_room(self.id())
|
||||
.ok_or(UIError::Application(IambError::NotJoined))?;
|
||||
|
||||
match field {
|
||||
RoomField::Name => {
|
||||
let ev = RoomNameEventContent::new(value.into());
|
||||
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||
},
|
||||
RoomField::Tag(tag) => {
|
||||
let mut info = TagInfo::new();
|
||||
info.order = Some(1.0);
|
||||
|
||||
let _ = room.set_tag(tag, info).await.map_err(IambError::from)?;
|
||||
},
|
||||
RoomField::Topic => {
|
||||
let ev = RoomTopicEventContent::new(value);
|
||||
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||
},
|
||||
}
|
||||
|
||||
Ok(vec![])
|
||||
},
|
||||
RoomAction::Unset(field) => {
|
||||
let room = store
|
||||
.application
|
||||
.get_joined_room(self.id())
|
||||
.ok_or(UIError::Application(IambError::NotJoined))?;
|
||||
|
||||
match field {
|
||||
RoomField::Name => {
|
||||
let ev = RoomNameEventContent::new(None);
|
||||
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||
},
|
||||
RoomField::Tag(tag) => {
|
||||
let _ = room.remove_tag(tag).await.map_err(IambError::from)?;
|
||||
},
|
||||
RoomField::Topic => {
|
||||
let ev = RoomTopicEventContent::new("".into());
|
||||
let _ = room.send_state_event(ev).await.map_err(IambError::from)?;
|
||||
},
|
||||
}
|
||||
|
||||
Ok(vec![])
|
||||
},
|
||||
@@ -209,6 +391,14 @@ impl TerminalCursor for RoomState {
|
||||
|
||||
impl WindowOps<IambInfo> for RoomState {
|
||||
fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
|
||||
if let MatrixRoom::Invited(_) = self.room() {
|
||||
self.refresh_room(store);
|
||||
}
|
||||
|
||||
if let MatrixRoom::Invited(invited) = self.room() {
|
||||
self.draw_invite(invited.clone(), area, buf, store);
|
||||
}
|
||||
|
||||
match self {
|
||||
RoomState::Chat(chat) => chat.draw(area, buf, focused, store),
|
||||
RoomState::Space(space) => {
|
||||
@@ -224,10 +414,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> {
|
||||
|
||||
@@ -4,7 +4,13 @@ use regex::Regex;
|
||||
|
||||
use matrix_sdk::ruma::OwnedRoomId;
|
||||
|
||||
use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
|
||||
use modalkit::tui::{
|
||||
buffer::Buffer,
|
||||
layout::{Alignment, Rect},
|
||||
style::{Modifier as StyleModifier, Style},
|
||||
text::{Span, Spans},
|
||||
widgets::{Paragraph, StatefulWidget, Widget},
|
||||
};
|
||||
use modalkit::widgets::{ScrollActions, TerminalCursor, WindowOps};
|
||||
|
||||
use modalkit::editing::{
|
||||
@@ -32,6 +38,9 @@ use modalkit::editing::{
|
||||
base::{
|
||||
Axis,
|
||||
CloseFlags,
|
||||
CompletionDisplay,
|
||||
CompletionSelection,
|
||||
CompletionType,
|
||||
Count,
|
||||
EditRange,
|
||||
EditTarget,
|
||||
@@ -51,7 +60,9 @@ use modalkit::editing::{
|
||||
TargetShape,
|
||||
ViewportContext,
|
||||
WordStyle,
|
||||
WriteFlags,
|
||||
},
|
||||
completion::CompletionList,
|
||||
context::{EditContext, Resolve},
|
||||
cursor::{CursorGroup, CursorState},
|
||||
history::HistoryList,
|
||||
@@ -60,8 +71,9 @@ use modalkit::editing::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
base::{IambBufferId, IambInfo, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||
message::{Message, MessageCursor, MessageKey},
|
||||
base::{IambBufferId, IambInfo, IambResult, ProgramContext, ProgramStore, RoomFocus, RoomInfo},
|
||||
config::ApplicationSettings,
|
||||
message::{Message, MessageCursor, MessageKey, Messages},
|
||||
};
|
||||
|
||||
fn nth_key_before(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageKey {
|
||||
@@ -102,12 +114,31 @@ fn nth_after(pos: MessageKey, n: usize, info: &RoomInfo) -> MessageCursor {
|
||||
nth_key_after(pos, n, info).into()
|
||||
}
|
||||
|
||||
fn prevmsg<'a>(key: &MessageKey, info: &'a RoomInfo) -> Option<&'a Message> {
|
||||
info.messages.range(..key).next_back().map(|(_, v)| v)
|
||||
}
|
||||
|
||||
pub struct ScrollbackState {
|
||||
/// The room identifier.
|
||||
room_id: OwnedRoomId,
|
||||
|
||||
/// The buffer identifier used for saving marks, etc.
|
||||
id: IambBufferId,
|
||||
|
||||
/// The currently selected message in the scrollback.
|
||||
cursor: MessageCursor,
|
||||
|
||||
/// Contextual info about the viewport used during rendering.
|
||||
viewctx: ViewportContext<MessageCursor>,
|
||||
|
||||
/// The jumplist of visited messages.
|
||||
jumped: HistoryList<MessageCursor>,
|
||||
|
||||
/// Whether the full message should be drawn during the next render() call.
|
||||
///
|
||||
/// This is used to ensure that ^E/^Y work nicely when the cursor is currently
|
||||
/// on a multiline message.
|
||||
show_full_on_redraw: bool,
|
||||
}
|
||||
|
||||
impl ScrollbackState {
|
||||
@@ -116,8 +147,20 @@ impl ScrollbackState {
|
||||
let cursor = MessageCursor::default();
|
||||
let viewctx = ViewportContext::default();
|
||||
let jumped = HistoryList::default();
|
||||
let show_full_on_redraw = false;
|
||||
|
||||
ScrollbackState { room_id, id, cursor, viewctx, jumped }
|
||||
ScrollbackState {
|
||||
room_id,
|
||||
id,
|
||||
cursor,
|
||||
viewctx,
|
||||
jumped,
|
||||
show_full_on_redraw,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn goto_latest(&mut self) {
|
||||
self.cursor = MessageCursor::latest();
|
||||
}
|
||||
|
||||
/// Set the dimensions and placement within the terminal window for this list.
|
||||
@@ -125,6 +168,21 @@ impl ScrollbackState {
|
||||
self.viewctx.dimensions = (area.width as usize, area.height as usize);
|
||||
}
|
||||
|
||||
pub fn get_key(&self, info: &mut RoomInfo) -> Option<MessageKey> {
|
||||
self.cursor
|
||||
.timestamp
|
||||
.clone()
|
||||
.or_else(|| info.messages.last_key_value().map(|kv| kv.0.clone()))
|
||||
}
|
||||
|
||||
pub fn get_mut<'a>(&mut self, messages: &'a mut Messages) -> Option<&'a mut Message> {
|
||||
if let Some(k) = &self.cursor.timestamp {
|
||||
messages.get_mut(k)
|
||||
} else {
|
||||
messages.last_entry().map(|o| o.into_mut())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn messages<'a>(
|
||||
&self,
|
||||
range: EditRange<MessageCursor>,
|
||||
@@ -148,7 +206,13 @@ impl ScrollbackState {
|
||||
}
|
||||
}
|
||||
|
||||
fn scrollview(&mut self, idx: MessageKey, pos: MovePosition, info: &RoomInfo) {
|
||||
fn scrollview(
|
||||
&mut self,
|
||||
idx: MessageKey,
|
||||
pos: MovePosition,
|
||||
info: &RoomInfo,
|
||||
settings: &ApplicationSettings,
|
||||
) {
|
||||
let selidx = if let Some(key) = self.cursor.to_key(info) {
|
||||
key
|
||||
} else {
|
||||
@@ -165,7 +229,8 @@ impl ScrollbackState {
|
||||
|
||||
for (key, item) in info.messages.range(..=&idx).rev() {
|
||||
let sel = selidx == key;
|
||||
let len = item.show(sel, &self.viewctx).lines.len();
|
||||
let prev = prevmsg(key, info);
|
||||
let len = item.show(prev, sel, &self.viewctx, info, settings).lines.len();
|
||||
|
||||
if key == &idx {
|
||||
lines += len / 2;
|
||||
@@ -187,7 +252,8 @@ impl ScrollbackState {
|
||||
|
||||
for (key, item) in info.messages.range(..=&idx).rev() {
|
||||
let sel = key == selidx;
|
||||
let len = item.show(sel, &self.viewctx).lines.len();
|
||||
let prev = prevmsg(key, info);
|
||||
let len = item.show(prev, sel, &self.viewctx, info, settings).lines.len();
|
||||
|
||||
lines += len;
|
||||
|
||||
@@ -202,7 +268,7 @@ impl ScrollbackState {
|
||||
}
|
||||
}
|
||||
|
||||
fn shift_cursor(&mut self, info: &RoomInfo) {
|
||||
fn shift_cursor(&mut self, info: &RoomInfo, settings: &ApplicationSettings) {
|
||||
let last_key = if let Some(k) = info.messages.last_key_value() {
|
||||
k.0
|
||||
} else {
|
||||
@@ -220,6 +286,7 @@ impl ScrollbackState {
|
||||
let mut lines = 0;
|
||||
|
||||
let cursor_key = self.cursor.timestamp.as_ref().unwrap_or(last_key);
|
||||
let mut prev = prevmsg(cursor_key, info);
|
||||
|
||||
for (idx, item) in info.messages.range(corner_key.clone()..) {
|
||||
if idx == cursor_key {
|
||||
@@ -227,13 +294,15 @@ impl ScrollbackState {
|
||||
break;
|
||||
}
|
||||
|
||||
lines += item.show(false, &self.viewctx).height().max(1);
|
||||
lines += item.show(prev, false, &self.viewctx, info, settings).height().max(1);
|
||||
|
||||
if lines >= self.viewctx.get_height() {
|
||||
// We've reached the end of the viewport; move cursor into it.
|
||||
self.cursor = idx.clone().into();
|
||||
break;
|
||||
}
|
||||
|
||||
prev = Some(item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,7 +451,7 @@ impl ScrollbackState {
|
||||
continue;
|
||||
}
|
||||
|
||||
if needle.is_match(msg.as_ref()) {
|
||||
if needle.is_match(msg.event.body().as_ref()) {
|
||||
mc = MessageCursor::from(key.clone()).into();
|
||||
count -= 1;
|
||||
}
|
||||
@@ -406,7 +475,7 @@ impl ScrollbackState {
|
||||
break;
|
||||
}
|
||||
|
||||
if needle.is_match(msg.as_ref()) {
|
||||
if needle.is_match(msg.event.body().as_ref()) {
|
||||
mc = MessageCursor::from(key.clone()).into();
|
||||
count -= 1;
|
||||
}
|
||||
@@ -447,6 +516,7 @@ impl WindowOps<IambInfo> for ScrollbackState {
|
||||
cursor: self.cursor.clone(),
|
||||
viewctx: self.viewctx.clone(),
|
||||
jumped: self.jumped.clone(),
|
||||
show_full_on_redraw: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,6 +526,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
|
||||
}
|
||||
@@ -473,7 +560,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 {
|
||||
@@ -532,7 +619,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
let needle = match ctx.get_search_regex() {
|
||||
Some(re) => re,
|
||||
None => {
|
||||
let lsearch = store.registers.get(&Register::LastSearch);
|
||||
let lsearch = store.registers.get(&Register::LastSearch)?;
|
||||
let lsearch = lsearch.value.to_string();
|
||||
|
||||
Regex::new(lsearch.as_ref())?
|
||||
@@ -556,7 +643,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
},
|
||||
|
||||
_ => {
|
||||
let msg = format!("Unknown editing target: {:?}", motion);
|
||||
let msg = format!("Unknown editing target: {motion:?}");
|
||||
let err = EditError::Unimplemented(msg);
|
||||
|
||||
return Err(err);
|
||||
@@ -567,6 +654,8 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
self.cursor = pos;
|
||||
}
|
||||
|
||||
self.show_full_on_redraw = true;
|
||||
|
||||
return Ok(None);
|
||||
},
|
||||
EditAction::Yank => {
|
||||
@@ -616,7 +705,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
let needle = match ctx.get_search_regex() {
|
||||
Some(re) => re,
|
||||
None => {
|
||||
let lsearch = store.registers.get(&Register::LastSearch);
|
||||
let lsearch = store.registers.get(&Register::LastSearch)?;
|
||||
let lsearch = lsearch.value.to_string();
|
||||
|
||||
Regex::new(lsearch.as_ref())?
|
||||
@@ -641,7 +730,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
},
|
||||
|
||||
_ => {
|
||||
let msg = format!("Unknown motion: {:?}", motion);
|
||||
let msg = format!("Unknown motion: {motion:?}");
|
||||
let err = EditError::Unimplemented(msg);
|
||||
|
||||
return Err(err);
|
||||
@@ -652,7 +741,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
let mut yanked = EditRope::from("");
|
||||
|
||||
for (_, msg) in self.messages(range, info) {
|
||||
yanked += EditRope::from(msg.as_ref());
|
||||
yanked += EditRope::from(msg.event.body());
|
||||
yanked += EditRope::from('\n');
|
||||
}
|
||||
|
||||
@@ -664,7 +753,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
flags |= RegisterPutFlags::APPEND;
|
||||
}
|
||||
|
||||
store.registers.put(®ister, cell, flags);
|
||||
store.registers.put(®ister, cell, flags)?;
|
||||
}
|
||||
|
||||
return Ok(None);
|
||||
@@ -672,7 +761,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
|
||||
// Everything else is a modifying action.
|
||||
EditAction::ChangeCase(_) => Err(EditError::ReadOnly),
|
||||
EditAction::ChangeNumber(_) => Err(EditError::ReadOnly),
|
||||
EditAction::ChangeNumber(_, _) => Err(EditError::ReadOnly),
|
||||
EditAction::Delete => Err(EditError::ReadOnly),
|
||||
EditAction::Format => Err(EditError::ReadOnly),
|
||||
EditAction::Indent(_) => Err(EditError::ReadOnly),
|
||||
@@ -701,6 +790,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,
|
||||
@@ -729,7 +829,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
HistoryAction::Checkpoint => Ok(None),
|
||||
HistoryAction::Undo(_) => Err(EditError::Failure("Nothing to undo".into())),
|
||||
HistoryAction::Redo(_) => Err(EditError::Failure("Nothing to redo".into())),
|
||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {:?}", act))),
|
||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {act:?}"))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -786,7 +886,7 @@ impl EditorActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
|
||||
Ok(None)
|
||||
},
|
||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {:?}", act))),
|
||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {act:?}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -806,14 +906,14 @@ 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)
|
||||
},
|
||||
|
||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {:?}", act))),
|
||||
_ => Err(EditError::Unimplemented(format!("Unknown action: {act:?}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -912,7 +1012,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
return Err(err);
|
||||
},
|
||||
_ => {
|
||||
let msg = format!("Messages scrollback doesn't support {:?}", act);
|
||||
let msg = format!("Messages scrollback doesn't support {act:?}");
|
||||
let err = EditError::Unimplemented(msg);
|
||||
|
||||
return Err(err);
|
||||
@@ -930,7 +1030,8 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
ctx: &ProgramContext,
|
||||
store: &mut ProgramStore,
|
||||
) -> EditResult<EditInfo, IambInfo> {
|
||||
let info = store.application.get_room_info(self.room_id.clone());
|
||||
let info = store.application.rooms.get_or_default(self.room_id.clone());
|
||||
let settings = &store.application.settings;
|
||||
let mut corner = self.viewctx.corner.clone();
|
||||
|
||||
let last_key = if let Some(k) = info.messages.last_key_value() {
|
||||
@@ -956,7 +1057,8 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
|
||||
for (key, item) in info.messages.range(..=&corner_key).rev() {
|
||||
let sel = key == cursor_key;
|
||||
let txt = item.show(sel, &self.viewctx);
|
||||
let prev = prevmsg(key, info);
|
||||
let txt = item.show(prev, sel, &self.viewctx, info, settings);
|
||||
let len = txt.height().max(1);
|
||||
let max = len.saturating_sub(1);
|
||||
|
||||
@@ -980,12 +1082,16 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
}
|
||||
},
|
||||
MoveDir2D::Down => {
|
||||
let mut prev = prevmsg(&corner_key, info);
|
||||
|
||||
for (key, item) in info.messages.range(&corner_key..) {
|
||||
let sel = key == cursor_key;
|
||||
let txt = item.show(sel, &self.viewctx);
|
||||
let txt = item.show(prev, sel, &self.viewctx, info, settings);
|
||||
let len = txt.height().max(1);
|
||||
let max = len.saturating_sub(1);
|
||||
|
||||
prev = Some(item);
|
||||
|
||||
if key != &corner_key {
|
||||
corner.text_row = 0;
|
||||
}
|
||||
@@ -1018,7 +1124,7 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
}
|
||||
|
||||
self.viewctx.corner = corner;
|
||||
self.shift_cursor(info);
|
||||
self.shift_cursor(info, settings);
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
@@ -1038,10 +1144,11 @@ impl ScrollActions<ProgramContext, ProgramStore, IambInfo> for ScrollbackState {
|
||||
Err(err)
|
||||
},
|
||||
Axis::Vertical => {
|
||||
let info = store.application.get_room_info(self.room_id.clone());
|
||||
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() {
|
||||
self.scrollview(key, pos, info);
|
||||
self.scrollview(key, pos, info, settings);
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
@@ -1104,14 +1211,43 @@ impl TerminalCursor for ScrollbackState {
|
||||
}
|
||||
}
|
||||
|
||||
fn render_jump_to_recent(area: Rect, buf: &mut Buffer, focused: bool) -> Rect {
|
||||
if area.height <= 5 || area.width <= 20 {
|
||||
return area;
|
||||
}
|
||||
|
||||
let top = Rect::new(area.x, area.y, area.width, area.height - 1);
|
||||
let bar = Rect::new(area.x, area.y + top.height, area.width, 1);
|
||||
let msg = vec![
|
||||
Span::raw("Use "),
|
||||
Span::styled("G", Style::default().add_modifier(StyleModifier::BOLD)),
|
||||
Span::raw(if focused { "" } else { " in scrollback" }),
|
||||
Span::raw(" to jump to latest message"),
|
||||
];
|
||||
|
||||
Paragraph::new(Spans::from(msg))
|
||||
.alignment(Alignment::Center)
|
||||
.render(bar, buf);
|
||||
|
||||
return top;
|
||||
}
|
||||
|
||||
pub struct Scrollback<'a> {
|
||||
room_focused: bool,
|
||||
focused: bool,
|
||||
store: &'a mut ProgramStore,
|
||||
}
|
||||
|
||||
impl<'a> Scrollback<'a> {
|
||||
pub fn new(store: &'a mut ProgramStore) -> Self {
|
||||
Scrollback { focused: false, store }
|
||||
Scrollback { room_focused: false, focused: false, store }
|
||||
}
|
||||
|
||||
/// Indicate whether the room window is currently focused, regardless of whether the scrollback
|
||||
/// also is.
|
||||
pub fn room_focus(mut self, focused: bool) -> Self {
|
||||
self.room_focused = focused;
|
||||
self
|
||||
}
|
||||
|
||||
/// Indicate whether the scrollback is currently focused.
|
||||
@@ -1125,8 +1261,13 @@ 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 area = info.render_typing(area, buf, &self.store.application.settings);
|
||||
let info = self.store.application.rooms.get_or_default(state.room_id.clone());
|
||||
let settings = &self.store.application.settings;
|
||||
let area = if state.cursor.timestamp.is_some() {
|
||||
render_jump_to_recent(area, buf, self.focused)
|
||||
} else {
|
||||
info.render_typing(area, buf, &self.store.application.settings)
|
||||
};
|
||||
|
||||
state.set_term_info(area);
|
||||
|
||||
@@ -1149,21 +1290,28 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
||||
};
|
||||
|
||||
let corner = &state.viewctx.corner;
|
||||
let corner_key = match (&corner.timestamp, &cursor.timestamp) {
|
||||
(_, None) => nth_key_before(cursor_key.clone(), height, info),
|
||||
(None, _) => nth_key_before(cursor_key.clone(), height, info),
|
||||
(Some(k), _) => k.clone(),
|
||||
let corner_key = if let Some(k) = &corner.timestamp {
|
||||
k.clone()
|
||||
} else {
|
||||
nth_key_before(cursor_key.clone(), height, info)
|
||||
};
|
||||
|
||||
let foc = self.focused || cursor.timestamp.is_some();
|
||||
let full = std::mem::take(&mut state.show_full_on_redraw) || cursor.timestamp.is_none();
|
||||
let mut lines = vec![];
|
||||
let mut sawit = false;
|
||||
let mut prev = prevmsg(&corner_key, info);
|
||||
|
||||
for (key, item) in info.messages.range(&corner_key..) {
|
||||
let sel = key == cursor_key;
|
||||
let txt = item.show(self.focused && sel, &state.viewctx);
|
||||
let txt = item.show(prev, foc && sel, &state.viewctx, info, settings);
|
||||
|
||||
prev = Some(item);
|
||||
|
||||
let incomplete_ok = !full || !sel;
|
||||
|
||||
for (row, line) in txt.lines.into_iter().enumerate() {
|
||||
if sawit && lines.len() >= height {
|
||||
if sawit && lines.len() >= height && incomplete_ok {
|
||||
// Check whether we've seen the first line of the
|
||||
// selected message and can fill the screen.
|
||||
break;
|
||||
@@ -1198,7 +1346,16 @@ impl<'a> StatefulWidget for Scrollback<'a> {
|
||||
y += 1;
|
||||
}
|
||||
|
||||
let first_key = info.messages.first_key_value().map(|f| f.0.clone());
|
||||
if self.room_focused &&
|
||||
settings.tunables.read_receipt_send &&
|
||||
state.cursor.timestamp.is_none()
|
||||
{
|
||||
// If the cursor is at the last message, then update the read marker.
|
||||
info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone());
|
||||
}
|
||||
|
||||
// Check whether we should load older messages for this room.
|
||||
let first_key = info.messages.first_key_value().map(|(k, _)| k.clone());
|
||||
if first_key == state.viewctx.corner.timestamp {
|
||||
// If the top of the screen is the older message, load more.
|
||||
self.store.application.mark_for_load(state.room_id.clone());
|
||||
@@ -1211,10 +1368,10 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::tests::*;
|
||||
|
||||
#[test]
|
||||
fn test_search_messages() {
|
||||
#[tokio::test]
|
||||
async fn test_search_messages() {
|
||||
let room_id = TEST_ROOM1_ID.clone();
|
||||
let mut store = mock_store();
|
||||
let mut store = mock_store().await;
|
||||
let mut scrollback = ScrollbackState::new(room_id.clone());
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
@@ -1255,9 +1412,9 @@ mod tests {
|
||||
assert_eq!(scrollback.cursor, MSG1_KEY.clone().into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_movement() {
|
||||
let mut store = mock_store();
|
||||
#[tokio::test]
|
||||
async fn test_movement() {
|
||||
let mut store = mock_store().await;
|
||||
let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone());
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
@@ -1289,9 +1446,9 @@ mod tests {
|
||||
assert_eq!(scrollback.cursor, MSG1_KEY.clone().into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dirscroll() {
|
||||
let mut store = mock_store();
|
||||
#[tokio::test]
|
||||
async fn test_dirscroll() {
|
||||
let mut store = mock_store().await;
|
||||
let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone());
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
@@ -1305,10 +1462,11 @@ mod tests {
|
||||
assert_eq!(scrollback.viewctx.dimensions, (0, 0));
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::latest());
|
||||
|
||||
// Set a terminal width of 60, and height of 3, rendering in scrollback as:
|
||||
// Set a terminal width of 60, and height of 4, rendering in scrollback as:
|
||||
//
|
||||
// |------------------------------------------------------------|
|
||||
// MSG2: | @user2:example.com helium |
|
||||
// MSG2: | Wednesday, December 31 1969 |
|
||||
// | @user2:example.com helium |
|
||||
// MSG3: | @user2:example.com this |
|
||||
// | is |
|
||||
// | a |
|
||||
@@ -1316,14 +1474,15 @@ mod tests {
|
||||
// | message |
|
||||
// MSG4: | @user1:example.com help |
|
||||
// MSG5: | @user2:example.com character |
|
||||
// MSG1: | @user1:example.com writhe |
|
||||
// MSG1: | XXXday, Month NN 20XX |
|
||||
// | @user1:example.com writhe |
|
||||
// |------------------------------------------------------------|
|
||||
let area = Rect::new(0, 0, 60, 3);
|
||||
let area = Rect::new(0, 0, 60, 4);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
scrollback.draw(area, &mut buffer, true, &mut store);
|
||||
|
||||
assert_eq!(scrollback.cursor, MessageCursor::latest());
|
||||
assert_eq!(scrollback.viewctx.dimensions, (60, 3));
|
||||
assert_eq!(scrollback.viewctx.dimensions, (60, 4));
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG4_KEY.clone(), 0));
|
||||
|
||||
// Scroll up a line at a time until we hit the first message.
|
||||
@@ -1352,6 +1511,11 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 0));
|
||||
|
||||
scrollback
|
||||
.dirscroll(prev, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 1));
|
||||
|
||||
scrollback
|
||||
.dirscroll(prev, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
@@ -1364,6 +1528,11 @@ mod tests {
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 0));
|
||||
|
||||
// Now scroll back down one line at a time.
|
||||
scrollback
|
||||
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG2_KEY.clone(), 1));
|
||||
|
||||
scrollback
|
||||
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
@@ -1404,28 +1573,33 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 0));
|
||||
|
||||
scrollback
|
||||
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 1));
|
||||
|
||||
// Cannot scroll down any further.
|
||||
scrollback
|
||||
.dirscroll(next, ScrollSize::Cell, &1.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 0));
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG1_KEY.clone(), 1));
|
||||
|
||||
// Scroll up two Pages (six lines).
|
||||
// Scroll up two Pages (eight lines).
|
||||
scrollback
|
||||
.dirscroll(prev, ScrollSize::Page, &2.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 1));
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 0));
|
||||
|
||||
// Scroll down two HalfPages (three lines).
|
||||
// Scroll down two HalfPages (four lines).
|
||||
scrollback
|
||||
.dirscroll(next, ScrollSize::HalfPage, &2.into(), &ctx, &mut store)
|
||||
.unwrap();
|
||||
assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cursorpos() {
|
||||
let mut store = mock_store();
|
||||
#[tokio::test]
|
||||
async fn test_cursorpos() {
|
||||
let mut store = mock_store().await;
|
||||
let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone());
|
||||
let ctx = ProgramContext::default();
|
||||
|
||||
@@ -1435,7 +1609,8 @@ mod tests {
|
||||
// Set a terminal width of 60, and height of 3, rendering in scrollback as:
|
||||
//
|
||||
// |------------------------------------------------------------|
|
||||
// MSG2: | @user2:example.com helium |
|
||||
// MSG2: | Wednesday, December 31 1969 |
|
||||
// | @user2:example.com helium |
|
||||
// MSG3: | @user2:example.com this |
|
||||
// | is |
|
||||
// | a |
|
||||
@@ -1443,7 +1618,8 @@ mod tests {
|
||||
// | message |
|
||||
// MSG4: | @user1:example.com help |
|
||||
// MSG5: | @user2:example.com character |
|
||||
// MSG1: | @user1:example.com writhe |
|
||||
// MSG1: | XXXday, Month NN 20XX |
|
||||
// | @user1:example.com writhe |
|
||||
// |------------------------------------------------------------|
|
||||
let area = Rect::new(0, 0, 60, 3);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use matrix_sdk::{
|
||||
room::Room as MatrixRoom,
|
||||
ruma::{OwnedRoomId, RoomId},
|
||||
};
|
||||
|
||||
use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
|
||||
use modalkit::tui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
style::{Color, Style},
|
||||
text::{Span, Spans, Text},
|
||||
widgets::StatefulWidget,
|
||||
};
|
||||
|
||||
use modalkit::{
|
||||
widgets::list::{List, ListState},
|
||||
@@ -16,10 +23,13 @@ use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus};
|
||||
|
||||
use crate::windows::RoomItem;
|
||||
|
||||
const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);
|
||||
|
||||
pub struct SpaceState {
|
||||
room_id: OwnedRoomId,
|
||||
room: MatrixRoom,
|
||||
list: ListState<RoomItem, IambInfo>,
|
||||
last_fetch: Option<Instant>,
|
||||
}
|
||||
|
||||
impl SpaceState {
|
||||
@@ -27,8 +37,15 @@ impl SpaceState {
|
||||
let room_id = room.room_id().to_owned();
|
||||
let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback);
|
||||
let list = ListState::new(content, vec![]);
|
||||
let last_fetch = None;
|
||||
|
||||
SpaceState { room_id, room, list }
|
||||
SpaceState { room_id, room, list, last_fetch }
|
||||
}
|
||||
|
||||
pub fn refresh_room(&mut self, store: &mut ProgramStore) {
|
||||
if let Some(room) = store.application.worker.client.get_room(self.id()) {
|
||||
self.room = room;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn room(&self) -> &MatrixRoom {
|
||||
@@ -44,6 +61,7 @@ impl SpaceState {
|
||||
room_id: self.room_id.clone(),
|
||||
room: self.room.clone(),
|
||||
list: self.list.dup(store),
|
||||
last_fetch: self.last_fetch,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,14 +106,26 @@ impl<'a> StatefulWidget for Space<'a> {
|
||||
type State = SpaceState;
|
||||
|
||||
fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
|
||||
let members = self.store.application.worker.space_members(state.room_id.clone()).unwrap();
|
||||
let mut empty_message = None;
|
||||
let need_fetch = match state.last_fetch {
|
||||
Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE,
|
||||
None => true,
|
||||
};
|
||||
|
||||
if need_fetch {
|
||||
let res = self.store.application.worker.space_members(state.room_id.clone());
|
||||
|
||||
match res {
|
||||
Ok(members) => {
|
||||
let items = members
|
||||
.into_iter()
|
||||
.filter_map(|id| {
|
||||
let (room, name) = self.store.application.worker.get_room(id.clone()).ok()?;
|
||||
let (room, _, tags) =
|
||||
self.store.application.worker.get_room(id.clone()).ok()?;
|
||||
let room_info = std::sync::Arc::new((room, tags));
|
||||
|
||||
if id != state.room_id {
|
||||
Some(RoomItem::new(room, name, self.store))
|
||||
Some(RoomItem::new(room_info, self.store))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -103,9 +133,25 @@ impl<'a> StatefulWidget for Space<'a> {
|
||||
.collect();
|
||||
|
||||
state.list.set(items);
|
||||
state.last_fetch = Some(Instant::now());
|
||||
},
|
||||
Err(e) => {
|
||||
let lines = vec![
|
||||
Spans::from("Unable to fetch space room hierarchy:"),
|
||||
Span::styled(e.to_string(), Style::default().fg(Color::Red)).into(),
|
||||
];
|
||||
|
||||
List::new(self.store)
|
||||
.focus(self.focused)
|
||||
.render(area, buffer, &mut state.list)
|
||||
empty_message = Text { lines }.into();
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let mut list = List::new(self.store).focus(self.focused);
|
||||
|
||||
if let Some(text) = empty_message {
|
||||
list = list.empty_message(text);
|
||||
}
|
||||
|
||||
list.render(area, buffer, &mut state.list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
879
src/worker.rs
879
src/worker.rs
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user