Class: Kotoshu::Cli::PersonalCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/kotoshu/cli/personal_command.rb

Overview

Thor subcommand for managing the user's personal dictionary.

The personal dictionary is a Hunspell-style .dic file at ~/.config/kotoshu/personal.dic (override via KOTOSHU_PERSONAL_DIC). Words added here are treated as correct during spell-checking.

Examples:

Add a word

kotoshu personal add Kotoshu

List personal words

kotoshu personal list

Remove a word

kotoshu personal remove Kotoshu

Import words from a file (one word per line)

kotoshu personal import project-terms.txt

Show the on-disk location

kotoshu personal path

Instance Method Summary collapse

Instance Method Details

#add(*words) ⇒ Object



29
30
31
32
33
34
# File 'lib/kotoshu/cli/personal_command.rb', line 29

def add(*words)
  raise Kotoshu::Cli::Errors::UsageError, "No words given" if words.empty?

  added = words.count { |word| Kotoshu::PersonalDictionary.add_word(word) }
  puts "Added #{added} #{plural(added, 'word')} to #{Kotoshu::PersonalDictionary.file_path}"
end

#clearObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kotoshu/cli/personal_command.rb', line 91

def clear
  unless options[:yes]
    print "Clear the entire personal dictionary? [y/N] "
    answer = $stdin.gets&.chomp&.downcase
    return unless ["y", "yes"].include?(answer)
  end

  file = Kotoshu::PersonalDictionary.file_path
  return unless File.exist?(file)

  File.write(file, "")
  puts "Cleared #{file}"
end

#import(*files) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kotoshu/cli/personal_command.rb', line 58

def import(*files)
  raise Kotoshu::Cli::Errors::UsageError, "No files given" if files.empty?

  candidates = []
  files.each do |path|
    raise Kotoshu::Cli::Errors::UsageError, "File not found: #{path}" unless File.exist?(path)

    File.foreach(path, chomp: true) do |line|
      word = line.strip
      next if word.empty? || word.start_with?("#")

      candidates << word
    end
  end

  if options[:dry_run]
    puts "Would import #{candidates.length} #{plural(candidates.length, 'word')}:"
    candidates.each { |w| puts "  #{w}" }
    return
  end

  added = candidates.count { |word| Kotoshu::PersonalDictionary.add_word(word) }
  puts "Imported #{added} #{plural(added, 'word')} from #{files.length} #{plural(files.length, 'file')}"
end

#listObject



45
46
47
48
49
50
51
52
53
# File 'lib/kotoshu/cli/personal_command.rb', line 45

def list
  words = Kotoshu::PersonalDictionary.words
  if words.empty?
    puts "(empty)"
    return
  end

  words.each { |word| puts word }
end

#pathObject



84
85
86
# File 'lib/kotoshu/cli/personal_command.rb', line 84

def path
  puts Kotoshu::PersonalDictionary.file_path
end

#remove(*words) ⇒ Object



37
38
39
40
41
42
# File 'lib/kotoshu/cli/personal_command.rb', line 37

def remove(*words)
  raise Kotoshu::Cli::Errors::UsageError, "No words given" if words.empty?

  removed = words.count { |word| Kotoshu::PersonalDictionary.remove_word(word) }
  puts "Removed #{removed} #{plural(removed, 'word')}"
end