From c65e4885b999e90732bf37ee954540bae58a4880 Mon Sep 17 00:00:00 2001 From: mms Date: Tue, 12 Nov 2024 23:23:01 +0100 Subject: feat: rename, again --- bin/chotto | 13 ++++++ bin/osugiru | 13 ------ lib/chotto.rb | 40 +++++++++++++++++++ lib/chotto/config.rb | 7 ++++ lib/chotto/database.rb | 19 +++++++++ lib/chotto/helpers.rb | 13 ++++++ lib/chotto/message.rb | 35 ++++++++++++++++ lib/chotto/messages.rb | 102 +++++++++++++++++++++++++++++++++++++++++++++++ lib/chotto/ruleset.rb | 11 ++++++ lib/chotto/tags.rb | 18 +++++++++ lib/osugiru.rb | 40 ------------------- lib/osugiru/config.rb | 7 ---- lib/osugiru/database.rb | 19 --------- lib/osugiru/helpers.rb | 13 ------ lib/osugiru/message.rb | 36 ----------------- lib/osugiru/messages.rb | 103 ------------------------------------------------ lib/osugiru/ruleset.rb | 11 ------ 17 files changed, 258 insertions(+), 242 deletions(-) create mode 100755 bin/chotto delete mode 100755 bin/osugiru create mode 100644 lib/chotto.rb create mode 100644 lib/chotto/config.rb create mode 100644 lib/chotto/database.rb create mode 100644 lib/chotto/helpers.rb create mode 100644 lib/chotto/message.rb create mode 100644 lib/chotto/messages.rb create mode 100644 lib/chotto/ruleset.rb create mode 100644 lib/chotto/tags.rb delete mode 100644 lib/osugiru.rb delete mode 100644 lib/osugiru/config.rb delete mode 100644 lib/osugiru/database.rb delete mode 100644 lib/osugiru/helpers.rb delete mode 100644 lib/osugiru/message.rb delete mode 100644 lib/osugiru/messages.rb delete mode 100644 lib/osugiru/ruleset.rb diff --git a/bin/chotto b/bin/chotto new file mode 100755 index 0000000..0dabd48 --- /dev/null +++ b/bin/chotto @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby + +require_relative "../lib/chotto.rb" + +CONFIG_FILEPATH = 'chotto/config.rb' +XDG_HOME = ENV.fetch('XDG_CONFIG_HOME', "#{Dir.home}/.config") +def start_chotto + config_file = File.read("#{XDG_HOME}/#{CONFIG_FILEPATH}") + instance_eval(config_file) + eval_rules +end + +start_chotto diff --git a/bin/osugiru b/bin/osugiru deleted file mode 100755 index 0ed7510..0000000 --- a/bin/osugiru +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env ruby - -require_relative "../lib/osugiru.rb" - -CONFIG_FILEPATH = 'osugiru/config.rb' -XDG_HOME = ENV.fetch('XDG_CONFIG_HOME', "#{Dir.home}/.config") -def start_osugiru - config_file = File.read("#{XDG_HOME}/#{CONFIG_FILEPATH}") - instance_eval(config_file) - eval_rules -end - -start_osugiru diff --git a/lib/chotto.rb b/lib/chotto.rb new file mode 100644 index 0000000..47a94c0 --- /dev/null +++ b/lib/chotto.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'notmuch' +require 'pry' + +require_relative 'chotto/config' +require_relative 'chotto/database' +require_relative 'chotto/helpers' +require_relative 'chotto/message' +require_relative 'chotto/messages' +require_relative 'chotto/ruleset' +require_relative 'chotto/tags' + +module Chotto + class << self + attr_accessor :config + attr_reader :db, :rule_sets + + def configure(&block) + @config ||= Config.new + @rule_sets = [] + + instance_eval(&block) + + @db = Database.new(path: config.database_path) + end + + def rule_set(name, &block) + @rule_sets << RuleSet.new(name, block) + end + end +end + +def eval_rules + Chotto.rule_sets.each do |rule_set| + rule_set.run + end +end + + diff --git a/lib/chotto/config.rb b/lib/chotto/config.rb new file mode 100644 index 0000000..267e72c --- /dev/null +++ b/lib/chotto/config.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Chotto + class Config + attr_accessor :database_path + end +end diff --git a/lib/chotto/database.rb b/lib/chotto/database.rb new file mode 100644 index 0000000..78b80fe --- /dev/null +++ b/lib/chotto/database.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Chotto + class Database + attr_reader :db + + def initialize(path:) + @db = ::Notmuch::Database.new(path, mode: Notmuch::MODE_READ_WRITE) + end + + def query(query) + db.query(query) + end + + def search_messages(query) + query(query).search_messages + end + end +end diff --git a/lib/chotto/helpers.rb b/lib/chotto/helpers.rb new file mode 100644 index 0000000..02ee81e --- /dev/null +++ b/lib/chotto/helpers.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Chotto + module Helpers + def self.header_name_from_dsl(method_name) + method_name + .to_s + .split('_') + .map(&:capitalize) + .join('-') + end + end +end diff --git a/lib/chotto/message.rb b/lib/chotto/message.rb new file mode 100644 index 0000000..ce7c067 --- /dev/null +++ b/lib/chotto/message.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Chotto + class Message + attr_reader :message + + def initialize(msg:) + @message = msg + @tags = @message.tags + end + + def method_missing(method_name, *_args) + handle_get(Chotto::Helpers.header_name_from_dsl(method_name)) + end + + def handle_get(header_name) + message.header(header_name) if message.header(header_name) + end + + def tags + @tags + end + + def tags=(new_tags) + @tags = new_tags + end + + def save! + message.remove_all_tags + tags.each do |tag| + message.add_tag(tag) + end + end + end +end diff --git a/lib/chotto/messages.rb b/lib/chotto/messages.rb new file mode 100644 index 0000000..7326148 --- /dev/null +++ b/lib/chotto/messages.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +module Chotto + class Messages + AND_CONJUCTION = :and + OR_CONJUCTION = :or + DEFAULT_CONJUCTION = AND_CONJUCTION + + Token = Struct.new(:conjuction, :field, :value, :messages) do + def to_query + conjuction_to_use = messages.next_token_id > 1 ? conjuction : '' + return "#{conjuction_to_use} (#{value})" if field == :direct + + "#{conjuction_to_use} (#{field}:#{value})" + end + end + + TokenGroup = Struct.new(:conjuction, :tokens, :messages) do + def initialize(*) + super + end + + def to_query + tokens.flat_map(&:to_query).join(' ').to_s + end + + def push_token(field, value) + if value.is_a? String + tokens.push(Token.new(conjuction, field, value, messages)) + elsif value.is_a? Array + group = TokenGroup.new(OR_CONJUCTION, [], messages) + value.each do |val| + group.push_token(field, val) + end + tokens.push group + + end + end + + def next_token_id + @token_count += 1 + end + end + + attr_accessor :query, :current_conjuction, :token_count + attr_reader :db + + def initialize(db:) + @db = db + @query = [] + @current_conjuction = AND_CONJUCTION + @token_count = 0 + end + + def or + @current_conjuction = OR_CONJUCTION + self + end + + def and + @current_conjuction = AND_CONJUCTION + self + end + + def filter(params) + case params + when String + add_filter_from_string(params) + when Hash + add_filter_from_hash(params) + end + + self + end + + def each + db.search_messages(query_string).each do |msg| + yield(Message.new(msg: msg)) + end + end + + def add_filter_from_string(string) + @query << Token.new(current_conjuction, :direct, string, self) + end + + def add_filter_from_hash(hash) + group = TokenGroup.new(current_conjuction, [], self) + hash.each do |key, value| + group.push_token(key, value) + end + @query << group + end + + def query_string + query.map(&:to_query).join(' ') + end + + def next_token_id + @token_count += 1 + end + end +end diff --git a/lib/chotto/ruleset.rb b/lib/chotto/ruleset.rb new file mode 100644 index 0000000..289aa2f --- /dev/null +++ b/lib/chotto/ruleset.rb @@ -0,0 +1,11 @@ +module Chotto + RuleSet = Struct.new(:name, :rule) do + def messages + Messages.new(db: Chotto.db) + end + + def run + instance_eval(&rule) + end + end +end diff --git a/lib/chotto/tags.rb b/lib/chotto/tags.rb new file mode 100644 index 0000000..3a4fb14 --- /dev/null +++ b/lib/chotto/tags.rb @@ -0,0 +1,18 @@ +module Chotto + class Tags + include Enumerable + + attr_accessor :tags, :message + + def initialize(tags:, message:) + @tags = tags + @messae = message + end + + def each(&block) + tags.each(&block) + end + end + + +end diff --git a/lib/osugiru.rb b/lib/osugiru.rb deleted file mode 100644 index 4e99212..0000000 --- a/lib/osugiru.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -require 'notmuch' -require 'pry' - -require_relative 'osugiru/config' -require_relative 'osugiru/database' -require_relative 'osugiru/helpers' -require_relative 'osugiru/message' -require_relative 'osugiru/messages' -require_relative 'osugiru/ruleset' -require_relative 'osugiru/tags' - -module Osugiru - class << self - attr_accessor :config - attr_reader :db, :rule_sets - - def configure(&block) - @config ||= Config.new - @rule_sets = [] - - instance_eval(&block) - - @db = Database.new(path: config.database_path) - end - - def rule_set(name, &block) - @rule_sets << RuleSet.new(name, block) - end - end -end - -def eval_rules - Osugiru.rule_sets.each do |rule_set| - rule_set.run - end -end - - diff --git a/lib/osugiru/config.rb b/lib/osugiru/config.rb deleted file mode 100644 index 033d501..0000000 --- a/lib/osugiru/config.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -module Osugiru - class Config - attr_accessor :database_path - end -end diff --git a/lib/osugiru/database.rb b/lib/osugiru/database.rb deleted file mode 100644 index 0d33a24..0000000 --- a/lib/osugiru/database.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Osugiru - class Database - attr_reader :db - - def initialize(path:) - @db = ::Notmuch::Database.new(path, mode: Notmuch::MODE_READ_WRITE) - end - - def query(query) - db.query(query) - end - - def search_messages(query) - query(query).search_messages - end - end -end diff --git a/lib/osugiru/helpers.rb b/lib/osugiru/helpers.rb deleted file mode 100644 index c493bbd..0000000 --- a/lib/osugiru/helpers.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module Osugiru - module Helpers - def self.header_name_from_dsl(method_name) - method_name - .to_s - .split('_') - .map(&:capitalize) - .join('-') - end - end -end diff --git a/lib/osugiru/message.rb b/lib/osugiru/message.rb deleted file mode 100644 index 0555724..0000000 --- a/lib/osugiru/message.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -module Osugiru - class Message - attr_reader :message - - def initialize(msg:) - @message = msg - @tags = @message.tags - end - - def method_missing(method_name, *_args) - handle_get(Osugiru::Helpers.header_name_from_dsl(method_name)) - end - - def handle_get(header_name) - message.header(header_name) if message.header(header_name) - end - - def tags - @tags - end - - def tags=(new_tags) - @tags = new_tags - end - - def save! - message.remove_all_tags - tags.each do |tag| - message.add_tag(tag) - end - binding.pry - end - end -end diff --git a/lib/osugiru/messages.rb b/lib/osugiru/messages.rb deleted file mode 100644 index 4af19b6..0000000 --- a/lib/osugiru/messages.rb +++ /dev/null @@ -1,103 +0,0 @@ -# frozen_string_literal: true - -require 'pry' -module Osugiru - class Messages - AND_CONJUCTION = :and - OR_CONJUCTION = :or - DEFAULT_CONJUCTION = AND_CONJUCTION - - Token = Struct.new(:conjuction, :field, :value, :messages) do - def to_query - conjuction_to_use = messages.next_token_id > 1 ? conjuction : '' - return "#{conjuction_to_use} (#{value})" if field == :direct - - "#{conjuction_to_use} (#{field}:#{value})" - end - end - - TokenGroup = Struct.new(:conjuction, :tokens, :messages) do - def initialize(*) - super - end - - def to_query - tokens.flat_map(&:to_query).join(' ').to_s - end - - def push_token(field, value) - if value.is_a? String - tokens.push(Token.new(conjuction, field, value, messages)) - elsif value.is_a? Array - group = TokenGroup.new(OR_CONJUCTION, [], messages) - value.each do |val| - group.push_token(field, val) - end - tokens.push group - - end - end - - def next_token_id - @token_count += 1 - end - end - - attr_accessor :query, :current_conjuction, :token_count - attr_reader :db - - def initialize(db:) - @db = db - @query = [] - @current_conjuction = AND_CONJUCTION - @token_count = 0 - end - - def or - @current_conjuction = OR_CONJUCTION - self - end - - def and - @current_conjuction = AND_CONJUCTION - self - end - - def filter(params) - case params - when String - add_filter_from_string(params) - when Hash - add_filter_from_hash(params) - end - - self - end - - def each - db.search_messages(query_string).each do |msg| - yield(Message.new(msg: msg)) - end - end - - def add_filter_from_string(string) - @query << Token.new(current_conjuction, :direct, string, self) - end - - def add_filter_from_hash(hash) - group = TokenGroup.new(current_conjuction, [], self) - hash.each do |key, value| - group.push_token(key, value) - end - @query << group - end - - def query_string - query.map(&:to_query).join(' ') - end - - def next_token_id - @token_count += 1 - end - end -end diff --git a/lib/osugiru/ruleset.rb b/lib/osugiru/ruleset.rb deleted file mode 100644 index a9bac16..0000000 --- a/lib/osugiru/ruleset.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Osugiru - RuleSet = Struct.new(:name, :rule) do - def messages - Messages.new(db: Osugiru.db) - end - - def run - instance_eval(&rule) - end - end -end -- cgit v1.2.3