summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2023-05-15 11:50:37 +0200
committerAram Drevekenin <aram@poor.dev>2023-05-15 11:50:37 +0200
commitce091ccb8e112c7a3e3f722daaca2ddf23e190a4 (patch)
treed31ca9060b0eb3415c8b2376f17329f967b02e99
parented9e8c05b97293208bb17ef2284c7f463ae54961 (diff)
style(fmt): rustfmt
-rw-r--r--default-plugins/fixture-plugin-for-tests/src/main.rs18
-rw-r--r--default-plugins/strider/src/main.rs61
-rw-r--r--default-plugins/strider/src/search.rs234
-rw-r--r--default-plugins/strider/src/state.rs22
-rw-r--r--xtask/src/ci.rs5
-rw-r--r--zellij-server/src/logging_pipe.rs2
-rw-r--r--zellij-server/src/plugins/mod.rs26
-rw-r--r--zellij-server/src/plugins/plugin_loader.rs63
-rw-r--r--zellij-server/src/plugins/plugin_map.rs165
-rw-r--r--zellij-server/src/plugins/unit/plugin_tests.rs91
-rw-r--r--zellij-server/src/plugins/wasm_bridge.rs119
-rw-r--r--zellij-server/src/plugins/zellij_exports.rs2
-rw-r--r--zellij-tile/src/lib.rs44
-rw-r--r--zellij-utils/src/consts.rs7
14 files changed, 593 insertions, 266 deletions
diff --git a/default-plugins/fixture-plugin-for-tests/src/main.rs b/default-plugins/fixture-plugin-for-tests/src/main.rs
index 4ef19adfc..e46086ca3 100644
--- a/default-plugins/fixture-plugin-for-tests/src/main.rs
+++ b/default-plugins/fixture-plugin-for-tests/src/main.rs
@@ -1,5 +1,5 @@
+use serde::{Deserialize, Serialize};
use zellij_tile::prelude::*;
-use serde::{Serialize, Deserialize};
// This is a fixture plugin used only for tests in Zellij
// it is not (and should not!) be included in the mainline executable
@@ -14,7 +14,7 @@ struct State {
#[derive(Default, Serialize, Deserialize)]
struct TestWorker {}
-impl <'de> ZellijWorker<'de> for TestWorker {
+impl<'de> ZellijWorker<'de> for TestWorker {
fn on_message(&mut self, message: String, payload: String) {
if message == "ping" {
post_message_to_plugin("pong".into(), payload);
@@ -36,21 +36,20 @@ impl ZellijPlugin for State {
fn update(&mut self, event: Event) -> bool {
match &event {
- Event::CustomMessage(message, payload)=> {
+ Event::CustomMessage(message, payload) => {
if message == "pong" {
self.received_payload = Some(payload.clone());
}
-
- }
+ },
Event::SystemClipboardFailure => {
// this is just to trigger the worker message
post_message_to(
"test",
"ping".to_owned(),
- "gimme_back_my_payload".to_owned()
+ "gimme_back_my_payload".to_owned(),
);
},
- _ => {}
+ _ => {},
}
let should_render = true;
self.received_events.push(event);
@@ -61,7 +60,10 @@ impl ZellijPlugin for State {
if let Some(payload) = self.received_payload.as_ref() {
println!("Payload from worker: {:?}", payload);
} else {
- println!("Rows: {:?}, Cols: {:?}, Received events: {:?}", rows, cols, self.received_events);
+ println!(
+ "Rows: {:?}, Cols: {:?}, Received events: {:?}",
+ rows, cols, self.received_events
+ );
}
}
}
diff --git a/default-plugins/strider/src/main.rs b/default-plugins/strider/src/main.rs
index 1e4ea555e..4f299c508 100644
--- a/default-plugins/strider/src/main.rs
+++ b/default-plugins/strider/src/main.rs
@@ -1,12 +1,12 @@
-mod state;
mod search;
+mod state;
use colored::*;
+use search::{ResultsOfSearch, SearchWorker};
+use serde_json;
use state::{refresh_directory, FsEntry, State, CURRENT_SEARCH_TERM};
-use search::{SearchWorker, ResultsOfSearch};
use std::{cmp::min, time::Instant};
use zellij_tile::prelude::*;
-use serde_json;
register_plugin!(State);
register_worker!(SearchWorker, search_worker);
@@ -41,27 +41,29 @@ impl ZellijPlugin for State {
if self.loading_animation_offset == u8::MAX {
self.loading_animation_offset = 0;
} else {
- self.loading_animation_offset = self.loading_animation_offset.saturating_add(1);
+ self.loading_animation_offset =
+ self.loading_animation_offset.saturating_add(1);
}
}
- }
- Event::CustomMessage(message, payload) => {
- match message.as_str() {
- "update_search_results" => {
- if let Ok(mut results_of_search) = serde_json::from_str::<ResultsOfSearch>(&payload) {
- if Some(results_of_search.search_term) == self.search_term {
- self.search_results = results_of_search.search_results.drain(..).collect();
- should_render = true;
- }
+ },
+ Event::CustomMessage(message, payload) => match message.as_str() {
+ "update_search_results" => {
+ if let Ok(mut results_of_search) =
+ serde_json::from_str::<ResultsOfSearch>(&payload)
+ {
+ if Some(results_of_search.search_term) == self.search_term {
+ self.search_results =
+ results_of_search.search_results.drain(..).collect();
+ should_render = true;
}
- },
- "done_scanning_folder" => {
- self.loading = false;
- should_render = true;
- },
- _ => {}
- }
- }
+ }
+ },
+ "done_scanning_folder" => {
+ self.loading = false;
+ should_render = true;
+ },
+ _ => {},
+ },
Event::Key(key) => match key {
// modes:
// 1. typing_search_term
@@ -69,27 +71,31 @@ impl ZellijPlugin for State {
// 3. normal
Key::Esc | Key::Char('\n') if self.typing_search_term() => {
self.accept_search_term();
- }
+ },
_ if self.typing_search_term() => {
self.append_to_search_term(key);
if let Some(search_term) = self.search_term.as_ref() {
std::fs::write(CURRENT_SEARCH_TERM, search_term.as_bytes()).unwrap();
- post_message_to("search", String::from("search"), String::from(&self.search_term.clone().unwrap()));
+ post_message_to(
+ "search",
+ String::from("search"),
+ String::from(&self.search_term.clone().unwrap()),
+ );
}
should_render = true;
- }
+ },
Key::Esc if self.exploring_search_results() => {
self.stop_exploring_search_results();
should_render = true;
- }
+ },
Key::Char('/') => {
self.start_typing_search_term();
should_render = true;
- }
+ },
Key::Esc => {
self.stop_typing_search_term();
should_render = true;
- }
+ },
Key::Up | Key::Char('k') => {
if self.exploring_search_results() {
self.move_search_selection_up();
@@ -192,7 +198,6 @@ impl ZellijPlugin for State {
}
fn render(&mut self, rows: usize, cols: usize) {
-
if self.typing_search_term() || self.exploring_search_results() {
return self.render_search(rows, cols);
}
diff --git a/default-plugins/strider/src/search.rs b/default-plugins/strider/src/search.rs
index c0617483b..299882eac 100644
--- a/default-plugins/strider/src/search.rs
+++ b/default-plugins/strider/src/search.rs
@@ -1,14 +1,14 @@
-use crate::state::{State, ROOT, CURRENT_SEARCH_TERM};
+use crate::state::{State, CURRENT_SEARCH_TERM, ROOT};
-use zellij_tile::prelude::*;
use unicode_width::UnicodeWidthStr;
+use zellij_tile::prelude::*;
-use walkdir::WalkDir;
-use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;
-use serde::{Serialize, Deserialize};
+use fuzzy_matcher::FuzzyMatcher;
+use serde::{Deserialize, Serialize};
+use walkdir::WalkDir;
-use std::io::{ self, BufRead };
+use std::io::{self, BufRead};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum SearchResult {
@@ -23,7 +23,7 @@ pub enum SearchResult {
line_number: usize,
score: i64,
indices: Vec<usize>,
- }
+ },
}
impl SearchResult {
@@ -31,10 +31,16 @@ impl SearchResult {
SearchResult::File {
path,
score,
- indices
+ indices,
}
}
- pub fn new_file_line(score: i64, indices: Vec<usize>, path: String, line: String, line_number: usize) -> Self {
+ pub fn new_file_line(
+ score: i64,
+ indices: Vec<usize>,
+ path: String,
+ line: String,
+ line_number: usize,
+ ) -> Self {
SearchResult::LineInFile {
path,
score,
@@ -64,52 +70,121 @@ impl SearchResult {
let reset_code = "\u{1b}[m";
let max_width = max_width.saturating_sub(3); // for the UI left line separator
match self {
- SearchResult::File { path, indices, .. } => {
+ SearchResult::File { path, indices, .. } => {
if is_selected {
- let line = self.render_line_with_indices(path, indices, max_width, None, Some(green_code), true);
+ let line = self.render_line_with_indices(
+ path,
+ indices,
+ max_width,
+ None,
+ Some(green_code),
+ true,
+ );
format!("{} | {}{}", green_foreground, reset_code, line)
} else {
- let line = self.render_line_with_indices(path, indices, max_width, None, None, true);
+ let line =
+ self.render_line_with_indices(path, indices, max_width, None, None, true);
format!(" | {}", line)
}
- }
- SearchResult::LineInFile { path, line, line_number, indices, .. } => {
+ },
+ SearchResult::LineInFile {
+ path,
+ line,
+ line_number,
+ indices,
+ ..
+ } => {
if is_selected {
- let first_line = self.render_line_with_indices(path, &vec![], max_width, None, Some(green_code), true);
+ let first_line = self.render_line_with_indices(
+ path,
+ &vec![],
+ max_width,
+ None,
+ Some(green_code),
+ true,
+ );
let line_indication_text = format!("{}-> {}", bold_code, line_number);
- let line_indication = format!("{}{}{}", orange_foreground, line_indication_text, reset_code); // TODO: also truncate
- let second_line = self.render_line_with_indices(line, indices, max_width.saturating_sub(line_indication_text.width()), None, Some(orange_code), false);
- format!(" {}│{} {}\n {}│{} {} {}", green_foreground, reset_code, first_line, green_foreground, reset_code, line_indication, second_line)
+ let line_indication = format!(
+ "{}{}{}",
+ orange_foreground, line_indication_text, reset_code
+ ); // TODO: also truncate
+ let second_line = self.render_line_with_indices(
+ line,
+ indices,
+ max_width.saturating_sub(line_indication_text.width()),
+ None,
+ Some(orange_code),
+ false,
+ );
+ format!(
+ " {}│{} {}\n {}│{} {} {}",
+ green_foreground,
+ reset_code,
+ first_line,
+ green_foreground,
+ reset_code,
+ line_indication,
+ second_line
+ )
} else {
- let first_line = self.render_line_with_indices(path, &vec![], max_width, None, None, true); // TODO:
+ let first_line =
+ self.render_line_with_indices(path, &vec![], max_width, None, None, true); // TODO:
let line_indication_text = format!("{}-> {}", bold_code, line_number);
- let second_line = self.render_line_with_indices(line, indices, max_width.saturating_sub(line_indication_text.width()), None, None, false);
- format!(" │ {}\n │ {} {}", first_line, line_indication_text, second_line)
+ let second_line = self.render_line_with_indices(
+ line,
+ indices,
+ max_width.saturating_sub(line_indication_text.width()),
+ None,
+ None,
+ false,
+ );
+ format!(
+ " │ {}\n │ {} {}",
+ first_line, line_indication_text, second_line
+ )
}
- }
+ },
}
}
- fn render_line_with_indices(&self, line_to_render: &String, indices: &Vec<usize>, max_width: usize, background_color: Option<usize>, foreground_color: Option<usize>, is_bold: bool) -> String {
+ fn render_line_with_indices(
+ &self,
+ line_to_render: &String,
+ indices: &Vec<usize>,
+ max_width: usize,
+ background_color: Option<usize>,
+ foreground_color: Option<usize>,
+ is_bold: bool,
+ ) -> String {
// TODO: get these from Zellij
let reset_code = "\u{1b}[m";
let underline_code = "\u{1b}[4m";
- let foreground_color = foreground_color.map(|c| format!("\u{1b}[38;5;{}m", c)).unwrap_or_else(|| format!(""));
- let background_color = background_color.map(|c| format!("\u{1b}[48;5;{}m", c)).unwrap_or_else(|| format!(""));
+ let foreground_color = foreground_color
+ .map(|c| format!("\u{1b}[38;5;{}m", c))
+ .unwrap_or_else(|| format!(""));
+ let background_color = background_color
+ .map(|c| format!("\u{1b}[48;5;{}m", c))
+ .unwrap_or_else(|| format!(""));
let bold = if is_bold { "\u{1b}[1m" } else { "" };
let non_index_character_style = format!("{}{}{}", background_color, foreground_color, bold);
- let index_character_style = format!("{}{}{}{}", background_color, foreground_color, bold, underline_code);
+ let index_character_style = format!(
+ "{}{}{}{}",
+ background_color, foreground_color, bold, underline_code
+ );
let mut truncate_start_position = None;
let mut truncate_end_position = None;
if line_to_render.width() > max_width {
let length_of_each_half = max_width.saturating_sub(4) / 2;
truncate_start_position = Some(length_of_each_half);
- truncate_end_position = Some(line_to_render.width().saturating_sub(length_of_each_half));
+ truncate_end_position =
+ Some(line_to_render.width().saturating_sub(length_of_each_half));
}
let mut first_half = format!("{}", reset_code);
let mut second_half = format!("{}", reset_code);
for (i, character) in line_to_render.chars().enumerate() {
- if (truncate_start_position.is_none() && truncate_end_position.is_none()) || Some(i) < truncate_start_position {
+ if (truncate_start_position.is_none() && truncate_end_position.is_none())
+ || Some(i) < truncate_start_position
+ {
if indices.contains(&i) {
first_half.push_str(&index_character_style);
first_half.push(character);
@@ -132,7 +207,10 @@ impl SearchResult {
}
}
if let Some(_truncate_start_position) = truncate_start_position {
- format!("{}{}{}[..]{}{}{}", first_half, reset_code, foreground_color, reset_code, second_half, reset_code)
+ format!(
+ "{}{}{}[..]{}{}{}",
+ first_half, reset_code, foreground_color, reset_code, second_half, reset_code
+ )
} else {
format!("{}{}", first_half, reset_code)
}
@@ -153,7 +231,8 @@ impl ResultsOfSearch {
}
}
pub fn limit_search_results(mut self, max_results: usize) -> Self {
- self.search_results.sort_by(|a, b| b.score().cmp(&a.score()));
+ self.search_results
+ .sort_by(|a, b| b.score().cmp(&a.score()));
self.search_results = if self.search_results.len() > max_results {
self.search_results.drain(..max_results).collect()
} else {
@@ -170,31 +249,34 @@ pub struct SearchWorker {
skip_hidden_files: bool,
}
-impl <'de> ZellijWorker<'de> for SearchWorker {
+impl<'de> ZellijWorker<'de> for SearchWorker {
// TODO: handle out of order messages, likely when rendering
fn on_message(&mut self, message: String, payload: String) {
- match message.as_str() { // TODO: deserialize to type
+ match message.as_str() {
+ // TODO: deserialize to type
"scan_folder" => {
self.populate_search_paths();
post_message_to_plugin("done_scanning_folder".into(), "".into());
- }
+ },
"search" => {
let search_term = payload;
let (search_term, matches) = self.search(search_term);
- let search_results = ResultsOfSearch::new(search_term, matches).limit_search_results(100);
- post_message_to_plugin("update_search_results".into(), serde_json::to_string(&search_results).unwrap());
- }
- "skip_hidden_files" => {
- match serde_json::from_str::<bool>(&payload) {
- Ok(should_skip_hidden_files) => {
- self.skip_hidden_files = should_skip_hidden_files;
- },
- Err(e) => {
- eprintln!("Failed to deserialize payload: {:?}", e);
- }
- }
- }
- _ => {}
+ let search_results =
+ ResultsOfSearch::new(search_term, matches).limit_search_results(100);
+ post_message_to_plugin(
+ "update_search_results".into(),
+ serde_json::to_string(&search_results).unwrap(),
+ );
+ },
+ "skip_hidden_files" => match serde_json::from_str::<bool>(&payload) {
+ Ok(should_skip_hidden_files) => {
+ self.skip_hidden_files = should_skip_hidden_files;
+ },
+ Err(e) => {
+ eprintln!("Failed to deserialize payload: {:?}", e);
+ },
+ },
+ _ => {},
}
}
}
@@ -206,7 +288,7 @@ impl SearchWorker {
}
let mut matches = vec![];
let mut matcher = SkimMatcherV2::default().use_cache(true).element_limit(100); // TODO: no hard
- // coded limit!
+ // coded limit!
self.search_file_names(&search_term, &mut matcher, &mut matches);
self.search_file_contents(&search_term, &mut matcher, &mut matches);
@@ -221,7 +303,13 @@ impl SearchWorker {
}
fn populate_search_paths(&mut self) {
for entry in WalkDir::new(ROOT).into_iter().filter_map(|e| e.ok()) {
- if self.skip_hidden_files && entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) {
+ if self.skip_hidden_files
+ && entry
+ .file_name()
+ .to_str()
+ .map(|s| s.starts_with('.'))
+ .unwrap_or(false)
+ {
continue;
}
let file_path = entry.path().display().to_string();
@@ -232,11 +320,15 @@ impl SearchWorker {
for (index, line) in lines.enumerate() {
match line {
Ok(line) => {
- self.search_file_contents.push((file_path.clone(), index + 1, line));
+ self.search_file_contents.push((
+ file_path.clone(),
+ index + 1,
+ line,
+ ));
},
Err(_) => {
break; // probably a binary file, skip it
- }
+ },
}
}
}
@@ -245,17 +337,37 @@ impl SearchWorker {
self.search_paths.push(file_path);
}
}
- fn search_file_names(&self, search_term: &str, matcher: &mut SkimMatcherV2, matches: &mut Vec<SearchResult>) {
+ fn search_file_names(
+ &self,
+ search_term: &str,
+ matcher: &mut SkimMatcherV2,
+ matches: &mut Vec<SearchResult>,
+ ) {
for entry in &self.search_paths {
if let Some((score, indices)) = matcher.fuzzy_indices(&entry, &search_term) {
- matches.push(SearchResult::new_file_name(score, indices, entry.to_owned()));
+ matches.push(SearchResult::new_file_name(
+ score,
+ indices,
+ entry.to_owned(),
+ ));
}
}
}
- fn search_file_contents(&self, search_term: &str, matcher: &mut SkimMatcherV2, matches: &mut Vec<SearchResult>) {
+ fn search_file_contents(
+ &self,
+ search_term: &str,
+ matcher: &mut SkimMatcherV2,
+ matches: &mut Vec<SearchResult>,
+ ) {
for (file_name, line_number, line_entry) in &self.search_file_contents {
if let Some((score, indices)) = matcher.fuzzy_indices(&line_entry, &search_term) {
- matches.push(SearchResult::new_file_line(score, indices, file_name.clone(), line_entry.clone(), *line_number));
+ matches.push(SearchResult::new_file_line(
+ score,
+ indices,
+ file_name.clone(),
+ line_entry.clone(),
+ *line_number,
+ ));
}
}
}
@@ -265,12 +377,20 @@ impl State {
pub fn render_search(&mut self, rows: usize, cols: usize) {
if let Some(search_term) = self.search_term.as_ref() {
let mut to_render = String::new();
- to_render.push_str(&format!(" \u{1b}[38;5;51;1mSEARCH:\u{1b}[m {}\n", search_term));
+ to_render.push_str(&format!(
+ " \u{1b}[38;5;51;1mSEARCH:\u{1b}[m {}\n",
+ search_term
+ ));
let mut rows_left_to_render = rows.saturating_sub(3);
if self.loading && self.search_results.is_empty() {
to_render.push_str(&self.render_loading());
}
- for (i, result) in self.search_results.iter().enumerate().take(rows.saturating_sub(3)) {
+ for (i, result) in self
+ .search_results
+ .iter()
+ .enumerate()
+ .take(rows.saturating_sub(3))
+ {
let result_height = result.rendered_height();
if result_height + 1 > rows_left_to_render {
break;
diff --git a/default-plugins/strider/src/state.rs b/default-plugins/strider/src/state.rs
index 617e7488d..7ced4932c 100644
--- a/default-plugins/strider/src/state.rs
+++ b/default-plugins/strider/src/state.rs
@@ -44,8 +44,8 @@ impl State {
self.typing_search_term = false;
}
}
- }
- _ => {}
+ },
+ _ => {},
}
}
pub fn accept_search_term(&mut self) {
@@ -106,18 +106,28 @@ impl State {
}
pub fn open_search_result(&mut self) {
match self.search_results.get(self.selected_search_result) {
- Some(SearchResult::File{ path, score, indices }) => {
+ Some(SearchResult::File {
+ path,
+ score,
+ indices,
+ }) => {
let file_path = PathBuf::from(path);
open_file(file_path.strip_prefix(ROOT).unwrap());
- }
- Some(SearchResult::LineInFile{ path, score, indices, line, line_number }) => {
+ },
+ Some(SearchResult::LineInFile {
+ path,
+ score,
+ indices,
+ line,
+ line_number,
+ }) => {
let file_path = PathBuf::from(path);
open_file_with_line(file_path.strip_prefix(