Skip to content

Commit

Permalink
new channel transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 31, 2024
1 parent e6c5965 commit 7045295
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 69 deletions.
24 changes: 0 additions & 24 deletions .config/config.json5

This file was deleted.

8 changes: 4 additions & 4 deletions .config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ ctrl-d = "ScrollPreviewHalfPageDown"
ctrl-u = "ScrollPreviewHalfPageUp"
enter = "SelectEntry"
ctrl-y = "CopyEntryToClipboard"
ctrl-s = "ToggleRemoteControl"
alt-s = "ToggleSendToChannel"
ctrl-r = "ToggleRemoteControl"
ctrl-s = "ToggleSendToChannel"

[keybindings.RemoteControl]
esc = "Quit"
Expand All @@ -18,7 +18,7 @@ up = "SelectPrevEntry"
ctrl-n = "SelectNextEntry"
ctrl-p = "SelectPrevEntry"
enter = "SelectEntry"
ctrl-s = "ToggleRemoteControl"
ctrl-r = "ToggleRemoteControl"

[keybindings.SendToChannel]
esc = "Quit"
Expand All @@ -27,4 +27,4 @@ up = "SelectPrevEntry"
ctrl-n = "SelectNextEntry"
ctrl-p = "SelectPrevEntry"
enter = "SelectEntry"
alt-s = "ToggleSendToChannel"
ctrl-s = "ToggleSendToChannel"
3 changes: 0 additions & 3 deletions .envrc

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "television"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
description = "The revolution will be televised."
license = "MIT"
Expand Down Expand Up @@ -87,7 +87,7 @@ debug = true


[profile.release]
lto = "fat"
lto = "thin"

[target.'cfg(target_os = "macos")'.dependencies]
crossterm = { version = "0.28.1", features = ["serde", "use-dev-tty"] }
3 changes: 2 additions & 1 deletion crates/television/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ macro_rules! define_transitions {
}

define_transitions! {
Files => [Text],
Text => [Files, Text],
Files => [Files, Text],
GitRepos => [Files, Text],
}

Expand Down
2 changes: 1 addition & 1 deletion crates/television/channels/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl OnAir for Channel {
.matched_items(
offset
..(num_entries + offset)
.min(snapshot.matched_item_count()),
.min(snapshot.matched_item_count()),
)
.map(move |item| {
snapshot.pattern().column_pattern(0).indices(
Expand Down
38 changes: 29 additions & 9 deletions crates/television/channels/files.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use devicons::FileIcon;
use nucleo::{
pattern::{CaseMatching, Normalization},
Config, Injector, Nucleo,
};
use std::{path::PathBuf, sync::Arc};

use super::{OnAir, TelevisionChannel};
use crate::entry::Entry;
use crate::fuzzy::MATCHER;
use crate::previewers::PreviewType;
use crate::utils::files::{walk_builder, DEFAULT_NUM_THREADS};
use crate::utils::strings::preprocess_line;
use devicons::FileIcon;
use nucleo::{
pattern::{CaseMatching, Normalization},
Config, Injector, Nucleo,
};
use std::collections::HashSet;
use std::{path::PathBuf, sync::Arc};

pub struct Channel {
matcher: Nucleo<String>,
Expand All @@ -19,8 +19,8 @@ pub struct Channel {
total_count: u32,
running: bool,
crawl_handle: tokio::task::JoinHandle<()>,
// PERF: cache results (to make deleting characters smoother) but like
// a shallow cache (maybe more like a stack actually? so we just pop result sets)
// PERF: cache results (to make deleting characters smoother) with
// a shallow stack of sub-patterns as keys (e.g. "a", "ab", "abc")
}

impl Channel {
Expand Down Expand Up @@ -64,6 +64,26 @@ impl From<&mut TelevisionChannel> for Channel {
.collect(),
)
}
c @ TelevisionChannel::Files(_) => {
let entries = c.results(c.result_count(), 0);
Self::new(
entries
.iter()
.map(|entry| PathBuf::from(entry.name.clone()))
.collect(),
)
}
c @ TelevisionChannel::Text(_) => {
let entries = c.results(c.result_count(), 0);
Self::new(
entries
.iter()
.map(|entry| PathBuf::from(entry.display_name()))
.collect::<HashSet<_>>()
.into_iter()
.collect(),
)
}
_ => unreachable!(),
}
}
Expand Down
42 changes: 42 additions & 0 deletions crates/television/channels/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ impl Channel {
}
}

fn from_text_entries(entries: Vec<Entry>) -> Self {
let matcher = Nucleo::new(
Config::DEFAULT.match_paths(),
Arc::new(|| {}),
None,
1,
);
let injector = matcher.injector();
let load_handle = tokio::spawn(async move {
let mut lines_in_mem = 0;
for entry in entries {
if lines_in_mem > MAX_LINES_IN_MEM {
break;
}
injector.push(
CandidateLine::new(
entry.display_name().into(),
entry.value.unwrap(),
entry.line_number.unwrap(),
),
|c, cols| {
cols[0] = c.line.clone().into();
},
);
lines_in_mem += 1;
}
});

Channel {
matcher,
last_pattern: String::new(),
result_count: 0,
total_count: 0,
running: false,
crawl_handle: load_handle,
}
}

const MATCHER_TICK_TIMEOUT: u64 = 2;
}

Expand Down Expand Up @@ -148,6 +186,10 @@ impl From<&mut TelevisionChannel> for Channel {
.collect(),
)
}
c @ TelevisionChannel::Text(_) => {
let entries = c.results(c.result_count(), 0);
Self::from_text_entries(entries)
}
_ => unreachable!(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/television/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ impl Picker {
self.view_offset = self.view_offset.saturating_sub(1);
}
} else {
self.view_offset = total_items.saturating_sub(height.saturating_sub(2));
self.view_offset =
total_items.saturating_sub(height.saturating_sub(2));
self.select(Some(total_items.saturating_sub(1)));
self.relative_select(Some(height.saturating_sub(3)));
}
Expand Down
12 changes: 6 additions & 6 deletions crates/television/ui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl Television {
.fg(crate::television::DEFAULT_INPUT_FG)
.bold(),
))
.block(arrow_block);
.block(arrow_block);
f.render_widget(arrow, inner_input_chunks[0]);

