twd-m4sc0 1.3.0__py3-none-any.whl → 1.4.0__py3-none-any.whl

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.
twd/twd.py CHANGED
@@ -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)
@@ -78,6 +109,8 @@ def get_package_version():
78
109
  return "Unknown version"
79
110
 
80
111
  def main():
112
+ global TWD_FILE
113
+
81
114
  parser = argparse.ArgumentParser(description="Temporarily save and navigate to working directories.")
82
115
 
83
116
  parser.add_argument('-s', '--save', nargs='?', const='', help="Save the current or specified directory")
@@ -97,12 +130,16 @@ def main():
97
130
  if args.shell:
98
131
  print('''
99
132
  function twd() {
100
- output=$(python3 -m twd "$@");
101
- if [[ "$1" == "-g" ]]; then
102
- eval "$output";
103
- else
104
- echo "$output";
105
- 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";
106
143
  }
107
144
  ''')
108
145
  return 0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: twd_m4sc0
3
- Version: 1.3.0
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
@@ -0,0 +1,11 @@
1
+ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ tests/test_twd.py,sha256=XrxOo99oqida_7qZ48pA6BCazZekDmG5syw9NvjZWDU,484
3
+ twd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ twd/__main__.py,sha256=gM_Py51pQFH3CXdDIuPzBW6eNlFH1UHeRAmgAPWQyik,61
5
+ twd/twd.py,sha256=JOuDk8vTq9eQv9M2EmPN00DCepvz3AhRWzOclVdyu9I,5135
6
+ twd_m4sc0-1.4.0.dist-info/LICENSE,sha256=eQSDjcD_fvOwfjmrzxKJhtZsSI39seMawuvsgeD_dfw,1062
7
+ twd_m4sc0-1.4.0.dist-info/METADATA,sha256=vdGcYcgEt0r-qnppstWf5IZrpb-TirKp3kqKhjPGvXI,2166
8
+ twd_m4sc0-1.4.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
9
+ twd_m4sc0-1.4.0.dist-info/entry_points.txt,sha256=QfYDHHjipkVN4oalpACFmIeYHb7GQCJY4SC12bTsiQQ,37
10
+ twd_m4sc0-1.4.0.dist-info/top_level.txt,sha256=PXToru2Yr2Xh3F_F-pHXtuOQVp5x7KKCPFf94P_VI5U,10
11
+ twd_m4sc0-1.4.0.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- tests/test_twd.py,sha256=XrxOo99oqida_7qZ48pA6BCazZekDmG5syw9NvjZWDU,484
3
- twd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- twd/__main__.py,sha256=gM_Py51pQFH3CXdDIuPzBW6eNlFH1UHeRAmgAPWQyik,61
5
- twd/twd.py,sha256=XZPt2QuU6Huf4lx69C1zO36Xd8Vm6vcvwXeZWwmtlSE,4122
6
- twd_m4sc0-1.3.0.dist-info/LICENSE,sha256=eQSDjcD_fvOwfjmrzxKJhtZsSI39seMawuvsgeD_dfw,1062
7
- twd_m4sc0-1.3.0.dist-info/METADATA,sha256=dO_TCb51kfq20yC11PzQiyqhMkYAoBlSJOHIekTL-0E,2166
8
- twd_m4sc0-1.3.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
9
- twd_m4sc0-1.3.0.dist-info/entry_points.txt,sha256=QfYDHHjipkVN4oalpACFmIeYHb7GQCJY4SC12bTsiQQ,37
10
- twd_m4sc0-1.3.0.dist-info/top_level.txt,sha256=PXToru2Yr2Xh3F_F-pHXtuOQVp5x7KKCPFf94P_VI5U,10
11
- twd_m4sc0-1.3.0.dist-info/RECORD,,