Skip to content

Commit c4a116f

Browse files
committed
初始化提交
1 parent ff4ae5c commit c4a116f

8 files changed

Lines changed: 770 additions & 2 deletions

File tree

README.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,103 @@
1-
# devc
2-
开发者剪切板工具
1+
# devc - 开发者剪切板监听与翻译工具
2+
3+
## 简介
4+
5+
`devc` 是一款运行在 Windows 系统下的剪切板监听小工具,使用 Go 语言开发,通过调用 Windows API
6+
实现功能。它能够记录每天的剪切板内容,并提供翻译功能,支持中英互译,特别适用于开发人员在编写代码或数据库表字段时,快速将中文字段翻译为英文字段。
7+
8+
## 功能特点
9+
10+
- **剪切板内容记录**:每天自动记录剪切板中的内容。
11+
- **翻译功能**
12+
- 支持中英互译。
13+
- 支持关键字翻译,可自定义关键字长度。
14+
- 翻译后的内容可以重新写入剪切板。
15+
- **命名规则转换**
16+
- 支持大驼峰命名(PascalCase)。
17+
- 支持小驼峰命名(camelCase)。
18+
- 支持下划线命名(snake_case)。
19+
- **命令行配置**
20+
- 支持通过命令行参数进行个性化配置。
21+
- 支持将 CMD 窗口置顶。
22+
- **应用场景**
23+
- 特别适合数据库表字段设置,可将中文字段自动翻译为英文字段。
24+
25+
## 命令行参数
26+
27+
运行 `devc -h` 可查看所有支持的命令行参数及其说明:
28+
29+
```bash
30+
devc -h
31+
Usage of devc:
32+
-han int
33+
[关键字翻译] 汉语关键字长度,默认支持4个汉字,最少2个汉字。例如:订单编号、订单状态、创建时间 (default 4)
34+
-hump int
35+
[关键字翻译] 将翻译内容写入剪切板,命名规则:1.大驼峰 2.小驼峰 3.下划线 (default 1)
36+
-top
37+
[窗口] 置顶当前 CMD 窗口
38+
-tran
39+
[翻译工具] 开启自动检测翻译,关键字翻译将关闭
40+
-w
41+
[翻译工具] 将翻译内容写入剪切板
42+
```
43+
44+
[**注意**]:关键字翻译和翻译工具命令不能同时,同时使用优先翻译工具,未指定参数默认关键字翻译
45+
46+
### 参数说明
47+
48+
- `-han`:设置汉语关键字的长度,默认为 4 个汉字,最少为 2 个汉字。
49+
- `-hump`:设置翻译内容的命名规则:
50+
- `1`:大驼峰命名(PascalCase)。
51+
- `2`:小驼峰命名(camelCase)。
52+
- `3`:下划线命名(snake_case)。
53+
- `-top`:将当前 CMD 窗口置顶。
54+
- `-tran`:开启自动检测翻译,关键字翻译将关闭。
55+
- `-w`:将翻译后的内容写入剪切板。
56+
57+
## 使用示例
58+
59+
### 基础使用
60+
61+
```bash
62+
devc -han=3 -hump=2
63+
```
64+
65+
- 指定关键字长度为 3 个汉字。
66+
- 使用小驼峰命名规则(camelCase)。
67+
68+
### 置顶 CMD 窗口
69+
70+
```bash
71+
devc -top
72+
```
73+
74+
- 将当前 CMD 窗口置顶。
75+
76+
### 自动翻译
77+
78+
```bash
79+
devc -tran -w
80+
```
81+
82+
- 开启自动检测翻译。
83+
- 将翻译后的内容写入剪切板。
84+
85+
## 安装与运行
86+
87+
1. **下载**:从 [GitHub 仓库](https://github.com/systemmin/devc) 下载最新版本的 `devc`
88+
2. **安装**:将下载的文件解压到任意目录,或丢到 `C:\Windows\System32` 目录下可直接 `cmd` 使用。
89+
3. **运行**:打开 CMD 窗口,切换到解压目录,运行以下命令:
90+
```bash
91+
devc -h
92+
```
93+
94+
## 注意事项
95+
96+
- 本工具仅支持 Windows 系统。
97+
- 如果遇到问题,请查看日志文件或联系开发者。
98+
- 关键字翻译和翻译工具命令不能同时,同时使用优先翻译工具,未指定参数默认关键字翻译
99+
100+
## 贡献与反馈
101+
102+
欢迎通过 GitHub 提交 Issue 或 Pull Request,帮助改进 `devc`
103+

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module devc
2+
3+
go 1.23.0

internal/classify_text.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @Time : 2025/3/18 10:57
3+
* @File : classify_text.go
4+
* @Software: dev_clip
5+
* @Author : Mr.Fang
6+
* @Description: 文本内容分类
7+
*/
8+
9+
package internal
10+
11+
import (
12+
"fmt"
13+
"regexp"
14+
"strings"
15+
)
16+
17+
const (
18+
CHINESE = iota // 中文
19+
CAMEL_CASE
20+
SNAKE_CASE
21+
CODE_SNIPPET
22+
ENGLISH
23+
OTHER
24+
)
25+
26+
// 纯中文检测
27+
func isChinese(text string) bool {
28+
re := regexp.MustCompile(`^[\p{Han}]+$`)
29+
return re.MatchString(text)
30+
}
31+
32+
// IsCamelCase 驼峰命名检测
33+
func IsCamelCase(text string) bool {
34+
// 小驼峰
35+
re := regexp.MustCompile(`^[a-z]+(?:[A-Z][a-z]*)+$`)
36+
match := re.MatchString(text)
37+
if !match {
38+
// 大驼峰
39+
re = regexp.MustCompile(`^[A-Z][a-z]+(?:[A-Z][a-z]*)+$`)
40+
return re.MatchString(text)
41+
}
42+
return match
43+
}
44+
45+
// 下划线命名检测
46+
func isSnakeCase(text string) bool {
47+
re := regexp.MustCompile(`^[a-z]+(?:_[a-z]+)+$`)
48+
return re.MatchString(text)
49+
}
50+
51+
// IsCodeSnippet 代码片段检测(通过特殊符号判断)
52+
func IsCodeSnippet(text string) bool {
53+
codeChars := []string{"{", "}", ";", "(", ")", "[", "]", "<", ">", "=", "\""}
54+
for _, char := range codeChars {
55+
if strings.Contains(text, char) {
56+
// [A-Za-z]+\(
57+
if char == "(" {
58+
re := regexp.MustCompile(`[A-Za-z]+\(+`)
59+
return re.MatchString(text)
60+
}
61+
return true
62+
}
63+
}
64+
// 第二种情况;空格拆分只有一个长度,并且中间有.连接
65+
if len(strings.Split(text, " ")) == 1 && strings.Contains(text, ".") {
66+
_, b := ExtractURLs(text)
67+
if b {
68+
return false
69+
}
70+
return true
71+
}
72+
return false
73+
}
74+
75+
// IsEnglish 纯英文检测(排除驼峰命名、下划线命名、代码片段)
76+
func IsEnglish(text string) bool {
77+
if IsCamelCase(text) || isSnakeCase(text) || IsCodeSnippet(text) {
78+
return false
79+
}
80+
re := regexp.MustCompile(`^[a-zA-Z\s.,!?'’]+$`)
81+
if re.MatchString(text) {
82+
return true
83+
}
84+
return IsEnglishContent(text)
85+
}
86+
87+
// IsEnglishContent 内容检测,只要英文占比超过 60%
88+
func IsEnglishContent(text string) bool {
89+
re := regexp.MustCompile(`^[a-zA-Z\s.,!?'’]+$`)
90+
split := strings.Split(text, " ")
91+
l := len(split)
92+
count := 0
93+
for _, s := range split {
94+
if re.MatchString(s) {
95+
count++
96+
}
97+
}
98+
percentage, _ := CalcPercentage(float64(count), float64(l))
99+
return percentage > 60
100+
}
101+
102+
// CalcPercentage 计算百分比 numerator 分母 denominator 分子
103+
func CalcPercentage(numerator, denominator float64) (int, error) {
104+
if denominator == 0 {
105+
return 0, fmt.Errorf("除数为 0 ")
106+
}
107+
percentage := (numerator / denominator) * 100
108+
return int(percentage), nil
109+
}
110+
111+
// ExtractURLs 提取 URL,并检测是否全是 URL
112+
func ExtractURLs(text string) ([]string, bool) {
113+
re := regexp.MustCompile(`https?://[^\s]+`)
114+
urls := re.FindAllString(text, -1)
115+
return urls, len(urls) > 0 && len(urls[0]) == len(text)
116+
}
117+
118+
// ClassifyText 分类文本
119+
func ClassifyText(text string) int {
120+
if isChinese(text) {
121+
fmt.Println("纯中文内容")
122+
return CHINESE
123+
} else if IsCamelCase(text) {
124+
fmt.Println("驼峰命名")
125+
return CAMEL_CASE
126+
} else if isSnakeCase(text) {
127+
fmt.Println("下划线命名")
128+
return SNAKE_CASE
129+
} else if IsCodeSnippet(text) {
130+
fmt.Println("代码片段")
131+
return CODE_SNIPPET
132+
} else if IsEnglish(text) {
133+
fmt.Println("纯英文内容")
134+
return ENGLISH
135+
} else {
136+
fmt.Println("无法分类的内容")
137+
return OTHER
138+
}
139+
}

0 commit comments

Comments
 (0)