~zanneth/hirogen

bd6db8b43e4d99bf7c64b5f265a234063431a112 — Charles Magahern 2 years ago 7423fa8
Improve repeated find commands
4 files changed, 48 insertions(+), 27 deletions(-)

M src/document.rs
M src/document_window.rs
M src/find_dialog.rs
M test-data/random.dat
M src/document.rs => src/document.rs +18 -9
@@ 256,8 256,11 @@ impl Document
        if self.show_offsets {
            let offset_text = "Offset";
            let offset_col_char_count = self.calculate_offset_column_width_chars() + 1;
            header_text += offset_text;
            header_text += &" ".repeat(offset_col_char_count - offset_text.len());
            
            if offset_col_char_count > offset_text.len() {
                header_text += offset_text;
                header_text += &" ".repeat(offset_col_char_count - offset_text.len());
            }
        }
        
        // write byte indices


@@ 456,7 459,8 @@ impl Document
        };
        
        let mut off: isize = match directive.direction {
            FindDirection::All => 0,
            FindDirection::FromBeginning => 0,
            FindDirection::FromEnd       => (data.len() - 1) as isize,
            
            FindDirection::Backward => {
                match &self.selection {


@@ 474,8 478,8 @@ impl Document
        };
        
        let inc: isize = match directive.direction {
            FindDirection::All | FindDirection::Forward => 1,
            FindDirection::Backward                     => -1
            FindDirection::FromBeginning | FindDirection::Forward => 1,
            FindDirection::FromEnd | FindDirection::Backward      => -1
        };
        
        let needle_bytes = match &directive.needle {


@@ 568,10 572,15 @@ impl Document
    
    fn calculate_offset_column_width_chars(&self) -> usize
    {
        ((((self.get_rows_count() * BYTES_PER_ROW) as f64)
            .log(16.0)
            .floor()) as usize + 1)
            .max(8) // minimum of 8 characters
        let rows_count = self.get_rows_count();
        if rows_count > 0 {
            ((((self.get_rows_count() * BYTES_PER_ROW) as f64)
                .log(16.0)
                .floor()) as usize + 1)
                .max(8) // minimum of 8 characters
        } else {
            0
        }
    }
    
    fn calculate_offset_column_width_pixels(&self) -> f64

M src/document_window.rs => src/document_window.rs +25 -14
@@ 529,24 529,12 @@ impl DocumentWindow
    
    fn handle_find_next_action(self: &DocumentWindowRef)
    {
        let directive = self.state.borrow().last_find_directive.clone();
        if let Some(directive) = &directive {
            let mut directive_copy = directive.clone();
            directive_copy.direction = FindDirection::Forward;

            self.handle_find(&directive_copy);
        }
        self.handle_repeated_find(&FindDirection::Forward);
    }

    fn handle_find_prev_action(self: &DocumentWindowRef)
    {
        let directive = self.state.borrow().last_find_directive.clone();
        if let Some(directive) = &directive {
            let mut directive_copy = directive.clone();
            directive_copy.direction = FindDirection::Backward;

            self.handle_find(&directive_copy);
        }
        self.handle_repeated_find(&FindDirection::Backward);
    }
    
    fn handle_goto_action(self: &DocumentWindowRef)


@@ 1021,6 1009,29 @@ impl DocumentWindow
        
        found_result_and_updated_sel
    }
    
    fn handle_repeated_find(self: &DocumentWindowRef, direction: &FindDirection)
    {
        use FindDirection::*;

        let directive = self.state.borrow().last_find_directive.clone();
        if let Some(directive) = &directive {
            let mut directive_copy = directive.clone();
            directive_copy.direction = *direction;

            let found_result = self.handle_find(&directive_copy);
            if !found_result {
                // Try to wrap around.
                match direction {
                    Forward  => directive_copy.direction = FromBeginning,
                    Backward => directive_copy.direction = FromEnd,
                    _        => ()
                }
                
                self.handle_find(&directive_copy);
            }
        }
    }

    fn scroll_page(self: &DocumentWindowRef, direction: &Direction)
    {

M src/find_dialog.rs => src/find_dialog.rs +5 -4
@@ 34,7 34,8 @@ pub enum FindNeedle
#[derive(Copy, Clone, Debug)]
pub enum FindDirection
{
    All,
    FromBeginning,
    FromEnd,
    Backward,
    Forward,
}


@@ 159,9 160,9 @@ impl FindDialog
    fn selected_direction(self: &FindDialogRef) -> FindDirection
    {
        *[
            (FindDirection::All,        &self.direction_all_radio),
            (FindDirection::Backward,   &self.direction_backward_radio),
            (FindDirection::Forward,    &self.direction_forward_radio)
            (FindDirection::FromBeginning, &self.direction_all_radio),
            (FindDirection::Backward,      &self.direction_backward_radio),
            (FindDirection::Forward,       &self.direction_forward_radio)
        ]
            .iter()
            .filter(|(_, b)| b.get_active())

M test-data/random.dat => test-data/random.dat +0 -0