Skip to content

Commit

Permalink
Fixed download flagging.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Jan 26, 2025
1 parent d8e1652 commit 9f5c113
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 49 deletions.
8 changes: 4 additions & 4 deletions crates/dizbase/src/file_base/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ impl Pattern {
}

pub fn matches(&self, str: &str) -> bool {
self.matches_with(str, MatchOptions::new())
self.matches_with(str, &MatchOptions::new())
}

/// Return if the given `str` matches this `Pattern` using the specified
/// match options.
pub fn matches_with(&self, str: &str, options: MatchOptions) -> bool {
pub fn matches_with(&self, str: &str, options: &MatchOptions) -> bool {
self.matches_from(true, str.chars(), 0, options) == MatchResult::Match
}

fn matches_from(&self, mut follows_separator: bool, mut file: std::str::Chars, i: usize, options: MatchOptions) -> MatchResult {
fn matches_from(&self, mut follows_separator: bool, mut file: std::str::Chars, i: usize, options: &MatchOptions) -> MatchResult {
for (ti, token) in self.tokens[i..].iter().enumerate() {
match *token {
PatternToken::AnySequence => {
Expand Down Expand Up @@ -203,7 +203,7 @@ fn parse_char_specifiers(s: &[char]) -> Vec<CharSpecifier> {
cs
}

fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: MatchOptions) -> bool {
fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: &MatchOptions) -> bool {
for &specifier in specifiers.iter() {
match specifier {
CharSpecifier::SingleChar(sc) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ impl IcyBoardState {
self.new_line().await?;
let files = base.file_headers.iter_mut().collect::<Vec<_>>();
log::info!("Files: {}", files.len());
let mut list: FileList<'_> = FileList::new(files, help);
let mut list: FileList<'_> = FileList::new(file_base_path.clone(), files, help);
list.display_file_list(self).await?;

if self.session.num_lines_printed > 0 {
FileList::filebase_more(self, help).await?;
FileList::filebase_more(self, &file_base_path.clone(), help).await?;
}
self.session.non_stop_off();
self.session.more_requested = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::{Path, PathBuf};

use crate::{
icy_board::state::{functions::MASK_COMMAND, IcyBoardState},
Res,
Expand All @@ -13,7 +15,11 @@ use crate::{
},
vm::TerminalTarget,
};
use dizbase::file_base::{metadata::MetadaType, FileBase, FileEntry};
use dizbase::file_base::{
metadata::MetadaType,
pattern::{MatchOptions, Pattern},
FileBase, FileEntry,
};
use humanize_bytes::humanize_bytes_decimal;

impl IcyBoardState {
Expand Down Expand Up @@ -121,19 +127,20 @@ impl IcyBoardState {
self.new_line().await?;
let files = base.find_files(search_pattern.as_str())?;

let mut list = FileList::new(files, help);
let mut list = FileList::new(file_base_path.clone(), files, help);
list.display_file_list(self).await
}
}

pub struct FileList<'a> {
pub path: PathBuf,
pub files: Vec<&'a mut FileEntry>,
pub help: &'a str,
}

impl<'a> FileList<'a> {
pub fn new(files: Vec<&'a mut FileEntry>, help: &'a str) -> Self {
Self { files, help }
pub fn new(path: PathBuf, files: Vec<&'a mut FileEntry>, help: &'a str) -> Self {
Self { path, files, help }
}

pub async fn display_file_list(&mut self, cmd: &mut IcyBoardState) -> Res<()> {
Expand Down Expand Up @@ -189,7 +196,7 @@ impl<'a> FileList<'a> {
printed_lines = true;
if cmd.session.num_lines_printed >= cmd.session.page_len as usize {
cmd.session.num_lines_printed = 0;
Self::filebase_more(cmd, &self.help).await?;
Self::filebase_more(cmd, &self.path, &self.help).await?;
if cmd.session.disp_options.abort_printout {
cmd.session.cancel_batch = cmd.session.disp_options.abort_printout;
return Ok(());
Expand All @@ -214,7 +221,7 @@ impl<'a> FileList<'a> {
Ok(())
}

pub async fn filebase_more(cmd: &mut IcyBoardState, help: &str) -> Res<()> {
pub async fn filebase_more(cmd: &mut IcyBoardState, path: &Path, help: &str) -> Res<()> {
loop {
let input = cmd
.input_field(
Expand All @@ -232,7 +239,7 @@ impl<'a> FileList<'a> {
match input.as_str() {
"F" => {
// flag
let _input = cmd
let input = cmd
.input_field(
IceText::FlagForDownload,
60,
Expand All @@ -242,41 +249,32 @@ impl<'a> FileList<'a> {
display_flags::NEWLINE | display_flags::UPCASE | display_flags::LFAFTER | display_flags::HIGHASCII,
)
.await?;
/*
if !input.is_empty() {
for f in files {
if f.name().eq_ignore_ascii_case(&input) {
let metadata = f.get_metadata()?;
for m in metadata {
if m.metadata_type == MetadaType::FilePath {
cmd.display_text(IceText::CheckingFileTransfer, display_flags::NEWLINE).await?;
let file_name = unsafe { OsString::from_encoded_bytes_unchecked(m.data.clone()) };
let file_path = PathBuf::from(file_name);
if !file_path.exists() {
cmd.session.op_text = input.clone();
cmd.display_text(IceText::NotFoundOnDisk, display_flags::NEWLINE | display_flags::LFBEFORE)
.await?;
} else {
cmd.session.flag_for_download(file_path.clone());
cmd.set_color(TerminalTarget::Both, IcbColor::Dos(10)).await?;
cmd.println(
TerminalTarget::Both,
&format!(
"{:<12} {}",
file_path.file_name().unwrap().to_string_lossy(),
humanize_bytes_decimal!(f.size()).to_string()
),
)
.await?;
}
break;
}
let mut files = FileBase::open(path)?;
let mut found = false;
let mut options = MatchOptions::new();
options.case_sensitive = false;
if let Ok(pattern) = Pattern::new(&input) {
cmd.display_text(IceText::CheckingFileTransfer, display_flags::NEWLINE).await?;
cmd.set_color(TerminalTarget::Both, IcbColor::Dos(10)).await?;
for f in &mut files.file_headers {
if pattern.matches_with(&f.name(), &options) {
let size = f.size();
cmd.session.flag_for_download(f.full_path.clone());
cmd.println(TerminalTarget::Both, &format!("{:<12} {}", f.name(), humanize_bytes_decimal!(size).to_string()))
.await?;
found = true;
}
break;
}
cmd.reset_color(TerminalTarget::Both).await?;
}
}*/

if !found {
cmd.session.op_text = input.clone();
cmd.display_text(IceText::NotFoundOnDisk, display_flags::NEWLINE | display_flags::LFBEFORE)
.await?;
}
}
}
"V" => {
// view: TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl IcyBoardState {

let files = base.find_newer_files(time_stamp)?;

let mut list = FileList::new(files, help);
let mut list = FileList::new(file_base_path, files, help);
list.display_file_list(self).await
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl IcyBoardState {
self.new_line().await?;
let files = base.find_files_with_pattern(search_pattern.as_str())?;

let mut list = FileList::new(files, help);
let mut list = FileList::new(file_base_path, files, help);
list.display_file_list(self).await?;

self.session.non_stop_off();
Expand Down
5 changes: 4 additions & 1 deletion crates/icy_board_tui/src/config_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub enum ListValue {
pub struct ListItem {
pub id: String,
title: String,
label_width: u16,
pub status: String,
pub text_field_state: TextfieldState,
pub value: ListValue,
Expand All @@ -136,6 +137,7 @@ impl ListItem {
id: id.to_string(),
status: format!("{}", title),
text_field_state: TextfieldState::default(),
label_width: title.len() as u16,
title,
value,
}
Expand All @@ -150,6 +152,7 @@ impl ListItem {
while self.title.len() < width as usize {
self.title.push(' ');
}
self.label_width = width;
self
}

Expand All @@ -164,7 +167,7 @@ impl ListItem {
let area = Rect {
x: left_area.x,
y: left_area.y,
width: title.len() as u16 + 1,
width: self.label_width,
height: left_area.height,
};
Text::from(title)
Expand Down
14 changes: 12 additions & 2 deletions crates/icy_board_tui/src/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct TextfieldState {

impl TextfieldState {
pub fn set_cursor_position(&self, frame: &mut Frame) {
frame.set_cursor_position((self.area.x + self.cursor_position, self.area.y));
frame.set_cursor_position((self.area.x + self.cursor_position - self.first_char as u16, self.area.y));
}

pub fn max_len(&self) -> u16 {
Expand Down Expand Up @@ -67,6 +67,11 @@ impl TextfieldState {

_ => {}
}
if self.cursor_position < self.first_char as u16 {
self.first_char = self.cursor_position as usize;
} else if self.cursor_position >= self.first_char as u16 + self.area.width {
self.first_char = (self.cursor_position as usize + 1 - self.area.width as usize).min(value.len());
}
}

fn insert_key(&mut self, value: &mut String, ch: char) {
Expand Down Expand Up @@ -146,7 +151,12 @@ impl StatefulWidget for TextField {
return;
}
state.area = area;
buf.set_string(area.x, area.y, self.value[state.first_char..].to_string(), self.text_style);
let mut s = self.value[state.first_char..].to_string();
while s.len() as u16 > area.width {
s.pop();
}

buf.set_string(area.x, area.y, s, self.text_style);
let len = self.value.len().saturating_sub(state.first_char);
buf.set_string(
area.x + len as u16,
Expand Down

0 comments on commit 9f5c113

Please sign in to comment.