File transfer activity refactoring OK

This commit is contained in:
veeso
2021-03-21 13:18:53 +01:00
parent fd4c4a3772
commit 977becd5c9
3 changed files with 35 additions and 23 deletions

View File

@@ -470,7 +470,8 @@ impl FileTransferActivity {
} }
self.umount_saveas(); self.umount_saveas();
// Reload files // Reload files
match self.tab { // NOTE: Swapped is intentional match self.tab {
// NOTE: Swapped is intentional
FileExplorerTab::Local => self.update_remote_filelist(), FileExplorerTab::Local => self.update_remote_filelist(),
FileExplorerTab::Remote => self.update_local_filelist(), FileExplorerTab::Remote => self.update_local_filelist(),
} }
@@ -693,7 +694,7 @@ impl FileTransferActivity {
// Make log entries // Make log entries
let mut table: TableBuilder = TableBuilder::default(); let mut table: TableBuilder = TableBuilder::default();
for (idx, record) in self.log_records.iter().enumerate() { for (idx, record) in self.log_records.iter().enumerate() {
let record_rows = textwrap::wrap(record.msg.as_str(), (width as usize) - 35); // -35 'cause log prefix let record_rows = textwrap::wrap(record.msg.as_str(), (width as usize) - 38); // -35 'cause log prefix -3 cause of log line cursor
// Add row if not first row // Add row if not first row
if idx > 0 { if idx > 0 {
table.add_row(); table.add_row();

View File

@@ -86,6 +86,7 @@ impl FileTransferActivity {
Box::new(LogBox::new( Box::new(LogBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::LightGreen) .with_foreground(Color::LightGreen)
.bold()
.build(), .build(),
)), )),
); );

View File

@@ -95,10 +95,7 @@ impl OwnStates {
/// ///
/// Reset list index to last element /// Reset list index to last element
pub fn reset_list_index(&mut self) { pub fn reset_list_index(&mut self) {
self.list_index = match self.list_len { self.list_index = 0; // Last element is always 0
0 => 0,
_ => self.list_len - 1,
};
} }
} }
@@ -144,8 +141,9 @@ impl Component for LogBox {
None => Vec::new(), None => Vec::new(),
Some(table) => table Some(table) => table
.iter() .iter()
.map(|row| { .enumerate()
let columns: Vec<Span> = row .map(|(idx, row)| {
let mut columns: Vec<Span> = row
.iter() .iter()
.map(|col| { .map(|col| {
Span::styled( Span::styled(
@@ -157,7 +155,19 @@ impl Component for LogBox {
) )
}) })
.collect(); .collect();
ListItem::new(Spans::from(columns)) let mut row: Vec<Span> = Vec::with_capacity(columns.len() + 1);
// Push green cursor if selected line
row.push(Span::styled(
match self.states.list_index == idx {
true => "> ",
false => " ",
},
Style::default()
.fg(self.props.foreground)
.bg(self.props.background),
));
row.append(&mut columns);
ListItem::new(Spans::from(row))
}) })
.collect(), // Make List item from TextSpan .collect(), // Make List item from TextSpan
}; };
@@ -218,24 +228,24 @@ impl Component for LogBox {
// Match event // Match event
if let InputEvent::Key(key) = ev { if let InputEvent::Key(key) = ev {
match key.code { match key.code {
KeyCode::Down => { KeyCode::Up => {
// Update states // Update states
self.states.incr_list_index(); self.states.incr_list_index();
Msg::None Msg::None
} }
KeyCode::Up => { KeyCode::Down => {
// Update states // Update states
self.states.decr_list_index(); self.states.decr_list_index();
Msg::None Msg::None
} }
KeyCode::PageDown => { KeyCode::PageUp => {
// Update states // Update states
for _ in 0..8 { for _ in 0..8 {
self.states.incr_list_index(); self.states.incr_list_index();
} }
Msg::None Msg::None
} }
KeyCode::PageUp => { KeyCode::PageDown => {
// Update states // Update states
for _ in 0..8 { for _ in 0..8 {
self.states.decr_list_index(); self.states.decr_list_index();
@@ -302,7 +312,7 @@ mod tests {
.build(), .build(),
); );
// Verify states // Verify states
assert_eq!(component.states.list_index, 1); assert_eq!(component.states.list_index, 0);
assert_eq!(component.states.list_len, 2); assert_eq!(component.states.list_len, 2);
assert_eq!(component.states.focus, false); assert_eq!(component.states.focus, false);
// Focus // Focus
@@ -311,8 +321,8 @@ mod tests {
component.blur(); component.blur();
assert_eq!(component.states.focus, false); assert_eq!(component.states.focus, false);
// Increment list index // Increment list index
component.states.list_index -= 1; component.states.list_index += 1;
assert_eq!(component.states.list_index, 0); assert_eq!(component.states.list_index, 1);
// Update // Update
component.update( component.update(
component component
@@ -333,38 +343,38 @@ mod tests {
.build(), .build(),
); );
// Verify states // Verify states
assert_eq!(component.states.list_index, 2); // Last item assert_eq!(component.states.list_index, 0); // Last item
assert_eq!(component.states.list_len, 3); assert_eq!(component.states.list_len, 3);
// get value // get value
assert_eq!(component.get_value(), Payload::Unsigned(2)); assert_eq!(component.get_value(), Payload::Unsigned(0));
// RenderData // RenderData
assert_eq!(component.states.list_index, 2); assert_eq!(component.states.list_index, 0);
// Set cursor to 0 // Set cursor to 0
component.states.list_index = 0; component.states.list_index = 0;
// Handle inputs // Handle inputs
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Down))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::Up))),
Msg::None Msg::None
); );
// Index should be incremented // Index should be incremented
assert_eq!(component.states.list_index, 1); assert_eq!(component.states.list_index, 1);
// Index should be decremented // Index should be decremented
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Up))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::Down))),
Msg::None Msg::None
); );
// Index should be incremented // Index should be incremented
assert_eq!(component.states.list_index, 0); assert_eq!(component.states.list_index, 0);
// Index should be 2 // Index should be 2
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageDown))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageUp))),
Msg::None Msg::None
); );
// Index should be incremented // Index should be incremented
assert_eq!(component.states.list_index, 2); assert_eq!(component.states.list_index, 2);
// Index should be 0 // Index should be 0
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageUp))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageDown))),
Msg::None Msg::None
); );
// Index should be incremented // Index should be incremented