3 个版本
0.1.2 | 2023年3月27日 |
---|---|
0.1.1 | 2023年3月15日 |
0.1.0 | 2023年3月15日 |
806 在 文本处理 中
每月下载 31 次
34KB
664 代码行
refac:自动编辑文本。
工作流程
- 选择一些文本。
- 运行命令,写下您想要更改的说明。
- 永远不再直接编辑文本。
此工具调用 openai api 的 edits
端点。您需要自己的 api 密钥才能使用它。使用 refac login
输入您的 api 密钥。它将被保存在您的家目录中,以供将来使用。
此工具使用您的 openai 账户,因此使用并不完全免费。它使用 gpt-4 模型的聊天完成端点,每次完成约为 ~$0.10。您可以使用 https://platform.openai.com/account 查看您的使用情况。
设置
# This tool can be installed using cargo.
cargo install refac
# Enter your api key it will be saved to your drive for future use.
refac login
试用一下
> refac tor 'The quick brown fox jumps over the lazy dog.' 'convert to all caps'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
> refac tor '
def add(a: int, b: int):
return a + b
' 'turn this into a command line program that accepts a and b as arguments, printing the result'`
# I've transformed your `add` function into a command-line script that accepts two integer arguments and prints their sum.
# Based on the syntax of your code, I assume you're using Python. If this is incorrect, please let me know.
# Run the script with `python add.py <a> <b>` where `<a>` and `<b>` are the integers you want to add.
# --refac
import sys
def add(a: int, b: int):
return a + b
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python add.py <a> <b>")
sys.exit(1)
try:
a = int(sys.argv[1])
b = int(sys.argv[2])
except ValueError:
print("Both arguments must be integers.")
sys.exit(1)
result = add(a, b)
print(f"The result of {a} + {b} is {result}.")
> refac tor '
fn factorial(a: usize) -> usize {
if a <= 1 {
1
} else {
a * factorial(a - 1)
}
}
' 'switch to an iterative implementation'
fn factorial(a: usize) -> usize {
// Switched to an iterative implementation using a loop.
// --refac
let mut result = 1;
for i in 1..=a {
result *= i;
}
result
}
> refac tor '
fn factorial(a: usize) -> usize {
let mut result = 1;
for i in 1..=a {
result *= i;
}
result
}
' 'use an iterator and .fold()'
fn factorial(a: usize) -> usize {
(1..=a).fold(1, |acc, x| acc * x)
}
> refac tor '' 'implement hello world in rust'
// Here's a simple "Hello, World!" program in Rust.
// Compile it with `rustc hello_world.rs` and run it with `./hello_world`.
// --refac
fn main() {
println!("Hello, World!");
}
> refac tor '
Hey [Name],
I\'m writing to ask if you can lend me $100. I promise I\'ll pay you back as soon as possible.
Thanks,
[Your Name]
' 'make the email more formal and appropriate for a professional setting'
Subject: Request for Temporary Financial Assistance
Dear [Name],
I hope this email finds you well. I am reaching out to request a temporary financial loan of $100. Due to unforeseen circumstances, I find myself in a situation where I require immediate support. Please know that I do not take this request lightly, and I understand the responsibility that comes with borrowing money.
I would like to emphasize my commitment to repaying the loan as promptly as possible. Your assistance during this challenging time would be greatly appreciated, and I assure you that I am doing everything in my power to improve my financial situation.
Should you have any concerns, questions, or require additional information, please do not hesitate to reach out to me. I am more than happy to discuss the matter further.
Thank you very much for your time and consideration. I eagerly await your response.
Sincerely,
在您最喜欢的文本编辑器中使用 Refac
首先,请确保您已安装
- refac
- 使用
refac login
输入您的 api 密钥
Emacs
安装并登录后,将以下代码块添加到您的 init.el 中
(defun refac-git-style-diff (a b)
(with-temp-buffer (let ((temp-file-a (make-temp-file "a"))
(temp-file-b (make-temp-file "b")))
(unwind-protect (progn (write-region a nil temp-file-a)
(write-region b nil temp-file-b)
(call-process "diff" nil t nil "-u" temp-file-a temp-file-b)
(buffer-string))
(delete-file temp-file-a)
(delete-file temp-file-b)))))
(defun refac-filter-diff-output (diff-output)
(with-temp-buffer (insert diff-output)
(goto-char (point-min))
(while (not (eobp))
(let ((line
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(if (or (string-prefix-p "--- " line)
(string-prefix-p "+++ " line)
(string-prefix-p "\\ No newline at end of file" line))
(delete-region (line-beginning-position)
(1+ (line-end-position)))
(forward-line))))
(buffer-string)))
(defun refac-call-executable (selected-text transform)
(let (result exit-status refac-executable)
(setq refac-executable (executable-find "refac"))
(if refac-executable (with-temp-buffer
(setq exit-status (call-process refac-executable nil t nil "tor" selected-text transform))
(setq result (buffer-string)))
(error
"refac executable not found"))
(if (zerop exit-status) result
(error
"refac returned a non-zero exit status: %d. Error: %s"
exit-status
result))))
(defun refac (start end)
(interactive "r")
(let* ((selected-text
(buffer-substring-no-properties
start
end))
(transform (read-string "Enter transformation instruction: ")))
(let ((result (refac-call-executable selected-text transform)))
(delete-region start end)
(insert result)
(let ((diff-output (refac-git-style-diff selected-text result)))
(message (refac-filter-diff-output diff-output))))))
如果您喜欢这种类型的话,可以将其绑定到某个键。
(global-set-key (kbd "C-c r") 'refac)
非 Emacs
欢迎您的贡献!
许可证
在以下任一许可证下获得许可
- Apache License, Version 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
由您选择。
贡献
除非您明确声明,否则任何有意提交以包含在作品中的贡献,根据 Apache-2.0 许可证的定义,应按上述方式双重许可,不附加任何额外条款或条件。
依赖关系
~6–21MB
~277K SLoC