Triggering code after the window has been shown - rust

I'm using gtk-rs. I want to trigger some code after the window has been shown (has displayed on the screen):
window.connect_show(clone!(#weak window => move |_| {
let command = format!("sleep 0.1; wmctrl -r \"CSS\" -e 1,640,100,680,768");
println!("2");
run_command(&command);
}));
println!("1");
window.show();
println!("3");
This will print: 1, 2, 3. Meaning that connect_show is triggering right before window.show();
This won't let the command wmctrl -r \"CSS\" -e 1,640,100,680,768" reposition and resize the window. A delay is needed to accomplish that:
"sleep 0.1; wmctrl -r \"CSS\" -e 1,640,100,680,768"
Is there another way to make the command work without having to use sleep 0.1?
Here is the whole code.

Related

How to get Windowid from PID from a electron Application

I want to get the WindowID from the PID of an Electron Process (for example riot-desktop)
I tried to get it with xdotools like this:
$ xdotool search --pid $(pgrep riot)
nothing is printed
$ pgrep riot
30461
$ xdotool search --pid 30461
nothing is printed again
You have to search for the pid of the subprocess of electron.
You can get the pid of the subprocess with:
PID=$(ps h -C electron | grep riot | cut -f1 -d"?")
now you can search for the pid
xdotool search --pid $PID
you can combine both commands to a single command
xdotool search --pid $(ps h -C electron | grep riot | grep witzerstorfer | cut -f1 -d"?")
if your ps returns multiple pid's you have to add more grep commands, for example if you work with profiles the command could look like this:
PID=$(ps h -C electron | grep riot | grep $PROFILENAME | cut -f1 -d"?")
If you want to find windowID of your electron application then you can use
win.getMediaSourceId()
Returns String - Window id in the format of DesktopCapturerSource's id. For example "window:1324:0".
More precisely the format is window:id:other_id where id is HWND on Windows, CGWindowID (uint64_t) on macOS and Window (unsigned long) on Linux. other_id is used to identify web contents (tabs) so within the same top level window.
Example:
// In the main process.
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ width: 800, height: 600 })
// Load a remote URL
win.loadURL('https://github.com')
// Or load a local HTML file
win.loadURL(`file://${__dirname}/app/index.html`)
// Print window id
console.log('window id is', win.getMediaSourceId())

ZSH - wrap all commands with a function

I'd like to print the PID of any process I run in my command at the beginning (even if it's not a background process).
I came up with this:
fn() {
eval $1
}
Now whenever I run a command, I want it to be processed as
fn "actual_command_goes_here"&; wait
So that this will trigger a background process (which will print the PID) and then run the actual command. Example:
$ fn "sleep 5; date +%s; sleep 5; date +%s; sleep 2"&; wait
[1] 29901
1479143885
1479143890
[1] + 29901 done fn "..."
My question is: is there any way to create a wrapper function for all commands in Bash/ZSH? That is, when ever I run ls, it should actually run fn "ls"&; wait.

tmux: how to open file under cursor

