1use crate::core::file_stats::FileStats;
2use crate::core::file_stats::weight::WeightConfig;
3pub use crate::core::path_matcher::SortStrategy;
4use crate::core::path_matcher::{MatchStats, PathMatchers, ShowMode};
5use crate::core::path_store::{PathContext, PathStore};
6use crate::core::path_view::{PathGrid, PathList, PathTree, ViewMode};
7use crate::core::patterns_expand::PatternContext;
8use std::path::Path;
9
10pub fn execute<OnMatch, OnMismatch>(
13 enter_path: &str,
14 patterns: &[String],
15 is_case_sensitive: bool,
16 sort_strategy: SortStrategy,
17 show_mode: ShowMode,
18 view_mode: ViewMode,
19 weight_cfg: WeightConfig, no_root: bool,
21 print_info: bool,
22 no_emoji: bool,
23 i18n: &crate::i18n::I18n,
24 mut on_match: OnMatch,
25 mut on_mismatch: OnMismatch,
26) -> MatchStats
27where
28 OnMatch: FnMut(&FileStats),
29 OnMismatch: FnMut(&FileStats),
30{
31 let pattern_ctx = PatternContext::new(patterns);
34 let path_ctx = PathContext::resolve(enter_path).unwrap_or_else(|e| {
35 eprintln!("❌ {}", e);
36 std::process::exit(1);
37 });
38
39 if print_info {
42 println!("{}", i18n.cli_base_abs(&path_ctx.base_absolute));
43 println!("{}", i18n.cli_target_abs(&path_ctx.entry_absolute));
44 println!("{}", i18n.cli_target_rel(&path_ctx.entry_relative));
45 println!("---------------------------------------");
46 println!("{}", i18n.cli_case_sensitive(is_case_sensitive));
47 println!(
48 "{}",
49 i18n.cli_patterns_raw(&format!("{:?}", pattern_ctx.raw))
50 );
51 println!(
52 "{}",
53 i18n.cli_patterns_tok(&format!("{:?}", pattern_ctx.tok))
54 );
55 println!("---------------------------------------");
56 } else {
57 println!("---------------------------------------");
58 }
59
60 let matchers =
63 PathMatchers::new(&pattern_ctx.tok, is_case_sensitive).expect("Błąd kompilacji wzorców");
64
65 let paths_store = PathStore::scan(&path_ctx.entry_absolute);
68 let paths_set = paths_store.get_index();
69 let entry_abs = path_ctx.entry_absolute.clone();
70
71 let mut stats = matchers.evaluate(
74 &paths_store.list,
75 &paths_set,
76 sort_strategy,
77 show_mode,
78 |raw_path| {
79 let stats = FileStats::fetch(raw_path, &entry_abs);
80 on_match(&stats);
81 },
82 |raw_path| {
83 let stats = FileStats::fetch(raw_path, &entry_abs);
84 on_mismatch(&stats);
85 },
86 );
87
88 let root_name = if no_root {
91 None
92 } else {
93 Path::new(&path_ctx.entry_absolute)
94 .file_name()
95 .and_then(|n| n.to_str())
96 };
97
98 let do_include = show_mode == ShowMode::Include || show_mode == ShowMode::Context;
99 let do_exclude = show_mode == ShowMode::Exclude || show_mode == ShowMode::Context;
100
101 match view_mode {
102 ViewMode::Grid => {
103 if do_include {
104 stats.m_matched.grid = Some(PathGrid::build(
105 &stats.m_matched.paths,
106 &path_ctx.entry_absolute,
107 sort_strategy,
108 &weight_cfg,
109 root_name,
110 no_emoji,
111 ));
112 }
113 if do_exclude {
114 stats.x_mismatched.grid = Some(PathGrid::build(
115 &stats.x_mismatched.paths,
116 &path_ctx.entry_absolute,
117 sort_strategy,
118 &weight_cfg,
119 root_name,
120 no_emoji,
121 ));
122 }
123 }
124 ViewMode::Tree => {
125 if do_include {
126 stats.m_matched.tree = Some(PathTree::build(
127 &stats.m_matched.paths,
128 &path_ctx.entry_absolute,
129 sort_strategy,
130 &weight_cfg,
131 root_name,
132 no_emoji,
133 ));
134 }
135 if do_exclude {
136 stats.x_mismatched.tree = Some(PathTree::build(
137 &stats.x_mismatched.paths,
138 &path_ctx.entry_absolute,
139 sort_strategy,
140 &weight_cfg,
141 root_name,
142 no_emoji,
143 ));
144 }
145 }
146 ViewMode::List => {
147 if do_include {
148 stats.m_matched.list = Some(PathList::build(
149 &stats.m_matched.paths,
150 &path_ctx.entry_absolute,
151 sort_strategy,
152 &weight_cfg,
153 no_emoji,
154 ));
155 }
156 if do_exclude {
157 stats.x_mismatched.list = Some(PathList::build(
158 &stats.x_mismatched.paths,
159 &path_ctx.entry_absolute,
160 sort_strategy,
161 &weight_cfg,
162 no_emoji,
163 ));
164 }
165 }
166 }
167
168 stats
169}