let interactive_input_block = Block::default();
Expand Down Expand Up @@ -500,8 +500,8 @@ impl Television {
.fg(crate::television::DEFAULT_RESULTS_COUNT_FG)
.italic(),
))
.block(result_count_block)
.alignment(Alignment::Right);
.block(result_count_block)
.alignment(Alignment::Right);
f.render_widget(result_count_paragraph, inner_input_chunks[2]);

// Make the cursor visible and ask tui-rs to put it at the
Expand All @@ -510,9 +510,9 @@ impl Television {
// Put cursor past the end of the input text
inner_input_chunks[1].x
+ u16::try_from(
self.results_picker.input.visual_cursor().max(scroll)
- scroll,
)?,
self.results_picker.input.visual_cursor().max(scroll)
- scroll,
)?,
// Move one line down, from the border to the input line
inner_input_chunks[1].y,
));
Expand Down
6 changes: 3 additions & 3 deletions crates/television/ui/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ impl Television {
self.preview_scroll.unwrap_or(0),
self.preview_pane_height,
)
.block(preview_block)
.alignment(Alignment::Left)
.scroll((self.preview_scroll.unwrap_or(0), 0))
.block(preview_block)
.alignment(Alignment::Left)
.scroll((self.preview_scroll.unwrap_or(0), 0))
}
// meta
PreviewContent::Loading => self
Expand Down
10 changes: 5 additions & 5 deletions crates/television/ui/remote_control.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::channels::OnAir;
use crate::television::Television;
use crate::ui::BORDER_COLOR;
use crate::ui::logo::build_remote_logo_paragraph;
use crate::ui::mode::mode_color;
use crate::ui::results::{build_results_list, ResultsListColors};
use crate::ui::BORDER_COLOR;
use color_eyre::eyre::Result;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::prelude::Style;
Expand All @@ -28,7 +28,7 @@ impl Television {
Constraint::Length(3),
Constraint::Length(20),
]
.as_ref(),
.as_ref(),
)
.split(*area);
self.draw_rc_channels(f, &layout[0])?;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Television {
.fg(crate::television::DEFAULT_INPUT_FG)
.bold(),
))
.block(prompt_symbol_block);
.block(prompt_symbol_block);
f.render_widget(arrow, inner_input_chunks[0]);

let interactive_input_block = Block::default();
Expand All @@ -131,8 +131,8 @@ impl Television {
// Put cursor past the end of the input text
inner_input_chunks[1].x
+ u16::try_from(
self.rc_picker.input.visual_cursor().max(scroll) - scroll,
)?,
self.rc_picker.input.visual_cursor().max(scroll) - scroll,
)?,
// Move one line down, from the border to the input line
inner_input_chunks[1].y,
));
Expand Down
12 changes: 6 additions & 6 deletions crates/television/ui/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ where
}
Line::from(spans)
}))
.direction(list_direction)
.highlight_style(
Style::default().bg(results_list_colors.result_selected_bg),
)
.highlight_symbol("> ")
.block(results_block)
.direction(list_direction)
.highlight_style(
Style::default().bg(results_list_colors.result_selected_bg),
)
.highlight_symbol("> ")
.block(results_block)
}

impl Television {
Expand Down
9 changes: 6 additions & 3 deletions crates/television/utils/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ pub fn slice_at_char_boundaries(
start_byte_index: usize,
end_byte_index: usize,
) -> &str {
if start_byte_index > end_byte_index || start_byte_index > s.len() || end_byte_index > s.len() {
if start_byte_index > end_byte_index
|| start_byte_index > s.len()
|| end_byte_index > s.len()
{
return EMPTY_STRING;
}
&s[prev_char_boundary(s, start_byte_index)
Expand Down Expand Up @@ -138,8 +141,8 @@ pub fn preprocess_line(line: &str) -> String {
line
}
}
.trim_end_matches(['\r', '\n', '\0'])
.as_bytes(),
.trim_end_matches(['\r', '\n', '\0'])
.as_bytes(),
TAB_WIDTH,
)
}
Expand Down

0 comments on commit 7045295

Please sign in to comment.