filematcher/main.v
2026-01-09 13:25:44 +08:00

176 lines
4.6 KiB
V

// main.v
module main
import os
import time
import file_matcher
fn main() {
// 直接读取目录
mut current_dir := '.'
print(' (): ')
mut input := os.get_line()
if input.trim_space() != '' {
current_dir = input
}
// 读取搜索模式
mut search_pattern := '*'
print(' (* ): ')
input = os.get_line()
if input.trim_space() != '' {
search_pattern = input
}
// 验证目录
if !os.is_dir(current_dir) {
println(': "$current_dir" !')
return
}
println('')
println(': $current_dir')
println(': $search_pattern')
println('...')
println('')
mut scanner := new_scanner(12)
scanner.do_scan_with_search(current_dir, search_pattern)
}
// 原始Scanner结构体
struct Scanner {
mut:
max_worker int = 32
worker_count int
search_request chan string
work_done chan bool
// 搜索相关字段
search_pattern string
search_mode file_matcher.MatchMode
match_count int
search_active bool
}
fn new_scanner(max_worker_count int) &Scanner {
return &Scanner{
max_worker: max_worker_count
search_request: chan string{cap: 100}
work_done: chan bool{cap: 100}
}
}
// 设置搜索条件
fn (mut s Scanner) set_search(pattern string, mode file_matcher.MatchMode) {
s.search_pattern = pattern
s.search_mode = mode
s.search_active = pattern != '' && pattern != '*'
s.match_count = 0
println(': =$pattern, =$mode, =$s.search_active')
}
// 增强的扫描函数,支持文件过滤
fn (mut s Scanner) scanner_with_filter(path string, is_master bool) {
items := os.ls(path) or {
// 只在路径不是系统目录时显示错误
if !path.contains('System Volume Information') &&
!path.contains('\$RECYCLE.BIN') &&
!path.contains('Windows') {
println(': 访 "$path"')
}
if is_master {
s.work_done <- true
}
return
}
for item in items {
full_path := os.join_path(path, item)
if os.is_dir(full_path) {
if s.worker_count < s.max_worker {
s.search_request <- full_path
} else {
s.scanner_with_filter(full_path, false)
}
} else {
// 如果没有激活搜索,或者文件匹配模式,则输出
if !s.search_active {
println('$full_path')
s.match_count++
} else {
if file_matcher.match_file(s.search_pattern, item, s.search_mode) {
println('$full_path')
s.match_count++
}
}
}
}
if is_master {
s.work_done <- true
}
}
// 执行扫描(可选择是否搜索)
fn (mut s Scanner) do_scan_with_search(path string, search_pattern string) {
start := time.now()
// 设置搜索条件(如果提供了搜索模式)
if search_pattern != '' {
// 自动检测搜索模式
mut mode := file_matcher.MatchMode.exact
if search_pattern == '*' {
// 显示所有文件,不激活搜索
mode = .exact
s.search_active = false
s.search_pattern = ''
} else if file_matcher.is_wildcard_pattern(search_pattern) {
mode = .wildcard
} else if search_pattern.contains('%') {
mode = .like
}
s.set_search(search_pattern, mode)
println(': $search_pattern (${mode})')
} else {
s.search_active = false
s.search_pattern = ''
}
s.worker_count = 1
s.match_count = 0
go s.scanner_with_filter(path, true)
s.watting_for_workers()
end := time.now()
duration := end - start
println('')
if s.search_active {
println(' ${s.match_count} ')
} else {
println(' ${s.match_count} ')
}
println(': ${duration.seconds()} ')
}
// 等待工作线程完成
fn (mut s Scanner) watting_for_workers() {
for {
select {
path := <- s.search_request {
s.worker_count++
go s.scanner_with_filter(path, true)
}
_ := <- s.work_done {
s.worker_count--
if s.worker_count == 0 {
return
}
}
}
}
}