From 8846234d8ddb0b00a8838eccdd2fd8448f59f764 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 30 Aug 2022 02:10:11 +0200 Subject: [PATCH] Extract links from message --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/ui/euph/links.rs | 13 ++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5082530..c1bdecb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -184,6 +184,7 @@ dependencies = [ "directories", "edit", "euphoxide", + "linkify", "log", "open", "parking_lot", @@ -545,6 +546,15 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linkify" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96dd5884008358112bc66093362197c7248ece00d46624e2cf71e50029f8cff5" +dependencies = [ + "memchr", +] + [[package]] name = "lock_api" version = "0.4.7" diff --git a/Cargo.toml b/Cargo.toml index 5c98f49..a7e184c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ tokio = { version = "1.20.1", features = ["full"] } toml = "0.5.9" unicode-segmentation = "1.9.0" unicode-width = "0.1.9" +linkify = "0.9.0" [dependencies.time] version = "0.3.13" diff --git a/src/ui/euph/links.rs b/src/ui/euph/links.rs index 2512b69..59c23ac 100644 --- a/src/ui/euph/links.rs +++ b/src/ui/euph/links.rs @@ -2,6 +2,7 @@ use std::io; use crossterm::event::KeyCode; use crossterm::style::{ContentStyle, Stylize}; +use linkify::{LinkFinder, LinkKind}; use crate::ui::input::{key, InputEvent, KeyBindingsList, KeyEvent}; use crate::ui::widgets::list::ListState; @@ -23,12 +24,14 @@ pub enum EventResult { impl LinksState { pub fn new(content: &str) -> Self { - // TODO Extract links + let links = LinkFinder::new() + .links(content) + .filter(|l| *l.kind() == LinkKind::Url) + .map(|l| l.as_str().to_string()) + .collect(); + Self { - links: vec![ - "https://example.com/".to_string(), - "https://plugh.de/".to_string(), - ], + links, list: ListState::new(), } }