From b2b3be3a6e51c21509a7b6a9c16edbf0db655c79 Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Wed, 10 Apr 2024 18:07:11 +0800 Subject: [PATCH] Update .ameba.yml --- .ameba.yml | 58 ++++++++++++++++++------------ README.md | 2 ++ src/cli.cr | 11 +++--- src/translater/bing.cr | 2 +- src/translater/selenium/session.cr | 6 ++-- src/translater/volc.cr | 4 +-- 6 files changed, 50 insertions(+), 33 deletions(-) diff --git a/.ameba.yml b/.ameba.yml index 918670c..2073ade 100644 --- a/.ameba.yml +++ b/.ameba.yml @@ -1,24 +1,34 @@ # This configuration file was generated by `ameba --gen-config` -# on 2024-04-03 14:18:15 UTC using Ameba version 1.6.1. +# on 2024-04-10 10:06:54 UTC using Ameba version 1.6.1. # The point is for the user to remove these configuration records # one by one as the reported problems are removed from the code base. -# Problems found: 7 +# Problems found: 4 # Run `ameba --only Lint/NotNil` for details Lint/NotNil: Description: Identifies usage of `not_nil!` calls Excluded: - src/translater/selenium/session.cr - - src/translater/ali.cr + - src/translater.cr + Enabled: true + Severity: Warning + +# Problems found: 9 +# Run `ameba --only Style/ParenthesesAroundCondition` for details +Style/ParenthesesAroundCondition: + Description: Disallows redundant parentheses around control expressions + ExcludeTernary: false + AllowSafeAssignment: false + Excluded: + - src/translater/selenium/session.cr - src/translater/bing.cr - src/translater/youdao.cr - src/translater/baidu.cr - - src/translater/tencent.cr - - src/translater/volc.cr + - src/cli.cr Enabled: true - Severity: Warning + Severity: Convention -# Problems found: 8 +# Problems found: 11 # Run `ameba --only Naming/BlockParameterName` for details Naming/BlockParameterName: Description: Disallows non-descriptive block parameter names @@ -26,8 +36,8 @@ Naming/BlockParameterName: AllowNamesEndingInNumbers: true Excluded: - src/translater/ali.cr - - src/translater.cr - src/cli.cr + - src/translater.cr AllowedNames: - _ - e @@ -52,12 +62,13 @@ Naming/BlockParameterName: Enabled: true Severity: Convention -# Problems found: 1 +# Problems found: 2 # Run `ameba --only Documentation/DocumentationAdmonition` for details Documentation/DocumentationAdmonition: Description: Reports documentation admonitions Timezone: UTC Excluded: + - src/translater/volc.cr - src/cli.cr Admonitions: - TODO @@ -66,23 +77,26 @@ Documentation/DocumentationAdmonition: Enabled: true Severity: Warning -# Problems found: 1 -# Run `ameba --only Lint/ShadowingOuterLocalVar` for details -Lint/ShadowingOuterLocalVar: - Description: Disallows the usage of the same name as outer local variables for block - or proc arguments +# Problems found: 2 +# Run `ameba --only Metrics/CyclomaticComplexity` for details +Metrics/CyclomaticComplexity: + Description: Disallows methods with a cyclomatic complexity higher than `MaxComplexity` + MaxComplexity: 10 Excluded: - - src/cli.cr + - src/translater.cr Enabled: true Severity: Warning # Problems found: 2 -# Run `ameba --only Style/ParenthesesAroundCondition` for details -Style/ParenthesesAroundCondition: - Description: Disallows redundant parentheses around control expressions - ExcludeTernary: false - AllowSafeAssignment: false +# Run `ameba --only Lint/DebugCalls` for details +Lint/DebugCalls: + Description: Disallows debug-related calls Excluded: - - src/cli.cr + - 1.cr + MethodNames: + - p + - p! + - pp + - pp! Enabled: true - Severity: Convention + Severity: Warning diff --git a/README.md b/README.md index 23a8537..362c1c4 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ 默认会随机选择一个支持的引擎来翻译,这可以避免过于频繁的使用某一翻译引擎而导致 IP 被封。 +搭配 [goldendict](https://github.com/goldendict/goldendict) 作为词典之一,一起食用效果更佳! + ```sh ╰─ $ bin/translater 'hello world!' Using Ali diff --git a/src/cli.cr b/src/cli.cr index 89660a7..d058f37 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -31,8 +31,9 @@ PROFILE_DB_FILE = "sqlite3:#{find_db_path("profile.db")}" SESSION_DB_FILE = "sqlite3:#{find_db_path("session.db")}" def profile_db_exists? - # TODO: 还要检测文件大小 - File.exists?(PROFILE_DB_FILE.split(':')[1]) + db_file = PROFILE_DB_FILE.split(':')[1] + + File.exists?(db_file) && File.info(db_file).size > 0 end enum TargetLanguage @@ -104,8 +105,8 @@ USAGE inputs = e.split(",") engine_list = [] of String - inputs.each do |input| - if (engine = Engine.parse?(input)) + inputs.each do |i| + if (engine = Engine.parse?(i)) engine_list << engine.to_s else abort "Supported options: #{Engine.names.map(&.downcase).join ", "}" @@ -122,7 +123,7 @@ USAGE db.query "select name from fastest_engine limit 1;" do |rs| rs.each do # If fastest_engine is empty, this block will be ignored. - if engine = Engine.parse(rs.read(String)) + if (engine = Engine.parse(rs.read(String))) engine_list = [engine.to_s] end end diff --git a/src/translater/bing.cr b/src/translater/bing.cr index 123a511..6dca0dc 100644 --- a/src/translater/bing.cr +++ b/src/translater/bing.cr @@ -30,7 +30,7 @@ class Translater gets end - while result = document_manager.execute_script(%{return document.querySelector("#{output_selector}").value}) + while (result = document_manager.execute_script %{return document.querySelector("#{output_selector}").value}) break unless result.strip == "..." sleep 0.1 diff --git a/src/translater/selenium/session.cr b/src/translater/selenium/session.cr index efadbc7..921adcc 100644 --- a/src/translater/selenium/session.cr +++ b/src/translater/selenium/session.cr @@ -21,7 +21,7 @@ class Selenium::Session element : Selenium::Element? = nil timeout(seconds) do - until element = find_by_selector(selector, was_hidden: was_hidden) + until (element = find_by_selector selector, was_hidden: was_hidden) sleep 0.1 end end @@ -36,7 +36,7 @@ class Selenium::Session element = nil loop do - until element = find_by_selector(selector, was_hidden: was_hidden) + until (element = find_by_selector selector, was_hidden: was_hidden) sleep 0.1 end @@ -52,7 +52,7 @@ class Selenium::Session end def find_by_selector_wait!(selector : String, *, was_hidden : Bool = false) : Selenium::Element - until element = find_by_selector(selector, was_hidden: was_hidden) + until (element = find_by_selector selector, was_hidden: was_hidden) sleep 0.1 end diff --git a/src/translater/volc.cr b/src/translater/volc.cr index 47db34e..8c8a8a0 100644 --- a/src/translater/volc.cr +++ b/src/translater/volc.cr @@ -1,4 +1,4 @@ -# FIXME: still not work! +# FIXME: volc still not work! class Translater class Volc @@ -80,7 +80,7 @@ class Translater output_editor_selector = "div.slate-editor[contenteditable='false']" # 这两个有内容时才存在 - input_selector = "#{input_editor_selector} span[data-slate-string='true']" + _input_selector = "#{input_editor_selector} span[data-slate-string='true']" output_selector = "#{output_editor_selector} span[data-slate-string='true']" input_ele = session.find_by_selector_wait! input_editor_selector