Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Enhancement: Merged all offline functions in one module. #198

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions package/clip.py

This file was deleted.

10 changes: 0 additions & 10 deletions package/display.py

This file was deleted.

93 changes: 93 additions & 0 deletions package/offline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os
import shutil
import subprocess
import sys
from package.password import valid_password

def display_or_copy_snippet(snippet_name, password, action):
if not isinstance(password, int) or not valid_password(password):
raise ValueError("Incorrect password")

base_dir = os.path.dirname(__file__)
snippets_dir = os.path.join(base_dir, "stash")

try:
snippet_path = os.path.join(snippets_dir, snippet_name)
with open(snippet_path, "r") as file:
content = file.read()

if action == "display":
print(content)
elif action == "copy":
copy_to_clipboard(content)
print("Snippet copied to clipboard.")
except FileNotFoundError:
print("Error: No file found with the specified name.")
except Exception as e:
print(f"Error: {e}")


def show(snippet_name=None, password=None):
if snippet_name is None and password is None:
base_dir = os.path.dirname(__file__)
snippets_dir = os.path.join(base_dir, "stash")
list_snippets(snippets_dir)
return

display_or_copy_snippet(snippet_name, password, action="display")


def clip(snippet_name, password):
display_or_copy_snippet(snippet_name, password, action="copy")

def write(snippet_name, password):
if not isinstance(password, int) or not valid_password(password):
raise ValueError("Incorrect password")

base_dir = os.path.dirname(__file__)
snippets_dir = os.path.join(base_dir, "stash")

try:
snippet_path = os.path.join(snippets_dir, snippet_name)
output_path = os.path.join(base_dir, snippet_name)

shutil.copyfile(snippet_path, output_path)
print(f"File '{snippet_path}' copied successfully to '{output_path}'.")
except FileNotFoundError:
print("Error: No file found with the specified name.")
except Exception as e:
print(f"Error: {e}")


def copy_to_clipboard(text):
try:
if "linux" in sys.platform:
if shutil.which("xclip") is None:
raise RuntimeError("xclip not found. Install it.")
subprocess.run(["xclip", "-selection", "clipboard"], input=text.strip().encode(), check=True)

elif "win32" in sys.platform:
subprocess.run(["C:\\Windows\\System32\\clip.exe"], input=text.strip().encode(), check=True)

elif "darwin" in sys.platform:
subprocess.run(["/usr/bin/pbcopy"], input=text.strip().encode(), check=True)

else:
raise OSError("Unsupported operating system")
except subprocess.CalledProcessError:
print("Error: Failed to copy to clipboard.")


def list_snippets(snippets_dir):
try:
contents = os.listdir(snippets_dir)
for file in contents:
print(file)
except FileNotFoundError:
print("Error: Stash directory not found.")
except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
pass
5 changes: 5 additions & 0 deletions package/password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from datetime import datetime

def valid_password(password: int) -> bool:
current_time = int(datetime.now().strftime("%H%M"))
return password == current_time
84 changes: 0 additions & 84 deletions package/show.py

This file was deleted.

36 changes: 0 additions & 36 deletions package/write.py

This file was deleted.

Loading