Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into downgrade_go_1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
fde-capu committed Mar 30, 2024
2 parents da40f5d + 112f520 commit 687b3ce
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# Define variables
GOBIN := ./bin
GOBIN := ./bin/navi
GOPACKAGES := $(shell go list ./... | grep -v /vendor)

# Build command
build:
go build -o $(GOBIN)/navi ./main.go
-mkdir -p ./bin
go build -o $(GOBIN) ./main.go

# Run command
run: build
$(GOBIN)/navi
$(GOBIN)

# Test command (assuming you have unit tests)
test:
go test -v $(GOPACKAGES)

# Clean command
clean:
rm -rf $(GOBIN)
rm $(GOBIN) # This was too dengerous when $(GOBIN) was only './bin'!

# Live reload command (optional)
livereload:
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ export GEMINI_API_KEY="..."
```
You can obtain your Gemini API key from the [Gemini API Management page](https://aistudio.google.com/app/apikey).
## Vim integration
### Plugin load and installation
Edit `navi.vim` to set `g:NaviBin` to your custom binary location. Optionally, change the preferred key combination (defaults to `<C-k>`).
In Vim, you simply have to `:source navi.vim`.
However, if you want the plugin to be persistent, make it load when Vim starts: copy `navi.vim` to any directory present in your runtime path (`:echo &runtimepath`). Restart Vim. Verify if the script is loaded with `:scriptnames`.
### Usage
In Normal Mode, press your preferred key combination (or <C-k> by default) to pass your current line to `Navi`. In Visual Mode, you may select more lines.
## Caution
While Navi uses AI to generate shell commands, it's important to understand that AI isn't perfect. Always review the generated commands before executing them, especially if you're working in a production environment or dealing with sensitive data. Navi is a tool designed to assist you, but it doesn't replace good judgment and understanding of shell commands.
Expand Down
72 changes: 72 additions & 0 deletions navi.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
" Navi for Vim.
" Set `g:NaviBin` path below.
" Mapped to Ctrl+K (you can change bellow).
" On Normal Mode passes the current line.
" Or select many lines.

let g:NaviBin = '_'
let g:gretting = '...waiting for Navi...'

let g:selectionCallCalled = 0
autocmd CursorMoved * let g:selectionCallCalled = 0

nnoremap <C-k> :<C-u>call SingleLineCall()<CR>
xnoremap <C-k> :<C-u>call SelectionCall()<CR>
function! EscapeSpecialChars(str)
let escapedStr = substitute(a:str, "'", "''", 'g')
let escapedStr = substitute(escapedStr, '"', '\\"', 'g')
let escapedStr = substitute(escapedStr, "`", "\\`", 'g')
return escapedStr
endfunction

function! CleanAnsi(str)
let clean = substitute(a:str, '[\033\e\x1B]\?\[[0-9;]*m', '', 'g')
let clean = substitute(clean, '^>> \+', '', 'g')
return clean
endfunction

fun! Navi(s)
" echo "Original string: " . a:s
let s = EscapeSpecialChars(a:s)
" echo "Escaped string: " . s
let return_from_navi = system(g:NaviBin . " \'" . s . "\'")
let return_from_navi = trim(return_from_navi)
let lines = split(return_from_navi, '\v\n')
let out = []
for line in lines
call add(out, CleanAnsi(line))
endfor
return out
endfunction

function! PrintOut(out)
let current_line = line('.')
call append(current_line, '<<<<<<<')
call append(current_line, a:out)
call append(current_line, '>>>>>>>')
endfunction

function! SelectionCall()
if g:selectionCallCalled == 1
return
endif
g:selectionCallCalled = 1
echo g:gretting
let start_pos = getpos("'<")
let end_pos = getpos("'>")
let start_line = start_pos[1]
let end_line = end_pos[1]
execute "normal " . end_line . "G"
let lines = getline(start_line, end_line)
let line = join(lines, '\n')
let out = Navi(line)
call PrintOut(out)
endfunction

function! SingleLineCall()
echo g:gretting
let line = getline('.')
let out = Navi(line)
call PrintOut(out)
endfunction

0 comments on commit 687b3ce

Please sign in to comment.