dotfiles

regexghost dotfiles and scripts
git clone https://git.regexghost.com/dotfiles.git
Log | Files | Refs | README

commit 32e3038cefd6008f5ec9ccd136a23fdab4800bd9
parent 0dd516c6fc6bb6af3a393903d8bc9b09d021437d
Author: regexghost <regexghost@protonmail.com>
Date:   Sat,  9 May 2026 20:00:26 +0100

added substitution program

Diffstat:
Mdotfiles/.config/btop/btop.conf | 51+++++----------------------------------------------
Mdotfiles/.config/micro/settings.json | 4++--
Mdotfiles/.config/nano/nanorc | 1+
Ahelpers/subgo/Makefile | 13+++++++++++++
Ahelpers/subgo/sub.go | 292+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ahelpers/subgo/test.sh | 166+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msetup.sh | 4++--
Mterminal-scripts/directory_bookmarks.c | 6+++---
8 files changed, 484 insertions(+), 53 deletions(-)

diff --git a/dotfiles/.config/btop/btop.conf b/dotfiles/.config/btop/btop.conf @@ -1,4 +1,4 @@ -#? Config file for btop v. 1.4.4 +#? Config file for btop v. 1.2.13 #* Name of a btop++/bpytop/bashtop formatted ".theme" file, "Default" and "TTY" for builtin themes. #* Themes should be placed in "../share/btop/themes" relative to binary or "$HOME/.config/btop/themes" @@ -37,9 +37,6 @@ graph_symbol = "braille" # Graph symbol to use for graphs in cpu box, "default", "braille", "block" or "tty". graph_symbol_cpu = "default" -# Graph symbol to use for graphs in gpu box, "default", "braille", "block" or "tty". -graph_symbol_gpu = "default" - # Graph symbol to use for graphs in cpu box, "default", "braille", "block" or "tty". graph_symbol_mem = "default" @@ -49,7 +46,7 @@ graph_symbol_net = "default" # Graph symbol to use for graphs in cpu box, "default", "braille", "block" or "tty". graph_symbol_proc = "default" -#* Manually set which boxes to show. Available values are "cpu mem net proc" and "gpu0" through "gpu5", separate values with whitespace. +#* Manually set which boxes to show. Available values are "cpu mem net proc", separate values with whitespace. shown_boxes = "cpu mem net proc" #* Update time in milliseconds, recommended 2000 ms or above for better sample times for graphs. @@ -89,9 +86,6 @@ proc_left = False #* (Linux) Filter processes tied to the Linux kernel(similar behavior to htop). proc_filter_kernel = False -#* In tree-view, always accumulate child process resources in the parent process. -proc_aggregate = False - #* Sets the CPU stat shown in upper half of the CPU graph, "total" is always available. #* Select from a list of detected attributes from the options menu. cpu_graph_upper = "total" @@ -100,9 +94,6 @@ cpu_graph_upper = "total" #* Select from a list of detected attributes from the options menu. cpu_graph_lower = "total" -#* If gpu info should be shown in the cpu box. Available values = "Auto", "On" and "Off". -show_gpu_info = "Auto" - #* Toggles if the lower CPU graph should be inverted. cpu_invert_lower = True @@ -150,7 +141,7 @@ background_update = True custom_cpu_name = "" #* Optional filter for shown disks, should be full path of a mountpoint, separate multiple values with whitespace " ". -#* Only disks matching the filter will be shown. Prepend exclude= to only show disks not matching the filter. Examples: disk_filter="/boot /home/user", disks_filter="exclude=/boot /home/user" +#* Begin line with "exclude=" to change to exclude filter, otherwise defaults to "most include" filter. Example: disks_filter="exclude=/boot /home/user". disks_filter = "" #* Show graphs instead of meters for memory values. @@ -210,45 +201,12 @@ net_sync = True #* Starts with the Network Interface specified here. net_iface = "" -#* "True" shows bitrates in base 10 (Kbps, Mbps). "False" shows bitrates in binary sizes (Kibps, Mibps, etc.). "Auto" uses base_10_sizes. -base_10_bitrate = "Auto" - #* Show battery stats in top right if battery is present. show_battery = True #* Which battery to use if multiple are present. "Auto" for auto detection. selected_battery = "Auto" -#* Show power stats of battery next to charge indicator. -show_battery_watts = True - #* Set loglevel for "~/.config/btop/btop.log" levels are: "ERROR" "WARNING" "INFO" "DEBUG". #* The level set includes all lower levels, i.e. "DEBUG" will show all logging info. -log_level = "WARNING" - -#* Measure PCIe throughput on NVIDIA cards, may impact performance on certain cards. -nvml_measure_pcie_speeds = True - -#* Measure PCIe throughput on AMD cards, may impact performance on certain cards. -rsmi_measure_pcie_speeds = True - -#* Horizontally mirror the GPU graph. -gpu_mirror_graph = True - -#* Custom gpu0 model name, empty string to disable. -custom_gpu_name0 = "" - -#* Custom gpu1 model name, empty string to disable. -custom_gpu_name1 = "" - -#* Custom gpu2 model name, empty string to disable. -custom_gpu_name2 = "" - -#* Custom gpu3 model name, empty string to disable. -custom_gpu_name3 = "" - -#* Custom gpu4 model name, empty string to disable. -custom_gpu_name4 = "" - -#* Custom gpu5 model name, empty string to disable. -custom_gpu_name5 = "" +log_level = "WARNING" +\ No newline at end of file diff --git a/dotfiles/.config/micro/settings.json b/dotfiles/.config/micro/settings.json @@ -1,9 +1,9 @@ { - "autoclose": false, "aspell.check": "off", - "hlsearch": true, + "autoclose": false, "colorscheme": "retro", "ftoptions": false, + "hlsearch": true, "savecursor": true, "softwrap": true } diff --git a/dotfiles/.config/nano/nanorc b/dotfiles/.config/nano/nanorc @@ -1,3 +1,4 @@ +include "/usr/share/nano/*.nanorc" include "~/.config/nano/markdown.nanorc" set afterends diff --git a/helpers/subgo/Makefile b/helpers/subgo/Makefile @@ -0,0 +1,13 @@ +normal: + go build -o sub sub.go + +clean: + rm sub + +install: + cp sub "${HOME}/.local/bin" + +test: + ./test.sh + +full: normal install clean diff --git a/helpers/subgo/sub.go b/helpers/subgo/sub.go @@ -0,0 +1,292 @@ +package main + +import ( + "fmt" + "os" + "regexp" + "bufio" + "strings" + "io" + "path/filepath" + "crypto/sha256" + "encoding/hex" + "path" +) + +type Paths struct { + InputFilepath string + TrueName string + SubsDir string +} + +type SubZone struct { + StartLine int + EndLine int + ZoneName string + Hash string + CurrentInstance SubInstance +} + +type SubInstance struct { + Filename string + Hash string + SubName string + Content []string +} + +// Helpers + +func panicIfErr(err error) { + if err != nil { + panic(err) + } +} + +func hashLines(lines []string) string { + hash := sha256.New() + asString := joinLines(lines) + hash.Write([]byte(asString)) + return hex.EncodeToString(hash.Sum(nil)) +} + +func hashFile(filepath string) string { + hash := sha256.New() + + file, err := os.Open(filepath) + panicIfErr(err) + + _, err = io.Copy(hash, file) + panicIfErr(err) + + return hex.EncodeToString(hash.Sum(nil)) +} + +// Get Zone and Sub names +// Zone = general name of area, Sub = specific instance of that area + +func extractZoneName(line string) string { + r, _ := regexp.Compile("Start Substitute - ([^ ]*)") + return r.FindStringSubmatch(line)[1] +} + +func extractZoneAndSubName(filename string) (string, string) { + r, _ := regexp.Compile(".*Substitution-([^-]*)-([^-]*)") + x := r.FindStringSubmatch(filename) + return x[1], x[2] +} + +// File handling + +func readFileIntoLines(filename string) (lines []string) { + file, err := os.Open(filename) + panicIfErr(err) + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + return lines +} + +func fileParse(inputFile string) []string { + lines := readFileIntoLines(inputFile) + return lines +} + +func joinLines(lines []string) string { + return strings.Join(lines, "\n") + "\n" +} + +func writeLinesToFile(lines []string, outputFile string) { + file, err := os.Create(outputFile) + panicIfErr(err) + defer file.Close() + _, err = file.Write([]byte(joinLines(lines))) + panicIfErr(err) +} + +// Input new name + +func userInputName(prompt string) (name string) { + fmt.Print("Enter name for sub (" + prompt + "): ") + fmt.Scanln(&name) + return name +} + +func nameAlreadyTaken(name string, otherInstances []SubInstance) bool { + for _, inst := range otherInstances { + if name == inst.SubName { + return true + } + } + return false +} + +// Get relevant subs for a specific file, and zone +func getRelevantInstances(paths Paths, zoneName string) (relevantInstances []SubInstance) { + entries, err := os.ReadDir(paths.SubsDir) + panicIfErr(err) + for _, e := range entries { + if strings.HasPrefix(e.Name(), zoneName + "-Substitution-") && strings.HasSuffix(e.Name(), paths.TrueName) { + _, name := extractZoneAndSubName(e.Name()) + newInstance := SubInstance { + Filename: e.Name(), + Content: fileParse(filepath.Join(paths.SubsDir, e.Name())), + SubName: name, + } + newInstance.Hash = hashLines(newInstance.Content) + + relevantInstances = append(relevantInstances, newInstance) + } + } + + return relevantInstances +} + +// Extract zones from file +func extractZones(lines []string) ([]SubZone) { + var curZone SubZone + var zones []SubZone + for i, line := range lines { + if strings.Contains(line, "Start Substitute") { + curZone = SubZone{} + curZone.StartLine = i + curZone.ZoneName = extractZoneName(line) + } + if strings.Contains(line, "End Substitute") { + curZone.EndLine = i + instance := SubInstance { + Content: lines[curZone.StartLine:i+1], + } + instance.Hash = hashLines(instance.Content) + curZone.CurrentInstance = instance + zones = append(zones, curZone) + } + } + + return zones +} + +// Check for any new subsitutions and handle them +func newSubs(paths Paths) { + lines := fileParse(paths.InputFilepath) + zones := extractZones(lines) + + for _, zone := range zones { + match := false + hash := zone.CurrentInstance.Hash + relevantInstances := getRelevantInstances(paths, zone.ZoneName) + // If there are existing instances for this zone, check for a match + for _, inst := range relevantInstances { + if inst.Hash == hash { + match = true + break + } + } + + // If we get a match don't save again + if !match { + newSub(paths, zone, relevantInstances) + } + } +} + +// Get name for new substitution and save +func newSub(paths Paths, zone SubZone, otherInstances []SubInstance) { + name := userInputName(zone.ZoneName) + for nameAlreadyTaken(name, otherInstances) { + name = userInputName(zone.ZoneName) + } + saveNewSub(paths, zone, name) +} + +// Write a new sub to file +func saveNewSub(paths Paths, zone SubZone, name string) { + filename := paths.SubsDir + "/" + zone.ZoneName + "-Substitution-" + name + "-" + paths.TrueName + writeLinesToFile(zone.CurrentInstance.Content, filename) +} + +// Prompt the user to pick between substitutions +func userPickSub(paths Paths, line string) []string { + zoneName := extractZoneName(line) + relevantInstances := getRelevantInstances(paths, zoneName) + for i, inst := range relevantInstances { + fmt.Printf("%d: %s\n", i+1, inst.SubName) + } + var choice int + fmt.Print("Pick sub to use: ") + fmt.Scanln(&choice) + chosenInst := relevantInstances[choice-1] + return readFileIntoLines(path.Join(paths.SubsDir, chosenInst.Filename)) +} + +// Assemble a final config file from base file and user chosen substitutions +func makeFinalFile(paths Paths, outputFile string) { + lines := fileParse(paths.InputFilepath) + var finalLines []string + for i, line := range lines { + _ = i + if strings.Contains(line, "Start Substitute") { + newLines := userPickSub(paths, line) + finalLines = append(finalLines, newLines...) + } else { + finalLines = append(finalLines, line) + } + } + + writeLinesToFile(finalLines, outputFile) +} + +// Remove all substitutions from a file, leaving only the header +func cleanFile(paths Paths, outputFile string) { + lines := fileParse(paths.InputFilepath) + zones := extractZones(lines) + + var finalLines []string + previous := -1 + for _, zone := range zones { + finalLines = append(finalLines, lines[previous+1:zone.StartLine+1]...) + previous = zone.EndLine + } + finalLines = append(finalLines, lines[previous+1:]...) + + writeLinesToFile(finalLines, outputFile) +} + +func printHelp() { + fmt.Println("Usage:") + fmt.Println(" sub save proper_filename /path/to/subs/ path/to/input_file") + fmt.Println(" sub make proper_filename /path/to/subs/ path/to/clean_file path/to/output_file") + fmt.Println(" e.g. sub make tmux.conf /path/to/subs/ path/to/tmux.conf final/tmux.conf") + fmt.Println(" sub clean proper_filename /path/to/subs path/to/input_file path/to/output_file") +} + +func main() { + if len(os.Args) < 5 { + printHelp() + os.Exit(1) + } + command := os.Args[1] + paths := Paths{ + InputFilepath: os.Args[4], + SubsDir: os.Args[3], + TrueName: os.Args[2], + } + + if command == "save" { + newSubs(paths) + } else if command == "clean" { + if len(os.Args) != 6 { + printHelp() + os.Exit(1) + } + outputFile := os.Args[5] + cleanFile(paths, outputFile) + } else if command == "make" { + if len(os.Args) != 6 { + printHelp() + os.Exit(1) + } + outputFile := os.Args[5] + makeFinalFile(paths, outputFile) + } +} diff --git a/helpers/subgo/test.sh b/helpers/subgo/test.sh @@ -0,0 +1,166 @@ +#!/bin/sh + +echo "Setup:" + +make > /dev/null + +[ -d testinput/ ] || mkdir testinput +[ -d testinput/subfolder ] || mkdir testinput/subfolder +[ -d testoutput/ ] || mkdir testoutput +[ -d testoutput/subfolder ] || mkdir testoutput/subfolder + +echo "<p> + <h1>h1</h1> + <li> + <ul>thing</ul> + <ul>thing2</ul> + <ul>thing3</ul> + <!-- Start Substitute - HTML_Test --> + <ul>one line</ul> + <ul>two line</ul> + <!-- End Substitute --> + <ul>thing6</ul> + </ul> +</p> +" > testinput/test.html + +echo "bind -n C-Tab select-window -n +bind -n C-BTab select-window -p +# Start Substitute - tmux_Test +set -g mouse on +# End Substitute +set -g escape-time 0 + +# Start Substitute - Other +option 1 +option 2 +option 3 +# End Substitute +option 4 +option 5 +" > testinput/subfolder/tmux.conf + +./sub save test.html testinput/ testinput/test.html << EOF +sub1 +EOF +echo "" +sed 's/two line/other line/g' testinput/test.html > /tmp/out && mv /tmp/out testinput/test.html +./sub save test.html testinput/ testinput/test.html << EOF +sub_2 +EOF +echo "" + +./sub save tmux.conf testinput/subfolder testinput/subfolder/tmux.conf << EOF +mouse_on +other_sub +EOF +echo "" +sed 's/mouse on/mouse off/g' testinput/subfolder/tmux.conf > /tmp/out && mv /tmp/out testinput/subfolder/tmux.conf +./sub save tmux.conf testinput/subfolder testinput/subfolder/tmux.conf << EOF +mouse_off +EOF +echo "" + +./sub clean test.html testinput/ testinput/test.html testinput/test_clean.html +./sub clean tmux.conf testinput/subfolder testinput/subfolder/tmux.conf testinput/subfolder/tmux_clean.conf + +./sub make test.html testinput/ testinput/test_clean.html testoutput/test_out.html << EOF +2 +EOF +echo "" +./sub make tmux.conf testinput/subfolder testinput/subfolder/tmux_clean.conf testoutput/subfolder/tmux_out.conf << EOF +1 +1 +EOF +echo "" +echo "" + + +echo "Tests:" +echo "No output = all tests passed" + +echo " <!-- Start Substitute - HTML_Test --> + <ul>one line</ul> + <ul>two line</ul> + <!-- End Substitute -->" > /tmp/testfile +diff /tmp/testfile testinput/HTML_Test-Substitution-sub1-test.html +echo " <!-- Start Substitute - HTML_Test --> + <ul>one line</ul> + <ul>other line</ul> + <!-- End Substitute -->" > /tmp/testfile +diff /tmp/testfile testinput/HTML_Test-Substitution-sub_2-test.html + +echo "# Start Substitute - tmux_Test +set -g mouse on +# End Substitute" > /tmp/testfile +diff /tmp/testfile testinput/subfolder/tmux_Test-Substitution-mouse_on-tmux.conf +echo "# Start Substitute - tmux_Test +set -g mouse off +# End Substitute" > /tmp/testfile +diff /tmp/testfile testinput/subfolder/tmux_Test-Substitution-mouse_off-tmux.conf + +echo "# Start Substitute - Other +option 1 +option 2 +option 3 +# End Substitute" > /tmp/testfile +diff /tmp/testfile testinput/subfolder/Other-Substitution-other_sub-tmux.conf + +echo "<p> + <h1>h1</h1> + <li> + <ul>thing</ul> + <ul>thing2</ul> + <ul>thing3</ul> + <!-- Start Substitute - HTML_Test --> + <ul>thing6</ul> + </ul> +</p> +" > /tmp/testfile +diff /tmp/testfile testinput/test_clean.html +echo "bind -n C-Tab select-window -n +bind -n C-BTab select-window -p +# Start Substitute - tmux_Test +set -g escape-time 0 + +# Start Substitute - Other +option 4 +option 5 +" > /tmp/testfile +diff /tmp/testfile testinput/subfolder/tmux_clean.conf +echo "<p> + <h1>h1</h1> + <li> + <ul>thing</ul> + <ul>thing2</ul> + <ul>thing3</ul> + <!-- Start Substitute - HTML_Test --> + <ul>one line</ul> + <ul>other line</ul> + <!-- End Substitute --> + <ul>thing6</ul> + </ul> +</p> +" > /tmp/testfile +diff /tmp/testfile testoutput/test_out.html + +echo "bind -n C-Tab select-window -n +bind -n C-BTab select-window -p +# Start Substitute - tmux_Test +set -g mouse off +# End Substitute +set -g escape-time 0 + +# Start Substitute - Other +option 1 +option 2 +option 3 +# End Substitute +option 4 +option 5 +" > /tmp/testfile +diff /tmp/testfile testoutput/subfolder/tmux_out.conf + +make clean > /dev/null +rm -rf testinput/ +rm -rf testoutput/ diff --git a/setup.sh b/setup.sh @@ -10,7 +10,7 @@ if [ "$1" = "save" ]; then basename=$(basename "$filepath") repo_dirname=$(dirname "$repo_path") mkdir -p "$repo_dirname" - if grep -q "START SUBSTITUTE" "$real_path"; then + if grep -q "Start Substitute" "$real_path"; then sub save "$basename" "$repo_dirname" "$real_path" "$repo_path" else cp "$real_path" "$repo_path" @@ -26,7 +26,7 @@ elif [ "$1" = "make" ]; then real_dirname=$(dirname "$real_path") repo_dirname=$(dirname "$repo_path") mkdir -p "$real_dirname" - if grep -q "START SUBSTITUTE" "$repo_path"; then + if grep -q "Start Substitute" "$repo_path"; then sub make "$basename" "$repo_dirname" "$repo_path" "$real_path" else echo cp "$real_path" "$repo_path" diff --git a/terminal-scripts/directory_bookmarks.c b/terminal-scripts/directory_bookmarks.c @@ -5,9 +5,9 @@ #include <unistd.h> #include <linux/limits.h> -#define BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script_data/directory_bookmarks.txt" -#define FILE_BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script_data/file_bookmarks.txt" -#define RANGER_BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script_data/ranger_directory_bookmarks.conf" +#define BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script-data/directory_bookmarks.txt" +#define FILE_BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script-data/file_bookmarks.txt" +#define RANGER_BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script-data/ranger_directory_bookmarks.conf" // This script is based on apparix: https://github.com/micans/apparix