Skip to content

Instantly share code, notes, and snippets.

@owenzhao
owenzhao / gist:92379e44167ad1cda78962b998e9dd54
Created February 14, 2025 04:41
update-ollama-models.sh
#!/bin/zsh
# 检查 Ollama 是否已安装
if ! command -v ollama &> /dev/null; then
echo "错误: Ollama 未安装"
exit 1
fi
# 函数:显示带颜色的消息
print_message() {
@owenzhao
owenzhao / delete_branches_single_project.sh
Created December 22, 2024 04:09
备份指定文件夹,然后删除指定文件夹除main之外的所有分支
#!/bin/zsh
# 检查是否提供了目录路径参数
if [ $# -eq 0 ]; then
echo "用法: $0 <目录路径>"
exit 1
fi
# 获取目录路径
project_dir=$1
@owenzhao
owenzhao / queue.swift
Created January 15, 2017 12:57
queue
@IBAction func printHello(_ sender: Any) {
// 队列,以消息绑定为例
if shouldWaitSwitch.isOn {
while true {
let isSuccess = WaitingTaskQueue.append(task: "你好!")
if isSuccess { break } else { continue }
}
if let _ = shouldWaitSwitch.actions(forTarget: WaitingTaskQueue.self, forControlEvent: .valueChanged) { // already set
return
@owenzhao
owenzhao / notification.swift
Last active January 15, 2017 12:56
notification
@IBAction func printHello(_ sender: Any) {
// 消息绑定
if shouldWaitSwitch.isOn {
if let _ = shouldWaitSwitch.actions(forTarget: self, forControlEvent: .valueChanged) { // already set
return
}
shouldWaitSwitch.addTarget(self, action: #selector(printHello(_:)), for: .valueChanged)
}
else {
@owenzhao
owenzhao / delay.swift
Last active January 15, 2017 12:55
delay
@IBAction func printHello(_ sender: Any) {
// 延迟
if shouldWaitSwitch.isOn {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { [unowned self] (timer) in
if self.shouldWaitSwitch.isOn { return }
print("你好!")
timer.invalidate()
})
}
@owenzhao
owenzhao / polling.swift
Created January 15, 2017 12:53
polling
@IBAction func printHello(_ sender: Any) {
// 忙等待
DispatchQueue(label: "default").async {
while true {
if self.shouldWaitSwitch.isOn { continue }
print("你好!")
break
}
}
@owenzhao
owenzhao / MenuAction.swift
Created November 23, 2016 10:24
Menu Actions
//MARK: - Help Menu
extension NSApplication {
@IBAction func contactDeveloper(_ sender: Any) {
let mailAddress = "your email address"
let mailBody = NSLocalizedString("Please use Chinese or English in your mail, if you can.", comment: "mail body")
let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
service.recipients = [mailAddress]
service.perform(withItems: [mailBody])
}
}
@owenzhao
owenzhao / progressIndicatorRunLoopPause.swift
Created November 1, 2016 14:54
stop main runloop to change the running order of progress indicator
func userDefaultsDidChange() {
guard let controller = tableViewController else { return }
let progressIndicator = { () -> NSProgressIndicator in
let frame = view.frame
let x = (frame.width - 50) / 2
let y = (frame.height - 50) / 2
let piFrame = NSMakeRect(x, y, 50, 50)
let pi = NSProgressIndicator(frame: piFrame)
pi.style = .spinningStyle
@owenzhao
owenzhao / openFilesDelay.swift
Last active November 1, 2016 15:17
Use delay to gather files
var rawFilenames = [String]()
// instance counter
var timer:NSTimer! = nil
func application(sender: NSApplication, openFiles filenames: [String]) {
if shouldQuitAppAfterConvert == nil {
shouldQuitAppAfterConvert = true
}
if timer != nil {
timer.invalidate()
@owenzhao
owenzhao / userDefaultsDelayByGCD.swift
Created November 1, 2016 13:45
use delay to run away from UserDefaults' bug
class TableViewController: NSViewController, NSTableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
run()
DispatchQueue.main.asyncAfter(wallDeadline: .now() + .milliseconds(100)) { [unowned self] () -> () in
NotificationCenter.default.post(name: TableViewControllerDidAppear, object: self)
}
}