i am a vim user and got used to the gf command, which opens the file under the cursor.
Now i wanted to ask, if there is something like that for tmux.
I can navigate through a tmux-pane and it happens often that there is a file-path under the cursor. Now i like to have the possibility to open that file under the cursor with vim.
A: in the current window
B: in another window which includes and opened vim
Maybe there is a possibility to run a sh-script in that navigation-mode when invoking a special key-combination? that would make it possible to write my own scripts like i got used to in vim with vimscript.
I am already using some vi-copy modes in mux .tmux.conf
# VIM
# ===================================================================
# Vimlike copy mode.
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
# Enable vi keys.
setw -g mode-keys vi
# https://coderwall.com/p/4b0d0a/how-to-copy-and-paste-with-tmux-on-ubuntu
bind -t vi-copy y copy-pipe "xclip -sel clip -i"
I've been searching for an answer for years and ended up making a tmux plugin: https://github.com/artemave/tmux_super_fingers. It still baffles me how such in integral part of terminal based workflow is not a solved problem (well, it is now).
To achieve what you want, you need to use the stdin in your command line (xargs can do that) and tell tmux, in a new-window, to open the data with the arguments from the copy buffer:
bind -t vi-copy y copy-pipe "xargs -I{} tmux new-window 'vim {}'"
This needs more tuning (getting the right session, the right command, use $EDITOR instead of vim etc.
It is quite dangerous: Think copying /foo/bar/my;rm -rf /.
Also, as-is, this will only work for paths relative to tmux' working directory.
There's a mod for tmux allowing to bind an action of any complexity in 'mode': http://ershov.github.io/tmux/
There's an example of how to mark the word under cursor using that patch:
proc is_word_char {c} {
print [scan $c %c]
return [expr {$c > " " && $c != "\x7f"}]
}
proc mark-current-word {} {
clear-selection
set l [copy-mode-screenline]
set x [copy-mode-get-cx]
if {![is_word_char [string range $l $x $x]]} return
incr x
while {[is_word_char [string range $l $x $x]]} {
cursor-right
incr x
}
incr x -2
begin-selection
while {[is_word_char [string range $l $x $x]]} {
cursor-left
if {$x < 1} return
incr x -1
}
}
# Open selection in a vim mini-window (no shell and files)
bind-key -t vi-copy y tcl {
split-window -c [f #{pane_current_path}] -l 5 "
echo -n [shell-quote [copy-mode-selection]] | vim -R -"
}
Hence, to open the current file in vim:
mark-current-word
split-window -c [f #{pane_current_path}] -l 5 "vim -R [shell-quote [copy-mode-selection]]"
So i got it running with the following binding:
bind -t vi-copy y copy-pipe "xargs -I{} tmux send-keys -t 1 ';edit {}' Enter && tmux select-pane -t 1"
notes
i changed vim command : to ;
i have a open vim in pane 1

Simulating a spinner for progress in Bash

I am currently learning how to make scripts a bit more verbose. The below code shows a spinner. However, I am having difficulties modifying this spinner to have the words such as 'Downloading'. I want both the words and spinner to appear beside each other. I am not asking how to implement spinner for progress but how to concatenate with words. How could achieve this goal?
sp='/-\|'
sc=0
spin() {
printf "\b${sp:sc++:1}"
((sc==${#sp})) && sc=0
}
endspin() {
printf "\r%s\n" "$#"
}
until work_done; do
spin
some_work ...
done
endspin
You can do so like this
sp='/-\|'
sc=0
spin() {
printf "\r${sp:sc++:1} $1"
((sc==${#sp})) && sc=0
}
endspin() {
printf "\r%s\n" "$#"
}
work_done() {
false
}
some_work() {
sleep 1
}
until work_done; do
spin "Downloading"
some_work ...
done
endspin
While I admire the DIY spirit of Ed and Jakuje I also like to reuse other folks code. If you'd rather recycle than recreate consider Louis Marascio's spinner. I put his spinner() function into my shell library and it is easy to use:
#!/bin/bash
. lib.sh
run_10s &
echo -n wait
spinner $!
echo -e "\rdone"
displays
$ ./test_lib
wait [/]
for 10 seconds with the spinner spinning and then it clears that line left containing wait with the \r and you are left with just
$ ./test_lib
done
on the screen.

Bash: Split stdout from multiple concurrent commands into columns

I am running multiple commands in a bash script using single ampersands like so:
commandA & commandB & commandC
They each have their own stdout output but they are all mixed together and flood the console in an incoherent mess.
I'm wondering if there is an easy way to pipe their outputs into their own columns... using the column command or something similar. ie. something like:
commandA | column -1 & commandB | column -2 & commandC | column -3
New to this kind of thing, but from initial digging it seems something like pr might be the ticket? or the column command...?
Regrettably answering my own question.
None of the supplied solutions were exactly what I was looking for. So I developed my own command line utility: multiview. Maybe others will benefit?
It works by piping processes' stdout/stderr to a command interface and then by launching a "viewer" to see their outputs in columns:
fooProcess | multiview -s & \
barProcess | multiview -s & \
bazProcess | multiview -s & \
multiview
This will display a neatly organized column view of their outputs. You can name each process as well by adding a string after the -s flag:
fooProcess | multiview -s "foo" & \
barProcess | multiview -s "bar" & \
bazProcess | multiview -s "baz" & \
multiview
There are a few other options, but thats the gist of it.
Hope this helps!
pr is a solution, but not a perfect one. Consider this, which uses process substitution (<(command) syntax):
pr -m -t <(while true; do echo 12; sleep 1; done) \
<(while true; do echo 34; sleep 2; done)
This produces a marching column of the following:
12 34
12 34
12 34
12 34
Though this trivially provides the output you want, the columns do not advance individually—they advance together when all files have provided the same output. This is tricky, because in theory the first column should produce twice as much output as the second one.
You may want to investigate invoking tmux or screen in a tiled mode to allow the columns to scroll separately. A terminal multiplexer will provide the necessary machinery to buffer output and scroll it independently, which is important when showing output side-by-side without allowing excessive output from commandB to scroll commandA and commandC off-screen. Remember that scrolling each column separately will require a lot of screen redrawing, and the only way to avoid screen redraws is to have all three columns produce output simultaneously.
As a last-ditch solution, consider piping each output to a command that indents each column by a different number of characters:
this is something that commandA outputs and is
and here is something that commandB outputs
interleaved with the other output, but visually
you might have an easier time distinguishing one
here is something that commandC outputs
which is also interleaved with the others
from the other
Script print out three vertical rows and a timer each row containing the output from a single script.
Comment on anything you dont understand and ill add answers to my answer as needed
Hope this helps :)
#!/bin/bash
#Script by jidder
count=0
Elapsed=0
control_c()
{
tput rmcup
rm tail.tmp
rm tail2.tmp
rm tail3.tmp
stty sane
}
Draw()
{
tput clear
echo "SCRIPT 1 Elapsed time =$Elapsed seconds"
echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
tail -n10 tail.tmp
tput cup 25 0
echo "Script 2 "
echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
tail -n10 tail2.tmp
tput cup 50 0
echo "Script 3 "
echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
tail -n10 tail3.tmp
}
Timer()
{
if [[ $count -eq 10 ]]; then
Draw
((Elapsed = Elapsed + 1))
count=0
fi
}
main()
{
stty -icanon time 0 min 0
tput smcup
Draw
count=0
keypress=''
MYSCRIPT1.sh > tail.tmp &
MYSCRIPT2.sh > tail2.tmp &
MYSCRIPT3.sh > tail3.tmp &
while [ "$keypress" != "q" ]; do
sleep 0.1
read keypress
(( count = count + 2 ))
Timer
done
stty sane
tput rmcup
rm tail.tmp
rm tail2.tmp
rm tail3.tmp
echo "Thanks for using this script."
exit 0
}
main
trap control_c SIGINT

Resources