You are not logged in.

#426 2020-11-03 19:16:02

Ogashi
Member
Registered: 2017-03-05
Posts: 5

Re: Dmenu Hacking Thread

qinohe wrote:

You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine

Exactly, a little bit overkilling and now I am frustrated with the script I made and wanted to make it work.

Trilbi wrote:

Ogashi, the problem is a trailing newline(s) at the end of path2file.  You'll need to strip that or rethink your script.

I also thought of that, tried using printf to get rid of the new line, now I also tried

path2file=$(awk ' /\47[0-9]/  {print $4} ' ~/.viminfo | dmenu -p 'Edit recent file:' | tr -d '\n')

But I keep having the same behaviour, it is driving me nuts.

qinohe wrote:
eval alacritty -e vim -o "$path2file"

This actually works. I read the manual you cite, but it does not mention the dangerous stuff. I have looked it up a bit and I see why is not a very good idea to use it, although in this case the operands concatenated should be fine.

I fail to see why it does not work without eval, maybe it means that the variable holding the path is not passed to vim correctly?

Offline

#427 2020-11-03 19:33:16

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

Ogashi wrote:
qinohe wrote:

You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine

Exactly, a little bit overkilling and now I am frustrated with the script I made and wanted to make it work.

I know, but like I said, you use it in an already opened vim instance with a keybind, you could use it for more things, like search, find and open in a vim tab.
It  may be a biy overkill, but ,I like using it this way;)

eval alacritty -e vim -o "$path2file"

This actually works. I read the manual you cite, but it does not mention the dangerous stuff. I have looked it up a bit and I see why is not a very good idea to use it, although in this case the operands concatenated should be fine.

I fail to see why it does not work without eval, maybe it means that the variable holding the path is not passed to vim correctly?

Yes, the variable doesn't get passed correctly. About 'eval' mostly referred to as 'evil' can do nasty things when the variable would be a command, it would simply execute it without you being able to prevent that. In this case however you're variable is a path which don't pose a threat. It would be good to avoid 'eval' as much as possible, better read this wooledge wiki https://mywiki.wooledge.org/BashFAQ/048 it explains it much better than me.

edit: Btw. you shouldn't use the command as is, because if you don't open a file and simply escape dmenu, it would still try to open a 'no name' file, check if there is a file otherwise exit

[ -n "$path2file" ] && alacritty -e vim -o "$path2file" || exit

Last edited by qinohe (2020-11-03 19:57:26)

Offline

#428 2020-11-03 22:20:00

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

@Ogashi, Let me explain a little more why the plugin way could be a good idea.
Mostly you simply open the file you want, but, if you already have an instance running, using the plugin in combination with dmenu works very comfortable.
Lets say we have your script and mine for searching files and opening them in a tab, you would only need one plugin(with two functions) and two dmenu scripts. which is in fact not so bloated anymore, as you could add more functions and separate dmenu scripts to your liking. I showed in post#423 what you would need to make it working for your script, but here it is for both;) You would also not have to call alacritty and eval anymore unless you choose that method still...

Two keybinds in your vimrc or exrc

map <c-t> :call DmenuVimFind("tabe")<cr>
map <c-f> :call DmenuVimEdit("tabe")<cr>

One plugin with the two functions(DmenuVimEdit & DmenuVimFind) , put it in ~/.vim/plugin/dedit.vim

" Vim Dmenu edit plugin
" Original Author:      qinohe
" Maintainer:           qinohe
" Last Change:          2020 Nov 02
" License:              Public

" Dmenu edit
" Remove newline from string
function! Chomp(str)
  return substitute(a:str, '\n$', '', '')
endfunction

" Edit file from vim history(viminfo) & rm temp file
function! DmenuVimEdit(cmd)
  let dmena = system("./.config/dmenu/d-script/dmenu_vim_edit")
  if exists("dmena")
    let dmenb = Chomp(system("cat /tmp/vde"))
  endif

  if filereadable("/tmp/vde")
    execute a:cmd . " " . dmenb
  endif
  let dmenc = system('rm -rf /tmp/vde')
endfunction

