refactoring: more refactoring and fixing doctests

This commit is contained in:
Alexandre Pasmantier 2024-11-10 01:03:12 +01:00
parent ae938dcfc0
commit 8dd7f23734
21 changed files with 61 additions and 63 deletions

View File

@ -8,7 +8,7 @@ use tracing::{debug, info};
use crate::app::App;
use crate::cli::Cli;
use television_channels::channels::stdin::Channel as StdinChannel;
use television_utils::utils::is_readable_stdin;
use television_utils::stdin::is_readable_stdin;
pub mod action;
pub mod app;

View File

@ -1,6 +1,6 @@
use crate::ui::input::Input;
use ratatui::widgets::ListState;
use television_utils::utils::strings::EMPTY_STRING;
use television_utils::strings::EMPTY_STRING;
#[derive(Debug)]
pub struct Picker {

View File

@ -17,7 +17,7 @@ use television_channels::channels::{
use television_channels::entry::{Entry, ENTRY_PLACEHOLDER};
use television_previewers::previewers;
use television_previewers::previewers::Previewer;
use television_utils::utils::strings::EMPTY_STRING;
use television_utils::strings::EMPTY_STRING;
use tokio::sync::mpsc::UnboundedSender;
#[derive(

View File

@ -14,7 +14,7 @@ use television_channels::entry::Entry;
use television_previewers::previewers::{
Preview, PreviewContent, FILE_TOO_LARGE_MSG, PREVIEW_NOT_SUPPORTED_MSG,
};
use television_utils::utils::strings::{shrink_with_ellipsis, EMPTY_STRING};
use television_utils::strings::{shrink_with_ellipsis, EMPTY_STRING};
// preview
pub const DEFAULT_PREVIEW_TITLE_FG: Color = Color::Blue;

View File

@ -11,7 +11,7 @@ use ratatui::Frame;
use std::str::FromStr;
use television_channels::channels::OnAir;
use television_channels::entry::Entry;
use television_utils::utils::strings::{
use television_utils::strings::{
next_char_boundary, slice_at_char_boundaries,
};

View File

@ -3,8 +3,8 @@ use crate::entry::Entry;
use crate::entry::PreviewType;
use devicons::FileIcon;
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::utils::indices::sep_name_and_value_indices;
use television_utils::utils::strings::preprocess_line;
use television_utils::indices::sep_name_and_value_indices;
use television_utils::strings::preprocess_line;
use tracing::debug;
#[derive(Debug, Clone)]

View File

@ -3,8 +3,8 @@ use devicons::FileIcon;
use super::OnAir;
use crate::entry::{Entry, PreviewType};
use television_fuzzy::matcher::{config::Config, Matcher};
use television_utils::utils::indices::sep_name_and_value_indices;
use television_utils::utils::strings::preprocess_line;
use television_utils::indices::sep_name_and_value_indices;
use television_utils::strings::preprocess_line;
#[derive(Debug, Clone)]
struct EnvVar {

View File

@ -4,8 +4,8 @@ use devicons::FileIcon;
use std::collections::HashSet;
use std::path::PathBuf;
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::utils::files::{walk_builder, DEFAULT_NUM_THREADS};
use television_utils::utils::strings::preprocess_line;
use television_utils::files::{walk_builder, DEFAULT_NUM_THREADS};
use television_utils::strings::preprocess_line;
pub struct Channel {
matcher: Matcher<String>,

View File

@ -8,8 +8,8 @@ use tracing::debug;
use crate::channels::OnAir;
use crate::entry::{Entry, PreviewType};
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::utils::files::{walk_builder, DEFAULT_NUM_THREADS};
use television_utils::utils::strings::preprocess_line;
use television_utils::files::{walk_builder, DEFAULT_NUM_THREADS};
use television_utils::strings::preprocess_line;
pub struct Channel {
matcher: Matcher<String>,

View File

@ -6,7 +6,7 @@ use devicons::FileIcon;
use super::OnAir;
use crate::entry::{Entry, PreviewType};
use television_fuzzy::matcher::{config::Config, Matcher};
use television_utils::utils::strings::preprocess_line;
use television_utils::strings::preprocess_line;
pub struct Channel {
matcher: Matcher<String>,

View File

@ -9,12 +9,12 @@ use std::{
sync::{atomic::AtomicUsize, Arc},
};
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
use television_utils::utils::strings::{
proportion_of_printable_ascii_characters, PRINTABLE_ASCII_THRESHOLD,
use television_utils::files::{
is_not_text, walk_builder, DEFAULT_NUM_THREADS,
};
use television_utils::utils::{
files::{is_not_text, walk_builder, DEFAULT_NUM_THREADS},
strings::preprocess_line,
use television_utils::strings::{
preprocess_line, proportion_of_printable_ascii_characters,
PRINTABLE_ASCII_THRESHOLD,
};
use tracing::{debug, warn};

View File

@ -31,8 +31,8 @@ impl Entry {
///
/// Additional fields can be set using the builder pattern.
/// ```
/// use television::entry::{Entry, PreviewType};
/// use television::devicons::FileIcon;
/// use television_channels::entry::{Entry, PreviewType};
/// use devicons::FileIcon;
///
/// let entry = Entry::new("name".to_string(), PreviewType::EnvVar)
/// .with_display_name("display_name".to_string())

View File

@ -9,7 +9,7 @@ use television_channels::entry::Entry;
use crate::previewers::cache::PreviewCache;
use crate::previewers::{meta, Preview, PreviewContent};
use television_utils::utils::files::walk_builder;
use television_utils::files::walk_builder;
#[derive(Debug, Default)]
pub struct DirectoryPreviewer {

View File

@ -16,13 +16,13 @@ use tracing::{debug, warn};
use super::cache::PreviewCache;
use crate::previewers::{meta, Preview, PreviewContent};
use television_channels::entry;
use television_utils::utils::files::FileType;
use television_utils::utils::files::{get_file_size, is_known_text_extension};
use television_utils::utils::strings::{
use television_utils::files::FileType;
use television_utils::files::{get_file_size, is_known_text_extension};
use television_utils::strings::{
preprocess_line, proportion_of_printable_ascii_characters,
PRINTABLE_ASCII_THRESHOLD,
};
use television_utils::utils::syntax::{self, load_highlighting_assets};
use television_utils::syntax::{self, load_highlighting_assets};
#[derive(Debug, Default)]
pub struct FilePreviewer {

View File

@ -6,7 +6,7 @@ use infer::Infer;
use lazy_static::lazy_static;
use tracing::debug;
use crate::utils::default_num_threads;
use crate::threads::default_num_threads;
lazy_static::lazy_static! {
pub static ref DEFAULT_NUM_THREADS: usize = default_num_threads().into();

View File

@ -1 +1,6 @@
pub mod utils;
pub mod files;
pub mod indices;
pub mod stdin;
pub mod strings;
pub mod syntax;
pub mod threads;

View File

@ -1,10 +1,3 @@
use std::num::NonZeroUsize;
pub mod files;
pub mod indices;
pub mod strings;
pub mod syntax;
/// Heuristic to determine if stdin is readable.
///
/// This is used to determine if we should use the stdin channel.
@ -66,19 +59,3 @@ pub fn is_readable_stdin() -> bool {
!std::io::stdin().is_terminal() && imp()
}
/// Get the number of threads to use by default.
///
/// This will use the number of available threads if possible, but will default to 1 if the number
/// of available threads cannot be determined. It will also never use more than 32 threads to avoid
/// startup overhead.
pub fn default_num_threads() -> NonZeroUsize {
// default to 1 thread if we can't determine the number of available threads
let default = NonZeroUsize::MIN;
// never use more than 32 threads to avoid startup overhead
let limit = NonZeroUsize::new(32).unwrap();
std::thread::available_parallelism()
.unwrap_or(default)
.min(limit)
}

View File

@ -8,7 +8,7 @@ use std::fmt::Write;
///
/// # Examples
/// ```
/// use strings::next_char_boundary;
/// use television_utils::strings::next_char_boundary;
///
/// let s = "Hello, World!";
/// assert_eq!(next_char_boundary(s, 0), 0);
@ -42,7 +42,7 @@ pub fn next_char_boundary(s: &str, start: usize) -> usize {
///
/// # Examples
/// ```
/// use strings::prev_char_boundary;
/// use television_utils::strings::prev_char_boundary;
///
/// let s = "Hello, World!";
/// assert_eq!(prev_char_boundary(s, 0), 0);
@ -69,7 +69,7 @@ pub fn prev_char_boundary(s: &str, start: usize) -> usize {
///
/// # Examples
/// ```
/// use strings::slice_at_char_boundaries;
/// use television_utils::strings::slice_at_char_boundaries;
///
/// let s = "Hello, World!";
/// assert_eq!(slice_at_char_boundaries(s, 0, 0), "");
@ -103,19 +103,18 @@ pub fn slice_at_char_boundaries(
///
/// # Examples
/// ```
/// use strings::slice_up_to_char_boundary;
/// use television_utils::strings::slice_up_to_char_boundary;
///
/// let s = "Hello, World!";
/// assert_eq!(slice_up_to_char_boundary(s, 0), "");
/// assert_eq!(slice_up_to_char_boundary(s, 1), "H");
/// assert_eq!(slice_up_to_char_boundary(s, 13), "Hello, World!");
///
/// let s = "👋
/// 🌍!";
/// let s = "👋\n🌍!";
/// assert_eq!(slice_up_to_char_boundary(s, 0), "");
/// assert_eq!(slice_up_to_char_boundary(s, 1), "👋");
/// assert_eq!(slice_up_to_char_boundary(s, 4), "👋");
/// assert_eq!(slice_up_to_char_boundary(s, 7), "👋🌍");
/// assert_eq!(slice_up_to_char_boundary(s, 7), "👋\n🌍");
/// ```
pub fn slice_up_to_char_boundary(s: &str, byte_index: usize) -> &str {
&s[..next_char_boundary(s, byte_index)]
@ -160,7 +159,7 @@ const APPLICATION_PROGRAM_COMMAND_CHARACTER: char = '\u{009F}';
///
/// # Examples
/// ```
/// use strings::replace_non_printable;
/// use television_utils::strings::replace_non_printable;
///
/// let input = b"Hello, World!";
/// let output = replace_non_printable(input, 2);
@ -237,7 +236,7 @@ pub const PRINTABLE_ASCII_THRESHOLD: f32 = 0.7;
///
/// # Examples
/// ```
/// use strings::proportion_of_printable_ascii_characters;
/// use television_utils::strings::proportion_of_printable_ascii_characters;
///
/// let buffer = b"Hello, World!";
/// let proportion = proportion_of_printable_ascii_characters(buffer);
@ -270,7 +269,7 @@ const MAX_LINE_LENGTH: usize = 300;
///
/// # Examples
/// ```
/// use strings::preprocess_line;
/// use television_utils::strings::preprocess_line;
///
/// let line = "Hello, World!";
/// let processed = preprocess_line(line);
@ -307,11 +306,11 @@ pub fn preprocess_line(line: &str) -> String {
///
/// # Examples
/// ```
/// use strings::shrink_with_ellipsis;
/// use television_utils::strings::shrink_with_ellipsis;
///
/// let s = "Hello, World!";
/// assert_eq!(shrink_with_ellipsis(s, 13), "Hello, World!");
/// assert_eq!(shrink_with_ellipsis(s, 6), "Hed!");
/// assert_eq!(shrink_with_ellipsis(s, 6), "H…!");
/// ```
pub fn shrink_with_ellipsis(s: &str, max_length: usize) -> String {
if s.len() <= max_length {

View File

@ -0,0 +1,17 @@
use std::num::NonZeroUsize;
/// Get the number of threads to use by default.
///
/// This will use the number of available threads if possible, but will default to 1 if the number
/// of available threads cannot be determined. It will also never use more than 32 threads to avoid
/// startup overhead.
pub fn default_num_threads() -> NonZeroUsize {
// default to 1 thread if we can't determine the number of available threads
let default = NonZeroUsize::MIN;
// never use more than 32 threads to avoid startup overhead
let limit = NonZeroUsize::new(32).unwrap();
std::thread::available_parallelism()
.unwrap_or(default)
.min(limit)
}