From 74cda27d3854a4cc9f33fe29685d5ce8e5c93002 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sun, 13 Oct 2024 12:08:30 +0300 Subject: [PATCH] update: Added code stats, starship.rs, nvim python and more Signed-off-by: Sagi Dayan --- .gitignore | 1 + files/dotfiles/alacritty/alacritty.toml | 2 +- files/dotfiles/nvim/lua/user/init_plugins.lua | 22 ++ files/dotfiles/nvim/lua/user/lsp/mason.lua | 12 + files/dotfiles/nvim/lua/user/packer.lua | 8 + .../dotfiles/shellconfig/codestats.plugin.zsh | 205 ++++++++++++++++++ files/dotfiles/zshrc | 6 + roles/base/tasks/main.yml | 7 + 8 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 files/dotfiles/shellconfig/codestats.plugin.zsh diff --git a/.gitignore b/.gitignore index 30d0742..082b9ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ report.xml +files/dotfiles/nvim/plugin/ diff --git a/files/dotfiles/alacritty/alacritty.toml b/files/dotfiles/alacritty/alacritty.toml index e05b84a..3a32755 100644 --- a/files/dotfiles/alacritty/alacritty.toml +++ b/files/dotfiles/alacritty/alacritty.toml @@ -3,7 +3,7 @@ import = [ ] [window] -#opacity = 0.7 +#opacity = 0.9 dynamic_padding = true decorations = "none" diff --git a/files/dotfiles/nvim/lua/user/init_plugins.lua b/files/dotfiles/nvim/lua/user/init_plugins.lua index 953431f..d89f3e7 100644 --- a/files/dotfiles/nvim/lua/user/init_plugins.lua +++ b/files/dotfiles/nvim/lua/user/init_plugins.lua @@ -51,6 +51,7 @@ local dapui_ok, dapui = pcall(require, "dapui") local dap_virt_txt_ok, dap_virt_txt = pcall(require, "nvim-dap-virtual-text") local mason_dap_ok, mason_dap = pcall(require, "mason-nvim-dap") local dapgo_ok, dapgo = pcall(require, "dap-go") +local dap_python_ok, dap_python = pcall(require, "dap-python") if dapui_ok then dapui.setup() end @@ -70,6 +71,9 @@ end if dapgo_ok then dapgo.setup() end +if dap_python_ok then + dap_python.setup("python") +end -- end debugger -- @@ -88,3 +92,21 @@ if harpoon_ok then } }) end + +local codestats_ok, codestats = pcall(require, "codestats") +if codestats_ok then + local key = os.getenv("CODESTATS_API_KEY") or "" + if key ~= "" then + codestats.setup { + username = 'goomba', -- needed to fetch profile data + base_url = 'https://codestats.net', -- codestats.net base url + api_key = key, + send_on_exit = true, -- send xp on nvim exit + send_on_timer = true, -- send xp on timer + timer_interval = 60000, -- timer interval in milliseconds (minimum 1000ms to prevent DDoSing codestat.net servers) + curl_timeout = 5, -- curl request timeout in seconds + } + else + print("Missing codestats API key... If you want to use CodeStats. Please set CODESTATS_API_KEY env var") + end +end diff --git a/files/dotfiles/nvim/lua/user/lsp/mason.lua b/files/dotfiles/nvim/lua/user/lsp/mason.lua index 7893496..5dcd116 100644 --- a/files/dotfiles/nvim/lua/user/lsp/mason.lua +++ b/files/dotfiles/nvim/lua/user/lsp/mason.lua @@ -1,8 +1,20 @@ local servers = { "pyright", "jsonls", + "ruff", } +-- Python virtual environments + +local is_vitrual_env = os.getenv("VIRTUAL_ENV") or '' ~= '' +if is_vitrual_env then + print("Running in python venv") + vim.g.python3_host_prog = '/venv/bin/python' + vim.g.python_host_prog = '/venv/bin/python' +end + +-- + local settings = { ui = { icons = { diff --git a/files/dotfiles/nvim/lua/user/packer.lua b/files/dotfiles/nvim/lua/user/packer.lua index 4fcadf4..640cc54 100644 --- a/files/dotfiles/nvim/lua/user/packer.lua +++ b/files/dotfiles/nvim/lua/user/packer.lua @@ -22,6 +22,7 @@ return require("packer").startup(function(use) 'simrat39/symbols-outline.nvim', 'L3MON4D3/LuaSnip', 'saadparwaiz1/cmp_luasnip', + 'nvimtools/none-ls.nvim', } -- Debugger @@ -29,6 +30,7 @@ return require("packer").startup(function(use) use 'theHamsta/nvim-dap-virtual-text' use "jay-babu/mason-nvim-dap.nvim" use 'leoluz/nvim-dap-go' + use 'mfussenegger/nvim-dap-python' -- file tree use { @@ -92,4 +94,10 @@ return require("packer").startup(function(use) -- Catppuccin use { "catppuccin/nvim", as = "catppuccin" } + + -- Code Stats + use { + 'liljaylj/codestats.nvim', + requires = { { "nvim-lua/plenary.nvim" } } + } end) diff --git a/files/dotfiles/shellconfig/codestats.plugin.zsh b/files/dotfiles/shellconfig/codestats.plugin.zsh new file mode 100644 index 0000000..18a521b --- /dev/null +++ b/files/dotfiles/shellconfig/codestats.plugin.zsh @@ -0,0 +1,205 @@ +# https://gitlab.com/code-stats/code-stats-zsh + +_codestats_version="0.3.11" + +zmodload zsh/datetime + +declare -g -i _codestats_xp=0 +declare -g -i _codestats_pulse_time=${EPOCHSECONDS} + +# Because each `curl` call is forked into a subshell to keep the interactive +# shell responsive, the consecutive error count cannot be updated in a +# variable. So we use a temp file, one error per line. +_codestats_consecutive_errors=$(mktemp) + +# Logging: write to file if CODESTATS_LOG_FILE is set and exists +_codestats_log() +{ + if [[ -w "${CODESTATS_LOG_FILE}" ]]; then + # EPOCHSECONDS is an integer, so disable globbing/splitting warning + # shellcheck disable=SC2086 + echo "$(\strftime %Y-%m-%dT%H:%M:%S ${EPOCHSECONDS}) ($$) $*" >> "${CODESTATS_LOG_FILE}" + fi +} + +# Widget wrapper: add a keypress and call original widget +_codestats_call_widget() +{ + # Bracketed-paste-magic calls self-insert when pasting and sets $PASTED. + # Don't add xp for pasting. + + # shellcheck disable=SC2004 + if (( ! ${+PASTED} )); then + _codestats_xp+=15 + fi + + builtin zle "$@" +} + +# Rebind widgets +_codestats_rebind_widgets() +{ + local w + for w in \ + self-insert \ + delete-char \ + backward-delete-char \ + accept-line + do + # call the internal version, ie. the one beginning with `.` + eval "_codestats_${w} () { _codestats_call_widget .${w} -- \"\$@\" }" + zle -N ${w} _codestats_${w} + + _codestats_log "Wrapped and rebound the ${w} widget." + done +} + +# Pulse sending function +_codestats_send_pulse() +{ + # Check that error count hasn't been exceeded + local -i error_count=0 + if [[ -r "${_codestats_consecutive_errors}" ]]; then + error_count=$(wc -l < "${_codestats_consecutive_errors}") + fi + if (( error_count > 4 )); then + _codestats_log "Received too many consecutive errors! Stopping..." + _codestats_stop "Received ${error_count} consecutive errors when trying to save XP." + return + fi + + # If there's accumulated XP, send it + if (( _codestats_xp > 0 )); then + local url + url="$(_codestats_pulse_url)" + + _codestats_log "Sending pulse (${_codestats_xp} xp) to ${url}" + + local payload + payload=$(_codestats_payload ${_codestats_xp}) + + \curl \ + --max-time 5 \ + --header "Content-Type: application/json" \ + --header "X-API-Token: ${CODESTATS_API_KEY}" \ + --user-agent "code-stats-zsh/${_codestats_version}" \ + --data "${payload}" \ + --request POST \ + --silent \ + --output /dev/null \ + --write-out "%{http_code}" \ + "${url}" \ + | _codestats_handle_response_status \ + &| + + _codestats_xp=0 + fi +} + +# Error handling based on HTTP status +_codestats_handle_response_status() +{ + local _status + _status=$(\cat -) + case ${_status} in + 000) + _codestats_log "Network error!" + # don't stop; maybe the network will start working eventually + ;; + 200 | 201 ) + _codestats_log "Success (${_status})!" + # clear error count + echo -n >! "${_codestats_consecutive_errors}" + ;; + 3* ) + _codestats_log "Unexpected redirect ${_status}!" + _codestats_stop "Server responded with a redirect. Perhaps code-stats-zsh is out of date?" + # this problem will probably not go away. stop immediately. + ;; + 4* | 5* ) + _codestats_log "Server responded with error ${_status}!" + # some of 4xx and 5xx statuses may indicate a temporary problem + echo "${_status}" >>! "${_codestats_consecutive_errors}" + ;; + *) + _codestats_log "Unexpected response status ${_status}!" + # whatever happened, stop if it persists + echo "${_status}" >>! "${_codestats_consecutive_errors}" + ;; + esac +} + +_codestats_pulse_url() +{ + echo "${CODESTATS_API_URL:-https://codestats.net}/api/my/pulses" +} + +# Create API payload +_codestats_payload() +{ + # shellcheck disable=SC2086 + cat < 10 )); then + _codestats_send_pulse + _codestats_pulse_time=${EPOCHSECONDS} + fi +} + +_codestats_exit() +{ + _codestats_log "Shell is exiting. Calling _codestats_send_pulse one last time." + _codestats_send_pulse + + # remove temp file + rm -f "${_codestats_consecutive_errors}" +} + +_codestats_init() +{ + _codestats_log "Initializing code-stats-zsh@${_codestats_version}..." + + _codestats_rebind_widgets + + # Call the polling function on each new prompt + autoload -U add-zsh-hook + add-zsh-hook precmd _codestats_poll + + # Send pulse on shell exit + add-zsh-hook zshexit _codestats_exit + + _codestats_log "Initialization complete." +} + +# Stop because there was an error. Overwrite handler functions. +_codestats_stop() +{ + _codestats_log "Stopping zsh-code-stats. Overwriting hook functions with no-ops." + >&2 echo "code-stats-zsh: $* Stopping." + _codestats_poll() { true; } + _codestats_exit() { true; } + + # remove temp file + rm -f "${_codestats_consecutive_errors}" +} + +if [[ -n "${CODESTATS_API_KEY}" ]]; then + _codestats_init +else + echo "code-stats-zsh requires CODESTATS_API_KEY to be set!" + false +fi + +if [[ -n "${CODESTATS_LOG_FILE}" && ! -w "${CODESTATS_LOG_FILE}" ]]; then + echo "Warning: CODESTATS_LOG_FILE needs to exist and be writable!" +fi + diff --git a/files/dotfiles/zshrc b/files/dotfiles/zshrc index 0a8089d..f2d2e3a 100644 --- a/files/dotfiles/zshrc +++ b/files/dotfiles/zshrc @@ -108,6 +108,10 @@ source ~/.shellconfig/aliases.sh source ~/.shellconfig/environment.sh # Functions source ~/.shellconfig/functions.sh +# Codestats if env var is set +if [ -n "$1" ]; then + source ~/.shellconfig/codestats.plugin.zsh +fi # Auto complete functions if [ -d ~/.shellconfig/autocomplete ]; then [ "$(ls -A ~/.shellconfig/autocomplete/)" ] && source <(cat ~/.shellconfig/autocomplete/*) @@ -120,3 +124,5 @@ fi if [[ $DEVICE_ROLE == "WORKSTATION" ]]; then [ -z $TMUX ] && (tmux new-session ~/.shellconfig/workspaces/open_workspace.sh) fi + +eval "$(starship init zsh)" diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml index b02adf0..23c5a65 100644 --- a/roles/base/tasks/main.yml +++ b/roles/base/tasks/main.yml @@ -1,3 +1,9 @@ +- name: Enable copr for starship.rs + become: true + community.general.copr: + state: enabled + name: atim/starship + - name: Installing packages become: true package: @@ -23,6 +29,7 @@ - bat - fzf - unzip + - starship state: present - name: Copy gitconfig base