twd-m4sc0 1.2.2__tar.gz → 1.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: twd_m4sc0
3
- Version: 1.2.2
3
+ Version: 1.4.0
4
4
  Summary: A tool to temporarily save and go to a working directory
5
5
  Home-page: https://github.com/m4sc0/twd
6
6
  Author: m4sc0
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="twd_m4sc0",
5
- version="1.2.2",
5
+ version="1.4.0",
6
6
  packages=find_packages(),
7
7
  entry_points={
8
8
  "console_scripts": [
@@ -1,28 +1,60 @@
1
1
  import os
2
2
  import argparse
3
3
  import sys
4
+ import json
4
5
  from importlib.metadata import version, PackageNotFoundError
5
6
 
6
- TWD_FILE = os.path.join(os.path.expanduser("~"), ".twd")
7
+ TWD_DIR = os.path.join(os.path.expanduser("~"), ".twd")
8
+ CONFIG_FILE = os.path.join(TWD_DIR, 'config')
9
+
10
+ DEFAULT_CONFIG = {
11
+ "data_file": "~/.twd/data",
12
+ "output_behaviour": 2
13
+ }
14
+
15
+ os.makedirs(TWD_DIR, exist_ok=True)
16
+
17
+ def load_config():
18
+ if not os.path.exists(CONFIG_FILE):
19
+ with open(CONFIG_FILE, 'w') as file:
20
+ json.dump(DEFAULT_CONFIG, file, indent=4)
21
+ return DEFAULT_CONFIG
22
+ else:
23
+ with open(CONFIG_FILE, 'r') as file:
24
+ try:
25
+ return json.load(file)
26
+ except json.JSONDecodeError as e:
27
+ print(f"Error loading config: {e}")
28
+ return DEFAULT_CONFIG
29
+
30
+ CONFIG = load_config()
31
+
32
+ TWD_FILE = os.path.expanduser(CONFIG.get("data_file", "~/.twd/data"))
33
+
34
+ def ensure_data_file_exists():
35
+ if not os.path.exists(TWD_FILE):
36
+ with open(TWD_FILE, 'w') as f:
37
+ f.write("")
38
+
39
+ ensure_data_file_exists()
7
40
 
8
41
  def get_absolute_path(path):
9
42
  return os.path.abspath(path)
10
43
 
11
- def output_handler(message=None, path=None, output=True, simple_output=False):
12
- """
13
- Handles all output based on the flags --no-output and --simple-output.
14
- - message: Regular output string
15
- - path: Path to be printed (in simple output mode)
16
- - output: Whether output is enabled
17
- - simple_output: Whether only essential output should be shown
18
- """
44
+ def output_handler(message=None, path=None, output=True, simple_output=False, message_type=0):
19
45
  if not output:
20
46
  return
21
47
 
22
- if simple_output and path:
23
- print(path)
24
- elif not simple_output and message:
25
- print(message)
48
+ if CONFIG["output_behaviour"] == 0:
49
+ return
50
+
51
+ if message_type == 1:
52
+ print(f"1;{message}")
53
+ elif message_type == 0:
54
+ if simple_output and path:
55
+ print(f"0;{path}")
56
+ elif not simple_output and message:
57
+ print(f"0;{message}")
26
58
 
27
59
  def save_directory(path=None, output=True, simple_output=False):
28
60
  if path is None:
@@ -38,7 +70,6 @@ def save_directory(path=None, output=True, simple_output=False):
38
70
  def load_directory():
39
71
  if not os.path.exists(TWD_FILE):
40
72
  return None
41
-
42
73
  with open(TWD_FILE, "r") as f:
43
74
  return f.read().strip()
44
75
 
@@ -50,7 +81,7 @@ def go_to_directory(output=True, simple_output=False):
50
81
  return 1
51
82
  else:
52
83
  if os.path.exists(TWD):
53
- output_handler(f"cd {TWD}", TWD, output, simple_output)
84
+ output_handler(f"cd {TWD}", TWD, output, simple_output, message_type=1)
54
85
  return 0
55
86
  else:
56
87
  output_handler(f"Directory does not exist: {TWD}", None, output, simple_output)
@@ -64,6 +95,13 @@ def show_directory(output=True, simple_output=False):
64
95
  else:
65
96
  output_handler(f"Current TWD: {TWD}", TWD, output, simple_output)
66
97
 
98
+ def unset_directory(output=True, simple_output=False):
99
+ if not os.path.exists(TWD_FILE):
100
+ output_handler(f"No TWD file found", None, output, simple_output)
101
+ else:
102
+ os.remove(TWD_FILE)
103
+ output_handler(f"TWD File deleted and TWD unset", None, output, simple_output)
104
+
67
105
  def get_package_version():
68
106
  try:
69
107
  return version("twd_m4sc0")
@@ -71,11 +109,14 @@ def get_package_version():
71
109
  return "Unknown version"
72
110
 
73
111
  def main():
112
+ global TWD_FILE
113
+
74
114
  parser = argparse.ArgumentParser(description="Temporarily save and navigate to working directories.")
75
115
 
76
116
  parser.add_argument('-s', '--save', nargs='?', const='', help="Save the current or specified directory")
77
117
  parser.add_argument('-g', '--go', action='store_true', help="Go to the saved directory")
78
118
  parser.add_argument('-l', '--list', action='store_true', help="Show saved TWD")
119
+ parser.add_argument('-u', '--unset', action='store_true', help="Unset the saved TWD")
79
120
  parser.add_argument('-v', '--version', action='version', version=f'TWD Version: {get_package_version()}', help='Show the current version of TWD installed')
80
121
  parser.add_argument('--shell', action='store_true', help="Output shell function for integration")
81
122
  parser.add_argument('--simple-output', action='store_true', help="Only print essential output (new directory, absolute path, etc.)")
@@ -89,12 +130,16 @@ def main():
89
130
  if args.shell:
90
131
  print('''
91
132
  function twd() {
92
- output=$(python3 -m twd "$@");
93
- if [[ "$1" == "-g" ]]; then
94
- eval "$output";
95
- else
96
- echo "$output";
97
- fi
133
+ output=$(python3 -m twd "$@");
134
+ while IFS= read -r line; do
135
+ type=$(echo "$line" | cut -d';' -f1);
136
+ message=$(echo "$line" | cut -d';' -f2-);
137
+ if [[ "$type" == "1" ]]; then
138
+ eval "$message";
139
+ else
140
+ echo "$message";
141
+ fi;
142
+ done <<< "$output";
98
143
  }
99
144
  ''')
100
145
  return 0
@@ -105,6 +150,8 @@ def main():
105
150
  return go_to_directory(output, simple_output)
106
151
  elif args.list:
107
152
  show_directory(output, simple_output)
153
+ elif args.unset:
154
+ unset_directory(output, simple_output)
108
155
  else:
109
156
  parser.print_help()
110
157
  return 1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: twd_m4sc0
3
- Version: 1.2.2
3
+ Version: 1.4.0
4
4
  Summary: A tool to temporarily save and go to a working directory
5
5
  Home-page: https://github.com/m4sc0/twd
6
6
  Author: m4sc0
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes