Class: Rufio::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/config.rb

Constant Summary collapse

DEFAULT_LANGUAGE =

Default language settings

'en'
AVAILABLE_LANGUAGES =
%w[en ja].freeze
CONFIG_DIR =

設定ディレクトリとファイルパス

File.expand_path('~/.config/rufio').freeze
CONFIG_RB_PATH =
File.join(CONFIG_DIR, 'config.rb').freeze
SCRIPT_PATHS_YML =
File.join(CONFIG_DIR, 'script_paths.yml').freeze
BOOKMARKS_YML =
File.join(CONFIG_DIR, 'bookmarks.yml').freeze
YAML_CONFIG_PATH =

後方互換性のためのパス(非推奨)

File.join(CONFIG_DIR, 'config.yml').freeze
LOCAL_YAML_PATH =
'./rufio.yml'
MESSAGES =

Multi-language message definitions

{
  'en' => {
    # Application messages
    'app.interrupted' => 'rufio interrupted',
    'app.error_occurred' => 'Error occurred',
    'app.terminated' => 'rufio terminated',

    # File operations
    'file.not_found' => 'File not found',
    'file.not_readable' => 'File not readable',
    'file.read_error' => 'File read error',
    'file.binary_file' => 'Binary file',
    'file.cannot_preview' => 'Cannot preview',
    'file.encoding_error' => 'Character encoding error - cannot read file',
    'file.preview_error' => 'Preview error',
    'file.error_prefix' => 'Error',

    # Keybind messages
    'keybind.invalid_key' => 'invalid key',
    'keybind.search_text' => 'Search text: ',
    'keybind.no_matches' => 'No matches found.',
    'keybind.press_any_key' => 'Press any key to continue...',
    'keybind.input_filename' => 'Enter filename: ',
    'keybind.input_dirname' => 'Enter directory name: ',
    'keybind.invalid_filename' => 'Invalid filename (cannot contain / or \\)',
    'keybind.invalid_dirname' => 'Invalid directory name (cannot contain / or \\)',
    'keybind.file_exists' => 'File already exists',
    'keybind.directory_exists' => 'Directory already exists',
    'keybind.file_created' => 'File created',
    'keybind.directory_created' => 'Directory created',
    'keybind.creation_error' => 'Creation error',

    # UI messages
    'ui.operation_prompt' => 'Operation: ',

    # Help text
    'help.full' => 'j/k:move h:back l:enter o:open g/G:top/bottom r:refresh f:filter s:search F:content a/A:create m/p/x:ops b:bookmark z:zoxide 1-9:goto q:quit',
    'help.short' => 'j/k:move h:back l:enter o:open f:filter s:search b:bookmark z:zoxide 1-9:goto q:quit',

    # Health check messages
    'health.title' => 'rufio Health Check',
    'health.ruby_version' => 'Ruby version',
    'health.required_gems' => 'Required gems',
    'health.fzf' => 'fzf (file search)',
    'health.rga' => 'rga (content search)',
    'health.zoxide' => 'zoxide (directory history)',
    'health.bat' => 'bat (syntax highlight)',
    'health.file_opener' => 'System file opener',
    'health.summary' => 'Summary:',
    'health.ok' => 'OK',
    'health.warnings' => 'Warnings',
    'health.errors' => 'Errors',
    'health.all_passed' => 'All checks passed! rufio is ready to use.',
    'health.critical_missing' => 'Some critical components are missing. rufio may not work properly.',
    'health.optional_missing' => 'Some optional features are unavailable. Basic functionality will work.',
    'health.all_gems_installed' => 'All required gems installed',
    'health.missing_gems' => 'Missing gems',
    'health.gem_install_instruction' => 'Run: gem install',
    'health.tool_not_found' => 'not found',
    'health.unknown_platform' => 'Unknown platform',
    'health.file_open_may_not_work' => 'File opening may not work properly',
    'health.macos_opener' => 'macOS file opener',
    'health.linux_opener' => 'Linux file opener',
    'health.windows_opener' => 'Windows file opener',
    'health.install_brew' => 'Install: brew install',
    'health.install_apt' => 'Install: apt install',
    'health.install_guide' => 'Install: Check the installation guide for your platform',
    'health.rga_releases' => 'Install: https://github.com/phiresky/ripgrep-all/releases',
    'health.ruby_upgrade_needed' => 'Please upgrade Ruby to version 2.7.0 or higher'
  },

  'ja' => {
    # Application messages
    'app.interrupted' => 'rufio interrupted',
    'app.error_occurred' => 'Error occurred',
    'app.terminated' => 'rufio terminated',

    # File operations
    'file.not_found' => 'File not found',
    'file.not_readable' => 'File not readable',
    'file.read_error' => 'File read error',
    'file.binary_file' => 'Binary file',
    'file.cannot_preview' => 'Cannot preview',
    'file.encoding_error' => 'Encoding error - cannot read file',
    'file.preview_error' => 'Preview error',
    'file.error_prefix' => 'Error',

    # Keybind messages
    'keybind.invalid_key' => 'Invalid key',
    'keybind.search_text' => 'Search: ',
    'keybind.no_matches' => 'No matches found.',
    'keybind.press_any_key' => 'Press any key to continue...',
    'keybind.input_filename' => 'Enter filename: ',
    'keybind.input_dirname' => 'Enter directory name: ',
    'keybind.invalid_filename' => 'Invalid filename (cannot contain / or \\)',
    'keybind.invalid_dirname' => 'Invalid directory name (cannot contain / or \\)',
    'keybind.file_exists' => 'File already exists',
    'keybind.directory_exists' => 'Directory already exists',
    'keybind.file_created' => 'File created',
    'keybind.directory_created' => 'Directory created',
    'keybind.creation_error' => 'Creation error',

    # UI messages
    'ui.operation_prompt' => 'Operation: ',

    # Help text
    'help.full' => 'j/k:move h:back l:enter o:open g/G:top/end r:refresh f:filter s:search F:content a/A:create m/c/x:ops b:bookmark z:zoxide 1-9:jump q:quit',
    'help.short' => 'j/k:move h:back l:enter o:open f:filter s:search b:bookmark z:zoxide 1-9:jump q:quit',

    # Health check messages
    'health.title' => 'rufio Health Check',
    'health.ruby_version' => 'Ruby version',
    'health.required_gems' => 'Required gems',
    'health.fzf' => 'fzf (file search)',
    'health.rga' => 'rga (content search)',
    'health.zoxide' => 'zoxide (directory history)',
    'health.bat' => 'bat (syntax highlight)',
    'health.file_opener' => 'System file opener',
    'health.summary' => 'Summary:',
    'health.ok' => 'OK',
    'health.warnings' => 'Warnings',
    'health.errors' => 'Errors',
    'health.all_passed' => 'All checks passed! rufio is ready to use.',
    'health.critical_missing' => 'Critical components missing. rufio may not work properly.',
    'health.optional_missing' => 'Optional features unavailable. Basic features will work.',
    'health.all_gems_installed' => 'All required gems are installed',
    'health.missing_gems' => 'Missing gems',
    'health.gem_install_instruction' => 'Run: gem install',
    'health.tool_not_found' => 'not found',
    'health.unknown_platform' => 'Unknown platform',
    'health.file_open_may_not_work' => 'File open may not work properly',
    'health.macos_opener' => 'macOS file opener',
    'health.linux_opener' => 'Linux file opener',
    'health.windows_opener' => 'Windows file opener',
    'health.install_brew' => 'Install: brew install',
    'health.install_apt' => 'Install: apt install',
    'health.install_guide' => 'Install: Check the installation guide for your platform',
    'health.rga_releases' => 'Install: https://github.com/phiresky/ripgrep-all/releases',
    'health.ruby_upgrade_needed' => 'Please upgrade Ruby to version 2.7.0 or higher'
  }
}.freeze

Class Method Summary collapse

Class Method Details

.add_bookmark(path, bookmark_path, name) ⇒ Object

ブックマークを追加

Parameters:

  • path (String)

    YAMLファイルのパス

  • bookmark_path (String)

    ブックマークするパス

  • name (String)

    ブックマーク名



296
297
298
299
300
# File 'lib/rufio/config.rb', line 296

def add_bookmark(path, bookmark_path, name)
  bookmarks = load_bookmarks_from_yml(path)
  bookmarks << { path: bookmark_path, name: name }
  save_bookmarks_to_yml(path, bookmarks)
end

.add_script_path(path, new_path) ⇒ Object

スクリプトパスを追加

Parameters:

  • path (String)

    YAMLファイルのパス

  • new_path (String)

    追加するパス



245
246
247
248
249
250
251
252
# File 'lib/rufio/config.rb', line 245

def add_script_path(path, new_path)
  paths = load_script_paths(path)
  expanded = File.expand_path(new_path)
  return if paths.include?(expanded)

  paths << expanded
  save_script_paths(path, paths)
end

.available_languagesObject



189
190
191
# File 'lib/rufio/config.rb', line 189

def available_languages
  AVAILABLE_LANGUAGES.dup
end

.current_languageObject



166
167
168
# File 'lib/rufio/config.rb', line 166

def current_language
  @current_language ||= detect_language
end

.current_language=(lang) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/rufio/config.rb', line 170

def current_language=(lang)
  if AVAILABLE_LANGUAGES.include?(lang.to_s)
    @current_language = lang.to_s
  else
    raise ArgumentError, "Unsupported language: #{lang}. Available: #{AVAILABLE_LANGUAGES.join(', ')}"
  end
end

.load_bookmarks_from_yml(path = BOOKMARKS_YML) ⇒ Array<Hash>

ブックマークを読み込む

Parameters:

  • path (String) (defaults to: BOOKMARKS_YML)

    YAMLファイルのパス

Returns:

  • (Array<Hash>)

    ブックマーク配列



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rufio/config.rb', line 271

def load_bookmarks_from_yml(path = BOOKMARKS_YML)
  return [] unless File.exist?(path)

  yaml = YAML.safe_load(File.read(path), symbolize_names: true)
  return [] unless yaml.is_a?(Array)

  yaml.select { |b| b.is_a?(Hash) && b[:path] && b[:name] }
rescue StandardError => e
  warn "Failed to load bookmarks: #{e.message}"
  []
end

.load_config_rb(path = CONFIG_RB_PATH) ⇒ Object

config.rb を読み込む

Parameters:

  • path (String) (defaults to: CONFIG_RB_PATH)

    config.rb のパス



317
318
319
320
321
322
323
# File 'lib/rufio/config.rb', line 317

def load_config_rb(path = CONFIG_RB_PATH)
  return unless File.exist?(path)

  load path
rescue StandardError => e
  warn "Failed to load config.rb: #{e.message}"
end

.load_script_paths(path = SCRIPT_PATHS_YML) ⇒ Array<String>

スクリプトパスを読み込む

Parameters:

  • path (String) (defaults to: SCRIPT_PATHS_YML)

    YAMLファイルのパス

Returns:

  • (Array<String>)

    展開済みのパス配列



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/rufio/config.rb', line 222

def load_script_paths(path = SCRIPT_PATHS_YML)
  return [] unless File.exist?(path)

  yaml = YAML.safe_load(File.read(path))
  return [] unless yaml.is_a?(Array)

  yaml.map { |p| File.expand_path(p) }
rescue StandardError => e
  warn "Failed to load script paths: #{e.message}"
  []
end

.load_yaml_config(path) ⇒ Hash

YAML設定ファイルを読み込む

Parameters:

  • path (String)

    設定ファイルのパス

Returns:

  • (Hash)

    設定内容(シンボルキー)



352
353
354
355
356
357
358
359
360
# File 'lib/rufio/config.rb', line 352

def load_yaml_config(path)
  return {} unless File.exist?(path)

  yaml = YAML.safe_load(File.read(path), symbolize_names: true)
  yaml || {}
rescue StandardError => e
  warn "Failed to load YAML config: #{e.message}"
  {}
end

.message(key, **interpolations) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/rufio/config.rb', line 178

def message(key, **interpolations)
  msg = MESSAGES.dig(current_language, key) || MESSAGES.dig(DEFAULT_LANGUAGE, key) || key
  
  # Simple interpolation support
  interpolations.each do |placeholder, value|
    msg = msg.gsub("%{#{placeholder}}", value.to_s)
  end
  
  msg
end

.migrate_from_config_yml(old_config_yml, script_paths_yml, bookmarks_yml) ⇒ Object

古い config.yml から新形式にマイグレーション

Parameters:

  • old_config_yml (String)

    古い config.yml のパス

  • script_paths_yml (String)

    新しい script_paths.yml のパス

  • bookmarks_yml (String)

    新しい bookmarks.yml のパス



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rufio/config.rb', line 333

def migrate_from_config_yml(old_config_yml, script_paths_yml, bookmarks_yml)
  return unless File.exist?(old_config_yml)

  old_config = load_yaml_config(old_config_yml)

  # script_paths のマイグレーション
  if old_config[:script_paths].is_a?(Array) && !old_config[:script_paths].empty?
    save_script_paths(script_paths_yml, old_config[:script_paths])
  end

  # bookmarks のマイグレーション
  if old_config[:bookmarks].is_a?(Array) && !old_config[:bookmarks].empty?
    save_bookmarks_to_yml(bookmarks_yml, old_config[:bookmarks])
  end
end

.reload_yaml_config!Object

YAML設定をリロード



204
205
206
# File 'lib/rufio/config.rb', line 204

def reload_yaml_config!
  @yaml_config = nil
end

.remove_bookmark(path, name) ⇒ Object

ブックマークを削除

Parameters:

  • path (String)

    YAMLファイルのパス

  • name (String)

    削除するブックマーク名



305
306
307
308
309
# File 'lib/rufio/config.rb', line 305

def remove_bookmark(path, name)
  bookmarks = load_bookmarks_from_yml(path)
  bookmarks.reject! { |b| b[:name] == name }
  save_bookmarks_to_yml(path, bookmarks)
end

.remove_script_path(path, remove_path) ⇒ Object

スクリプトパスを削除

Parameters:

  • path (String)

    YAMLファイルのパス

  • remove_path (String)

    削除するパス



257
258
259
260
261
262
# File 'lib/rufio/config.rb', line 257

def remove_script_path(path, remove_path)
  paths = load_script_paths(path)
  expanded = File.expand_path(remove_path)
  paths.delete(expanded)
  save_script_paths(path, paths)
end

.reset_config!Object

全設定をリセット



209
210
211
212
213
# File 'lib/rufio/config.rb', line 209

def reset_config!
  @yaml_config = nil
  @script_paths = nil
  @bookmarks = nil
end

.reset_language!Object



193
194
195
# File 'lib/rufio/config.rb', line 193

def reset_language!
  @current_language = nil
end

.save_bookmarks_to_yml(path, bookmarks) ⇒ Object

ブックマークを保存

Parameters:

  • path (String)

    YAMLファイルのパス

  • bookmarks (Array<Hash>)

    ブックマーク配列



286
287
288
289
290
# File 'lib/rufio/config.rb', line 286

def save_bookmarks_to_yml(path, bookmarks)
  ensure_config_directory(path)
  data = bookmarks.map { |b| { 'path' => b[:path], 'name' => b[:name] } }
  File.write(path, YAML.dump(data))
end

.save_script_paths(path, paths) ⇒ Object

スクリプトパスを保存

Parameters:

  • path (String)

    YAMLファイルのパス

  • paths (Array<String>)

    パス配列



237
238
239
240
# File 'lib/rufio/config.rb', line 237

def save_script_paths(path, paths)
  ensure_config_directory(path)
  File.write(path, YAML.dump(paths))
end

.save_yaml_config(path, key, value) ⇒ Object

YAML設定ファイルにセクションを保存

Parameters:

  • path (String)

    設定ファイルのパス

  • key (Symbol, String)

    保存するキー

  • value (Object)

    保存する値



366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/rufio/config.rb', line 366

def save_yaml_config(path, key, value)
  ensure_config_directory(path)

  existing = if File.exist?(path)
               YAML.safe_load(File.read(path), symbolize_names: true) || {}
             else
               {}
             end

  existing[key.to_sym] = value
  File.write(path, YAML.dump(stringify_keys(existing)))
  reload_yaml_config! if path == YAML_CONFIG_PATH
end

.yaml_configHash

YAML設定を取得(キャッシュあり)

Returns:

  • (Hash)

    YAML設定



199
200
201
# File 'lib/rufio/config.rb', line 199

def yaml_config
  @yaml_config ||= load_yaml_config(YAML_CONFIG_PATH)
end