<commit_before>#!/usr/bin/python
# -*- coding: UTF-8 -*-

############################################################################
# Copyright (c) 2009   unohu <<EMAIL>>                            #
#                                                                          #
# Permission to use, copy, modify, and/or distribute this software for any #
# purpose with or without fee is hereby granted, provided that the above   #
# copyright notice and this permission notice appear in all copies.        #
#                                                                          #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES #
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         #
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   #
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    #
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  #
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           #
#                                                                          #
############################################################################

"""
Tiling window on any window manager. 

Usage:
  stiler.py layout (next|prev)
  stiler.py (focus|move|swap) (up|down|left|right)
  stiler.py (grow|shrink) (height|width)
  stiler.py cycle 
  stiler.py anticycle 
  stiler.py (save|load) <layout_id>
  stiler.py -h | --help

Options:
  -h --help     Show this screen.
"""

import os
import re
import subprocess
import time
from config import *
import config


def _exec_and_output(cmd):
    ENCODING = 'utf8'
    return subprocess.check_output(cmd, shell=True).decode(ENCODING)


def _exec(cmd):
    # print(cmd)
    os.system(cmd)


r_wmctrl_lG = '^([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(.+)$'
r_wmctrl_d = '(\d)+.+?(\d+)x(\d+).+?(\d+),(\d+).+?(\d+),(\d+).+?(\d+)x(\d+)'


def initialize1():
    desk_output = _exec_and_output("wmctrl -d").strip().split("\n")

    current = [x for x in desk_output if x.split()[1] == "*"][0]
    current = re.findall(r_wmctrl_d, current.strip())[0]
    desktop, _, _, desktop_x, desktop_y, orig_x, orig_y, width, height = current

    return desktop, desktop_x, desktop_y, orig_x, orig_y, width, height


def initialize2(desktop):
    s = _exec_and_output(
        "xdpyinfo | grep 'dimension' | awk -F: '{ print $2 }' | awk '{ print $1 }' ")
    x, y = s.split('x')
    resx, resy = int(x), int(y)

    win_output = _exec_and_output("wmctrl -lG").strip().split("\n")

    win_list = []
    win_list_all = []
    win_filtered_all = []
    for win in win_output:
        winid, _desktop, x, y, w, h, host, name = re.findall(r_wmctrl_lG, win)[
            0]
        if not _desktop == desktop:
            continue
        if host == 'N/A':
            continue
        if name in config.EXCLUDE_APPLICATIONS:
            continue
        win_list_all.append(int(winid, 16))
        win_filtered_all.append(win)

        x, y = int(x), int(y)
        if x < 0 or x >= resx or y < 0 or y >= resy:
            continue
        win_list.append(int(winid, 16))
        # TODO use xwininfo to exclude minimized windows

    WinPosInfoAll = win_filtered_all
    WinPosInfoAll = [re.findall(r_wmctrl_lG, w)[0] for w in WinPosInfoAll]
    WinPosInfoAll = {int(_id, 16): (_name, [int(x), int(y), int(
        w), int(h)]) for _id, _ws, x, y, w, h, _host, _name in WinPosInfoAll}
    for _id in WinPosInfoAll:
        WinPosInfoAll[_id][1][1] += -72 + 44

    return win_list, win_list_all,  WinPosInfoAll


def get_active_window():
    active = int(_exec_and_output("xdotool getactivewindow").split()[0])
    if active not in WinList:
        active = None
    if not None == active:
        h = PERSISTENT_DATA.get('active_history', [])
        h.insert(0, active)
        PERSISTENT_DATA['active_history'] = h[:1000]
    return active


def get_last_active_window():
    for active in PERSISTENT_DATA.get('active_history', []):
        if active in WinList:
            return active

if os.path.exists(config.TempFile):
    PERSISTENT_DATA_ALL = eval(open(config.TempFile).read())
else:
    PERSISTENT_DATA_ALL = {}

desktop, desktop_x, desktop_y, OrigXstr, OrigYstr, MaxWidthStr, MaxHeightStr = initialize1()
WinList, WinListAll, WinPosInfo = initialize2(desktop)
Desktop = '%s,%s,%s' % (desktop, desktop_x, desktop_y)

