aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormms <git@sapka.me>2024-11-19 21:50:07 +0100
committermms <git@sapka.me>2024-11-19 21:50:07 +0100
commit64ab548e16fdc8fa862e2b432464f3d67f5d9db8 (patch)
tree75c8e273d47eb54af67da5c6299876601f054e93
parent8004fd7f9fc91ce9ec02b1b8e8eac639b17d0eb8 (diff)
feat: allow for --new param
-rwxr-xr-xbin/chotto16
-rw-r--r--lib/.chotto.rb.swp (renamed from spec/lib/.messages_spec.rb.swp)bin12288 -> 12288 bytes
-rw-r--r--lib/chotto.rb26
-rw-r--r--lib/chotto/config.rb4
-rw-r--r--spec/.chotto_spec.rb.swp (renamed from lib/chotto/.messages.rb.swp)bin12288 -> 12288 bytes
-rw-r--r--spec/chotto_spec.rb5
-rw-r--r--spec/lib/database_spec.rb1
-rw-r--r--todo.txt2
8 files changed, 40 insertions, 14 deletions
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/spec/lib/.messages_spec.rb.swp b/lib/.chotto.rb.swp
index 98cda14..288ee5d 100644
--- a/spec/lib/.messages_spec.rb.swp
+++ b/lib/.chotto.rb.swp
Binary files 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/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/lib/chotto/.messages.rb.swp b/spec/.chotto_spec.rb.swp
index d31c1b5..f54f5fd 100644
--- a/lib/chotto/.messages.rb.swp
+++ b/spec/.chotto_spec.rb.swp
Binary files 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/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