Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Support #26

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,38 @@ You should have [FZF](https://github.com/junegunn/fzf) installed and have your o

## <a name="neovim"> </a>How to use it in Neovim

Manually
### Installation
#### Use [lazy.nvim](https://lazy.folke.io/installation), Recommended
```lua
require("lazy").setup({
spec = {
-- add your plugins here
{
"ChaosNyaruko/ondict",
event = "VeryLazy",
build = function(plugin)
require("ondict").install(plugin.dir)
end,
dev = false,
config = function()
require("ondict").setup("localhost:1345") -- If you already have a running ondict server, you can just specify the address.
end
},
},
-- Configure any other settings here. See the documentation for more details.
-- colorscheme that will be used when installing plugins.
-- automatically check for plugin updates
checker= { enabled = false },
})
```

#### Manually
```console
cd ~/.local/share/nvim/site/pack/packer/start/
git clone https://github.com/ChaosNyaruko/ondict.git
cd ondict
go install .
```

### Mapping examples
```vimscript
nnoremap <leader>d <cmd>lua require("ondict").query()<cr>
Expand Down
68 changes: 41 additions & 27 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,62 @@ ondict -i -e mdx
参见[与Neovim集成](#neovim)
![Gif](./assets/e1_mdx_nvim.gif)

### 与MacOS的hammerspoon集成
![Gif](./assets/e1_mdx_hammerspoon.gif)

##### 已知问题:
如果您使用hammerspoon的"task"功能(即"hs.task.new"然后"xx::start"),某些词的查询会阻塞进程,无法看到结果(因为还没有返回),比如"test"。但在真正的web模式下没有这样的问题,这种情况只在hammerspoon中出现。

目前还不知道原因,同样的词查询在[Neovim集成](#neovim)中也能正常工作,后者也使用Lua作为其异步运行时。因此我猜测可能与实现有关,这可能是hammerspoon的一个bug。

##### 解决方案
使用hs.execute代替hs.task(注意shell转义),这是执行任务的"同步"方法。普通查询足够快,您不会注意到差异,会"立即"看到结果。参见[示例](https://github.com/ChaosNyaruko/dotfiles/blob/mini/hammerspoon/init.lua#L90)

### 与FZF集成(实验性功能,仅支持MacOS)
```console
ondict -fzf
```
您需要安装[FZF](https://github.com/junegunn/fzf),并且ondict服务器监听在localhost:1345(目前正在开发中)
![Gif](./assets/ondict_fzf.gif)
# <a name="neovim"></a>如何在Neovim中使用
1. 使用插件管理器或手动安装插件。
2. 使用 `:lua require("ondict").query()` 来查询光标下的单词(\<cword\>)。
3. 为自己定义一个更方便的映射来调用它。注意:在可视模式下,请使用 "\<cmd\>lua require("ondict").query()\<cr\>"。这样可以捕获"选中"的单词。否则,"模式"会被改变,只能查询光标下的单词(\<cword\>)。

使用[lazy](https://github.com/folke/lazy.nvim)自动安装"ondict"二进制文件:
## 安装
### 使用[lazy插件管理器](https://github.com/folke/lazy.nvim),比较推荐这一种(现在应该很少neovim用户手动装插件吧)
```lua
{
"ChaosNyaruko/ondict",
event = "VeryLazy",
build = function(plugin)
require("ondict").install(plugin.dir)
end
}
require("lazy").setup({
spec = {
-- add your plugins here
{
"ChaosNyaruko/ondict",
event = "VeryLazy",
build = function(plugin)
require("ondict").install(plugin.dir)
end,
dev = false,
config = function()
require("ondict").setup("localhost:1345") -- If you already have a running ondict server, you can just specify the address.
end
},
},
-- Configure any other settings here. See the documentation for more details.
-- colorscheme that will be used when installing plugins.
-- automatically check for plugin updates
checker= { enabled = false },
})
```

手动安装
### 手动安装
```console
cd ~/.local/share/nvim/site/pack/packer/start/
git clone https://github.com/ChaosNyaruko/ondict.git
cd ondict
go install .
```

### 映射示例
## 映射示例
```vimscript
nnoremap <leader>d <cmd>lua require("ondict").query()<cr>
vnoremap <leader>d <cmd>lua require("ondict").query()<cr>
Expand All @@ -195,23 +226,6 @@ vim.keymap.set("n", "<leader>d", require("ondict").query)
vim.keymap.set("v", "<leader>d", require("ondict").query)
```

### 与MacOS的hammerspoon集成
![Gif](./assets/e1_mdx_hammerspoon.gif)

##### 已知问题:
如果您使用hammerspoon的"task"功能(即"hs.task.new"然后"xx::start"),某些词的查询会阻塞进程,无法看到结果(因为还没有返回),比如"test"。但在真正的web模式下没有这样的问题,这种情况只在hammerspoon中出现。

目前还不知道原因,同样的词查询在[Neovim集成](#neovim)中也能正常工作,后者也使用Lua作为其异步运行时。因此我猜测可能与实现有关,这可能是hammerspoon的一个bug。

##### 解决方案
使用hs.execute代替hs.task(注意shell转义),这是执行任务的"同步"方法。普通查询足够快,您不会注意到差异,会"立即"看到结果。参见[示例](https://github.com/ChaosNyaruko/dotfiles/blob/mini/hammerspoon/init.lua#L90)

### 与FZF集成(实验性功能,仅支持MacOS)
```console
ondict -fzf
```
您需要安装[FZF](https://github.com/junegunn/fzf),并且ondict服务器监听在localhost:1345(目前正在开发中)
![Gif](./assets/ondict_fzf.gif)

# <a name="离线"></a>离线词典文件
将词典文件放在$HOME/.config/ondict/dicts中,支持的格式有:
Expand Down
44 changes: 44 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"

log "github.com/sirupsen/logrus"

"github.com/ChaosNyaruko/ondict/history"
)

func request(netConn net.Conn, e, f string, r int) error {
httpc := http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return netConn, nil
},
},
}
if r&0x1 != 0 {
if err := history.Append(*word); err != nil {
log.Warnf("append %s to history err: %v", *word, err)
}
}
res, err := httpc.Get(fmt.Sprintf("http://fakedomain/dict?query=%s&engine=%s&format=%s&record=%d", url.QueryEscape(*word), e, f, r&0x2))
if err != nil {
log.SetOutput(os.Stderr)
log.Fatalf("new request error %v", err)
}
defer res.Body.Close()
if res, err := io.ReadAll(res.Body); err != nil {
return fmt.Errorf("read body error %v", err)
} else {
fmt.Println(string(res))

}

return nil
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func main() {
if err != nil {
log.Fatalf("getting ondict path error: %v", err)
}
network, addr = autoNetworkAddressPosix(dp, "")
network, addr = autoNetworkAddress(dp, "")
if _, err := os.Stat(addr); err == nil {
if err := os.Remove(addr); err != nil {
log.Fatalf("removing remote socket file: %v", err)
Expand Down Expand Up @@ -159,7 +159,7 @@ func main() {
if err != nil {
log.Fatalf("getting ondict path error: %v", err)
}
network, address = autoNetworkAddressPosix(dp, "")
network, address = autoNetworkAddress(dp, "")
log.Debugf("auto mode dp: %v, network: %v, address: %v", dp, network, address)
netConn, err = net.DialTimeout(network, address, dialTimeout)

Expand Down
30 changes: 30 additions & 0 deletions remote_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"os"
"os/exec"
)

var (
startRemote = startRemoteDefault
autoNetworkAddress = autoNetworkAddressDefault
)

func autoNetworkAddressDefault(goplsPath, id string) (network string, address string) {
if id != "" {
panic("identified remotes are not supported on windows")
}
return "tcp", "localhost:37374"
}

func startRemoteDefault(dp string, args ...string) error {
cmd := exec.Command(dp, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
return fmt.Errorf("startRemote server err: %v", err)
}
// return cmd.Wait()
return nil
}
45 changes: 9 additions & 36 deletions remote.go → remote_posix.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package main

import (
"context"
"crypto/sha256"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"syscall"

"github.com/ChaosNyaruko/ondict/history"
log "github.com/sirupsen/logrus"
)

func init() {
startRemote = startRemotePosix
autoNetworkAddress = autoNetworkAddressPosix
}

// autoNetworkAddressPosix resolves an id on the 'auto' pseduo-network to a
// real network and address. On unix, this uses unix domain sockets.
// copied from x/gopls
Expand Down Expand Up @@ -55,7 +57,7 @@ func autoNetworkAddressPosix(goplsPath, id string) (network string, address stri
return "unix", filepath.Join(runtimeDir, fmt.Sprintf("%s-%s-daemon.%s%s", basename, shortHash, user, idComponent))
}

func startRemote(dp string, args ...string) error {
func startRemotePosix(dp string, args ...string) error {
cmd := exec.Command(dp, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand All @@ -68,32 +70,3 @@ func startRemote(dp string, args ...string) error {
// return cmd.Wait()
return nil
}

func request(netConn net.Conn, e, f string, r int) error {
httpc := http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return netConn, nil
},
},
}
if r&0x1 != 0 {
if err := history.Append(*word); err != nil {
log.Warnf("append %s to history err: %v", *word, err)
}
}
res, err := httpc.Get(fmt.Sprintf("http://fakedomain/dict?query=%s&engine=%s&format=%s&record=%d", url.QueryEscape(*word), e, f, r&0x2))
if err != nil {
log.SetOutput(os.Stderr)
log.Fatalf("new request error %v", err)
}
defer res.Body.Close()
if res, err := io.ReadAll(res.Body); err != nil {
return fmt.Errorf("read body error %v", err)
} else {
fmt.Println(string(res))

}

return nil
}
Loading