From cbda44e7ca9d957dd48b231b7d463e89d5081053 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 24 Jul 2009 02:10:05 +0200 Subject: implemented #13 (special keys for quick navigation) --- code/search.rb | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 code/search.rb (limited to 'code/search.rb') diff --git a/code/search.rb b/code/search.rb new file mode 100644 index 00000000..2952f59a --- /dev/null +++ b/code/search.rb @@ -0,0 +1,141 @@ +module Fm + def goto(arg) + if arg.is_a? Directory::Entry + @pwd.pointed_file = arg.path + + elsif arg.is_a? String + @pwd.pointed_file = arg + + elsif arg.is_a? Numeric + @pwd.pos = arg + + else + lograise ArgumentError.new + end + end + + FIND_PROPERTIES = { + 's' => :size, + 'm' => :mtime, + 'c' => :ctime, + } + FIND_KEY_REGEXP = /f([#{ FIND_PROPERTIES.keys.join("") }])/ + + def quicksearch(n) + log Option.search_method + case Option.search_method + when *FIND_PROPERTIES.values + quicksearch_by_property(n, Option.search_method) + when :handler + quicksearch_by_handler(n) + when :regexp + quicksearch_by_regexp(n) + else + raise "Wrong search method!" + end rescue lograise + end + + def quicksearch_by_property(n, property) + sorted = @pwd.files.sort do |a, b| + b.send(property) <=> a.send(property) + end + + if @search_reset + @search_reset = false + else + sorted.wrap(sorted.index(currentfile) + n) + end + + goto(sorted.first) + end + + def quicksearch_by_handler(n) + sorted = @pwd.files.sort do |a, b| + a.handler.to_s <=> b.handler.to_s + end + + goto(sorted.first) + end + + def quicksearch_by_regexp(n) + begin + rx = Regexp.new(@search_string, Regexp::IGNORECASE) + rescue + return false + end + + ary = @pwd.files.dup + ary.wrap(@pwd.pos) + if n < 0 + ary.wrap(1) + ary.reverse! + end + ary.wrap(n.abs) + + for file in ary + if file.basename =~ rx + return goto(file) + end + end + end + + def search_reset! + @search_reset = true + end + + def search_reset(array) + if @search_reset + @search_reset = false + sorted.wrap(sorted.index(currentfile) + n) + end + end + + def search(str, offset=0, backwards=false) + begin + rx = Regexp.new(str, Regexp::IGNORECASE) + rescue + return false + end + + ary = @pwd.files_raw.dup + ary.wrap(@pwd.pos + offset) + + ary.reverse! if backwards + + for f in ary + g = File.basename(f) + if g =~ rx + @pwd.pointed_file = f + break + end + end + end + + def hints(str) + begin + rx = Regexp.new(str, Regexp::IGNORECASE) + rescue + return false + end + + ary = @pwd.files_raw.dup + ary.wrap(@pwd.pos) + + n = 0 + pointed = false + for f in ary + g = File.basename(f) + if g =~ rx + unless pointed + log "point at #{f}" + @pwd.pointed_file = f + pointed = true + end + n += 1 + end + end + + return n + end + +end -- cgit 1.4.1-2-gfad0