" Find file and open it in vim tab & rm temp file
function! DmenuVimFind(cmd)
  let dmena = system("./.config/dmenu/d-script/dmenu_vim_find")
  if exists("dmena")
    let dmenb = Chomp(system("cat /tmp/vde"))
  endif

  if filereadable("/tmp/vde")
    execute a:cmd . " " . dmenb
  endif
  let dmenc = system('rm -rf /tmp/vde')
endfunction
" End Dmenu Edit

And last two dmenu scripts, put them in ~/.config/dmenu/d-script or change the path in the plugin
first; dmenu_vim_edit

#!/bin/sh
# shellcheck disable=1090

[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
 . "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'

# Find recently edited file in viminfo
vde=$( vde 2> /dev/null ) || vde=/tmp/vde

input="$( awk '/\47[0-9]/  {print $4}' ~/.viminfo | $dmnews -p "Edit recent file:" )"

[ -n "$input" ] && echo "$input" > "$vde"

exit 0

second; dmenu_vim_find

#!/bin/sh
# shellcheck disable=1090

[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
 . "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'

vde=$( vde 2> /dev/null ) || vde=/tmp/vde

input="$( xsel -o | $dmnews -p "file search:" )"

[ "$input" != '' ] && result="$( locate -e -r "$input" | $dmnews  -p "search result:" )"

[ -n "$result" ] && echo "$result" > "$vde"

exit 0

Btw. I would also use a dmenurc which holds colors for dmenu, uses vertical instead of horiz. lists etc.
I put this in ~/.config/dmenu/dmenurc, an example;

# standardize dmenu
# RAL 9004 0n RAL 9003 & choice RAL 9007 or reversed.
dmnews='dmenu -i -l 40 -fn DejaVuSansMono-11 -nb #2e3032 -nf #f4f8f4 -sb #2e3032 -sf #8f8f8c'

Offline

#429 2020-12-10 20:48:50

jmarc
Member
Registered: 2018-09-13
Posts: 9

Re: Dmenu Hacking Thread

Hello all ,
i'am total noob in bash and dmenu scripting,
i don't if my has already been answered !

i have a file mylinks.txt like this :

blah1 blah1 blah1 @ http://ww.somesite1.com
blah2 blah2 blah2 @ http://ww.somesite2.com
blah3 blah3 blah3 @ http://ww.somesite3.com

How to display in dmenu the text blah.... and when selected output or go to url ?

cat mylinks.txt | cut -d'@' -f1 | dmenu

I'am stuck i really don't how to link the text blah to his url ?

Sorry for noobiness !

Offline

#430 2020-12-10 21:34:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Dmenu Hacking Thread

Assuming there are not slashes in the "blah blah section":

sed -n 's/'"$(sed 's/ @.*//' links.txt | dmenu)"' @ //p' links.txt

Although if you can change the format of links you can open up many other options.  For example, if you are using bash, layout the list of links as follows:

[blah1 blah1 blah1]=http://ww.somesite1.com
[blah2 blah2 blah2]=http://ww.somesite2.com
[blah3 blah3 blah3]=http://ww.somesite3.com

Then you can use this:

#!/bin/bash

declare -A links
eval links=($(cat ./links.txt))
echo ${links[$(printf '%s\n' "${!links[@]}" | dmenu)]}

Or if you were to use python, you might keep the links in a json format.

Last edited by Trilby (2020-12-10 21:42:30)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#431 2020-12-10 23:04:05

jmarc
Member
Registered: 2018-09-13
Posts: 9

Re: Dmenu Hacking Thread

Thanks you so much Trilby , you save my day.

Offline

#432 2020-12-10 23:09:35

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

Or, which at least takes the 'evil' away wink and uses bashes buitin 'readarray'

#!/bin/bash

readarray -t link < links
printf "%s\\n"  "${link[@]}" | dmenu

Offline

#433 2020-12-11 00:27:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Dmenu Hacking Thread

qinohe, that does not address the question at all.  Readarray cannot read associative arrays as far as I know - certainly not with that invocation, then your call to dmenu just returns the input rather than gives the url which is the same thing jmarc already had.

Last edited by Trilby (2020-12-11 00:37:39)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#434 2020-12-11 00:35:12

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Dmenu Hacking Thread

Yep, you're right, sorry for the noise...

Offline

Board footer

Powered by FluxBB