From 44f31b1f27c2a53cf8a4e053a50c245e379558b0 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 26 Jun 2024 12:18:32 +0800 Subject: Update --- Makefile | 4 +++ snippets/bash_server | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ snippets/dot_example | 27 ++++++++++++++++ vim/init/basic.vim | 5 ++- vim/init/keymaps.vim | 11 ++++--- vim/lua/chadrc.lua | 2 ++ vim/lua/mappings.lua | 30 ++++++++---------- vim/lua/options.lua | 2 ++ 8 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 snippets/bash_server create mode 100644 snippets/dot_example diff --git a/Makefile b/Makefile index ba4f2b5..6d3a8e0 100644 --- a/Makefile +++ b/Makefile @@ -106,3 +106,7 @@ alacritty: mpd: ln -sf `pwd`/mpd/ncmpcpp ~/.config/ncmpcpp rm -rf ~/.ncmpcpp + +snippets: + rm -rf ~/snippets + ln -sf `pwd`/snippets ~/snippets diff --git a/snippets/bash_server b/snippets/bash_server new file mode 100644 index 0000000..f008d11 --- /dev/null +++ b/snippets/bash_server @@ -0,0 +1,90 @@ +#!/bin/bash + +## Create the response FIFO +rm -f response +mkfifo response + +function handle_GET_home() { + RESPONSE=$(cat home.html | \ + sed "s/{{$COOKIE_NAME}}/$COOKIE_VALUE/") +} + +function handle_GET_login() { + RESPONSE=$(cat login.html) +} + +function handle_POST_login() { + RESPONSE=$(cat post-login.http | \ + sed "s/{{cookie_name}}/$INPUT_NAME/" | \ + sed "s/{{cookie_value}}/$INPUT_VALUE/") +} + +function handle_POST_logout() { + RESPONSE=$(cat post-logout.http | \ + sed "s/{{cookie_name}}/$COOKIE_NAME/" | \ + sed "s/{{cookie_value}}/$COOKIE_VALUE/") +} + +function handle_not_found() { + RESPONSE=$(cat 404.html) +} + +function handleRequest() { + ## Read request + while read line; do + echo $line + trline=$(echo $line | tr -d '[\r\n]') + + [ -z "$trline" ] && break + + HEADLINE_REGEX='(.*?)\s(.*?)\sHTTP.*?' + [[ "$trline" =~ $HEADLINE_REGEX ]] && + REQUEST=$(echo $trline | sed -E "s/$HEADLINE_REGEX/\1 \2/") + + CONTENT_LENGTH_REGEX='Content-Length:\s(.*?)' + [[ "$trline" =~ $CONTENT_LENGTH_REGEX ]] && + CONTENT_LENGTH=$(echo $trline | sed -E "s/$CONTENT_LENGTH_REGEX/\1/") + + COOKIE_REGEX='Cookie:\s(.*?)\=(.*?).*?' + [[ "$trline" =~ $COOKIE_REGEX ]] && + read COOKIE_NAME COOKIE_VALUE <<< $(echo $trline | sed -E "s/$COOKIE_REGEX/\1 \2/") + done + + ## Read body + if [ ! -z "$CONTENT_LENGTH" ]; then + BODY_REGEX='(.*?)=(.*?)' + + while read -n$CONTENT_LENGTH -t1 line; do + echo $line + trline=`echo $line | tr -d '[\r\n]'` + + [ -z "$trline" ] && break + + read INPUT_NAME INPUT_VALUE <<< $(echo $trline | sed -E "s/$BODY_REGEX/\1 \2/") + done + fi + + ## Route to the response handlers + case "$REQUEST" in + "GET /login") handle_GET_login ;; + "GET /") handle_GET_home ;; + "POST /login") handle_POST_login ;; + "POST /logout") handle_POST_logout ;; + *) handle_not_found ;; + esac + + echo -e "$RESPONSE" > response +} + +echo 'Listening on 3000...' + +## Keep server running forever +while true; do + ## 1. wait for FIFO + ## 2. creates a socket and listens to the port 3000 + ## 3. as soon as a request message arrives to the socket, pipes it to the handleRequest function + ## 4. the handleRequest function processes the request message and routes it to the response handler, which writes to the FIFO + ## 5. as soon as the FIFO receives a message, it's sent to the socket + ## 6. closes the connection (`-N`), closes the socket and repeat the loop + cat response | nc -lN 3000 | handleRequest +done diff --git a/snippets/dot_example b/snippets/dot_example new file mode 100644 index 0000000..fa95abc --- /dev/null +++ b/snippets/dot_example @@ -0,0 +1,27 @@ +digraph G { + + subgraph cluster_0 { + style=filled; + color=lightgrey; + node [style=filled,color=white]; + a0 -> a1 -> a2 -> a3; + label = "process #1"; + } + + subgraph cluster_1 { + node [style=filled]; + b0 -> b1 -> b2 -> b3; + label = "process #2"; + color=blue + } + start -> a0; + start -> b0; + a1 -> b3; + b2 -> a3; + a3 -> a0; + a3 -> end; + b3 -> end; + + start [shape=Mdiamond]; + end [shape=Msquare]; +} diff --git a/vim/init/basic.vim b/vim/init/basic.vim index c2996d2..5008b2f 100644 --- a/vim/init/basic.vim +++ b/vim/init/basic.vim @@ -170,7 +170,10 @@ autocmd BufRead /dev/shm/*.txt call SetPasswordFile() function SetPasswordFile() setlocal foldminlines=0 setlocal foldmethod=manual - setlocal foldtext= + function s:custom() + return "Password" + endfunction + setlocal foldtext=s:custom() norm! ggzfl endfunction diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index f789ceb..245905a 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim @@ -129,7 +129,7 @@ vnoremap so :source " MANAGE_VIMRC "---------------------------------------------------------------------- nnoremap e :edit $MYVIMRC -autocmd! bufwritepost $MYVIMRC source $MYVIMRC +autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC "---------------------------------------------------------------------- @@ -245,18 +245,19 @@ endfunc "---------------------------------------------------------------------- " Open a new buffer +nmap b :enew nmap O :e /tmp/buffer " Next buffer -noremap l :bn +noremap l :exe "buffer ".g:lastbuffer +" noremap :exe 'buffer '.g:lastbuffer " set filetype noremap ft :set filetype= -noremap fm :set foldmethod= +noremap f :set foldmetho = " Let l toggle between this and the last accessed buffer let g:lastbuffer = 1 -noremap :exe "buffer ".g:lastbuffer au BufLeave * let g:lastbuffer = bufnr() "---------------------------------------------------------------------- @@ -270,7 +271,7 @@ vnoremap [ ``>la] vnoremap { ``>la} vnoremap ` ``>la` vnoremap ``>la -vnoremap z ``>la」 +vnoremap Q ``>la」 "---------------------------------------------------------------------- diff --git a/vim/lua/chadrc.lua b/vim/lua/chadrc.lua index aa0d78c..aa9a78c 100644 --- a/vim/lua/chadrc.lua +++ b/vim/lua/chadrc.lua @@ -41,6 +41,8 @@ if M.ui.tabufline.enabled then vim.keymap.set("n", "", function() require("nvchad.tabufline").move_buf(1) end) vim.keymap.set("n", "", function() vim.cmd("tabprevious") end) vim.keymap.set("n", "", function() vim.cmd("tabnext") end) + vim.keymap.set("n", "", function() require("nvchad.tabufline").next() end, { desc = "buffer goto next" }) + vim.keymap.set("n", "", function() require("nvchad.tabufline").prev() end, { desc = "buffer goto prev" }) end diff --git a/vim/lua/mappings.lua b/vim/lua/mappings.lua index 45c44ec..9151f17 100644 --- a/vim/lua/mappings.lua +++ b/vim/lua/mappings.lua @@ -23,11 +23,6 @@ end, { desc = "format files" }) -- global lsp mappings vim.keymap.set("n", "ds", vim.diagnostic.setloclist, { desc = "lsp diagnostic loclist" }) --- tabufline -vim.keymap.set("n", "x", function() - require("nvchad.tabufline").close_buffer() -end, { desc = "buffer close" }) - -- Comment vim.keymap.set("n", "/", "gcc", { desc = "comment toggle", remap = true }) vim.keymap.set("v", "/", "gc", { desc = "comment toggle", remap = true }) @@ -66,8 +61,14 @@ vim.keymap.set('n', 'ss', function() require('telescope.actions').close(prompt_bufnr) vim.api.nvim_put(snippet_content, '', false, true) end + local edit_selected_snippet = function() + local file = require('telescope.actions.state').get_selected_entry()[1] + require('telescope.actions').close(prompt_bufnr) + vim.cmd(":e " .. cwd .. "/" .. file) + end map('i', '', insert_selected_snippet) + map('i', '', edit_selected_snippet) map('n', '', insert_selected_snippet) return true @@ -115,17 +116,12 @@ end, { desc = "Create a new snippet" }) -- map("t", "", "", { desc = "terminal escape terminal mode" }) -- new terminals -vim.keymap.set("n", "h", function() require("nvchad.term").new { pos = "sp" } end, - { desc = "terminal new horizontal term" }) -vim.keymap.set("n", "v", function() require("nvchad.term").new { pos = "vsp" } end, - { desc = "terminal new vertical window" }) +vim.keymap.set("n", "h", function() require("nvchad.term").new { pos = "sp" } end, { desc = "terminal new horizontal term" }) +vim.keymap.set("n", "v", function() require("nvchad.term").new { pos = "vsp" } end, { desc = "terminal new vertical window" }) -- toggleable -vim.keymap.set({ "n", "t" }, "", function() require("nvchad.term").toggle { pos = "vsp", id = "vtoggleTerm" } end, - { desc = "terminal toggleable vertical term" }) -vim.keymap.set({ "n", "t" }, "", function() require("nvchad.term").toggle { pos = "sp", id = "htoggleTerm" } end, - { desc = "terminal new horizontal term" }) -vim.keymap.set({ "n", "t" }, "", function() require("nvchad.term").toggle { pos = "float", id = "floatTerm" } end, - { desc = "terminal toggle floating term" }) +vim.keymap.set({ "n", "t" }, "", function() require("nvchad.term").toggle { pos = "vsp", id = "vtoggleTerm" } end, { desc = "terminal toggleable vertical term" }) +vim.keymap.set({ "n", "t" }, "", function() require("nvchad.term").toggle { pos = "sp", id = "htoggleTerm" } end, { desc = "terminal new horizontal term" }) +vim.keymap.set({ "n", "t" }, "", function() require("nvchad.term").toggle { pos = "float", id = "floatTerm" } end, { desc = "terminal toggle floating term" }) -- whichkey vim.keymap.set("n", "wK", "WhichKey ", { desc = "whichkey all keymaps" }) @@ -176,5 +172,5 @@ vim.keymap.set('n', '', ':HopChar1') -- [ Aerial ] vim.keymap.set("n", "{", "AerialPrev", {}) vim.keymap.set("n", "}", "AerialNext", {}) -vim.keymap.set("n", "a", "Telescope aerial") -vim.keymap.set("n", "A", function() require("aerial").toggle({ direction = "left" }) end) +vim.keymap.set("n", "gN", "Telescope aerial") +vim.keymap.set("n", "gn", function() require("aerial").toggle({ direction = "left" }) end) diff --git a/vim/lua/options.lua b/vim/lua/options.lua index c16ae64..1c2b881 100644 --- a/vim/lua/options.lua +++ b/vim/lua/options.lua @@ -4,6 +4,8 @@ require "nvchad.options" local o = vim.o +o.clipboard = '' + -- To enable cursorline! o.cursorlineopt ='both' -- cgit v1.2.3-70-g09d2