sebastiancarlos

joined 1 year ago
 
 

Source code:

#!/usr/bin/env bash

# runasm - Assemble, link, and run multiple assembly files, then delete them.
if [[ $# -eq 0 ]]; then
    echo "Usage: runasm  [ ...]"
    echo "  - Assemble, link, and run multiple assembly files, then delete them."
    echo "  - Name of executable is the name of the first file without extension."
    exit 1
fi

object_files=()
executable_file=${1%.*}

for assembly_file in "$@"; do
    # Avengers, assemble!
    object_file="${assembly_file%.*}.o"
    as "${assembly_file}" -o "${object_file}"
    if [[ $? -ne 0 ]]; then
        exit 1
    fi
    object_files+=("${object_file}")
done

# Link
ld "${object_files[@]}" -o "${executable_file}"
if [[ $? -ne 0 ]]; then
    exit 1
fi

# Run, remove created files, and return exit code
./"${executable_file}"
exit_code=$?
rm "${object_files[@]}" "${executable_file}" > /dev/null 2>&1
exit "${exit_code}"
 
[–] [email protected] 1 points 8 months ago

Cool, thanks! Look like there are things like xrandr for Wayland with this functionality. the burn-in is not so bad right now but I'll keep this in mind.

[–] [email protected] 2 points 8 months ago

Yes! I use it all the time. No idea why it's not more popular

[–] [email protected] 4 points 8 months ago (2 children)

.bashrc:

# Prompt
# "Make it simple, just the dollar sign"
# "Say no more, fam"
# - if error code is not 0, then prepend [N] where N is the error code
# - if user is root, use red and #
blue='\e[34m'
red='\e[31m'
bold='\e[1m'
reset='\e[0m'
PS1='$( status=$?; [ $status -ne 0 ] && echo "[$status] ")\['"$blue""$bold"'\]$\['"$reset"'\] '

if [[ $EUID -eq 0 ]]; then
  PS1='$( status=$?; [ $status -ne 0 ] && echo "[$status] ")\['"$red""$bold"'\]#\['"$reset"'\] '
fi

.inputrc:

# vi mode, change to 'emacs' here if you prefer
set editing-mode vi

# vi INSERT prompt
set vi-ins-mode-string "\1\e[30;44m\2 INS \1\e[0m\2 "

# vi NORMAL prompt
set vi-cmd-mode-string "\1\e[30;47m\2 NOR \1\e[0m\2 "
[–] [email protected] 1 points 8 months ago* (last edited 8 months ago)

answered in post. btw it seems this is a Sway only issue, so it makes sense it's ok in i3

[–] [email protected] 1 points 8 months ago

answered in post

[–] [email protected] 1 points 8 months ago (3 children)

answered in post

 

I need extremely small tiling windows, say something with only a width/height of 10px. However, by experimentation I found that my window manager Sway (and likely also i3) has a hard-coded minimum height of 60px and minimum width of 100px.

So, dear Linux gurus, do you know of a window manager with configurable minimum tiling window size? Or with no minimum at all? Bonus point for running natively on Wayland.

Edit: Found the culprit in the sway codebase:

sway/include/sway/tree/node.h

#define MIN_SANE_W 100
#define MIN_SANE_H 60

In short, I am not sane.

These constants are not used much in the codebase, so it shouldn't hurt to change it and compile from source. I will report results. Btw I opened an issue to see if they can be made configurable. Plus, some code archaeology suggests that this is not an issue in i3.

Thanks for your suggestions. Some, like Hyprland and dwl, sound promising, but I'll try to make it work with Sway first.

Why do I need this, you ask? It's a bit of a secret, but I've been working for about two years on a custom "operating system", or rather a suite of productivity tools unlike anything seen before. I'm about to finish it, but one of my last requirements to make it all click is a tiling window manager that is both extremely minimal and extremely customizable. It will eventually be released as free software for the benefit, amusement, and horror of everyone.

Also the top 20px of my screen has burn-in and I want to declare it unusable at the window manager level. You see, I use Linux not only to flex, but also to live frugally.

Edit 2: Compiling from source worked. Patch here:

diff --unified --recursive --text sway-git/include/sway/tree/node.h sway-git.new/include/sway/tree/node.h
***
sway-git/include/sway/tree/node.h	2023-10-23 19:21:15.915536904 +0200
+++ sway-git.new/include/sway/tree/node.h	2023-10-23 19:30:18.638894754 +0200
@@ -4,8 +4,8 @@
 #include 
 #include "list.h"
 
-#define MIN_SANE_W 100
-#define MIN_SANE_H 60
+#define MIN_SANE_W 20
+#define MIN_SANE_H 20
 
 struct sway_root;
 struct sway_output;
[–] [email protected] 2 points 8 months ago

Both are incomplete, but the article is no longer paywalled

[–] [email protected] 7 points 9 months ago
60
submitted 9 months ago* (last edited 9 months ago) by [email protected] to c/[email protected]
 

Source code

#!/usr/bin/env bash

# colorcat
# - cats a file, but if any line contains N hex colors, it appends the colors
#   (rendered as ansi escape sequences) to the end of the line.
# - input can be stdin, a file, or a hex color in plain text
if [[ "$#" -eq 1 && ! -f "$1" ]]; then
  echo "$1"
else
  cat "$@"
fi | while read -r line; do
  colors=""
  for word in $line; do
    if [[ "$word" =~ ^[^A-Fa-f0-9]*#?([A-Fa-f0-9]{6})[^A-Fa-f0-9]*$ ]]; then
      hex=${BASH_REMATCH[1]}
      r=$((16#${hex:0:2}))
      g=$((16#${hex:2:2}))
      b=$((16#${hex:4:2}))
      truecolor="\033[48;2;${r};${g};${b}m"
      reset="\033[0m"
      colors="${colors}${truecolor}  ${reset} "
    fi
  done
    echo -e "$line $colors" 
done
 

Source code:

# source of truth for URL aliases
# - used by url-alias-setup and url-alias
# - can be modified to add new aliases
declare -A __url_alias=(
  ["g"]="https://google.com"
  ["r"]="https://reddit.com"
  ["h"]="https://news.ycombinator.com"
)

# url-alias
# - print the current URL aliases
function url-alias () {
  local green="\033[32m"
  local cyan="\033[36m"
  local reset="\033[0m"

  echo "${green}URL aliases:${reset}"
  for alias in "${!__url_alias[@]}"; do
    echo "${cyan}${alias}${reset} -> ${__url_alias[${alias}]}"
  done

  echo "${green}To add new aliases, edit the ${cyan}__url_alias${green} array and run ${cyan}url-alias-setup${reset}"
}

# return either 'linux' or 'macos'
function get_platform () {
  case "$(uname -s)" in
    Darwin)
      echo "macos"
      ;;
    Linux)
      echo "linux"
      ;;
    *)
      echo "unsupported platform"
      exit 1
      ;;
  esac
}
platform=$(get_platform)

# url-alias-setup
# - sets up URL aliases
# - this is done by modifying the /etc/hosts file and the nginx configuration
# - if changes are made, nginx is (re)started
function url-alias-setup () {
  # nginx config (platform dependent) 
  if [[ "$platform" == "macos" ]]; then
    local nginx_config="/usr/local/etc/nginx/nginx.conf"
  else
    local nginx_config="/etc/nginx/nginx.conf"
  fi

  # create new nginx config and hosts file
  local new_hosts=""
  read -r -d '' new_nginx_config << 'EOF'
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
EOF
  
  for alias in "${!__url_alias[@]}"; do
    local url="${__url_alias[$alias]}"

    new_hosts="${new_hosts}\n127.0.0.1 ${alias}"

    read -r -d '' server_blocks << EOF
    server {
        listen       80;
        server_name  ${alias};
        location / {
          rewrite ^ ${url} permanent;
        }
    }
EOF
    new_nginx_config="${new_nginx_config}\n    ${server_blocks}"
  done
  new_nginx_config="${new_nginx_config}\n}"
  
  # replace files
  # if file already exists, prompt user to overwrite
  echo "Saving new nginx config and hosts file..."
  if [[ -f "${nginx_config}" ]]; then
    echo "File ${nginx_config} already exists. Overwrite? (y/n)"
    read -r overwrite
    if [[ "${overwrite}" != "y" ]]; then
      echo "Aborting..."
      return 1
    fi
  fi
  echo -e "${new_nginx_config}" | sudo tee "${nginx_config}" > /dev/null
  if [[ -f "/etc/hosts" ]]; then
    echo "File /etc/hosts already exists. Overwrite? (y/n)"
    read -r overwrite
    if [[ "${overwrite}" != "y" ]]; then
      echo "Aborting..."
      return 1
    fi
  fi
  echo -e "${new_hosts}" | sudo tee /etc/hosts > /dev/null

  # start or restart nginx
  echo "Restarting nginx..."
  if [[ "$platform" == "macos" ]]; then
    nginx -s reload
  else
    sudo systemctl restart nginx
  fi
}
[–] [email protected] 1 points 9 months ago

Looks like you sshd into my working titles

[–] [email protected] 23 points 9 months ago (7 children)

It's also traditional to eat raw meat, but we discovered fire at some point.

[–] [email protected] 27 points 9 months ago* (last edited 9 months ago) (10 children)

You also don’t need the dash for the short options.

True, but I refuse to entertain such a non-standard option format. It's already enough to tolerate find's.

 
14
submitted 9 months ago* (last edited 9 months ago) by [email protected] to c/[email protected]
 

Source code

#!/usr/bin/env bash

# get the texinfo node for a command
# - takes a command name, like 'cat'
# - if it belongs to a texinfo node, returns the node name, like 'coreutils' 
# - otherwise returns empty string
function __get_texinfo_node_for_command () {
  local coreutils_commands=(
    'cat' 'tac' 'nl' 'od' 'base32' 'base64' 'base64' 'basenc'
    'fmt' 'pr' 'fold' 
    'head' 'tail' 'split' 'csplit'  
    'wc' 'sum' 'cksum' 'b2sum' 'md5sum' 'sha1sum'    
    'sha224sum' 'sha256sum' 'sha384sum' 'sha512sum'
    'sort' 'shuf' 'uniq' 'comm' 'ptx' 'tsort' 'ptx'
    'cut' 'paste' 'join'
    'tr' 'expand' 'unexpand'
    'ls' 'dir' 'vdir' 'dircolors'
    'cp' 'dd' 'install' 'mv' 'rm' 'shred'
    'link' 'ln' 'mkdir' 'mkfifo' 'mknod' 'readlink' 'rmdir' 'unlink'
    'chown' 'chgrp' 'chmod' 'touch'
    'df' 'du' 'stat' 'sync' 'truncate'
    'echo' 'printf' 'yes'
    'false' 'true' 'test' 'expr' 'tee'
    'basename' 'dirname' 'pathchk' 'mktemp' 'realpath'
    'pwd' 'stty' 'printenv' 'tty'
    'id' 'logname' 'whoami' 'groups' 'users' 'who'
    'arch' 'date' 'nping' 'uname' 'hostname' 'hostid' 'uptime'
    'chcon' 'runcon'
    'chroot' 'env' 'nice' 'nohup' 'stdbuf' 'timeout'
    'kill' 'sleep'
    'factor' 'numfmt' 'seq' 
  )

  local bash_builtins=(
    '.' ':' '[' 'alias' 'bg' 'bind' 'break' 'builtin' 'caller' 'cd' 'command'
    'compgen' 'complete' 'compopt' 'continue' 'declare' 'dirs' 'disown' 'echo'
    'enable' 'eval' 'exec' 'exit' 'export' 'false' 'fc' 'fg' 'getopts' 'hash'
    'help' 'history' 'jobs' 'kill' 'let' 'local' 'logout' 'mapfile' 'popd'
    'printf' 'pushd' 'pwd' 'read' 'readarray' 'readonly' 'return' 'set'
    'shift' 'shopt' 'source' 'suspend' 'test' 'times' 'trap' 'type' 'typeset'
    'ulimit' 'umask' 'unalias' 'unset' 'wait'

    # and some reserved words too:
    '!' # this represents pipelines
    '[[' '{' 'case' 'if' 'for' 'function' 'select' 'time' 'until' 'while'
  )
  
  for cmd in "${bash_builtins[@]}"; do
    if [[ "$1" == "$cmd" ]]; then
      echo 'bash'
      return 0
    fi
  done
  for cmd in "${coreutils_commands[@]}"; do
    if [[ "$1" == "$cmd" ]]; then
      echo 'coreutils'
      return 0
    fi
  done

  echo ''
  return 0
}

# custom info
# - prepend 'coreutils' if argument is a coreutils command
# - prepend 'bash' if argument is a bash builtin
# - add some default options
function info () {
  # if there is only one non-option argument, and it matches a coreutils or
  # bash builtin, prepend the corresponding name
  local option_args=()
  local non_option_args=()
  for arg in "$@"; do
    if [[ ! "$arg" =~ ^- ]]; then
      non_option_args+=("$arg")
    else
      option_args+=("$arg")
    fi
  done
  if [[ "${#non_option_args[@]}" -eq 1 ]]; then
    info_node="$(__get_texinfo_node_for_command "${non_option_args[0]}")"
    if [[ -n "$info_node" ]]; then
        non_option_args=("${info_node}" "${non_option_args[@]}")
    fi
  fi

  # add default options and run
  option_args+=('--vi-keys' '--init-file' "${XDG_CONFIG_HOME}/infokey")
  command info "${option_args[@]}" "${non_option_args[@]}"
}
alias i="info"

# custom man
# - if receiving a command with info page, run info instead
function man () {
  if [[ "$#" -eq 1 && $(__get_texinfo_node_for_command "$1") != '' ]]; then
    info "$1"
  else
    command man "$1"
  fi
}
alias m="man"
27
submitted 9 months ago* (last edited 9 months ago) by [email protected] to c/[email protected]
 

Source code

#!/usr/bin/env bash

# diffc - diff commands
# - allows to call as: diffc 'command one' 'command two'
#   instead of:        diff  <(command one) <(command two)
#   (Just to save typing a few characters. Lol I'm a lazy programmer)
function diffc () {
  if [[ "$#" != "2" ]]; then
    echo "diffc requires two arguments"
    return 1
  fi

  local command=$(printf 'diff <( %s ) <( %s )' "$1" "$2")
  echo $command
  eval $command
}

# diffh - diff history
# - make a diff of the output of the last two commands in the shell history
function diffh () {
  # first one is 2nd to last. second is last
  # remove preceeding spaces
  local first=$(fc -ln -2 -2)
  local second=$(fc -ln -1 -1)

  # print and run diff
  local command=$(printf 'diff <( %s ) <( %s )' "${first}" "${second}")
  echo $command
  eval $command

  local error_code=$?

  # replace this 'diffh' entry in history with the command 
  history -s "$(echo $command)"
  
  return $error_code
}

[–] [email protected] 14 points 10 months ago (1 children)
 

Among the 15~ million lines of codes in the GCC source code, there is a single CSS line.

Who dares to call CSS bloat now? More like GSS eh.

$ cloc gcc-13.2.0
github.com/AlDanial/cloc v 1.98  T=113.87 s (1029.7 files/s, 143776.9 lines/s)
---------------------------------------------------------------------------------------
Language                             files          blank        comment           code
---------------------------------------------------------------------------------------
C++                                  31144         610803         701522        2961450
C                                    50773         410175         635758        2033424
PO File                                 44         332336         443572         916826
Ada                                   6610         290082         396614         831278
Go                                    5347         102068         191016         769730
...
Brainf***                                1              3              4             10
Lisp                                     1              4             12              7
DOS Batch                                2              0              0              4
CSS                                      1              0              0              1
---------------------------------------------------------------------------------------
SUM:                                117256        2322548        2988111       11061312
---------------------------------------------------------------------------------------
view more: ‹ prev next ›