summarylogtreecommitdiffstats
path: root/spaces.py
blob: 97a8cf921edaf17034658d2a73d0bfc1ea81a421 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

import sqlite3
import argparse
import subprocess

import time  # Make sure to import time if you're using it
from rich.console import Console
from rich.table import Table
import random

import os

class Spaces:

    def __init__(self):
        # self.name = name
        self.console = Console()

        ################################################################ database
    def create_db(self, cursor):
        # space table
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS spaces(
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                space_name TEXT NOT NULL ,
                UNIQUE(space_name)
            );
            """
        )
        # main table
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS items(
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                space_id INTEGER,
                app TEXT,
                workspace INTEGER ,
                FOREIGN KEY (space_id) REFERENCES spaces(id)
            );
            """
        )

    def new_record_workspace(self, cursor, spacename):
        cursor.execute(
            """
            INSERT OR IGNORE INTO spaces (space_name) VALUES (?);
            """,
            (spacename,)
        )

    def new_record_item(self, cursor, space_name, app, session):
        workspace_id = self.read_space_id_by_name(cursor,space_name)
        cursor.execute(
            """
            INSERT OR IGNORE INTO items (space_id , app , workspace) VALUES (?,?,?);
            """,
            (workspace_id, app, session)
        )

    def read_all_workspaces(self , cursor):
        spaces = cursor.execute(
            """
            SELECT * From spaces
            """
        )
        spaces = cursor.fetchall()
        return spaces

    def read_all_workspace_items(self, cursor, workspace):
        # Get space by name
        cursor.execute(
            """
            SELECT * FROM spaces WHERE space_name = (?)
            """,
            (workspace,)
        )
        space = cursor.fetchall()  

        if space is None:
            return []  


        cursor.execute(
            """
            SELECT * FROM items WHERE space_id = (?)
            """,
            (space[0][0],)  
        )
        results = None
        results = cursor.fetchall()  
        if not results:
            return None

        return results


    def read_space_id_by_name(self, cursor, workspace ) :
        cursor.execute(
            """
            SELECT * FROM spaces WHERE space_name = (?) 
            """,
            (workspace,)

        )

        result = cursor.fetchone()
        return result[0] if result else None  # Return the ID or None if not found



    def delete_item(self , cursor , workspace, app ,session) :

        workspace_id = self.read_space_id_by_name(cursor,workspace)
        workspace_items = self.read_all_workspace_items(cursor,workspace)
        print (workspace_items)
        print(workspace_id)
        for workspace in workspace_items :
            if (workspace[1] == workspace_id) and (workspace[2] == app) and (workspace[3] == session):
                cursor.execute(
                """
                DELETE from items WHERE id = (?) 
                """ , (workspace[0],)
                )


    def delete_all_items_by_workspace(self,cursor,workspace):
        workspace_id = self.read_space_id_by_name(cursor,workspace)
        cursor.execute(
            """
            DELETE from items WHERE space_id = (?) 
            """ , (workspace_id ,)
            )

    def delete_a_workspace(self , cursor , workspace):
        self.delete_all_items_by_workspace(cursor,workspace)
        workspace_id = self.read_space_id_by_name(cursor,workspace)
        cursor.execute(
            """
            DELETE from spaces WHERE id = (?) 
            """ , (workspace_id , )
            )


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

        ############################################################################### rich

    def create_table_items(self, name, spaces):
        # Create a table
        table = Table(title="workspace: " + name)

        # Add columns
        table.add_column("ID", justify="center", style="cyan", no_wrap=True)
        table.add_column("app_name", justify="left", style="magenta")
        table.add_column("session", justify="right", style="green")

        if spaces:
            
            for space in spaces:
                table.add_row(str(space[0]), str(space[2]), str(space[3]))

        self.console.print(table)  

    def create_table_workspaces(self, workspaces):
        # Create a table
        table = Table(title="List of workspaces => ")

        # Add columns
        table.add_column("ID", justify="left", style="cyan", no_wrap=True)
        table.add_column("workspace", justify="right", style="magenta")

        if workspaces :

            for workspace in workspaces:
                table.add_row(str(workspace[0]),str(workspace[1]))

        self.console.print(table)  

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

        ############################################################## args

    def make_ARGS(self):
        parser = argparse.ArgumentParser(description="This tool helps you make spaces and open apps on startup for work - programming - etc...")
        
        parser.add_argument("-m", "--make", type=str, help="Make a new space and name it. Usage: -m [name]")
        parser.add_argument("-rs", "--read_workspace", action="store_true", help="List all available workspaces")

        # -ai workspace_name app_name desktop_session
        parser.add_argument("-ai", "--add_item", type=str, nargs='+' , help="Make a new app on that space and name it. Usage: -ai [workspace_name] [app_name] [desktop_session] ")
     
       # -ri workspace_name app_name desktop_session
        parser.add_argument("-rmi", "--remove_item", type=str, nargs='+' , help="remove an app from your workspace. Usage: -ri [workspace_name] [app_name] [desktop_session] ")
       
       # -rw workspace_name 
        parser.add_argument("-rmw", "--remove_workspace", type=str , help="remove a workspace compeletly. Usage: -rw [workspace_name] ")

        parser.add_argument("-ri3", "--run_i3", type=str, help="run a workspace on i3wm. Usage: -r [workspace_name]")

        parser.add_argument("-xp", "--xprop", action="store_true", help="uses xprop to get app name and info - just run it and click on app")


        args = parser.parse_args()
        return args


        ################################################################## VMS


    def run_i3(self , app_list):
        for app in app_list:
            # Switch to the desired workspace
            subprocess.run(f'i3-msg "workspace {app[3]}"', shell=True)
            
            # Optional: Add a small delay to ensure the workspace switch is complete
            time.sleep(3)  # Adjust the delay as needed

            # Execute the application
            subprocess.Popen(app[2], shell=True)

            # Optional: Add a delay to allow the application to start
            time.sleep(3)  # Adjust the delay as needed

        ################################################################### app info
    def app_info(self):
        os.system('xprop | grep ^WM_NAME')

        ################################################################### main

    def spaces(self, args):

        # Expand the path to the user's home directory
        config_dir = os.path.expanduser('~/.config/spaces')

        # Create the directory if it doesn't exist
        try:
            os.makedirs(config_dir)
        except FileExistsError:
            # Directory already exists
            pass

        # Connect to the SQLite database
        db_path = os.path.join(config_dir, 'spaces.db')  # Specify a database file name

        conn = sqlite3.connect(db_path)

        cursor = conn.cursor()
        self.create_db(cursor)

        if args.read_workspace:
            workspaces = self.read_all_workspaces(cursor)  # Get all workspaces
            self.create_table_workspaces(workspaces)  # Pass them to the table
        elif args.make:
            self.new_record_workspace(cursor, args.make)
            ws_items = self.read_all_workspace_items(cursor, args.make)
            self.create_table_items(args.make, ws_items)
        elif args.add_item :
            self.new_record_item(cursor , args.add_item[0] , args.add_item[1] , args.add_item[2]  )
            ws_items = self.read_all_workspace_items(cursor , args.add_item[0])
            self.create_table_items(args.add_item[0] , ws_items)
        elif args.remove_item :
            self.delete_item(cursor , args.remove_item[0] ,args.remove_item[1],args.remove_item[2])
            ws_items = self.read_all_workspace_items(cursor , args.remove_item[0])
            self.create_table_items(args.remove_item[0] , ws_items)
        elif args.remove_workspace :
            self.delete_a_workspace(cursor , args.remove_workspace)
            print("worksapce  "+  args.remove_workspace + "  deleted")
        elif args.run_i3 :
            ws_items = self.read_all_workspace_items(cursor , args.run_i3)
            self.create_table_items(args.run_i3 , ws_items)
            self.run_i3(ws_items)
        elif args.xprop :
            self.app_info()




        cursor.close()
        conn.commit()
        conn.close()


if __name__ == "__main__":
    # user_name = input("Enter your name: ")
    app = Spaces()
    args = app.make_ARGS()

    # Call the main method
    app.spaces(args)