MaxWidth = int(MaxWidthStr) - LeftPadding - RightPadding
MaxHeight = int(MaxHeightStr) - TopPadding - BottomPadding<commit_msg>workspace mark<commit_after>#!/usr/bin/python
# -*- coding: UTF-8 -*-

############################################################################
# Copyright (c) 2009   unohu <<EMAIL>>                            #
#                                                                          #
# Permission to use, copy, modify, and/or distribute this software for any #
# purpose with or without fee is hereby granted, provided that the above   #
# copyright notice and this permission notice appear in all copies.        #
#                                                                          #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES #
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         #
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  #
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   #
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    #
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  #
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           #
#                                                                          #
############################################################################

"""
Tiling window on any window manager. 

Usage:
  stiler.py layout (next|prev)
  stiler.py (focus|move|swap) (up|down|left|right)
  stiler.py (grow|shrink) (height|width)
  stiler.py cycle 
  stiler.py anticycle 
  stiler.py (save|load) <layout_id>
  stiler.py -h | --help

Options:
  -h --help     Show this screen.
"""

import os
import re
import subprocess
import time
from config import *
import config


def _exec_and_output(cmd):
    ENCODING = 'utf8'
    return subprocess.check_output(cmd, shell=True).decode(ENCODING)


def _exec(cmd):
    # print(cmd)
    os.system(cmd)


r_wmctrl_lG = '^([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(.+)$'
r_wmctrl_d = '(\d)+.+?(\d+)x(\d+).+?(\d+),(\d+).+?(\d+),(\d+).+?(\d+)x(\d+)'


def initialize1():
    desk_output = _exec_and_output("wmctrl -d").strip().split("\n")

    current = [x for x in desk_output if x.split()[1] == "*"][0]
    current = re.findall(r_wmctrl_d, current.strip())[0]
    desktop, _, _, desktop_x, desktop_y, orig_x, orig_y, width, height = current

    return desktop, desktop_x, desktop_y, orig_x, orig_y, width, height


def initialize2(desktop):
    s = _exec_and_output(
        "xdpyinfo | grep 'dimension' | awk -F: '{ print $2 }' | awk '{ print $1 }' ")
    x, y = s.split('x')
    resx, resy = int(x), int(y)

    win_output = _exec_and_output("wmctrl -lG").strip().split("\n")

    win_list = []
    win_list_all = []
    WinPosInfoAll = {}
    for win in win_output:

        winid, _desktop, x, y, w, h, host, name = re.findall(r_wmctrl_lG, win)[
            0]

        if not _desktop == desktop:
            continue
        if host == 'N/A':
            continue
        if name in config.EXCLUDE_APPLICATIONS:
            continue

        winid = int(winid, 16)
        x, y = int(x), int(y)
        w, h = int(w), int(h)

        win_list_all.append(winid)
        WinPosInfoAll[winid] = name, [x, y - 72 + 44, w, h]

        if x < 0 or x >= resx or y < 0 or y >= resy:
            continue

        win_list.append(winid)
        # TODO use xwininfo to exclude minimized windows

    return win_list, win_list_all,  WinPosInfoAll


def get_active_window():
    active = int(_exec_and_output("xdotool getactivewindow").split()[0])
    if active not in WinList:
        active = None
    if not None == active:
        h = PERSISTENT_DATA.get('active_history', [])
        h.insert(0, active)
        PERSISTENT_DATA['active_history'] = h[:1000]
    return active


def get_last_active_window():
    for active in PERSISTENT_DATA.get('active_history', []):
        if active in WinList:
            return active

if os.path.exists(config.TempFile):
    PERSISTENT_DATA_ALL = eval(open(config.TempFile).read())
else:
    PERSISTENT_DATA_ALL = {}

desktop, desktop_x, desktop_y, OrigXstr, OrigYstr, MaxWidthStr, MaxHeightStr = initialize1()
WinList, WinListAll, WinPosInfo = initialize2(desktop)
Desktop = '%s,%s,%s' % (desktop, desktop_x, desktop_y)

MaxWidth = int(MaxWidthStr) - LeftPadding - RightPadding
MaxHeight = int(MaxHeightStr) - TopPadding - BottomPadding
{'fraction_non_alphanumeric': 0.16316139030329865, 'fraction_numerical': 0.0074164268319681205, 'mean_word_length': 2.6314308681672025, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2032003', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2836, 'n_words': 784}