summaryrefslogtreecommitdiff
path: root/.local/bin/shortcutgen
blob: 7f26997543859407ab4394cb059cebacd1a3c7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh

# Quit if a valid configuration source doesn't exist
[ -f "$XDG_CONFIG_HOME/shortcutrc" ] && src="$XDG_CONFIG_HOME/shortcutrc" || exit 1

# If you do not want to create a specific file, replace it's variable with /dev/null
awk -v lf_filepath="${XDG_CONFIG_HOME:-$HOME/.config}/lf/shortcuts" \
  -v gtk_filepath="${XDG_CONFIG_HOME:-$HOME/.config}/gtk-3.0/bookmarks" \
  -v zsh_filepath="${XDG_CACHE_HOME:-$HOME/.cache}/zsh-shortcuts" \
  -v env_filepath="${XDG_CACHE_HOME:-$HOME/.cache}/env-shortcuts" \
  '
BEGIN {
  FS=","
  OFS=","
}

# Function to remove whitespaces from a string
function remove_whitespace(str) {
  gsub(/[[:space:]]/, "", str)
  return str
}

# Function to expand ~ into full path (ex: /home/...)
function fullpath(str) {
  gsub(/~\//, ENVIRON["HOME"] "/", str)
  return str
}

{
  # Remove comments
  gsub(/#.*/, "")

  # Remove whitespaces from column 2
  $2 = remove_whitespace($2)

  # Remove leading and trailing whitespaces from column 3
  gsub(/^[[:space:]]+|[[:space:]]+$/, "", $3)

  # whenever a line contains only commas, replace it with an empty string
  # so that we can check if a line is empty or not
  gsub(/^,+$|^,+|,+$|,+,/, "", $0)

  # Check if line is empty or contains only whitespace
  if (NF) {

    # Convert column 2 to lowercase
    lc_col2 = tolower($2)

    # Convert column 2 to uppercase
    uc_col2 = toupper($2)

    # Write to $XDG_CONFIG_HOME/lf/shortcuts
    print "map g" lc_col2 " cd " $3 > lf_filepath

    # Write to $XDG_CACHE_HOME/zsh-shortcuts
    print "g" lc_col2 "() {cd " $3 "}" > zsh_filepath

    # Write to $XDG_CACHE_HOME/env-shortcuts
    print "export G" uc_col2 "=" $3 "" > env_filepath

    # Write to $XDG_CONFIG_HOME/gtk-3.0/bookmarks
    print "file://" fullpath($3) " " $1 > gtk_filepath
  }
}
' "$src"