Class: Slk::Services::EmojiSearcher

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/emoji_searcher.rb

Overview

Searches standard and workspace custom emoji

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir:, emoji_dir:, on_debug: nil) ⇒ EmojiSearcher

Returns a new instance of EmojiSearcher.



7
8
9
10
11
# File 'lib/slk/services/emoji_searcher.rb', line 7

def initialize(cache_dir:, emoji_dir:, on_debug: nil)
  @cache_dir = cache_dir
  @emoji_dir = emoji_dir
  @on_debug = on_debug
end

Instance Method Details

#search(query, workspaces: []) ⇒ Hash

Search emoji by pattern

Parameters:

  • query (String)

    Search query

  • workspaces (Array<Workspace>) (defaults to: [])

    Workspaces to search (nil for all)

Returns:

  • (Hash)

    Results grouped by source



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/slk/services/emoji_searcher.rb', line 17

def search(query, workspaces: [])
  pattern = Regexp.new(Regexp.escape(query), Regexp::IGNORECASE)
  results = []

  # Search standard emoji
  results.concat(search_standard(pattern))

  # Search workspace custom emoji
  workspaces.each do |workspace|
    results.concat(search_workspace(workspace, pattern))
  end

  # Group by source
  results.group_by { |r| r[:source] }
end

#search_standard(pattern) ⇒ Array<Hash>

Search standard emoji only

Parameters:

  • pattern (Regexp)

    Pattern to match

Returns:

  • (Array<Hash>)

    Matching emoji with :name, :char, :source



36
37
38
39
40
41
42
43
# File 'lib/slk/services/emoji_searcher.rb', line 36

def search_standard(pattern)
  gemoji = load_gemoji
  return [] unless gemoji

  gemoji.filter_map do |name, char|
    { name: name, char: char, source: 'standard' } if name.match?(pattern)
  end
end

#search_workspace(workspace, pattern) ⇒ Array<Hash>

Search a workspace’s custom emoji

Parameters:

  • workspace (Workspace)

    Workspace to search

  • pattern (Regexp)

    Pattern to match

Returns:

  • (Array<Hash>)

    Matching emoji with :name, :path, :source



49
50
51
52
53
54
55
56
57
# File 'lib/slk/services/emoji_searcher.rb', line 49

def search_workspace(workspace, pattern)
  workspace_dir = File.join(@emoji_dir, workspace.name)
  return [] unless Dir.exist?(workspace_dir)

  Dir.glob(File.join(workspace_dir, '*')).filter_map do |filepath|
    name = File.basename(filepath, '.*')
    { name: name, path: filepath, source: workspace.name } if name.match?(pattern)
  end
end