twd-m4sc0 1.3.0__tar.gz → 1.4.1__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.3.0
3
+ Version: 1.4.1
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
@@ -35,7 +35,7 @@ pip install twd-m4sc0
35
35
  2. Add the following line to your `.bashrc` or `.zshrc` to set up the shell function:
36
36
 
37
37
  ```bash
38
- eval $(twd --shell)
38
+ eval $(python3 -m twd --shell)
39
39
  ```
40
40
 
41
41
  3. Exit and reopen the terminal or reload using:
@@ -110,6 +110,19 @@ user:~/.config $ twd -s
110
110
  Saved TWD to /home/user/.config
111
111
  ```
112
112
 
113
+ #### Force
114
+
115
+ Forces an action
116
+
117
+ > Currently only implemented on the `-u` flag
118
+
119
+ - Example
120
+
121
+ ```bash
122
+ user:~/.config $ twd -u --force
123
+ TWD File deleted and TWD unset
124
+ ```
125
+
113
126
  # Contribution
114
127
 
115
128
  To set up a dev environment:
@@ -22,7 +22,7 @@ pip install twd-m4sc0
22
22
  2. Add the following line to your `.bashrc` or `.zshrc` to set up the shell function:
23
23
 
24
24
  ```bash
25
- eval $(twd --shell)
25
+ eval $(python3 -m twd --shell)
26
26
  ```
27
27
 
28
28
  3. Exit and reopen the terminal or reload using:
@@ -97,6 +97,19 @@ user:~/.config $ twd -s
97
97
  Saved TWD to /home/user/.config
98
98
  ```
99
99
 
100
+ #### Force
101
+
102
+ Forces an action
103
+
104
+ > Currently only implemented on the `-u` flag
105
+
106
+ - Example
107
+
108
+ ```bash
109
+ user:~/.config $ twd -u --force
110
+ TWD File deleted and TWD unset
111
+ ```
112
+
100
113
  # Contribution
101
114
 
102
115
  To set up a dev environment:
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="twd_m4sc0",
5
- version="1.3.0",
5
+ version="1.4.1",
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
- """
19
- if not output:
44
+ def output_handler(message=None, path=None, output=True, simple_output=False, message_type=0):
45
+ if not output or CONFIG["output_behaviour"] == 0:
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 not message and not path:
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)
@@ -59,15 +90,22 @@ def go_to_directory(output=True, simple_output=False):
59
90
  def show_directory(output=True, simple_output=False):
60
91
  TWD = load_directory()
61
92
 
62
- if TWD is None:
93
+ if TWD is None or TWD == '':
63
94
  output_handler("No TWD set", None, output, simple_output)
64
95
  else:
65
96
  output_handler(f"Current TWD: {TWD}", TWD, output, simple_output)
66
97
 
67
- def unset_directory(output=True, simple_output=False):
98
+ def unset_directory(output=True, simple_output=False, force=False):
68
99
  if not os.path.exists(TWD_FILE):
69
100
  output_handler(f"No TWD file found", None, output, simple_output)
70
101
  else:
102
+ if not force:
103
+ output_handler(r'''
104
+ If you want to execute unsetting the current TWD, please use "--force" and run again.
105
+
106
+
107
+ This feature is to prevent accidental execution.''', None, True, False)
108
+ return
71
109
  os.remove(TWD_FILE)
72
110
  output_handler(f"TWD File deleted and TWD unset", None, output, simple_output)
73
111
 
@@ -78,6 +116,8 @@ def get_package_version():
78
116
  return "Unknown version"
79
117
 
80
118
  def main():
119
+ global TWD_FILE
120
+
81
121
  parser = argparse.ArgumentParser(description="Temporarily save and navigate to working directories.")
82
122
 
83
123
  parser.add_argument('-s', '--save', nargs='?', const='', help="Save the current or specified directory")
@@ -88,21 +128,28 @@ def main():
88
128
  parser.add_argument('--shell', action='store_true', help="Output shell function for integration")
89
129
  parser.add_argument('--simple-output', action='store_true', help="Only print essential output (new directory, absolute path, etc.)")
90
130
  parser.add_argument('--no-output', action='store_true', help="Prevents the console from sending output")
91
-
131
+ parser.add_argument('-f', '--force', action='store_true', help="Force an action")
92
132
  args = parser.parse_args()
93
133
 
94
134
  output = not args.no_output
95
135
  simple_output = args.simple_output
96
136
 
97
137
  if args.shell:
98
- print('''
138
+ print(r'''
99
139
  function twd() {
100
140
  output=$(python3 -m twd "$@");
101
- if [[ "$1" == "-g" ]]; then
102
- eval "$output";
103
- else
104
- echo "$output";
105
- fi
141
+ while IFS= read -r line; do
142
+ if [[ -z "$line" ]]; then
143
+ continue;
144
+ fi;
145
+ type=$(echo "$line" | cut -d';' -f1);
146
+ message=$(echo "$line" | cut -d';' -f2-);
147
+ if [[ "$type" == "1" ]]; then
148
+ eval "$message";
149
+ else
150
+ echo "$message";
151
+ fi;
152
+ done <<< "$output";
106
153
  }
107
154
  ''')
108
155
  return 0
@@ -114,7 +161,8 @@ def main():
114
161
  elif args.list:
115
162
  show_directory(output, simple_output)
116
163
  elif args.unset:
117
- unset_directory(output, simple_output)
164
+ force = args.force
165
+ unset_directory(output, simple_output, force)
118
166
  else:
119
167
  parser.print_help()
120
168
  return 1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: twd_m4sc0
3
- Version: 1.3.0
3
+ Version: 1.4.1
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
@@ -35,7 +35,7 @@ pip install twd-m4sc0
35
35
  2. Add the following line to your `.bashrc` or `.zshrc` to set up the shell function:
36
36
 
37
37
  ```bash
38
- eval $(twd --shell)
38
+ eval $(python3 -m twd --shell)
39
39
  ```
40
40
 
41
41
  3. Exit and reopen the terminal or reload using:
@@ -110,6 +110,19 @@ user:~/.config $ twd -s
110
110
  Saved TWD to /home/user/.config
111
111
  ```
112
112
 
113
+ #### Force
114
+
115
+ Forces an action
116
+
117
+ > Currently only implemented on the `-u` flag
118
+
119
+ - Example
120
+
121
+ ```bash
122
+ user:~/.config $ twd -u --force
123
+ TWD File deleted and TWD unset
124
+ ```
125
+
113
126
  # Contribution
114
127
 
115
128
  To set up a dev environment:
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes