diff --git a/home/packages/cli/default.nix b/home/packages/cli/default.nix index befd2c7..7d6900b 100644 --- a/home/packages/cli/default.nix +++ b/home/packages/cli/default.nix @@ -3,5 +3,6 @@ _: { ./shared.nix ./desktop.nix ./wayland.nix + ./scripts ]; } diff --git a/home/packages/cli/scripts/default.nix b/home/packages/cli/scripts/default.nix new file mode 100644 index 0000000..307edbb --- /dev/null +++ b/home/packages/cli/scripts/default.nix @@ -0,0 +1,14 @@ +{ + pkgs, + ... +}: let + + scriptPkgs = (import ./packages {inherit pkgs;}); +in { + home.packages = with scriptPkgs; [ + bitcoin + cheatsh + colors256 + unix + ]; +} diff --git a/home/packages/cli/scripts/packages/bitcoin.nix b/home/packages/cli/scripts/packages/bitcoin.nix new file mode 100644 index 0000000..737e0d4 --- /dev/null +++ b/home/packages/cli/scripts/packages/bitcoin.nix @@ -0,0 +1,18 @@ +{pkgs, ...}: +(pkgs.writeShellApplication { + name = "bitcoin"; + runtimeInputs = with pkgs; [coreutils curl jq]; + text = '' + #!/bin/sh + + BTC_DATA=$(curl https://api.coindesk.com/v1/bpi/currentprice.json 2>/dev/null || echo 'ERR') + + if [ "$BTC_DATA" != "ERR" ]; then + BTC_PRICE=$(echo "$BTC_DATA" | jq -r ".bpi.USD.rate_float") + BTC_PRICE=$(printf "%.0f" "$BTC_PRICE") + echo \$"$BTC_PRICE" + else + echo "N/A" + fi + ''; +}) diff --git a/home/packages/cli/scripts/packages/cheatsh/cheat.sh b/home/packages/cli/scripts/packages/cheatsh/cheat.sh new file mode 100644 index 0000000..76dd346 --- /dev/null +++ b/home/packages/cli/scripts/packages/cheatsh/cheat.sh @@ -0,0 +1,112 @@ +#!/bin/sh + +# rm_trailing_slashes(string) +# +# Prints a string without any trailing slashes. +# This is used because cheat.sh doesn't play nicely with multiple slashes in +# URLs. +rm_trailing_slashes() { + string="${1-}" + last_char="$(printf "%s" "$string" | tail -c 1)" + if [ "$last_char" = "/" ]; then + rm_trailing_slashes "${string%?}" + else + echo "$string" + fi +} + +# pick_category(must_match, query, argument, recurse) +# +# Pick cheat.sh category. +# if must_match is 1, only allow listed options to be picked. +# if query is specified, pick sub-category of it, else pick global categories. +# if argument is specified, optionally perform must_match check and print it. +# if recurse is 1, if the selected option ends with /, run the function again. +# +# Prints the chosen category +pick_category() { + must_match="$1" + query="$(rm_trailing_slashes "$2")" + argument="$3" + recurse="$4" + + # Query all possible options + if [ -n "$query" ]; then + url="cheat.sh/$query/:list" + else + url="cheat.sh/:list" + fi + selectable="$(curl -s "$url")" + + # If argument is specified, print it, optionally perform must_match check. + if [ -n "$argument" ]; then + if [ "$must_match" -ne 1 ] || echo "$selectable" | grep -qe "\b$1\b"; then + selected="$argument" + else + echo "Invalid selection: '$argument'" + echo "For all selections, query $url" + exit 1 + fi + # Select the option with fzf, optionally allow other matches if must_match isn't set. + else + if [ "$must_match" -ne 1 ]; then + if [ -z "$selectable" ]; then + header="No selections found, you can use empty query to show category help, or type a custom query." + else + header="Use alt-enter to enter non-listed query. You can use empty queries to show category help." + fi + selected="$( + printf "\n%s" "$selectable" | + fzf --bind=alt-enter:print-query \ + --print-query \ + --prompt="cheat.sh/$query query>" \ + --header="$header" + )" + else + selected=$(printf "%s" "$selectable" | fzf --prompt="cheat.sh/$query category>") + # shellcheck disable=SC2181 + if [ $? -ne 0 ]; then + echo "Invalid selection: '$selected'" + echo "For all selections, query $url" + exit 1 + fi + fi + selected=$(printf "%s" "$selected" | tail -1) + fi + + # Replace spaces with '+' (cheat.sh resolves those as spaces) + selected="$(echo "$selected" | tr ' ' '+')" + + # Prepend the original query, if we have one + # Print the selected category, or subcategory with the category + if [ -n "$query" ]; then + result="$query/$selected" + else + result="$selected" + fi + + # Recurse, if specified and the result ended with / + if [ "$recurse" -eq 1 ]; then + if [ "$(printf "%s" "$selected" | tail -c 1)" = "/" ]; then + result="$(pick_category "$must_match" "$result" "$argument" 1)" + fi + fi + + # Print the result + printf "%s" "$result" +} + +# Select the cheatsheat category (language/core-util/...) +query=$(pick_category 1 "" "${1-}" 0) + +# If the query isn't already complete, select a sub-category +if ! echo "$query" | grep -qe ":"; then + query="$(pick_category 0 "$query" "${2-}" 1)" +fi + +# Construct the URL from given query and print it +url="cheat.sh/$query" +echo "$url" + +# Show the output of cheat.sh request +curl -s "$url" diff --git a/home/packages/cli/scripts/packages/cheatsh/default.nix b/home/packages/cli/scripts/packages/cheatsh/default.nix new file mode 100644 index 0000000..d210476 --- /dev/null +++ b/home/packages/cli/scripts/packages/cheatsh/default.nix @@ -0,0 +1,8 @@ +{pkgs, ...}: +(pkgs.writeShellApplication { + name = "cheat.sh"; + runtimeInputs = with pkgs; [coreutils curl jq gnugrep fzf]; + text = '' + ${builtins.readFile ./cheat.sh} + ''; +}) diff --git a/home/packages/cli/scripts/packages/colors256/colors-256.sh b/home/packages/cli/scripts/packages/colors256/colors-256.sh new file mode 100644 index 0000000..205fbc0 --- /dev/null +++ b/home/packages/cli/scripts/packages/colors256/colors-256.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash + +# # Print out 256 colors, with each number printed in its corresponding colour +# +# This file is uploaded on +# It was originally intended to be used as an alias that curled this URL and piped it into bash, however +# this is very unsafe as the owner can change the content of this gist at his convenience, meaning it was +# a potential security vulnerability. This is a script version of this alias to avoid this issue. +# +# The copyright for this file belongs to the original author: Tom Hale, 2016 +# This file was released under MIT license + +set -eu # Fail on errors or undeclared variables + +printable_colours=256 + +# Return a colour that contrasts with the given colour +# Bash only does integer division, so keep it integral +function contrast_colour { + colour="$1" + + if ((colour < 16)); then # Initial 16 ANSI colours + ((colour == 0)) && printf "15" || printf "0" + return + fi + + # Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8 + if ((colour > 231)); then # Greyscale ramp + ((colour < 244)) && printf "15" || printf "0" + return + fi + + # All other colours: + # 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5] + # See http://stackoverflow.com/a/27165165/5353461 + + # r=$(( (colour-16) / 36 )) + g=$((((colour - 16) % 36) / 6)) + # b=$(( (colour-16) % 6 )) + + # If luminance is bright, print number in black, white otherwise. + # Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601 + ((g > 2)) && printf "0" || printf "15" + return + + # Uncomment the below for more precise luminance calculations + + # # Calculate percieved brightness + # # See https://www.w3.org/TR/AERT#color-contrast + # # and http://www.itu.int/rec/R-REC-BT.601 + # # Luminance is in range 0..5000 as each value is 0..5 + # luminance=$(( (r * 299) + (g * 587) + (b * 114) )) + # (( $luminance > 2500 )) && printf "0" || printf "15" +} + +# Print a coloured block with the number of that colour +function print_colour { + local colour="$1" contrast + contrast=$(contrast_colour "$1") + printf "\e[48;5;%sm" "$colour" # Start block of colour + printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number + printf "\e[0m " # Reset colour +} + +# Starting at $1, print a run of $2 colours +function print_run { + local i + for ((i = "$1"; i < "$1" + "$2" && i < printable_colours; i++)); do + print_colour "$i" + done + printf " " +} + +# Print blocks of colours +function print_blocks { + local start="$1" i + local end="$2" # inclusive + local block_cols="$3" + local block_rows="$4" + local blocks_per_line="$5" + local block_length=$((block_cols * block_rows)) + + # Print sets of blocks + for ((i = start; i <= end; i += (blocks_per_line - 1) * block_length)); do + printf "\n" # Space before each set of blocks + # For each block row + for ((row = 0; row < block_rows; row++)); do + # Print block columns for all blocks on the line + for ((block = 0; block < blocks_per_line; block++)); do + print_run $((i + (block * block_length))) "$block_cols" + done + ((i += block_cols)) # Prepare to print the next row + printf "\n" + done + done +} + +print_run 0 16 # The first 16 colours are spread over the whole spectrum +printf "\n" +print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive +print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey diff --git a/home/packages/cli/scripts/packages/colors256/default.nix b/home/packages/cli/scripts/packages/colors256/default.nix new file mode 100644 index 0000000..ef40b74 --- /dev/null +++ b/home/packages/cli/scripts/packages/colors256/default.nix @@ -0,0 +1,9 @@ +{pkgs, ...}: +(pkgs.writeShellApplication { + name = "colors-256"; + runtimeInputs = with pkgs; [coreutils]; + text = '' + ${builtins.readFile ./colors-256.sh} + ''; +}) + diff --git a/home/packages/cli/scripts/packages/default.nix b/home/packages/cli/scripts/packages/default.nix new file mode 100644 index 0000000..2d9e5d7 --- /dev/null +++ b/home/packages/cli/scripts/packages/default.nix @@ -0,0 +1,13 @@ +{ + pkgs, + ... +}: let + packages = { + bitcoin = pkgs.callPackage ./bitcoin.nix {}; + cheatsh = pkgs.callPackage ./cheatsh {}; + colors256 = pkgs.callPackage ./colors256 {}; + unix = pkgs.callPackage ./unix {}; + }; +in + packages + diff --git a/home/packages/cli/scripts/packages/unix/default.nix b/home/packages/cli/scripts/packages/unix/default.nix new file mode 100644 index 0000000..1788100 --- /dev/null +++ b/home/packages/cli/scripts/packages/unix/default.nix @@ -0,0 +1,10 @@ +{pkgs, ...}: +(pkgs.writeShellApplication { + name = "unix"; + runtimeInputs = with pkgs; [coreutils]; + text = '' + ${builtins.readFile ./unix.sh} + ''; +}) + + diff --git a/home/packages/cli/scripts/packages/unix/unix.sh b/home/packages/cli/scripts/packages/unix/unix.sh new file mode 100644 index 0000000..54745b0 --- /dev/null +++ b/home/packages/cli/scripts/packages/unix/unix.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +#original artwork by http://www.sanderfocus.nl/#/portfolio/tech-heroes +#converted to shell by #nixers @ irc.unix.chat + +cat <<'eof' + ,_ ,_==▄▂ + , ▂▃▄▄▅▅▅▂▅¾. / / + ▄▆<´ "»▓▓▓%\ / / / / + ,▅7" ´>▓▓▓% / / > / >/% + ▐¶▓ ,»▓▓¾´ /> %/%// / / + ▓▃▅▅▅▃,,▄▅▅▅Æ\// ///>// />/ / + V║«¼.;→ ║<«.,`=// />//%/% / / + //╠<´ -²,)(▓~"-╝/¾/ %/>/ /> + / / / ▐% -./▄▃▄▅▐, /7//;//% / / + / ////`▌▐ %zWv xX▓▇▌//&;% / / + / / / %//%/¾½´▌▃▄▄▄▄▃▃▐¶\/& / + </ /)VY>7; \_ UNIX IS VERY SIMPLE IT JUST NEEDS A + / /</ //<///<_/%\▓ V%W%£)XY _/%‾\_, GENIUS TO UNDERSTAND ITS SIMPLICITY + / / //%/_,=--^/%/%%\¾%¶%%} /%%%%%%;\, + %/< /_/ %%%%%;X%%\%%;, _/%%%;, \ + / / %%%%%%;, \%%l%%;// _/%;, dmr + / %%%;, <;\-=-/ / + ;, l +eof diff --git a/home/programs/graphical/bars/eww/config/modules/bitcoin.yuck b/home/programs/graphical/bars/eww/config/modules/bitcoin.yuck index 963ef6c..d4e59f7 100644 --- a/home/programs/graphical/bars/eww/config/modules/bitcoin.yuck +++ b/home/programs/graphical/bars/eww/config/modules/bitcoin.yuck @@ -1,6 +1,6 @@ (defwidget bitcoin_module [] (eventbox - :onclick "~/.local/bin/scripts/cli/bitcoin | xargs -I_ ${EWW_CMD} update bitcoin=_" + :onclick "bitcoin | xargs -I{} ${EWW_CMD} update bitcoin={}" :class "module bitcoin" (box diff --git a/home/programs/graphical/bars/eww/config/modules/variables.yuck b/home/programs/graphical/bars/eww/config/modules/variables.yuck index 3dbc09e..f3be245 100644 --- a/home/programs/graphical/bars/eww/config/modules/variables.yuck +++ b/home/programs/graphical/bars/eww/config/modules/variables.yuck @@ -37,7 +37,7 @@ (defpoll bitcoin :interval "5m" :initial "$N/A" - `~/.local/bin/scripts/cli/bitcoin`) + `bitcoin`) ; TODO: Figure out how to store this one-time (defpoll kernel diff --git a/home/programs/graphical/bars/eww/default.nix b/home/programs/graphical/bars/eww/default.nix index 3988409..a6a680f 100644 --- a/home/programs/graphical/bars/eww/default.nix +++ b/home/programs/graphical/bars/eww/default.nix @@ -6,6 +6,8 @@ }: let inherit (lib) mkIf; + scriptPkgs = (import ../../../../packages/cli/scripts/packages {inherit pkgs;}); + cfg = osConfig.myOptions.home-manager.programs.bars.eww; in { config = mkIf cfg.enable { @@ -21,6 +23,7 @@ in { python3 bash coreutils + findutils gnugrep gawk netcat-openbsd @@ -36,6 +39,7 @@ in { wireplumber pulseaudio hyprland + scriptPkgs.bitcoin ]; in { Unit = { @@ -66,7 +70,7 @@ in { Unit = { Description = "Open bar0 eww (ElKowar's Wacky Widgets) window"; After = [ "eww.service" ]; - PartOf = [ "graphical-session.target" ]; + PartOf = [ "graphical-session.target" "eww.service" ]; }; Service = { diff --git a/home/programs/terminal/tools/git/aliases.nix b/home/programs/terminal/tools/git/aliases.nix index b9968bf..7972a97 100644 --- a/home/programs/terminal/tools/git/aliases.nix +++ b/home/programs/terminal/tools/git/aliases.nix @@ -1,4 +1,10 @@ { + pkgs, + ... +}: let + + scriptPkgs = (import ./bin {inherit pkgs;}); +in { programs.git = { aliases = { quickclone = "clone --single-branch --depth=1"; @@ -48,7 +54,7 @@ bD = "branch --delete --force"; bm = "branch --move"; bM = "branch --move --force"; - bb = "!~/.local/bin/scripts/cli/better-git-branch"; + bb = "!${scriptPkgs.better-git-branch}/bin/better-git-branch"; r = "rebase"; ri = "rebase -i"; diff --git a/home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh b/home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh new file mode 100644 index 0000000..5d4f4da --- /dev/null +++ b/home/programs/terminal/tools/git/bin/better-git-branch/better-git-branch.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Source: https://gist.github.com/schacon/e9e743dee2e92db9a464619b99e94eff + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +NO_COLOR='\033[0m' +BLUE='\033[0;34m' +YELLOW='\033[0;33m' +NO_COLOR='\033[0m' + +width1=5 +width2=6 +width3=30 +width4=20 +width5=20 + +# Function to count commits +count_commits() { + local branch="$1" + local base_branch="$2" + local ahead_behind + + ahead_behind=$(git rev-list --left-right --count "$base_branch"..."$branch") + echo "$ahead_behind" +} + +# Main script +main_branch=$(git rev-parse HEAD) + +printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" "Ahead" "Behind" "Branch" "Last Commit" " " + +# Separator line for clarity +printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" "-----" "------" "------------------------------" "-------------------" " " + +format_string="%(objectname:short)@%(refname:short)@%(committerdate:relative)" +IFS=$'\n' + +for branchdata in $(git for-each-ref --sort=-authordate --format="$format_string" refs/heads/ --no-merged); do + sha=$(echo "$branchdata" | cut -d '@' -f1) + branch=$(echo "$branchdata" | cut -d '@' -f2) + time=$(echo "$branchdata" | cut -d '@' -f3) + if [ "$branch" != "$main_branch" ]; then + # Get branch description + description=$(git config branch."$branch".description) + + # Count commits ahead and behind + ahead_behind=$(count_commits "$sha" "$main_branch") + ahead=$(echo "$ahead_behind" | cut -f2) + behind=$(echo "$ahead_behind" | cut -f1) + + # Display branch info + # shellcheck disable=SC2086 + printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" $ahead $behind $branch "$time" "$description" + fi +done diff --git a/home/programs/terminal/tools/git/bin/better-git-branch/default.nix b/home/programs/terminal/tools/git/bin/better-git-branch/default.nix new file mode 100644 index 0000000..80b4517 --- /dev/null +++ b/home/programs/terminal/tools/git/bin/better-git-branch/default.nix @@ -0,0 +1,4 @@ +{pkgs, ...}: + pkgs.writeShellScriptBin "better-git-branch" '' + ${builtins.readFile ./better-git-branch.sh} + '' diff --git a/home/programs/terminal/tools/git/bin/default.nix b/home/programs/terminal/tools/git/bin/default.nix new file mode 100644 index 0000000..8cf9cb0 --- /dev/null +++ b/home/programs/terminal/tools/git/bin/default.nix @@ -0,0 +1,9 @@ +{ + pkgs, + ... +}: let + packages = { + better-git-branch = pkgs.callPackage ./better-git-branch {}; + }; +in + packages