Thursday, April 25, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 756  / 2 Years ago, thu, june 30, 2022, 12:32:18

As many of you know, whenever you open a new terminal window on Ubuntu it gets located in the upper left part of the screen. The next one is located right below it. The third one is in the upper right, the fourth one in the bottom right part of the screen. I want to add that functionality to another Linux distribution which I'm using now. I just want to know how this is handled in Ubuntu.


Also that doesn't only work with gnome-terminal. It works with any terminal emulator that I've downloaded so far. So I highly think it's not an issue specific to the terminal emulator. Does anyone know how this is handled?


More From » command-line

 Answers
7

Thank you Nate. I used the idea you gave me and came up with that script that solves my problem. It's a python script that's specifically designed for my resolution which is 1920x1080. It also depends on a tool called wmctrl. Here is the script:


#!/usr/bin/python3

import os


def eliminate(iter):
res = []
for i in iter:
if i:
res.append(i)
return res


workspace = "wmctrl -d | grep '*' | cut -d ' ' -f1 > /tmp/curr_ws"

os.system(workspace)
out_workspace = ""
with open("/tmp/curr_ws","r") as f:
out_workspace = f.read()
out_workspace = int(out_workspace)
print("DEBUG: Current workspace:", out_workspace)

positions = 'wmctrl -lG | grep "kali:" | grep "^........... {0}" | tr -s " " | cut -d " " -f 3,4 > /tmp/positions'.format(out_workspace)
sizes = 'wmctrl -lG | grep "kali:" | grep "^........... {0}" | tr -s " " | cut -d " " -f 5,6 > /tmp/sizes'.format(out_workspace)

os.system(positions)
out_positions = ""
with open("/tmp/positions","r") as f:
out_positions = f.read()
out_positions = eliminate(out_positions.split("
"))

os.system(sizes)
out_sizes = ""
with open("/tmp/sizes","r") as f:
out_sizes = f.read()
out_sizes = eliminate(out_sizes.split("
"))

terminal_positions = [[110, 101, 854, 479], [973, 101, 854, 479], [110, 611, 854, 479], [973, 611, 854, 479]]
terminal_strings = ["gnome-terminal --geometry 105x25+100+45", "gnome-terminal --geometry 105x25+963+45",
"gnome-terminal --geometry 105x25+100+555", "gnome-terminal --geometry 105x25+963+555"]

parsed_positions = []
for i in range(0,len(out_positions)):
parsed_positions.append([ int(out_positions[i].split(" ")[0]),int(out_positions[i].split(" ")[1]),int(out_sizes[i].split(" ")[0]),int(out_sizes[i].split(" ")[1]) ])
print("DEBUG: Positions:", parsed_positions)

def check(term_pos, parsed_pos):
index = 0
overlap = False
for i in terminal_positions:
overlap = False
for j in parsed_positions:
ax0 = term_left_start = i[0]
ay0 = term_up_start = i[1]
ax1 = term_left_end = i[0] + i[2]
ay1 = term_up_end = i[1] + i[3]

bx0 = pars_left_start = j[0]
by0 = pars_up_start = j[1]
bx1 = pars_left_end = j[0] + j[2]
by1 = pars_up_end = j[1] + j[3]

# if term_left_start >= pars_left_end or term_left_end <= pars_left_start or term_up_end <= pars_up_start or term_up_start >= pars_up_end:
# overlap = True

if ((bx0 <= ax0) and (ax1 <= bx1)) or ((ax0 <= bx0 <= ax1) and (ax1 <= bx1)) or ((ax0 >= bx0) and (ax1 >= bx1 >= ax0)) or ((ax0 <= bx0 <= ax1) and (ax1 >= bx1 >= ax0)):
if ((by0 <= ay0) and (ay1 <= by1)) or ((ay0 <= by0 <= ay1) and (ay1 <= by1)) or ((ay0 >= by0) and (ay1 >= by1 >= ay0)) or ((ay0 <= by0 <= ay1) and (ay1 >= by1 >= ay0)):
overlap = True

if overlap:
index += 1
else:
return index


return -1


place = check(terminal_positions, parsed_positions)

if place == -1:
os.system("gnome-terminal")
else:
os.system(terminal_strings[place])

You have to include the location of the script in your PATH variable then assign a shortcut to a command that executes the script


[#1288] Friday, July 1, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
termetalli

Total Points: 326
Total Questions: 127
Total Answers: 110

Location: Sao Tome and Principe
Member since Sat, Sep 12, 2020
4 Years ago
termetalli questions
;