twd-m4sc0 3.0.3__tar.gz → 3.1.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.4
2
2
  Name: twd-m4sc0
3
- Version: 3.0.3
3
+ Version: 3.1.0
4
4
  Summary: TWD by m4sc0
5
5
  Author: m4sc0
6
6
  License: MIT
@@ -0,0 +1,67 @@
1
+ # twd
2
+
3
+ A command-line tool for managing and navigating between directories in the terminal.
4
+
5
+ ## What it does
6
+
7
+ `twd` lets you bookmark directories and quickly jump between them. Instead of typing out long paths or navigating through multiple `cd` commands, you save directories once and access them with vim-like motion bindings.
8
+
9
+ Think of it as a directory hub for places you visit frequently.
10
+
11
+ ## Why this exists
12
+
13
+ I got tired of constantly navigating through the same directory trees. This tool saves time when you're working across multiple projects or deep directory structures.
14
+
15
+ No AI was used in the development of this tool. It's written by hand, contains bugs, and isn't particularly optimized. But it works for what I need, and I'm continuing to improve it.
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ pip install twd-m4sc0
21
+ ```
22
+
23
+ ## Setup
24
+
25
+ To actually change directories (rather than just print paths), add this function to your `~/.bashrc` or `~/.zshrc`:
26
+
27
+ ```bash
28
+ t () {
29
+ binary="twd"
30
+ local target=$($binary "$@" 3>&1 >/dev/tty)
31
+ if [[ -n "$target" && -d "$target" ]]; then
32
+ cd "$target"
33
+ fi
34
+ }
35
+ ```
36
+
37
+ The tool can work without this setup, but you'll only get path output instead of directory changes. This also allows for a quicker launch — instead of having to type `twd` over and over you can start it with just `t` (might conflict with other programs or functions).
38
+
39
+ ## Dev environment
40
+
41
+ Clone the repo and install locally with
42
+
43
+ ```bash
44
+ pip install -e .
45
+ ```
46
+
47
+ It is recommend to create a virtual environment and install the dependencies beforehand.
48
+
49
+ ## How it works
50
+
51
+ `twd` uses file descriptor 3 to communicate the target directory to the shell function. The bash function captures this output and executes the `cd` command. This avoids using temporary files.
52
+
53
+ The interface is built with Textual, a Python TUI framework.
54
+
55
+ ## Version 3
56
+
57
+ This is a complete rewrite from version 2. If you're using v2.0.3 or earlier, the archived version is available [here](https://github.com/m4sc0/twd-archived).
58
+
59
+ Changes include better directory handling, a proper TUI instead of hand-rolled curses code, and generally cleaner implementation.
60
+
61
+ ## Status
62
+
63
+ The project is functional but has known issues. I'm actively developing it and fixing bugs as I find them. Check the issues tab for current problems and planned improvements. Contributions are always appreciated!
64
+
65
+ ## License
66
+
67
+ MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "twd-m4sc0"
3
- version = "3.0.3"
3
+ version = "3.1.0"
4
4
  description = "TWD by m4sc0"
5
5
  authors = [{name = "m4sc0"}]
6
6
  requires-python = ">=3.13"
@@ -24,10 +24,13 @@ def cli(ctx):
24
24
 
25
25
  path = TWDApp(manager=ctx.obj['manager']).run()
26
26
 
27
+ if not path:
28
+ print("Exiting...")
29
+ exit(0)
30
+
27
31
  # write to fd3
28
32
  os.write(3, bytes(str(path), "utf-8"))
29
-
30
- pass
33
+ exit(0)
31
34
 
32
35
  @cli.command()
33
36
  @click.argument('path')
@@ -100,5 +103,62 @@ def list_twds(ctx):
100
103
  click.echo("No TWDs saved yet. Use 'twd save <path> <alias>' to add one.")
101
104
  return
102
105
 
106
+ invalid_found = False
103
107
  for entry in entries:
108
+ if not invalid_found and not os.path.exists(entry.path):
109
+ invalid_found = True
104
110
  click.echo(f"{entry.alias:20} {entry.name:30} {entry.path}")
111
+
112
+ if invalid_found:
113
+ click.echo(f"\nInvalid TWD paths found. Run <twd clean> to remove them.")
114
+
115
+
116
+ @cli.command('clean')
117
+ @click.option('--yes', '-y', is_flag=True, help="Remove all invalid entries without asking")
118
+ @click.pass_context
119
+ def clean(ctx, yes):
120
+ """
121
+ clean twds from invalid paths
122
+ """
123
+ manager = ctx.obj['manager']
124
+
125
+ # all entries, valid and invalid
126
+ entries = manager.list_all()
127
+
128
+ # only invalids
129
+ invalids = []
130
+ for entry in entries:
131
+ if os.path.exists(entry.path):
132
+ continue
133
+
134
+ invalids.append(entry)
135
+
136
+ if len(invalids) == 0:
137
+ click.echo("No invalid TWDs found.")
138
+ return
139
+
140
+ click.echo(f"Found {len(invalids)} invalid TWDs\n")
141
+
142
+ # only valid
143
+ valid_entries = []
144
+ for entry in entries:
145
+ if entry not in invalids:
146
+ valid_entries.append(entry)
147
+
148
+ # remove all
149
+ if yes:
150
+ click.echo("Removing all invalid entries...")
151
+ manager._write_all(valid_entries)
152
+ click.echo(f"Done. {len(valid_entries)} TWDs left.")
153
+ return
154
+
155
+ to_keep = []
156
+ for inv in invalids:
157
+ if not click.confirm(f"Do you want to remove '{inv.alias}'?", default=True):
158
+ # user wants to keep invalid TWD (weird but i'll allow it)
159
+ to_keep.append(inv)
160
+
161
+ # write a unison list of valid entries and the ones to keep
162
+ final_entries = valid_entries + to_keep
163
+ manager._write_all(final_entries)
164
+ click.echo(f"Done. {len(final_entries)} TWDs left.")
@@ -6,7 +6,6 @@ from typing import List, Optional
6
6
  from datetime import datetime
7
7
 
8
8
  from .config import Config
9
- from .utils import search
10
9
 
11
10
  class Entry(BaseModel):
12
11
  """Data class for a signle TWD Entry"""
@@ -27,10 +26,6 @@ class Entry(BaseModel):
27
26
  def validate_path(cls, v):
28
27
  path = Path(v).expanduser().resolve()
29
28
 
30
- if not path.exists():
31
- raise ValueError(f"Path does not exist: {path}")
32
- if not path.is_dir():
33
- raise ValueError(f"Path is not a directory: {path}")
34
29
  return path
35
30
 
36
31
  def to_csv(self) -> List[str]:
@@ -56,6 +51,7 @@ class TwdManager:
56
51
  """twd entry manager stored in csv"""
57
52
 
58
53
  CSV_HEADERS = ["alias", "path", "name", "created_at"]
54
+ CSV_HEADERS_FANCY = ["Alias", "Path", "Description", "Created at"]
59
55
 
60
56
  def __init__(self, csv_path: Path):
61
57
  self.csv_path = csv_path
@@ -81,10 +77,7 @@ class TwdManager:
81
77
  next(reader) # skip headers
82
78
 
83
79
  for row in reader:
84
- try:
85
- entries.append(Entry.from_csv(row))
86
- except Exception as e:
87
- print(f"Warning: skipping invalid row: {e}")
80
+ entries.append(Entry.from_csv(row))
88
81
 
89
82
  return entries
90
83
 
@@ -0,0 +1,92 @@
1
+ from textual import on
2
+ from textual.app import ComposeResult
3
+ from textual.screen import ModalScreen
4
+ from textual.containers import Container, Horizontal
5
+ from textual.widgets import Button, Label
6
+ from typing import Union
7
+
8
+ from twd.data import Entry
9
+
10
+ class ConfirmModal(ModalScreen[bool]):
11
+ """A confirm modal"""
12
+
13
+ DEFAULT_CSS = """
14
+ ConfirmModal {
15
+ align: center middle;
16
+ }
17
+
18
+ ConfirmModal > Container {
19
+ width: auto;
20
+ height: auto;
21
+ border: thick $background 80%;
22
+ background: $surface;
23
+ }
24
+
25
+ ConfirmModal > Container > Label {
26
+ width: 100%;
27
+ content-align-horizontal: center;
28
+ margin-top: 1;
29
+ }
30
+
31
+ ConfirmModal > Container > Horizontal {
32
+ width: auto;
33
+ height: auto;
34
+ }
35
+
36
+ ConfirmModal > Container > Horizontal > Button {
37
+ margin: 2 4;
38
+ }
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ message: Union[str, None] = None,
44
+ confirm_text: str = "Yes",
45
+ cancel_text: str = "No"
46
+ ):
47
+ """
48
+ message: The message to display when popping the modal
49
+ confirm_text: Text to show for confirmation
50
+ cancel_text: Text to show for cancellation
51
+ """
52
+ self.message = message
53
+ self.confirm_text = confirm_text
54
+ self.cancel_text = cancel_text
55
+
56
+ super().__init__()
57
+
58
+ def compose_content(self) -> ComposeResult:
59
+ """
60
+ Abstract method for presenting custom shenanigans
61
+ """
62
+ if self.message:
63
+ yield Label(self.message, id="content")
64
+ else:
65
+ yield Label("Are you sure?", id="content")
66
+
67
+ def compose(self) -> ComposeResult:
68
+ with Container():
69
+ yield from self.compose_content()
70
+ with Horizontal():
71
+ yield Button(self.cancel_text, id="no", variant="error")
72
+ yield Button(self.confirm_text, id="yes", variant="success")
73
+
74
+ @on(Button.Pressed, "#no")
75
+ def no_decision(self) -> None:
76
+ """decision no"""
77
+ self.dismiss(False)
78
+
79
+ @on(Button.Pressed, "#yes")
80
+ def yes_decision(self) -> None:
81
+ """decision yes"""
82
+ self.dismiss(True)
83
+
84
+ class EntryDeleteModal(ConfirmModal):
85
+ """Confirmation modal with detailed entry information"""
86
+
87
+ def __init__(self, entry):
88
+ self.entry = entry
89
+ super().__init__(confirm_text="Delete", cancel_text="Cancel")
90
+
91
+ def compose_content(self) -> ComposeResult:
92
+ yield Label(f"Delete entry '{self.entry.name}'?", id="content")
@@ -1,4 +1,5 @@
1
1
  from enum import Enum
2
+ from datetime import datetime
2
3
  from textual import on
3
4
  from textual.app import App, ComposeResult, Binding
4
5
  from textual.containers import HorizontalGroup, VerticalScroll
@@ -8,7 +9,8 @@ from textual.color import Color
8
9
 
9
10
  from twd.config import Config
10
11
  from twd.data import TwdManager
11
- from twd.utils import search, linear_search
12
+ from twd.utils import fuzzy_search, linear_search
13
+ from twd.modals import ConfirmModal, EntryDeleteModal
12
14
 
13
15
  class Mode(Enum):
14
16
  NORMAL = "normal"
@@ -28,6 +30,7 @@ class TWDApp(App):
28
30
 
29
31
  # modify
30
32
  Binding("/", "enter_search_mode", "Search"),
33
+ Binding("d", "delete_entry", "Delete"),
31
34
  Binding("escape", "enter_normal_mode", "Normal", show=False),
32
35
  # TODO: edit
33
36
  # TODO: rename
@@ -77,7 +80,7 @@ class TWDApp(App):
77
80
 
78
81
  # add headers
79
82
  table = self.query_one(DataTable)
80
- table.add_columns(*self.manager.CSV_HEADERS)
83
+ table.add_columns(*self.manager.CSV_HEADERS_FANCY)
81
84
 
82
85
  self._populate_table()
83
86
 
@@ -93,7 +96,7 @@ class TWDApp(App):
93
96
 
94
97
  # fill data
95
98
  for entry in entries:
96
- table.add_row(entry.alias, str(entry.path), entry.name, entry.created_at)
99
+ table.add_row(entry.alias, str(entry.path), entry.name, entry.created_at.strftime("%Y-%m-%d %H:%M:%S"))
97
100
 
98
101
  def watch_mode(self, old_mode: Mode, new_mode: Mode) -> None:
99
102
  """
@@ -128,8 +131,11 @@ class TWDApp(App):
128
131
  """
129
132
  table = self.query_one(DataTable)
130
133
 
131
- current_row = table.cursor_coordinate.row
132
- next_row = (current_row + 1) % table.row_count
134
+ try:
135
+ current_row = table.cursor_coordinate.row
136
+ next_row = (current_row + 1) % table.row_count
137
+ except ZeroDivisionError as e:
138
+ return
133
139
 
134
140
  table.move_cursor(row=next_row)
135
141
 
@@ -139,8 +145,11 @@ class TWDApp(App):
139
145
  """
140
146
  table = self.query_one(DataTable)
141
147
 
142
- current_row = table.cursor_coordinate.row
143
- prev_row = (current_row - 1) % table.row_count
148
+ try:
149
+ current_row = table.cursor_coordinate.row
150
+ prev_row = (current_row - 1) % table.row_count
151
+ except ZeroDivisionError as e:
152
+ return
144
153
 
145
154
  table.move_cursor(row=prev_row)
146
155
 
@@ -160,6 +169,39 @@ class TWDApp(App):
160
169
  return
161
170
  self.mode = Mode.NORMAL
162
171
 
172
+ def action_delete_entry(self) -> None:
173
+ """
174
+ open confirm modal and delete entry if yes
175
+ """
176
+ if not self.mode == Mode.NORMAL:
177
+ return
178
+
179
+ table = self.query_one(DataTable)
180
+
181
+ # get row
182
+ row_key = table.coordinate_to_cell_key(table.cursor_coordinate).row_key
183
+ row_data = table.get_row(row_key)
184
+ alias = row_data[0]
185
+
186
+ # get entry
187
+ entry = self.manager.get(alias)
188
+
189
+ def check_delete(decision: bool | None) -> None:
190
+ """
191
+ open modal and return the decision
192
+ """
193
+ if not decision:
194
+ return
195
+
196
+ self.manager.remove(alias)
197
+
198
+ self._populate_table()
199
+
200
+ self.notify(f"Removed entry \"{entry.name}\"")
201
+
202
+ # self.push_screen(ConfirmModal(message=f"Are you sure want to delete '{entry.alias}'?"), check_delete)
203
+ self.push_screen(EntryDeleteModal(entry), check_delete)
204
+
163
205
  def action_exit(self) -> None:
164
206
  self.exit()
165
207
 
@@ -177,9 +219,13 @@ class TWDApp(App):
177
219
 
178
220
  # TODO: filter entries and repopulate table
179
221
 
180
- search_result = linear_search(query, all_entries)
222
+ if query is None:
223
+ self._populate_table()
224
+ return
225
+
226
+ search_result = fuzzy_search(query, all_entries)
181
227
 
182
- filtered = [entry for entry in all_entries if entry.alias in search_result]
228
+ filtered = [item[0] for item in search_result]
183
229
 
184
230
  self._populate_table(filtered)
185
231
 
@@ -1,39 +1,40 @@
1
1
  from rapidfuzz import fuzz
2
2
  from typing import List
3
3
 
4
- FUZZY_THRESHOLD = 50
5
-
6
4
  def normalize(name) -> str:
7
5
  return name.lower().replace('-', ' ').replace('_', ' ')
8
6
 
9
- def search(query, items, threshold = 50) -> List:
7
+ def fuzzy_search(query, items, threshold = 50) -> List:
10
8
  """
11
9
  query: search input
12
10
  items: list of (alias, name)
11
+ threshold: threshold score over which the entries are selected
13
12
 
14
- returns: list of (alias, name, score)
13
+ returns: list of (entry, score)
15
14
  """
16
15
 
17
16
  # return all items if query is empty
18
17
  if not query:
19
- return [(alias, name, 100) for alias, name in items]
18
+ return [(e, 100) for e in items]
20
19
 
21
20
  normalized_query = normalize(query)
22
21
  results = []
23
22
 
24
23
  # filtering
25
- for alias, name in items:
26
- alias_score = fuzz.WRatio(normalized_query, normalize(alias))
27
- name_score = fuzz.WRatio(normalized_query, normalize(name))
24
+ for entry in items:
25
+ alias, name = normalize(entry.alias), normalize(entry.name)
26
+
27
+ alias_score = fuzz.ratio(normalized_query, alias)
28
+ name_score = fuzz.ratio(normalized_query, name)
28
29
 
29
30
  # choose higher score
30
31
  best_score = max(alias_score, name_score)
31
32
 
32
33
  # filter out low scores
33
- if best_score >= threshold:
34
- results.append((alias, name, best_score))
34
+ if best_score > threshold:
35
+ results.append((entry, best_score))
35
36
 
36
- results.sort(key=lambda x: x[2], reverse=True)
37
+ results.sort(key=lambda x: x[1], reverse=True)
37
38
 
38
39
  return results
39
40
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: twd-m4sc0
3
- Version: 3.0.3
3
+ Version: 3.1.0
4
4
  Summary: TWD by m4sc0
5
5
  Author: m4sc0
6
6
  License: MIT
@@ -4,6 +4,7 @@ src/twd/__init__.py
4
4
  src/twd/cli.py
5
5
  src/twd/config.py
6
6
  src/twd/data.py
7
+ src/twd/modals.py
7
8
  src/twd/tui.py
8
9
  src/twd/tui.tcss
9
10
  src/twd/utils.py
twd_m4sc0-3.0.3/README.md DELETED
@@ -1,34 +0,0 @@
1
- # twd-m4sc0
2
-
3
- > [!IMPORTANT]
4
- > This is complete rewrite of the program `twd`. If you're using `<=v2.0.3` please make sure to upgrade to the newest major release. The archived version v2 can be viewed [here](https://github.com/m4sc0/twd-archived).
5
-
6
- > twd-m4sc0 / twd is a command-line tool that allows you to temporarily save a working directory and easily navigate back to it. It's designed for developers and users who frequently need to switch between directories in the terminal.
7
-
8
- That's what it was supposed to do at the start. Now it's more like a hub for your frequently visited directories. You can use it like a bookmark manager or for the quickest method of changing between directories that you can find.
9
-
10
- There are quite a few things I wanna make better this time.
11
-
12
- ### Better directory changing
13
-
14
- Previously twd wrote to a temp file, then a bash function used the contents if that file exists, cd's to the dir and deletes the file again. This time around I'm going a different way. I found the method `os.write(3, path)` which can write to [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) directly. This is the same way stdout and stderr are handled in the background (i think). But i'm using a fourth (0 = stdin, 1 = stdout, 2 = stderr), unused FD to write to. This is then captured in the bash function separately from the stdout.
15
-
16
- ### Improved TUI
17
-
18
- For some reason I thought writing a whole UI system using [curses](https://de.wikipedia.org/wiki/Curses) was a good idea. Well, now at least I know better and can say that I won't do that ever again.
19
-
20
- I'll use [Textual](https://textual.textualize.io/) now.
21
-
22
- ### Setup
23
-
24
- It's possible to use TWD without the feature of cd'ing anywhere. But that's kinda lame lol. To make sure it works as intended, copy the following snippet into something like a `~/.bashrc` file.
25
-
26
- ```bash
27
- t () {
28
- binary="twd"
29
- local target=$($binary "$@" 3>&1 >/dev/tty)
30
- if [[ -n "$target" && -d "$target" ]]; then
31
- cd "$target"
32
- fi
33
- }
34
- ```
File without changes
File without changes
File without changes
File without changes