You are not logged in.

#1 2020-02-12 00:38:47

damo
....moderator....
Registered: 2015-08-20
Posts: 5,633

jgmenu for bash_aliases

For those of us who have too many aliases to remember!

The script parses your .bash_aliases, and outputs each section into a submenu.
3DXbZxBt.jpg

I run it from a Tint2 executor, with the command

execp_lclick_command = parse-aliases | jgmenu --simple --config-file=/home/damo/.config/jgmenu/aliases.jgmenurc

It requires some preparation ie creating the sections in the file which group the different types of alias.
For example:

###[ls and dir aliases]#########
alias ls='ls -h --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'

alias ll='ls -al'
alias la='ls -A'
alias l='ls -CF'

####[FUNCTIONS]####
func_name(){    # function description wanted in submenu
      foo=bar
}

The number of hashes isn't too important, as long as the line contains "#[....]#", and alias commands should be surrounded with single-quotes.

Comments are ignored, unless on the same line as the function, when they are used in the submenu.

jgmenu trips up if commands contain certain escape characters, like "\n".

#!/bin/bash
##
## parse-aliases
##
## Script to parse '.bash_aliases' for output to jgmenu
## Written for Bunsenlabs
## Copyright (C) 2020 damo    <damo@bunsenlabs.org>

##  .bash_aliases needs to be in the following layout:
#       number of hashes aren't too important, as long as the line contains '#[...]#'
#       use single quotes around the command

########################################################################

#       ####[SECTION_TITLE]####
#           alias foo='bar'
#           alias foo2='bar2'
#       ####[FUNCTIONS_TITLE]####
#           func_name(){    # function description to be in submenu
#               foo=bar
#           }

########################################################################

# Run the menu with the command:    'parse-aliases | jgmenu --simple'
#
# If you want the menu formatting and theme to be independant of the default 'jgmenurc',
# create another, and add the path to the command.
# It is recommended to use a monospace font, which helps with a nice layout:
# eg "font = Monospace Regular 10"
#
#   'parse-aliases | jgmenu --simple --config-file=/path/to/aliases.jgmenurc
#
########################################################################

BL_COMMON_LIBDIR="/usr/lib/bunsen/common"   # source jgmenu helper functions

AL="$HOME/.bash_aliases"
SEP="#["                                # the line must contain '#[...]#'
MENU_TITLE="ALIASES in '.bash_aliases'"

nsec=0
declare -a title
declare -a aliases
declare -a arr_line
declare -a arr_funcs
unset section
unset str


if ! . "$BL_COMMON_LIBDIR/bl-include.cfg" 2> /dev/null; then
    echo $"Error: Failed to locate bl-include.cfg in $BL_COMMON_LIBDIR" >&2
    exit 1
fi

