Skip to content

Commit

Permalink
"2023-10-21 17:36:58.352429"
Browse files Browse the repository at this point in the history
  • Loading branch information
jhessin committed Oct 21, 2023
1 parent 3ad9a91 commit cd75b67
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 3 deletions.
61 changes: 58 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from PyQt6.QtCore import QObject, pyqtSignal, QRunnable, pyqtSlot, QThreadPool, Qt
from PyQt6.QtWidgets import (
QApplication, QMainWindow,
QPushButton, QListWidget, QMenuBar, QStatusBar, QPlainTextEdit, QFileDialog,
QPushButton, QListWidget, QMenuBar, QStatusBar, QPlainTextEdit, QFileDialog, QDialog, QDialogButtonBox,
QListWidgetItem,
)


Expand All @@ -33,6 +34,7 @@ def data_path(*relative_path: str) -> os.path:


PICKLE_FILE = data_path('repos.dat')
REPOS_DIRECTORY = os.path.join(os.path.expanduser('~'), 'repos')


class QTextEditLogger(logging.Handler, QObject):
Expand Down Expand Up @@ -75,9 +77,17 @@ def save_repos(data: QListWidget):
sys.exit()


def clone(repo_name: str) -> str:
print(REPOS_DIRECTORY)
os.makedirs(REPOS_DIRECTORY, exist_ok=True)
path = os.path.join(REPOS_DIRECTORY, repo_name.split('/')[-1])
print(f'cloning repo {repo_name} to {path}')
subprocess.run(['gh', 'repo', 'clone', repo_name, path])
return path


def push_repo(path: str):
print(f"committing and pushing {path}")
# TODO: commit all and push the repo
date = datetime.now()
subprocess.run(['git', 'add', '.'], cwd=path)
subprocess.run(['git', 'commit', f'-m "{date}"'], cwd=path)
Expand All @@ -86,7 +96,6 @@ def push_repo(path: str):

def pull_repo(path: str):
print(f"pulling {path}")
# TODO: pull the given repo
subprocess.run(['git', 'pull'], cwd=path)


Expand All @@ -110,6 +119,42 @@ def run(self):
self.signals.result.emit(root)


class RepoSelector(QDialog):
buttonBox: QDialogButtonBox
repoList: QListWidget

def __init__(self):
super().__init__()
uic.loadUi(resource_path('ui', 'RepoSelector.ui'), self)
self.result: str = ''
repo_list: list[str] = subprocess.run(
['gh', 'repo', 'list'],
capture_output=True,
encoding='utf-8').stdout.splitlines()
for repo in repo_list:
result: str = ''
for char in repo:
if char == '\t':
break
else:
result += char
self.add_to_repo_list(result)
self.repoList.currentItemChanged.connect(self.selection_changed)

def add_to_repo_list(self, item: str):
if len(self.repoList.findItems(item, Qt.MatchFlag.MatchExactly)) == 0:
self.repoList.addItem(item)

def selection_changed(self, new_selection: QListWidgetItem):
self.result = new_selection.text()

def accept(self):
if self.result == '':
return False
else:
super().accept()


class MainWindow(QMainWindow):
pullAllButton: QPushButton
pullRepoButton: QPushButton
Expand All @@ -118,6 +163,7 @@ class MainWindow(QMainWindow):

addRepo: QPushButton
rmRepo: QPushButton
cloneRepoButton: QPushButton

repoList: QListWidget
searchButton: QPushButton
Expand All @@ -144,12 +190,21 @@ def __init__(self):
self.searchButton.clicked.connect(self.search_for_repos)
self.rmRepo.clicked.connect(self.rm_repo)
self.addRepo.clicked.connect(self.manual_repo_add)
self.cloneRepoButton.clicked.connect(self.clone_repo)

def search_for_repos(self):
worker = RepoSearch()
worker.signals.result.connect(self.add_to_repo_list)
self.threadpool.start(worker)

def clone_repo(self):
# TODO: show repo window and clone and add repo to repos directory.
dialog = RepoSelector()
result = dialog.exec()
if result:
path = clone(dialog.result)
self.add_to_repo_list(path)

def add_to_repo_list(self, item: str):
if len(self.repoList.findItems(item, Qt.MatchFlag.MatchExactly)) == 0:
self.repoList.addItem(item)
Expand Down
7 changes: 7 additions & 0 deletions ui/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@
<item row="0" column="0" colspan="2">
<widget class="QListWidget" name="repoList"/>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="cloneRepoButton">
<property name="text">
<string>Clone</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
Expand Down
84 changes: 84 additions & 0 deletions ui/RepoSelector.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>400</width>
<height>300</height>
</size>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>240</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QListWidget" name="repoList">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>371</width>
<height>221</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit cd75b67

Please sign in to comment.