2024-09-22 16:56:30 +01:00
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
|
|
|
collections::VecDeque,
|
|
|
|
io::{Cursor, Read as _, Seek, SeekFrom, Write as _},
|
|
|
|
};
|
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
use crate::{float::FloatContent, hint::Shortcut};
|
2024-09-22 16:56:30 +01:00
|
|
|
|
2024-09-06 00:39:10 +01:00
|
|
|
use linutil_core::Command;
|
2024-09-22 16:56:30 +01:00
|
|
|
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent};
|
|
|
|
|
2024-08-08 22:12:34 +01:00
|
|
|
use ratatui::{
|
|
|
|
layout::Rect,
|
|
|
|
style::{Style, Stylize},
|
|
|
|
text::Line,
|
2024-09-19 03:00:09 +01:00
|
|
|
widgets::{Block, Borders, Clear, List},
|
2024-08-08 22:12:34 +01:00
|
|
|
Frame,
|
|
|
|
};
|
2024-09-22 16:56:30 +01:00
|
|
|
|
|
|
|
use ansi_to_tui::IntoText;
|
|
|
|
|
|
|
|
use tree_sitter_bash as hl_bash;
|
|
|
|
use tree_sitter_highlight::{self as hl, HighlightEvent};
|
|
|
|
use zips::zip_result;
|
|
|
|
|
2024-08-08 22:12:34 +01:00
|
|
|
pub struct FloatingText {
|
2024-09-22 16:56:30 +01:00
|
|
|
pub src: Vec<String>,
|
|
|
|
max_line_width: usize,
|
|
|
|
v_scroll: usize,
|
|
|
|
h_scroll: usize,
|
2024-09-23 03:31:52 +01:00
|
|
|
mode_title: String,
|
2024-09-22 16:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! style {
|
|
|
|
($r:literal, $g:literal, $b:literal) => {{
|
|
|
|
use anstyle::{Color, RgbColor, Style};
|
|
|
|
Style::new().fg_color(Some(Color::Rgb(RgbColor($r, $g, $b))))
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
const SYNTAX_HIGHLIGHT_STYLES: [(&str, anstyle::Style); 8] = [
|
|
|
|
("function", style!(220, 220, 170)), // yellow
|
|
|
|
("string", style!(206, 145, 120)), // brown
|
|
|
|
("property", style!(156, 220, 254)), // light blue
|
|
|
|
("comment", style!(92, 131, 75)), // green
|
|
|
|
("embedded", style!(206, 145, 120)), // blue (string expansions)
|
|
|
|
("constant", style!(79, 193, 255)), // dark blue
|
|
|
|
("keyword", style!(197, 134, 192)), // magenta
|
|
|
|
("number", style!(181, 206, 168)), // light green
|
|
|
|
];
|
|
|
|
|
|
|
|
fn get_highlighted_string(s: &str) -> Option<String> {
|
|
|
|
let mut hl_conf = hl::HighlightConfiguration::new(
|
|
|
|
hl_bash::LANGUAGE.into(),
|
|
|
|
"bash",
|
|
|
|
hl_bash::HIGHLIGHT_QUERY,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
let matched_tokens = &SYNTAX_HIGHLIGHT_STYLES
|
|
|
|
.iter()
|
|
|
|
.map(|hl| hl.0)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
hl_conf.configure(matched_tokens);
|
|
|
|
|
|
|
|
let mut hl = hl::Highlighter::new();
|
|
|
|
|
|
|
|
let mut style_stack = vec![anstyle::Style::new()];
|
|
|
|
let src = s.as_bytes();
|
|
|
|
|
|
|
|
let events = hl.highlight(&hl_conf, src, None, |_| None).ok()?;
|
|
|
|
|
|
|
|
let mut buf = Cursor::new(vec![]);
|
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event.unwrap() {
|
|
|
|
HighlightEvent::HighlightStart(h) => {
|
|
|
|
style_stack.push(SYNTAX_HIGHLIGHT_STYLES.get(h.0)?.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
HighlightEvent::HighlightEnd => {
|
|
|
|
style_stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
HighlightEvent::Source { start, end } => {
|
|
|
|
let style = style_stack.last()?;
|
|
|
|
zip_result!(
|
|
|
|
write!(&mut buf, "{}", style),
|
|
|
|
buf.write_all(&src[start..end]),
|
|
|
|
write!(&mut buf, "{style:#}"),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
zip_result!(
|
|
|
|
buf.seek(SeekFrom::Start(0)),
|
|
|
|
buf.read_to_string(&mut output),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Some(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! max_width {
|
|
|
|
($($lines:tt)+) => {{
|
|
|
|
$($lines)+.iter().fold(0, |accum, val| accum.max(val.len()))
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get_lines(s: &str) -> Vec<&str> {
|
|
|
|
s.lines().collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get_lines_owned(s: &str) -> Vec<String> {
|
|
|
|
get_lines(s).iter().map(|s| s.to_string()).collect()
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FloatingText {
|
2024-09-23 03:31:52 +01:00
|
|
|
pub fn new(text: String, title: &str) -> Self {
|
2024-09-22 16:56:30 +01:00
|
|
|
let src = get_lines(&text)
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let max_line_width = max_width!(src);
|
2024-09-19 01:17:08 +01:00
|
|
|
Self {
|
2024-09-22 16:56:30 +01:00
|
|
|
src,
|
2024-09-23 03:31:52 +01:00
|
|
|
mode_title: title.to_string(),
|
2024-09-22 16:56:30 +01:00
|
|
|
max_line_width,
|
|
|
|
v_scroll: 0,
|
|
|
|
h_scroll: 0,
|
2024-09-19 01:17:08 +01:00
|
|
|
}
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|
|
|
|
|
2024-09-23 03:31:52 +01:00
|
|
|
pub fn from_command(command: &Command, title: String) -> Option<Self> {
|
2024-09-22 16:56:30 +01:00
|
|
|
let (max_line_width, src) = match command {
|
2024-08-09 10:22:19 +01:00
|
|
|
Command::Raw(cmd) => {
|
2024-09-22 16:56:30 +01:00
|
|
|
// just apply highlights directly
|
|
|
|
(max_width!(get_lines(cmd)), Some(cmd.clone()))
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-22 17:50:43 +01:00
|
|
|
Command::LocalFile { file, .. } => {
|
2024-09-22 16:56:30 +01:00
|
|
|
// have to read from tmp dir to get cmd src
|
2024-09-22 17:50:43 +01:00
|
|
|
let raw = std::fs::read_to_string(file)
|
|
|
|
.map_err(|_| format!("File not found: {:?}", file))
|
2024-08-09 10:22:19 +01:00
|
|
|
.unwrap();
|
2024-09-22 16:56:30 +01:00
|
|
|
|
|
|
|
(max_width!(get_lines(&raw)), Some(raw))
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-22 16:56:30 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
// If command is a folder, we don't display a preview
|
2024-09-22 16:56:30 +01:00
|
|
|
Command::None => (0usize, None),
|
2024-08-09 10:22:19 +01:00
|
|
|
};
|
2024-09-22 16:56:30 +01:00
|
|
|
|
|
|
|
let src = get_lines_owned(&get_highlighted_string(&src?)?);
|
|
|
|
|
|
|
|
Some(Self {
|
|
|
|
src,
|
2024-09-23 03:31:52 +01:00
|
|
|
mode_title: title,
|
2024-09-22 16:56:30 +01:00
|
|
|
max_line_width,
|
|
|
|
h_scroll: 0,
|
|
|
|
v_scroll: 0,
|
|
|
|
})
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
2024-08-08 22:12:34 +01:00
|
|
|
fn scroll_down(&mut self) {
|
2024-09-22 16:56:30 +01:00
|
|
|
if self.v_scroll + 1 < self.src.len() {
|
|
|
|
self.v_scroll += 1;
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll_up(&mut self) {
|
2024-09-22 16:56:30 +01:00
|
|
|
if self.v_scroll > 0 {
|
|
|
|
self.v_scroll -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll_left(&mut self) {
|
|
|
|
if self.h_scroll > 0 {
|
|
|
|
self.h_scroll -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll_right(&mut self) {
|
|
|
|
if self.h_scroll + 1 < self.max_line_width {
|
|
|
|
self.h_scroll += 1;
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FloatContent for FloatingText {
|
|
|
|
fn draw(&mut self, frame: &mut Frame, area: Rect) {
|
|
|
|
// Define the Block with a border and background color
|
|
|
|
let block = Block::default()
|
|
|
|
.borders(Borders::ALL)
|
2024-09-23 03:31:52 +01:00
|
|
|
.title(self.mode_title.clone())
|
2024-09-19 01:17:08 +01:00
|
|
|
.title_alignment(ratatui::layout::Alignment::Center)
|
|
|
|
.title_style(Style::default().reversed())
|
2024-08-08 22:12:34 +01:00
|
|
|
.style(Style::default());
|
|
|
|
|
|
|
|
// Draw the Block first
|
|
|
|
frame.render_widget(block.clone(), area);
|
|
|
|
|
|
|
|
// Calculate the inner area to ensure text is not drawn over the border
|
|
|
|
let inner_area = block.inner(area);
|
2024-09-22 16:56:30 +01:00
|
|
|
let Rect { height, .. } = inner_area;
|
|
|
|
let lines = self
|
|
|
|
.src
|
2024-08-08 22:12:34 +01:00
|
|
|
.iter()
|
2024-09-22 16:56:30 +01:00
|
|
|
.skip(self.v_scroll)
|
|
|
|
.take(height as usize)
|
|
|
|
.flat_map(|l| l.into_text().unwrap())
|
|
|
|
.map(|line| {
|
|
|
|
let mut skipped = 0;
|
|
|
|
let mut spans = line
|
|
|
|
.into_iter()
|
|
|
|
.skip_while(|span| {
|
|
|
|
let skip = (skipped + span.content.len()) <= self.h_scroll;
|
|
|
|
if skip {
|
|
|
|
skipped += span.content.len();
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<VecDeque<_>>();
|
|
|
|
|
|
|
|
if spans.is_empty() {
|
|
|
|
Line::raw(Cow::Owned(String::new()))
|
|
|
|
} else {
|
|
|
|
if skipped < self.h_scroll {
|
|
|
|
let to_split = spans.pop_front().unwrap();
|
|
|
|
let new_content = to_split.content.clone().into_owned()
|
|
|
|
[self.h_scroll - skipped..]
|
|
|
|
.to_owned();
|
|
|
|
spans.push_front(to_split.content(Cow::Owned(new_content)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Line::from(Vec::from(spans))
|
2024-08-26 23:13:48 +01:00
|
|
|
}
|
2024-08-26 18:48:29 +01:00
|
|
|
})
|
2024-09-22 16:56:30 +01:00
|
|
|
.collect::<Vec<_>>();
|
2024-08-08 22:12:34 +01:00
|
|
|
|
|
|
|
// Create list widget
|
|
|
|
let list = List::new(lines)
|
|
|
|
.block(Block::default())
|
|
|
|
.highlight_style(Style::default().reversed());
|
|
|
|
|
2024-09-19 03:00:09 +01:00
|
|
|
// Clear the text underneath the floats rendered area
|
|
|
|
frame.render_widget(Clear, inner_area);
|
|
|
|
|
2024-08-08 22:12:34 +01:00
|
|
|
// Render the list inside the bordered area
|
|
|
|
frame.render_widget(list, inner_area);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
|
2024-09-22 16:56:30 +01:00
|
|
|
use KeyCode::*;
|
2024-08-08 22:12:34 +01:00
|
|
|
match key.code {
|
2024-09-22 16:56:30 +01:00
|
|
|
Down | Char('j') => self.scroll_down(),
|
|
|
|
Up | Char('k') => self.scroll_up(),
|
|
|
|
Left | Char('h') => self.scroll_left(),
|
|
|
|
Right | Char('l') => self.scroll_right(),
|
2024-08-09 10:22:19 +01:00
|
|
|
_ => {}
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
false
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_finished(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2024-09-06 22:36:12 +01:00
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
|
|
|
|
(
|
2024-10-03 21:07:12 +01:00
|
|
|
&self.mode_title,
|
2024-09-28 20:05:19 +01:00
|
|
|
Box::new([
|
|
|
|
Shortcut::new("Scroll down", ["j", "Down"]),
|
|
|
|
Shortcut::new("Scroll up", ["k", "Up"]),
|
|
|
|
Shortcut::new("Scroll left", ["h", "Left"]),
|
|
|
|
Shortcut::new("Scroll right", ["l", "Right"]),
|
2024-09-30 21:12:16 +01:00
|
|
|
Shortcut::new("Close window", ["Enter", "p", "q", "d", "g"]),
|
2024-09-28 20:05:19 +01:00
|
|
|
]),
|
|
|
|
)
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
2024-08-08 22:12:34 +01:00
|
|
|
}
|