Compare commits

...

8 Commits

Author SHA1 Message Date
Emre AKYÜZ
aac4982c7b
Merge 4854a853e44fc949d68410ed221626a7ceb7cb9a into 07952026753f1bbeaf5b6c4eb5414a9c51536041 2023-11-15 21:49:43 +01:00
Luciano
0795202675
lf-scope: adds SVG preview creating cache. (#1360) 2023-11-15 01:37:39 +00:00
appeasementPolitik
bbcbac64fa
Fix unsupported characters in vim-airline (#1317) 2023-11-15 01:35:53 +00:00
thirtysix
52fab9d50a
Make shell profile POSIX, remove unnecessary variable fallbacks (#1368) 2023-11-15 01:35:14 +00:00
thirtysix
0db54f9618
Fix pdf and plain text previws in lf (#1369)
* Fix pdf and plain text previws in lf

Pdf previws don't appear because are generated with wrong filename.
Plain text previws with `bat` don't respect terminal theme and have
annoying border.

* Revert pdf cache filename to work properly
2023-11-15 01:34:29 +00:00
Emre AKYÜZ
4854a853e4
simplify | improve | change jq to sed 2023-10-17 11:32:19 +03:00
Emre AKYÜZ
d3035edc7e
Simplify Further | Remove if statements
1- Used logical operators instead of if statements.
2- Improved indentations and blocks for better readibility.
3- Simplified in general.
2023-09-09 16:28:31 +03:00
Emre AKYÜZ
943220f884
Extremely Improved Bookmarks (Search + Strip URL + More)
The inspiration: (Luke Smith's Video): "Bookmarking for Unix Chads"
Instructions & Justifications & Very Detailed Explanation for Everything can be found below.

Short Summary
- Allows users to manage and navigate bookmarks using dmenu. 
- The user can perform actions such as adding, deleting, and editing bookmarks. The script also handles searching within specific websites if the bookmarked URL has the word "search" in it. Such as the SearxNG Instance: "paulgo.io/search?q=".
- One more interesting feature of it is that it modifies the order of bookmarks by their popularity (how frequently visited by you). 
- It can work with Dash and it has no bashisms.

Execution Time: Instant
Required Programs: dash (or bash) | jq | echo | grep | dmenu | notification daemon | browser | xclip (or "wl-paste" on Wayland)

Instructions:
These directories should exist: ~/.local/share/larbs/ 
1. pacman -s jq (it's a very small program (I.E. 690kb)
2. Open a browser and highlight (xclip -o users) or copy (Wayland users) a website's URL (starting with "http") such as "https://github.com" 
2. Run the script with a shortcut you created for the window manager that is used.
3. Write "@@" inside the terminal then enter (@@ can be changed of course). This opens the "Action Menu".
4. Select "Add a New Bookmark" option then enter.
5. The bookmark will be added inside the bookmarks menu. It can be edited or deleted later from the action menu that can be opened with "@@".
6. If a website that has the word "search" in it is added inside bookmarks such as a SearxNG instance "paulgo.io/search?q=" , its search function can be used. When selected, it will offer for a new prompt for the desired keywords.

Justification:

1. The script uses jq for parsing and modifying JSON data, which is a lightweight and powerful command-line JSON processor. This choice allows for easy manipulation of the JSON file that stores bookmarks.

2. The use of the while loop with the dmenu interface ensures that the user can perform multiple actions (adding, deleting, or editing bookmarks) in a single session without needing to restart the script.

3. The script checks for an existing URLQUERY_FILE and initializes it with an empty JSON array "[]" if it doesn't exist or is empty. This ensures that the file is always in a valid state for the script to operate.

4. The script sorts bookmarks by popularity, allowing users to quickly access their most frequently visited bookmarks. This is achieved by incrementing a popularity value each time a bookmark is opened.

5. The script supports searching within specific websites by checking if the word "search" is in the URL (More words can be added). This feature provides a convenient way to search within bookmarked websites directly from the script.

6. The use of the dash shell (#!/bin/sh) as the interpreter provides a lightweight and fast shell environment for the script, improving performance. Since the script is efficient enough so this performance difference is not huge, so it can also be used with bash.

Detailed Explanation

1. URLQUERY_FILE="~/.local/share/larbs/urlquery": Sets the path for the JSON file that will store the bookmarks.

2. The following block checks if the URLQUERY_FILE exists and if it's empty. If either condition is met, it initializes the file with an empty JSON array "[]". If there is no JSON array present, then no bookmark can be added.

`if [ ! -f "$URLQUERY_FILE" ] || [ ! -s "$URLQUERY_FILE" ]; then
  echo "[]" > "$URLQUERY_FILE"
fi`

3. ACTION_MENU='@@': Sets a special string for the action menu.

4. The read_bookmarks function reads the contents of the URLQUERY_FILE using jq. The write_bookmarks function writes the contents passed as an argument to a temporary file and then moves it to the URLQUERY_FILE. The update_bookmark_popularity function updates the popularity value of a bookmark, given its name.

5. The contains_search function checks if a given string contains the word "search". It returns 0 if it does, 1 otherwise.

6. bookmarks=$(read_bookmarks): Reads the bookmarks from the file and stores them in a variable.

7. touch "$URLQUERY_FILE": Ensures the URLQUERY_FILE exists by creating it if it doesn't.

8. The following while loop presents a menu to the user for choosing an action (Add, Delete, or Edit bookmark) or a bookmark to open. It continues until the user selects a bookmark to open or provides an empty input.

`while true; do
  SELECTION=$(echo "$bookmarks" | jq -r '. | sort_by(.[2]) | reverse | .[] | .[0]' | rofi -dmenu -p "Bookmark")

  if [ -z "$SELECTION" ] || [ "$SELECTION" != "$ACTION_MENU" ]; then
    write_bookmarks "$bookmarks"
    break
  fi

  ACTION=$(echo "Add a New Bookmark\nDelete a Bookmark\nEdit a Bookmark" | rofi -dmenu -p "Action")
  #...
done`

9. The case block inside the while loop performs the selected action (Add, Delete, or Edit) based on the user's input.

10. After the loop, the script checks if a valid bookmark is selected. If so, it updates the popularity value, writes the changes to the file, and opens the bookmark in Firefox or any browser. If the bookmark contains the word "search", it prompts the user for a search query before opening the URL in Firefox.

11. Creating and moving temporary files are for data integrity and safety. It ensures 0 file corruptions. Has no negative effect in terms of performance.
2023-04-24 12:43:35 +03:00
4 changed files with 137 additions and 19 deletions

View File

@ -13,10 +13,6 @@ image() {
fi
}
ifub() {
[ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1
}
# Note that the cache file name is a function of file information, meaning if
# an image appears in multiple places across the machine, it will not have to
# be regenerated once seen.
@ -29,10 +25,15 @@ case "$(file --dereference --brief --mime-type -- "$1")" in
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"
[ ! -f "$CACHE" ] && djvused "$1" -e 'select 1; save-page-with /dev/stdout' | convert -density 200 - "$CACHE.jpg" > /dev/null 2>&1
image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1" ;;
image/svg+xml)
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"
[ ! -f "$CACHE" ] && inkscape --convert-dpi-method=none -o "$CACHE.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$1"
image "$CACHE.png" "$2" "$3" "$4" "$5" "$1"
;;
image/*) image "$1" "$2" "$3" "$4" "$5" "$1" ;;
text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;;
text/troff) man ./ "$1" | col -b ;;
text/* | */xml | application/json | application/x-ndjson) bat --terminal-width "$(($4-2))" -f "$1" ;;
text/* | */xml | application/json | application/x-ndjson) bat -p --theme ansi --terminal-width "$(($4-2))" -f "$1" ;;
audio/* | application/octet-stream) mediainfo "$1" || exit 1 ;;
video/* )
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"

View File

@ -67,6 +67,14 @@ set noshowcmd
nm <leader>i :call ToggleIPA()<CR>
imap <leader>i <esc>:call ToggleIPA()<CR>a
nm <leader>q :call ToggleProse()<CR>
" vim-airline
if !exists('g:airline_symbols')
let g:airline_symbols = {}
endif
let g:airline_symbols.colnr = ' C:'
let g:airline_symbols.linenr = ' L:'
let g:airline_symbols.maxlinenr = '☰ '
" Shortcutting split navigation, saving a keypress:
map <C-h> <C-w>h

View File

@ -1,14 +1,12 @@
#!/bin/zsh
#!/bin/sh
# shellcheck disable=SC2155
# profile file. Runs on login. Environmental variables are set here.
# Profile file, runs on login. Environmental variables are set here.
# If you don't plan on reverting to bash, you can remove the link in ~/.profile
# to clean up.
# Add all directories in `~/.local/bin` to $PATH
export PATH="$PATH:$(find ~/.local/bin -type d | paste -sd ':' -)"
# Adds `~/.local/bin` to $PATH
export PATH="$PATH:${$(find ~/.local/bin -type d -printf %p:)%%:}"
unsetopt PROMPT_SP
unsetopt PROMPT_SP 2>/dev/null
# Default programs:
export EDITOR="nvim"
@ -52,7 +50,7 @@ export SQLITE_HISTORY="$XDG_DATA_HOME/sqlite_history"
export DICS="/usr/share/stardict/dic/"
export SUDO_ASKPASS="$HOME/.local/bin/dmenupass"
export FZF_DEFAULT_OPTS="--layout=reverse --height 40%"
export LESS=-R
export LESS="R"
export LESS_TERMCAP_mb="$(printf '%b' '')"
export LESS_TERMCAP_md="$(printf '%b' '')"
export LESS_TERMCAP_me="$(printf '%b' '')"
@ -61,15 +59,15 @@ export LESS_TERMCAP_se="$(printf '%b' '')"
export LESS_TERMCAP_us="$(printf '%b' '')"
export LESS_TERMCAP_ue="$(printf '%b' '')"
export LESSOPEN="| /usr/bin/highlight -O ansi %s 2>/dev/null"
export QT_QPA_PLATFORMTHEME="gtk2" # Have QT use gtk2 theme.
export MOZ_USE_XINPUT2="1" # Mozilla smooth scrolling/touchpads.
export QT_QPA_PLATFORMTHEME="gtk2" # Have QT use gtk2 theme.
export MOZ_USE_XINPUT2=1 # Mozilla smooth scrolling/touchpads.
export AWT_TOOLKIT="MToolkit wmname LG3D" # May have to install wmname
export _JAVA_AWT_WM_NONREPARENTING=1 # Fix for Java applications in dwm
export _JAVA_AWT_WM_NONREPARENTING=1 # Fix for Java applications in dwm
[ ! -f ${XDG_CONFIG_HOME:-$HOME/.config}/shell/shortcutrc ] && setsid shortcuts >/dev/null 2>&1
[ ! -f "$XDG_CONFIG_HOME/shell/shortcutrc" ] && setsid -f shortcuts >/dev/null 2>&1
# Start graphical server on user's current tty if not already running.
[ "$(tty)" = "/dev/tty1" ] && ! pidof -s Xorg >/dev/null 2>&1 && exec startx "$XINITRC"
# Switch escape and caps if tty and no passwd required:
sudo -n loadkeys ${XDG_DATA_HOME:-$HOME/.local/share}/larbs/ttymaps.kmap 2>/dev/null
sudo -n loadkeys "$XDG_DATA_HOME/larbs/ttymaps.kmap" 2>/dev/null

111
.local/bin/chadmarked Normal file
View File

@ -0,0 +1,111 @@
#!/bin/dash
URLQUERY_FILE="$HOME/.local/share/urlquery"
CLIPBOARD="xclip -o"
ACTION_MENU='@@'
DMENU() {
dmenu -i -l $1 -p "$2"
}
error_notify() {
notify-send "$1"
exit 1
}
ensure_file_exists() {
[ -f "$URLQUERY_FILE" ] || {
notify-send "$URLQUERY_FILE does not exist. Creating it now."
touch "$URLQUERY_FILE"
}
}
get_selection() {
cut -d= -f1 "$URLQUERY_FILE" | DMENU $LINE_COUNT "Bookmarks"
}
update_file() {
pattern="$1"
replacement="$2"
sed "/$pattern/c\\
$replacement" "$URLQUERY_FILE" > "$URLQUERY_FILE.tmp" && mv "$URLQUERY_FILE.tmp" "$URLQUERY_FILE" ||
error_notify "Failed to update the file."
}
is_valid_url() {
echo "$1" | grep -qE "^https?://[^[:space:]/?#][^[:space:]]+$"
}
add_bookmark() {
URL=$($CLIPBOARD)
is_valid_url "$URL" || error_notify "The clipboard content is not a valid URL."
grep -q "=$URL$" "$URLQUERY_FILE" && notify-send "The URL is already in the list." && return
NAME=$(DMENU 0 "Name")
[ -n "$NAME" ] && echo "${NAME}=${URL}" >> "$URLQUERY_FILE" && notify-send "'$NAME' is bookmarked."
}
delete_bookmark() {
NAME=$(get_selection)
[ -z "$NAME" ] && error_notify "Failed to delete the bookmark."
sed "/^$NAME=/d" "$URLQUERY_FILE" > "$URLQUERY_FILE.tmp"
mv "$URLQUERY_FILE.tmp" "$URLQUERY_FILE"
[ ! -s "$URLQUERY_FILE" ] || ! grep -qE "\S" "$URLQUERY_FILE" && rm "$URLQUERY_FILE"
notify-send "'$NAME' is deleted."
}
edit_name() {
OLD_NAME="$1"
NEW_NAME=$(DMENU 0 "New Name")
[ -z "$NEW_NAME" ] && return
URL=$(grep "^$OLD_NAME=" "$URLQUERY_FILE" | cut -d= -f2)
update_file "^$OLD_NAME=" "$NEW_NAME=$URL"
}
edit_url() {
NAME="$1"
NEW_URL=$(DMENU 0 "New URL")
[ -z "$NEW_URL" ] && return
update_file "^$NAME=.*" "$NAME=$NEW_URL"
}
edit_bookmark() {
NAME=$(get_selection)
[ -z "$NAME" ] && error_notify "Failed to edit the bookmark."
FIELD=$(echo "Name\nURL" | DMENU 2 "Edit")
case "$FIELD" in
"Name") edit_name "$NAME" ;;
"URL") edit_url "$NAME" ;;
esac
notify-send "'$NAME' is updated."
}
open_bookmark() {
URL=$(grep "^$SELECTION=" "$URLQUERY_FILE" | cut -d= -f2-)
case "$URL" in
*"search"*|*"wiki"*)
QUERY=$(DMENU 0 "Search")
URL="${URL}${QUERY}"
;;
esac
$BROWSER "$URL" || notify-send "Failed to open the URL."
}
ensure_file_exists
LINE_COUNT=$(wc -l < "$URLQUERY_FILE")
SELECTION=$(get_selection)
[ -z "$SELECTION" ] && exit
case "$SELECTION" in
"$ACTION_MENU")
ACTION=$(echo "Add\nDelete\nEdit" | DMENU 3 "Action")
case "$ACTION" in
"Add") add_bookmark ;;
"Delete") delete_bookmark ;;
"Edit") edit_bookmark ;;
esac
;;
*)
open_bookmark
;;
esac