From 64ab548e16fdc8fa862e2b432464f3d67f5d9db8 Mon Sep 17 00:00:00 2001 From: mms Date: Tue, 19 Nov 2024 21:50:07 +0100 Subject: feat: allow for --new param --- bin/chotto | 16 +++++++++++++--- lib/.chotto.rb.swp | Bin 0 -> 12288 bytes lib/chotto.rb | 26 +++++++++++++++++++------- lib/chotto/.messages.rb.swp | Bin 12288 -> 0 bytes lib/chotto/config.rb | 4 +--- spec/.chotto_spec.rb.swp | Bin 0 -> 12288 bytes spec/chotto_spec.rb | 5 +++++ spec/lib/.messages_spec.rb.swp | Bin 12288 -> 0 bytes spec/lib/database_spec.rb | 1 + todo.txt | 2 +- 10 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 lib/.chotto.rb.swp delete mode 100644 lib/chotto/.messages.rb.swp create mode 100644 spec/.chotto_spec.rb.swp delete mode 100644 spec/lib/.messages_spec.rb.swp diff --git a/bin/chotto b/bin/chotto index 61cfc75..1998af5 100755 --- a/bin/chotto +++ b/bin/chotto @@ -1,14 +1,24 @@ #!/usr/bin/env ruby # frozen_string_literal: true +require 'optparse' require_relative '../lib/chotto' CONFIG_FILEPATH = 'chotto/config.rb' XDG_HOME = ENV.fetch('XDG_CONFIG_HOME', "#{Dir.home}/.config") -def start_chotto +def start_chotto(options) config_file = File.read("#{XDG_HOME}/#{CONFIG_FILEPATH}") instance_eval(config_file) - eval_rules + eval_rules(options) end -start_chotto +options = {} +OptionParser.new do |opts| + opts.banner = 'Usage: chotto [options]' + + opts.on('-n', '--new', 'Run only for new mesages') do |v| + options[:only_new] = v + end +end.parse! + +start_chotto(options) diff --git a/lib/.chotto.rb.swp b/lib/.chotto.rb.swp new file mode 100644 index 0000000..288ee5d Binary files /dev/null and b/lib/.chotto.rb.swp differ diff --git a/lib/chotto.rb b/lib/chotto.rb index 71a1ac8..62e9a0d 100644 --- a/lib/chotto.rb +++ b/lib/chotto.rb @@ -12,17 +12,25 @@ require_relative 'chotto/ruleset' module Chotto class << self - attr_accessor :config - attr_reader :db, :rule_sets + attr_reader :rule_sets, :config def configure(&block) - @config ||= Config.new - @config.db_class = ::Notmuch::Database - @rule_sets = [] + @rule_sets ||= [] + @config ||= force_fresh_config instance_eval(&block) + end - @db = Database.new(path: config.database_path, db_class: config.db_class) + def force_fresh_config + @db = nil + @rule_sets = [] + @config = Config.new( + db_class: ::Notmuch::Database + ) + end + + def db + @db ||= Database.new(path: config.database_path, db_class: config.db_class) end def rule_set(name, &block) @@ -35,7 +43,11 @@ module Chotto end end -def eval_rules +def eval_rules(options = {}) + Chotto.configure do + config.only_new = options.fetch(:only_new, false) + end + Chotto.rule_sets.each(&:run) Chotto.close_db end diff --git a/lib/chotto/.messages.rb.swp b/lib/chotto/.messages.rb.swp deleted file mode 100644 index d31c1b5..0000000 Binary files a/lib/chotto/.messages.rb.swp and /dev/null differ diff --git a/lib/chotto/config.rb b/lib/chotto/config.rb index 2edfcc8..609ab5c 100644 --- a/lib/chotto/config.rb +++ b/lib/chotto/config.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true module Chotto - class Config - attr_accessor :database_path, :db_class - end + Config = Struct.new(:database_path, :db_class, :only_new) end diff --git a/spec/.chotto_spec.rb.swp b/spec/.chotto_spec.rb.swp new file mode 100644 index 0000000..f54f5fd Binary files /dev/null and b/spec/.chotto_spec.rb.swp differ diff --git a/spec/chotto_spec.rb b/spec/chotto_spec.rb index 9e8650b..41d949c 100644 --- a/spec/chotto_spec.rb +++ b/spec/chotto_spec.rb @@ -12,6 +12,7 @@ describe Chotto do before do $db_double = db_double # we need to access this double within block evaled elsewhere + p $db_double allow(db_double).to receive(:new).with('/path', mode: Notmuch::MODE_READ_WRITE).and_return(db_instance_double) allow(db_instance_double).to receive(:query).with(' (from:baltar@galactica.com)').and_return(db_query_double) @@ -30,6 +31,10 @@ describe Chotto do end end + after do + Chotto.force_fresh_config + end + it 'sends the rules to Notmuch - scenario 1' do expect(message_double).to receive(:tags).and_return([]) expect(message_double).to receive(:header).with('Subject').and_return('Number Six') diff --git a/spec/lib/.messages_spec.rb.swp b/spec/lib/.messages_spec.rb.swp deleted file mode 100644 index 98cda14..0000000 Binary files a/spec/lib/.messages_spec.rb.swp and /dev/null differ diff --git a/spec/lib/database_spec.rb b/spec/lib/database_spec.rb index 8da04e4..8ad8d34 100644 --- a/spec/lib/database_spec.rb +++ b/spec/lib/database_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'rspec' +require 'notmuch' require_relative '../../lib/chotto/database' RSpec.describe Chotto::Database do diff --git a/todo.txt b/todo.txt index bdd0109..2df882e 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,6 @@ Task list for 0.1.0 -[ ] allow for selecting order of messages in filter +[x] allow for selecting order of messages in filter [ ] add helper for searching other messages in the same thread as first [ ] add default filter: mailing lists [ ] add default filter: spam -- cgit v1.2.3