From 9d49c21dc973e4cb88d716b83b6b50db1e574649 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 14:26:29 +0300 Subject: workaround --- dotfiles/nvim/init.lua | 44 +++++++++++++++++++++++++++++++++++++++++ dotfiles/scripts/hx.nvim-prompt | 17 ++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 dotfiles/nvim/init.lua create mode 100755 dotfiles/scripts/hx.nvim-prompt diff --git a/dotfiles/nvim/init.lua b/dotfiles/nvim/init.lua new file mode 100644 index 0000000..ae4ab5d --- /dev/null +++ b/dotfiles/nvim/init.lua @@ -0,0 +1,44 @@ +require("CopilotChat").setup { + -- See Configuration section for options +} + +local timer = vim.loop.new_timer() -- Initialize the timer + +vim.api.nvim_create_autocmd("BufEnter", { + pattern = "*", + callback = function() + if vim.bo.filetype == "copilot-chat" then + local copilot_chat_buf = vim.api.nvim_get_current_buf() + vim.cmd("wincmd _") -- Maximize height + vim.cmd("wincmd |") -- Maximize width + local file_path = vim.fn.expand("~/.copilot_chat_output.txt") + + -- Start the timer with a 2-second interval + timer:start(1000, 1000, vim.schedule_wrap(function() + if copilot_chat_buf and vim.api.nvim_buf_is_valid(copilot_chat_buf) then + -- Get all lines in the buffer + local lines = vim.api.nvim_buf_get_lines(copilot_chat_buf, 0, -1, false) + + -- Check for the stopping condition + for _, line in ipairs(lines) do + if line:find("^COPILOT_END") then + print("Stopping write process: 'COPILOT_END' detected.") + timer:stop() + -- Write the buffer content to the file + vim.api.nvim_buf_call(copilot_chat_buf, function() + vim.cmd("write! " .. file_path) + end) + vim.cmd("qa!") + return + end + end + + -- Write the buffer content to the file + vim.api.nvim_buf_call(copilot_chat_buf, function() + vim.cmd("write! " .. file_path) + end) + end + end)) + end + end, +}) diff --git a/dotfiles/scripts/hx.nvim-prompt b/dotfiles/scripts/hx.nvim-prompt new file mode 100755 index 0000000..05ad1a3 --- /dev/null +++ b/dotfiles/scripts/hx.nvim-prompt @@ -0,0 +1,17 @@ +#!/usr/bin/env zsh + +declare -r STDIN_FILE=~/.hx-prompt-nvim-stdin +cat > $STDIN_FILE &>/dev/null +declare -r PROMPT="$(hx.prompt)" + +declare -r REPLY_FILE=~/.copilot_chat_output.txt +if [ -f $REPLY_FILE.done ]; then + rm $REPLY_FILE.done +fi + +tmux split-window -v "nvim +':CopilotChat $PROMPT for the following: $(cat $STDIN_FILE). If the result is code, code only without code-block at the beginning and the end. End the output with COPILOT_END.'; mv $REPLY_FILE $REPLY_FILE.done" + +while [ ! -f "$REPLY_FILE.done" ]; do + sleep 0.2 +done +sed -n '/## Copilot/,/^COPILOT_END/ { /^## Copilot/d; /\[file:/d; /^COPILOT_END/d; p; }' $REPLY_FILE.done -- cgit v1.2.3 From bde6ebc424a5953ea777fdae1aedbfaa940866aa Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 15:44:37 +0300 Subject: better --- dotfiles/nvim/init.lua | 23 ++++++++++++++--------- dotfiles/scripts/hx.nvim-prompt | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/dotfiles/nvim/init.lua b/dotfiles/nvim/init.lua index ae4ab5d..4a437e8 100644 --- a/dotfiles/nvim/init.lua +++ b/dotfiles/nvim/init.lua @@ -1,3 +1,4 @@ + require("CopilotChat").setup { -- See Configuration section for options } @@ -20,16 +21,20 @@ vim.api.nvim_create_autocmd("BufEnter", { local lines = vim.api.nvim_buf_get_lines(copilot_chat_buf, 0, -1, false) -- Check for the stopping condition + local user_line_count = 0 for _, line in ipairs(lines) do - if line:find("^COPILOT_END") then - print("Stopping write process: 'COPILOT_END' detected.") - timer:stop() - -- Write the buffer content to the file - vim.api.nvim_buf_call(copilot_chat_buf, function() - vim.cmd("write! " .. file_path) - end) - vim.cmd("qa!") - return + if line:find("^## User") then + user_line_count = user_line_count + 1 + if user_line_count >= 2 then + print("Stopping write process: Two '## User' lines detected.") + timer:stop() + -- Write the buffer content to the file + vim.api.nvim_buf_call(copilot_chat_buf, function() + vim.cmd("write! " .. file_path) + end) + vim.cmd("qa!") + return + end end end diff --git a/dotfiles/scripts/hx.nvim-prompt b/dotfiles/scripts/hx.nvim-prompt index 05ad1a3..09230ae 100755 --- a/dotfiles/scripts/hx.nvim-prompt +++ b/dotfiles/scripts/hx.nvim-prompt @@ -9,9 +9,9 @@ if [ -f $REPLY_FILE.done ]; then rm $REPLY_FILE.done fi -tmux split-window -v "nvim +':CopilotChat $PROMPT for the following: $(cat $STDIN_FILE). If the result is code, code only without code-block at the beginning and the end. End the output with COPILOT_END.'; mv $REPLY_FILE $REPLY_FILE.done" +tmux split-window -v "nvim +':CopilotChat $PROMPT for the following: $(cat $STDIN_FILE). If the result is code, code only without \`\`\`-markers at the beginning and the end.'; mv $REPLY_FILE $REPLY_FILE.done" while [ ! -f "$REPLY_FILE.done" ]; do sleep 0.2 done -sed -n '/## Copilot/,/^COPILOT_END/ { /^## Copilot/d; /\[file:/d; /^COPILOT_END/d; p; }' $REPLY_FILE.done +sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $REPLY_FILE.done -- cgit v1.2.3 From 12c227bf8403895a519217ff54a4bb02f3f68b08 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 17:46:14 +0300 Subject: better --- dotfiles/nvim/init.lua | 13 +++++++++++++ dotfiles/scripts/hx.nvim-prompt | 23 ++++++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/dotfiles/nvim/init.lua b/dotfiles/nvim/init.lua index 4a437e8..4f593a2 100644 --- a/dotfiles/nvim/init.lua +++ b/dotfiles/nvim/init.lua @@ -47,3 +47,16 @@ vim.api.nvim_create_autocmd("BufEnter", { end end, }) + +vim.api.nvim_create_user_command('CopilotAskFromInputFile', function() + local chat = require("CopilotChat") + local input_file = os.getenv("HOME") .. "/.copilot_chat_input.txt" + local file = io.open(input_file, "r") + if file then + local input = file:read("*all") + file:close() + chat.ask(input) + else + print("Error: Unable to open input file.") + end +end, { force = true, range = true }) diff --git a/dotfiles/scripts/hx.nvim-prompt b/dotfiles/scripts/hx.nvim-prompt index 09230ae..de6590c 100755 --- a/dotfiles/scripts/hx.nvim-prompt +++ b/dotfiles/scripts/hx.nvim-prompt @@ -1,17 +1,26 @@ #!/usr/bin/env zsh declare -r STDIN_FILE=~/.hx-prompt-nvim-stdin +declare -r INPUT_FILE=~/.copilot_chat_input.txt +declare -r OUTPUT_FILE=~/.copilot_chat_output.txt + cat > $STDIN_FILE &>/dev/null declare -r PROMPT="$(hx.prompt)" - -declare -r REPLY_FILE=~/.copilot_chat_output.txt -if [ -f $REPLY_FILE.done ]; then - rm $REPLY_FILE.done +if [ -f $OUTPUT_FILE.done ]; then + rm $OUTPUT_FILE.done fi -tmux split-window -v "nvim +':CopilotChat $PROMPT for the following: $(cat $STDIN_FILE). If the result is code, code only without \`\`\`-markers at the beginning and the end.'; mv $REPLY_FILE $REPLY_FILE.done" +cat < $INPUT_FILE +$PROMPT for the following: + +$(cat $STDIN_FILE). + +If the result is code, code only without \`\`\`-markers at the beginning and the end. +INPUT_FILE + +tmux split-window -v "nvim +':CopilotAskFromInputFile'; mv $OUTPUT_FILE $OUTPUT_FILE.done" -while [ ! -f "$REPLY_FILE.done" ]; do +while [ ! -f "$OUTPUT_FILE.done" ]; do sleep 0.2 done -sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $REPLY_FILE.done +sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $OUTPUT_FILE.done -- cgit v1.2.3 From 914af52d30e2b8518590a054170159dbeedb7411 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 18:10:09 +0300 Subject: better --- dotfiles/nvim/init.lua | 26 +++++++++++++++++--------- dotfiles/scripts/copilot | 32 ++++++++++++++++++++++++++++++++ dotfiles/scripts/hx.nvim-prompt | 26 -------------------------- 3 files changed, 49 insertions(+), 35 deletions(-) create mode 100755 dotfiles/scripts/copilot delete mode 100755 dotfiles/scripts/hx.nvim-prompt diff --git a/dotfiles/nvim/init.lua b/dotfiles/nvim/init.lua index 4f593a2..c3b8701 100644 --- a/dotfiles/nvim/init.lua +++ b/dotfiles/nvim/init.lua @@ -48,15 +48,23 @@ vim.api.nvim_create_autocmd("BufEnter", { end, }) -vim.api.nvim_create_user_command('CopilotAskFromInputFile', function() +vim.api.nvim_create_user_command('CopilotAsk', function(args) local chat = require("CopilotChat") - local input_file = os.getenv("HOME") .. "/.copilot_chat_input.txt" - local file = io.open(input_file, "r") - if file then - local input = file:read("*all") - file:close() - chat.ask(input) + local input + if args.args and args.args ~= "" then + input = args.args else - print("Error: Unable to open input file.") + local input_file = os.getenv("HOME") .. "/.copilot_chat_input.txt" + local file = io.open(input_file, "r") + if file then + input = file:read("*all") + file:close() + else + print("Error: Unable to open input file.") + return + end end -end, { force = true, range = true }) + chat.ask(input) +end, { force = true, range = true, nargs = "?" }) + + diff --git a/dotfiles/scripts/copilot b/dotfiles/scripts/copilot new file mode 100755 index 0000000..dcb2837 --- /dev/null +++ b/dotfiles/scripts/copilot @@ -0,0 +1,32 @@ +#!/usr/bin/env zsh + +declare -r STDIN_FILE=~/.copilot_prompt_stdin.txt +declare -r INPUT_FILE=~/.copilot_chat_input.txt +declare -r OUTPUT_FILE=~/.copilot_chat_output.txt +declare INPUT_PROMPT + +if [ -f $OUTPUT_FILE.done ]; then + rm $OUTPUT_FILE.done +fi +cat > $STDIN_FILE &>/dev/null + +if [ $# -eq 0 ]; then + INPUT_PROMPT="$(hx.prompt)" +else + INPUT_PROMPT="$@" +fi + +cat < $INPUT_FILE +$INPUT_PROMPT for the following: + +$(cat $STDIN_FILE) + +If the result is code, print out the code only, don't print the \`\`\`-markers around the code block. +INPUT_FILE + +tmux split-window -v "nvim +':CopilotAsk'; mv $OUTPUT_FILE $OUTPUT_FILE.done" + +while [ ! -f "$OUTPUT_FILE.done" ]; do + sleep 0.2 +done +sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $OUTPUT_FILE.done diff --git a/dotfiles/scripts/hx.nvim-prompt b/dotfiles/scripts/hx.nvim-prompt deleted file mode 100755 index de6590c..0000000 --- a/dotfiles/scripts/hx.nvim-prompt +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env zsh - -declare -r STDIN_FILE=~/.hx-prompt-nvim-stdin -declare -r INPUT_FILE=~/.copilot_chat_input.txt -declare -r OUTPUT_FILE=~/.copilot_chat_output.txt - -cat > $STDIN_FILE &>/dev/null -declare -r PROMPT="$(hx.prompt)" -if [ -f $OUTPUT_FILE.done ]; then - rm $OUTPUT_FILE.done -fi - -cat < $INPUT_FILE -$PROMPT for the following: - -$(cat $STDIN_FILE). - -If the result is code, code only without \`\`\`-markers at the beginning and the end. -INPUT_FILE - -tmux split-window -v "nvim +':CopilotAskFromInputFile'; mv $OUTPUT_FILE $OUTPUT_FILE.done" - -while [ ! -f "$OUTPUT_FILE.done" ]; do - sleep 0.2 -done -sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $OUTPUT_FILE.done -- cgit v1.2.3 From b40cbb40eaafd104671bbf997cc69655f8d2865e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 20:05:41 +0300 Subject: Update --- dotfiles/helix/config.toml | 2 +- dotfiles/scripts/copilot | 32 -------------------------------- dotfiles/scripts/hx.nvim-copilot-prompt | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 33 deletions(-) delete mode 100755 dotfiles/scripts/copilot create mode 100755 dotfiles/scripts/hx.nvim-copilot-prompt diff --git a/dotfiles/helix/config.toml b/dotfiles/helix/config.toml index d5fcc0a..1893b94 100644 --- a/dotfiles/helix/config.toml +++ b/dotfiles/helix/config.toml @@ -58,7 +58,7 @@ C-s = { e = ":set-option soft-wrap.enable true", d = ":set-option soft-wrap.enab C-q = ":buffer-close" # AI commands are good here. -C-p = { c = ":pipe aichat correct this sentence and only print out the corrected text", r = ":pipe aichat restructure and reword the input and dont leave information out and only print out the new text", a = ":pipe aichat rewrite this in a more casual style", n = ":pipe aichat these are book notes of mine. correct the grammar and re-organize the notes. use bullet points for short information and whole paragraphs for longer one. the output must be in Gemini Gemtext format with the star * as the bullet point symbol and not the minus - . dont leave out any content.", p = ":pipe hx.aichat-prompt" } +C-p = { c = ":pipe ai correct this sentence and only print out the corrected text", r = ":pipe ai restructure and reword the input and dont leave information out and only print out the new text", a = ":pipe ai rewrite this in a more casual style", n = ":pipe ai these are book notes of mine. correct the grammar and re-organize the notes. use bullet points for short information and whole paragraphs for longer one. the output must be in Gemini Gemtext format with the star * as the bullet point symbol and not the minus - . dont leave out any content.", p = ":pipe ai" } # Git commands C-g = { d = ":run-shell-command git diff", p = ":run-shell-command git pull", u = ":run-shell-command git push", t = ":run-shell-command tmux new-window -n hx-git-tig tig", c = ":run-shell-command tmux split-window -v 'git commit -a'" } diff --git a/dotfiles/scripts/copilot b/dotfiles/scripts/copilot deleted file mode 100755 index dcb2837..0000000 --- a/dotfiles/scripts/copilot +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env zsh - -declare -r STDIN_FILE=~/.copilot_prompt_stdin.txt -declare -r INPUT_FILE=~/.copilot_chat_input.txt -declare -r OUTPUT_FILE=~/.copilot_chat_output.txt -declare INPUT_PROMPT - -if [ -f $OUTPUT_FILE.done ]; then - rm $OUTPUT_FILE.done -fi -cat > $STDIN_FILE &>/dev/null - -if [ $# -eq 0 ]; then - INPUT_PROMPT="$(hx.prompt)" -else - INPUT_PROMPT="$@" -fi - -cat < $INPUT_FILE -$INPUT_PROMPT for the following: - -$(cat $STDIN_FILE) - -If the result is code, print out the code only, don't print the \`\`\`-markers around the code block. -INPUT_FILE - -tmux split-window -v "nvim +':CopilotAsk'; mv $OUTPUT_FILE $OUTPUT_FILE.done" - -while [ ! -f "$OUTPUT_FILE.done" ]; do - sleep 0.2 -done -sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $OUTPUT_FILE.done diff --git a/dotfiles/scripts/hx.nvim-copilot-prompt b/dotfiles/scripts/hx.nvim-copilot-prompt new file mode 100755 index 0000000..dcb2837 --- /dev/null +++ b/dotfiles/scripts/hx.nvim-copilot-prompt @@ -0,0 +1,32 @@ +#!/usr/bin/env zsh + +declare -r STDIN_FILE=~/.copilot_prompt_stdin.txt +declare -r INPUT_FILE=~/.copilot_chat_input.txt +declare -r OUTPUT_FILE=~/.copilot_chat_output.txt +declare INPUT_PROMPT + +if [ -f $OUTPUT_FILE.done ]; then + rm $OUTPUT_FILE.done +fi +cat > $STDIN_FILE &>/dev/null + +if [ $# -eq 0 ]; then + INPUT_PROMPT="$(hx.prompt)" +else + INPUT_PROMPT="$@" +fi + +cat < $INPUT_FILE +$INPUT_PROMPT for the following: + +$(cat $STDIN_FILE) + +If the result is code, print out the code only, don't print the \`\`\`-markers around the code block. +INPUT_FILE + +tmux split-window -v "nvim +':CopilotAsk'; mv $OUTPUT_FILE $OUTPUT_FILE.done" + +while [ ! -f "$OUTPUT_FILE.done" ]; do + sleep 0.2 +done +sed -n '/^## Copilot/,/^## User/ { /^## Copilot/d; /\[file:/d; /^## User/d; p; }' $OUTPUT_FILE.done -- cgit v1.2.3 From 93a3cf112417d47b859d85d6450ec5b75bdde492 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 20:05:58 +0300 Subject: jo --- dotfiles/scripts/ai | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 dotfiles/scripts/ai diff --git a/dotfiles/scripts/ai b/dotfiles/scripts/ai new file mode 100755 index 0000000..7515659 --- /dev/null +++ b/dotfiles/scripts/ai @@ -0,0 +1,7 @@ +#!/usr/bin/env zsh + +if [ $(uname) = Darwin ]; then + exec hx.nvim-copilot-prompt "$@" +else + exec hx.aichat-prompt "$@" +fi -- cgit v1.2.3 From 48178ad7d5a1f61790e6fa510971e0636d97d61a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 25 Apr 2025 20:06:48 +0300 Subject: fix --- dotfiles/fish/conf.d/ai.fish | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotfiles/fish/conf.d/ai.fish b/dotfiles/fish/conf.d/ai.fish index 5ad80d2..4e89bbb 100644 --- a/dotfiles/fish/conf.d/ai.fish +++ b/dotfiles/fish/conf.d/ai.fish @@ -2,4 +2,4 @@ abbr -a gpt chatgpt abbr -a gpti "chatgpt --interactive" abbr -a suggest 'gh copilot suggest' abbr -a explain 'gh copilot explain' -abbr -a ai 'aichat -e' +abbr -a aic 'aichat -e' -- cgit v1.2.3