Skip to content

Commit

Permalink
formatting and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 12, 2024
1 parent 05f5f2c commit a9092de
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
14 changes: 10 additions & 4 deletions crates/television/components/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ impl Component for Television {
frame.render_widget(arrow, inner_input_chunks[0]);

let interactive_input_block = Block::default();
let width = inner_input_chunks[1].width.max(3) - 3; // keep 2 for borders and 1 for cursor
// keep 2 for borders and 1 for cursor
let width = inner_input_chunks[1].width.max(3) - 3;
let scroll = self.input.visual_scroll(width as usize);
let input = Paragraph::new(self.input.value())
.scroll((0, u16::try_from(scroll).unwrap()))
Expand All @@ -471,7 +472,8 @@ impl Component for Television {
frame.render_widget(result_count, inner_input_chunks[2]);

if let Pane::Input = self.current_pane {
// Make the cursor visible and ask tui-rs to put it at the specified coordinates after rendering
// Make the cursor visible and ask tui-rs to put it at the
// specified coordinates after rendering
frame.set_cursor_position((
// Put cursor past the end of the input text
inner_input_chunks[1].x
Expand Down Expand Up @@ -578,14 +580,18 @@ impl Television {
lines.push(Line::from(vec![
build_line_number_span(i + 1).style(Style::default().fg(
// FIXME: this actually might panic in some edge cases
if matches!(target_line, Some(l) if l == u16::try_from(i).unwrap() + 1)
if matches!(
target_line,
Some(l) if l == u16::try_from(i).unwrap() + 1
)
{
DEFAULT_PREVIEW_GUTTER_SELECTED_FG
} else {
DEFAULT_PREVIEW_GUTTER_FG
},
)),
Span::styled(" │ ", Style::default().fg(DEFAULT_PREVIEW_GUTTER_FG).dim()),
Span::styled(" │ ",
Style::default().fg(DEFAULT_PREVIEW_GUTTER_FG).dim()),
Span::styled(
line.to_string(),
Style::default().fg(DEFAULT_PREVIEW_CONTENT_FG).bg(
Expand Down
53 changes: 53 additions & 0 deletions crates/television_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,32 @@ fn method_name_args(method_name: &str) -> Vec<proc_macro2::Ident> {
}
}

/// A derive macro implementing the `new` method for a `ConcreteChannel`.
/// # Example
/// ```rust
/// use television_derive::ConcreteChannel;
/// use crate::components::finders::EnvVarFinder;
///
/// #[derive(ConcreteChannel)]
/// pub struct EnvChannel {
/// pub finder: EnvVarFinder,
/// }
/// ```
/// This will generate the following implementation:
/// ```rust
/// impl EnvChannel {
/// pub fn new() -> Self {
/// Self {
/// finder: EnvVarFinder::new(),
/// }
/// }
/// }
/// ```
///
/// # Panics
/// The macro will panic if the object is not a struct or if the struct does not have a
/// field named `finder`.
///
#[proc_macro_derive(ConcreteChannel)]
pub fn concrete_channel_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
Expand Down Expand Up @@ -235,6 +261,33 @@ fn impl_concrete_channel(ast: &syn::DeriveInput) -> TokenStream {
.into()
}

/// A derive macro implementing the `From<CliTvChannel>` trait for `TvChannel`.
/// # Example
/// ```rust
/// use television_derive::FromCliChannel;
/// use crate::cli::CliTvChannel;
/// use crate::channels::TvChannel;
///
/// #[derive(FromCliChannel)]
/// pub enum TvChannel {
/// Env(EnvChannel),
/// Files(FilesChannel),
/// }
/// ```
/// This will generate the following implementation:
/// ```rust
/// impl TvChannel {
/// pub fn from_cli_channel(channel: CliTvChannel) -> Self {
/// match channel {
/// CliTvChannel::Env => TvChannel::Env(EnvChannel::new()),
/// CliTvChannel::Files => TvChannel::Files(FilesChannel::new()),
/// }
/// }
/// }
/// ```
///
/// # Panics
/// The macro will panic if the struct is not an enum or if the enum has no variants.
#[proc_macro_derive(FromCliChannel)]
pub fn from_cli_channel_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
edition = "2021"
max_width = 79

0 comments on commit a9092de

Please sign in to comment.