while read -r line;do
    [[ ! -n ${line// /} ]] && continue      # if no string then skip line
    if [[ $line == *"${SEP}"* ]];then       # get section titles, add to array
        func_flag=0
        unset str
        nsec=$((nsec+1))

        declare TEMP
        TEMP=$(mktemp --tmpdir aliases.XXX)
        aliases+=("$TEMP")                                      # tempfile for each section, add to array
        section=$(echo "$line" | awk -F '[][]' '{print $2}')    # extract section name
        title+=("${section}")
        [[ ${section} == "FUNCTIONS" ]] && func_flag=1          # section header like '####[FUNCTIONS]####'
    else    # lines between section headers
        if (( func_flag == 0 ));then                            # section header like '####[SECTION]####'
            line=$(echo "${line}" | sed -n -e '/^alias/p')      # get only lines beginning with 'alias'
            OIFS=$IFS                               # save Internal Field Separator
            if [[ -n $line ]];then                  # line not empty
                unset arr_line
                str="${line#* }"                    # remove 'alias'
                IFS='='                             # set field separator
                read -ra arr_line <<< "${str}"      # split line on '='
                
                if (( ${#arr_line[@]} > 1 ));then # check we haven't split the alias command, if it contains a '='
                    unset cmd
                    for ((i=1; i<${#arr_line[@]}; i++));do
                        cmd="$cmd=${arr_line[i]}"       # concatenate split strings, add missing '='
                        cmd="${cmd#?}"                  # remove leading '=', which we just managed to insert!
                        if [[ ${cmd} != "'"* ]];then
                            cmd="'${cmd}"               # add leading single quote back if missing
                        fi
                    done
                else
                    cmd="${arr_line[1]}"
                fi

                al="${arr_line[0]}"
                printf "%-10s %s\n" "${al}" "${cmd}" >> "$TEMP"
            fi
            IFS=$OIFS                                   # reset Internal Field Separator
        else
            if [[ $line == *'(){'* ]];then              # likely to be 'func(){' line
                unset arr_funcs
                OIFS=$IFS                               # save Internal Field Separator
                IFS='{'                                 # set field separator
                read -ra arr_funcs <<< "${line}"        # split line on '{'
                comments="${arr_funcs[1]}"              # any remaining string is comments
                comments="${comments/'#'/}"             # remove hash char
                comments=$(echo "${comments}" | sed -e 's/^[ \t]*//')   # remove leading whitespace
                printf "%-14s %s\n" "${arr_funcs[0]}" "${comments}" >> "$TEMP"
                IFS=$OIFS                               # reset Internal Field Separator
            fi
        fi
    fi
done < "${AL}"      # input .bash_aliases file

### Write out jgmenu ###
index=0
echo "^sep(${MENU_TITLE})"                 # "Title"

for item in ${!title[*]};do
    jgmenuSubmenu 'root' "${item}_alias" "${title[item]}"
    jgmenuSeparator "${item}_alias" "${title[item]}"
    jgmenuSubmenu "${item}_alias" "${title[item]}"

    while read -r line;do
        jgmenuItem "${item}_alias" "${line}"
    done < "${aliases[index]}"    
    rm "${aliases[index]}"                 # trash the tempfile
    index=$((index+1))
done

jgmenuEnd

exit

Github: jgmenu-scripts

I recommend using a monospace font to make the submenu formatting nicer. If you don't specify a csv file, jgmenu will use the default jgmenurc. Here is one for parse-aliases, with Lithium highlighting, and monospace font:

# aliases.jgmenurc
tint2_look          = 0
position_mode       = pointer
csv_cmd             = lx
menu_width          = 134
menu_margin_y       = 35
menu_padding_top    = 4
menu_padding_right  = 0
menu_padding_bottom = 4
menu_padding_left   = 0
menu_radius         = 1
sub_spacing         = 6
item_margin_x       = 1
item_margin_y       = 1
item_height         = 21
sep_height          = 4
sep_halign          = right
font                = Monospace Regular 10
icon_size           = 0
arrow_string        = ◄
arrow_width         = 8
color_menu_bg = #22373f 100
color_menu_border = #888888 100
color_norm_bg       = #C8CFCB 00
color_norm_fg = #d3dae3 100
color_sel_bg = #cc666a 100
color_sel_fg = #ffffff 100
color_sel_border = #cc666a 100
color_title_fg = #d3dae3 100
color_title_bg = #22373f 100
color_title_border  = #74998B 8
color_sep_fg = #535353 100
sep_markup = foreground="#cc666a"
color_scroll_ind = #cc666a 100

Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#2 2020-02-12 00:53:16

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,383
Website

Re: jgmenu for bash_aliases

Nice!

And a useful reference for setting up custom jgmenu menus in general. cool

I use this kind of one-off menu a lot, quite outside the regular BL root menu, triggered by launchers etc., and still haven't migrated them from openbox to jgmenu format. (They still work of course, because openbox is running.)

Last edited by johnraff (2020-02-12 00:56:14)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#3 2020-02-12 07:56:46

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,992
Website

Re: jgmenu for bash_aliases

Bookmarked.

Offline

#4 2020-02-12 17:29:02

misko_2083
Member
Registered: 2016-05-24
Posts: 299

Re: jgmenu for bash_aliases

@damo, there is a line where you use the title in the separator

...
echo "^sep(${MENU_TITLE})"                 # "Title"
...

Maybe it's better to use the text widget:

...
echo "@text,,6,6,150,20,2,left,top,auto,#000000 1,${MENU_TITLE}"  # "Title"
echo "^sep()"
...

Last edited by misko_2083 (2020-02-12 17:48:13)


Што ни оштровиди ум сагледати не може - љубав превазилази.

Offline

#5 2020-02-12 18:13:42

damo
....moderator....
Registered: 2015-08-20
Posts: 5,633

Re: jgmenu for bash_aliases

misko_2083 wrote:

@damo, there is a line where you use the title in the separator

...
echo "^sep(${MENU_TITLE})"                 # "Title"
...

Maybe it's better to use the text widget:

...
echo "@text,,6,6,150,20,2,left,top,auto,#000000 1,${MENU_TITLE}"  # "Title"
echo "^sep()"
...

Personal preference I guess. The rc file needs editing if you use the text widget.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#6 2020-02-13 09:10:34

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,383
Website

Re: jgmenu for bash_aliases

Not sure what this is doing:

damo wrote:
        declare TEMP

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#7 2020-02-13 13:49:56

damo
....moderator....
Registered: 2015-08-20
Posts: 5,633

Re: jgmenu for bash_aliases

johnraff wrote:

Not sure what this is doing:

damo wrote:
        declare TEMP

Neither do I! shellcheck didn't pick it up.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

Board footer

Powered by FluxBB