edir 2.23__tar.gz → 2.25__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.
@@ -6,7 +6,8 @@ check:
6
6
  flake8 $(PYNAME).py
7
7
  mypy $(PYNAME).py
8
8
  pyright $(PYNAME).py
9
- vermin -vv --exclude importlib.metadata --no-tips -i $(PYNAME).py
9
+ vermin -vv --exclude importlib.metadata --eval-annotations \
10
+ --no-tips -i $(PYNAME).py
10
11
 
11
12
  build:
12
13
  rm -rf dist
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: edir
3
- Version: 2.23
3
+ Version: 2.25
4
4
  Summary: Utility to rename, remove, and copy files/dirs using your editor
5
5
  Author-email: Mark Blakeney <mark.blakeney@bullet-systems.net>
6
6
  License: GPLv3
@@ -57,9 +57,9 @@ the following ways:
57
57
  3 ./c
58
58
  4 ./d
59
59
  ```
60
-
60
+
61
61
  But `edir` presents these as:
62
-
62
+
63
63
  ```
64
64
  1 ./a
65
65
  2 ./b
@@ -117,7 +117,8 @@ the following ways:
117
117
  form.
118
118
 
119
119
  11. `edir` adds a [`-i/--interactive` option](#previewing-changes) to
120
- show pending changes and prompt the user before actioning them.
120
+ show pending changes and prompt the user before actioning them. You
121
+ can also choose to re-edit the changes.
121
122
 
122
123
  12. `edir` adds a `-F/--files` option to only show files, or `-D/--dirs`
123
124
  to only show directories.
@@ -212,14 +213,14 @@ the following ways:
212
213
  1. Deleted files are removed and all renamed files and directories
213
214
  are renamed to temporaries. The temporaries are made on the same
214
215
  file-system as the target.
215
-
216
+
216
217
  2. Empty deleted directories are removed.
217
-
218
+
218
219
  3. Renamed temporary files and directories are renamed to their
219
220
  target name. Any required copies are created.
220
-
221
+
221
222
  4. Remaining deleted directories are removed.
222
-
223
+
223
224
  In simple terms, remember that files are processed before
224
225
  directories so you can rename files into a different directory and
225
226
  then delete the original directory, all in one edit. However in
@@ -276,16 +277,16 @@ option](#command-default-options) so you are always prompted.
276
277
  After a preview of pending changes is shown a prompt is presented for
277
278
  the user to enter a single key:
278
279
 
279
- `(P)roceed, (E)dit, (R)estart, (Q)uit: [p/e/r/q]?`
280
+ `(P)roceed, (E)dit, (R)estart, (Q)uit[default]: [p/e/r/q]?`
280
281
 
281
282
  where:
282
283
 
283
- |Option |Key|Action |
284
- |--- |---|--- |
285
- |`Proceed`|`p`|Proceed with the path changes. |
286
- |`Edit` |`e`|Edit the path list again, as it is was last edited. |
287
- |`Restart`|`r`|Restart editing the path list again, as it originally began.|
288
- |`Quit` |`q`|Quit immediately without making any changes. |
284
+ |Option |Key|Action |
285
+ |--- |---|--- |
286
+ |`Proceed`|`p`|Proceed with the path changes. |
287
+ |`Edit` |`e`|Edit the path list again, as it is was last edited. |
288
+ |`Restart`|`r`|Restart editing the path list again, as it originally began. |
289
+ |`Quit` |`q`|Quit immediately without making any changes. This is the default if no key is entered.|
289
290
 
290
291
  ## Installation or Upgrade
291
292
 
@@ -330,7 +331,7 @@ are ignored. Type `edir -h` to see all [supported
330
331
  options](#command-line-options).
331
332
 
332
333
  The options `--interactive`, `--all`, `--recurse`, `--quiet`,
333
- `--no-git`, `--trash`, `--suffix`, `--no-color`,
334
+ `--no-git`, `--trash`, `--suffix`, `--no-color`, `--no-invert-color`,
334
335
  `--group-dirs-first/last`, `--trash-program` are sensible candidates to
335
336
  consider setting as default. If you set these then "on-the-fly" negation
336
337
  options `-I`, `-A`, `-R`, `-Q`, `-g`, `-T`, `-Z` are also provided to
@@ -369,8 +370,8 @@ Type `edir -h` to view the usage summary:
369
370
 
370
371
  ```
371
372
  usage: edir [-h] [-i] [-I] [-a] [-A] [-r] [-R] [-q] [-Q] [-G] [-g] [-t]
372
- [-T] [--trash-program TRASH_PROGRAM] [-c] [-d] [-F | -D] [-L]
373
- [-N] [-M] [-S] [-E] [-X] [-Y] [-Z] [--suffix SUFFIX] [-V]
373
+ [-T] [--trash-program TRASH_PROGRAM] [-c] [-C] [-d] [-F | -D]
374
+ [-L] [-N] [-M] [-S] [-E] [-X] [-Y] [-Z] [--suffix SUFFIX] [-V]
374
375
  [args ...]
375
376
 
376
377
  Program to rename, remove, or copy files and directories using your editor.
@@ -381,7 +382,8 @@ positional arguments:
381
382
 
382
383
  options:
383
384
  -h, --help show this help message and exit
384
- -i, --interactive prompt with summary of changes before doing them
385
+ -i, --interactive prompt with summary of changes and allow re-edit
386
+ before proceeding
385
387
  -I, --no-interactive negate the -i/--interactive option
386
388
  -a, --all include all (including hidden) files
387
389
  -A, --no-all negate the -a/--all option
@@ -397,6 +399,8 @@ options:
397
399
  --trash-program TRASH_PROGRAM
398
400
  trash program to use, default="trash-put"
399
401
  -c, --no-color do not color rename/remove/copy messages
402
+ -C, --no-invert-color
403
+ do not invert the color to highlight error messages
400
404
  -d, --dirnames edit given directory names directly, not their
401
405
  contents
402
406
  -F, --files only show/edit files
@@ -411,7 +415,7 @@ options:
411
415
  -Y, --group-dirs-last
412
416
  group directories last (including when sorted)
413
417
  -Z, --no-group-dirs negate the options to group directories
414
- --suffix SUFFIX specify suffix for editor file, default=".sh"
418
+ --suffix SUFFIX specify suffix for temp editor file, default=".sh"
415
419
  -V, --version show edir version
416
420
 
417
421
  Note you can set default starting options in $HOME/.config/edir-
@@ -43,9 +43,9 @@ the following ways:
43
43
  3 ./c
44
44
  4 ./d
45
45
  ```
46
-
46
+
47
47
  But `edir` presents these as:
48
-
48
+
49
49
  ```
50
50
  1 ./a
51
51
  2 ./b
@@ -103,7 +103,8 @@ the following ways:
103
103
  form.
104
104
 
105
105
  11. `edir` adds a [`-i/--interactive` option](#previewing-changes) to
106
- show pending changes and prompt the user before actioning them.
106
+ show pending changes and prompt the user before actioning them. You
107
+ can also choose to re-edit the changes.
107
108
 
108
109
  12. `edir` adds a `-F/--files` option to only show files, or `-D/--dirs`
109
110
  to only show directories.
@@ -198,14 +199,14 @@ the following ways:
198
199
  1. Deleted files are removed and all renamed files and directories
199
200
  are renamed to temporaries. The temporaries are made on the same
200
201
  file-system as the target.
201
-
202
+
202
203
  2. Empty deleted directories are removed.
203
-
204
+
204
205
  3. Renamed temporary files and directories are renamed to their
205
206
  target name. Any required copies are created.
206
-
207
+
207
208
  4. Remaining deleted directories are removed.
208
-
209
+
209
210
  In simple terms, remember that files are processed before
210
211
  directories so you can rename files into a different directory and
211
212
  then delete the original directory, all in one edit. However in
@@ -262,16 +263,16 @@ option](#command-default-options) so you are always prompted.
262
263
  After a preview of pending changes is shown a prompt is presented for
263
264
  the user to enter a single key:
264
265
 
265
- `(P)roceed, (E)dit, (R)estart, (Q)uit: [p/e/r/q]?`
266
+ `(P)roceed, (E)dit, (R)estart, (Q)uit[default]: [p/e/r/q]?`
266
267
 
267
268
  where:
268
269
 
269
- |Option |Key|Action |
270
- |--- |---|--- |
271
- |`Proceed`|`p`|Proceed with the path changes. |
272
- |`Edit` |`e`|Edit the path list again, as it is was last edited. |
273
- |`Restart`|`r`|Restart editing the path list again, as it originally began.|
274
- |`Quit` |`q`|Quit immediately without making any changes. |
270
+ |Option |Key|Action |
271
+ |--- |---|--- |
272
+ |`Proceed`|`p`|Proceed with the path changes. |
273
+ |`Edit` |`e`|Edit the path list again, as it is was last edited. |
274
+ |`Restart`|`r`|Restart editing the path list again, as it originally began. |
275
+ |`Quit` |`q`|Quit immediately without making any changes. This is the default if no key is entered.|
275
276
 
276
277
  ## Installation or Upgrade
277
278
 
@@ -316,7 +317,7 @@ are ignored. Type `edir -h` to see all [supported
316
317
  options](#command-line-options).
317
318
 
318
319
  The options `--interactive`, `--all`, `--recurse`, `--quiet`,
319
- `--no-git`, `--trash`, `--suffix`, `--no-color`,
320
+ `--no-git`, `--trash`, `--suffix`, `--no-color`, `--no-invert-color`,
320
321
  `--group-dirs-first/last`, `--trash-program` are sensible candidates to
321
322
  consider setting as default. If you set these then "on-the-fly" negation
322
323
  options `-I`, `-A`, `-R`, `-Q`, `-g`, `-T`, `-Z` are also provided to
@@ -355,8 +356,8 @@ Type `edir -h` to view the usage summary:
355
356
 
356
357
  ```
357
358
  usage: edir [-h] [-i] [-I] [-a] [-A] [-r] [-R] [-q] [-Q] [-G] [-g] [-t]
358
- [-T] [--trash-program TRASH_PROGRAM] [-c] [-d] [-F | -D] [-L]
359
- [-N] [-M] [-S] [-E] [-X] [-Y] [-Z] [--suffix SUFFIX] [-V]
359
+ [-T] [--trash-program TRASH_PROGRAM] [-c] [-C] [-d] [-F | -D]
360
+ [-L] [-N] [-M] [-S] [-E] [-X] [-Y] [-Z] [--suffix SUFFIX] [-V]
360
361
  [args ...]
361
362
 
362
363
  Program to rename, remove, or copy files and directories using your editor.
@@ -367,7 +368,8 @@ positional arguments:
367
368
 
368
369
  options:
369
370
  -h, --help show this help message and exit
370
- -i, --interactive prompt with summary of changes before doing them
371
+ -i, --interactive prompt with summary of changes and allow re-edit
372
+ before proceeding
371
373
  -I, --no-interactive negate the -i/--interactive option
372
374
  -a, --all include all (including hidden) files
373
375
  -A, --no-all negate the -a/--all option
@@ -383,6 +385,8 @@ options:
383
385
  --trash-program TRASH_PROGRAM
384
386
  trash program to use, default="trash-put"
385
387
  -c, --no-color do not color rename/remove/copy messages
388
+ -C, --no-invert-color
389
+ do not invert the color to highlight error messages
386
390
  -d, --dirnames edit given directory names directly, not their
387
391
  contents
388
392
  -F, --files only show/edit files
@@ -397,7 +401,7 @@ options:
397
401
  -Y, --group-dirs-last
398
402
  group directories last (including when sorted)
399
403
  -Z, --no-group-dirs negate the options to group directories
400
- --suffix SUFFIX specify suffix for editor file, default=".sh"
404
+ --suffix SUFFIX specify suffix for temp editor file, default=".sh"
401
405
  -V, --version show edir version
402
406
 
403
407
  Note you can set default starting options in $HOME/.config/edir-
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: edir
3
- Version: 2.23
3
+ Version: 2.25
4
4
  Summary: Utility to rename, remove, and copy files/dirs using your editor
5
5
  Author-email: Mark Blakeney <mark.blakeney@bullet-systems.net>
6
6
  License: GPLv3
@@ -57,9 +57,9 @@ the following ways:
57
57
  3 ./c
58
58
  4 ./d
59
59
  ```
60
-
60
+
61
61
  But `edir` presents these as:
62
-
62
+
63
63
  ```
64
64
  1 ./a
65
65
  2 ./b
@@ -117,7 +117,8 @@ the following ways:
117
117
  form.
118
118
 
119
119
  11. `edir` adds a [`-i/--interactive` option](#previewing-changes) to
120
- show pending changes and prompt the user before actioning them.
120
+ show pending changes and prompt the user before actioning them. You
121
+ can also choose to re-edit the changes.
121
122
 
122
123
  12. `edir` adds a `-F/--files` option to only show files, or `-D/--dirs`
123
124
  to only show directories.
@@ -212,14 +213,14 @@ the following ways:
212
213
  1. Deleted files are removed and all renamed files and directories
213
214
  are renamed to temporaries. The temporaries are made on the same
214
215
  file-system as the target.
215
-
216
+
216
217
  2. Empty deleted directories are removed.
217
-
218
+
218
219
  3. Renamed temporary files and directories are renamed to their
219
220
  target name. Any required copies are created.
220
-
221
+
221
222
  4. Remaining deleted directories are removed.
222
-
223
+
223
224
  In simple terms, remember that files are processed before
224
225
  directories so you can rename files into a different directory and
225
226
  then delete the original directory, all in one edit. However in
@@ -276,16 +277,16 @@ option](#command-default-options) so you are always prompted.
276
277
  After a preview of pending changes is shown a prompt is presented for
277
278
  the user to enter a single key:
278
279
 
279
- `(P)roceed, (E)dit, (R)estart, (Q)uit: [p/e/r/q]?`
280
+ `(P)roceed, (E)dit, (R)estart, (Q)uit[default]: [p/e/r/q]?`
280
281
 
281
282
  where:
282
283
 
283
- |Option |Key|Action |
284
- |--- |---|--- |
285
- |`Proceed`|`p`|Proceed with the path changes. |
286
- |`Edit` |`e`|Edit the path list again, as it is was last edited. |
287
- |`Restart`|`r`|Restart editing the path list again, as it originally began.|
288
- |`Quit` |`q`|Quit immediately without making any changes. |
284
+ |Option |Key|Action |
285
+ |--- |---|--- |
286
+ |`Proceed`|`p`|Proceed with the path changes. |
287
+ |`Edit` |`e`|Edit the path list again, as it is was last edited. |
288
+ |`Restart`|`r`|Restart editing the path list again, as it originally began. |
289
+ |`Quit` |`q`|Quit immediately without making any changes. This is the default if no key is entered.|
289
290
 
290
291
  ## Installation or Upgrade
291
292
 
@@ -330,7 +331,7 @@ are ignored. Type `edir -h` to see all [supported
330
331
  options](#command-line-options).
331
332
 
332
333
  The options `--interactive`, `--all`, `--recurse`, `--quiet`,
333
- `--no-git`, `--trash`, `--suffix`, `--no-color`,
334
+ `--no-git`, `--trash`, `--suffix`, `--no-color`, `--no-invert-color`,
334
335
  `--group-dirs-first/last`, `--trash-program` are sensible candidates to
335
336
  consider setting as default. If you set these then "on-the-fly" negation
336
337
  options `-I`, `-A`, `-R`, `-Q`, `-g`, `-T`, `-Z` are also provided to
@@ -369,8 +370,8 @@ Type `edir -h` to view the usage summary:
369
370
 
370
371
  ```
371
372
  usage: edir [-h] [-i] [-I] [-a] [-A] [-r] [-R] [-q] [-Q] [-G] [-g] [-t]
372
- [-T] [--trash-program TRASH_PROGRAM] [-c] [-d] [-F | -D] [-L]
373
- [-N] [-M] [-S] [-E] [-X] [-Y] [-Z] [--suffix SUFFIX] [-V]
373
+ [-T] [--trash-program TRASH_PROGRAM] [-c] [-C] [-d] [-F | -D]
374
+ [-L] [-N] [-M] [-S] [-E] [-X] [-Y] [-Z] [--suffix SUFFIX] [-V]
374
375
  [args ...]
375
376
 
376
377
  Program to rename, remove, or copy files and directories using your editor.
@@ -381,7 +382,8 @@ positional arguments:
381
382
 
382
383
  options:
383
384
  -h, --help show this help message and exit
384
- -i, --interactive prompt with summary of changes before doing them
385
+ -i, --interactive prompt with summary of changes and allow re-edit
386
+ before proceeding
385
387
  -I, --no-interactive negate the -i/--interactive option
386
388
  -a, --all include all (including hidden) files
387
389
  -A, --no-all negate the -a/--all option
@@ -397,6 +399,8 @@ options:
397
399
  --trash-program TRASH_PROGRAM
398
400
  trash program to use, default="trash-put"
399
401
  -c, --no-color do not color rename/remove/copy messages
402
+ -C, --no-invert-color
403
+ do not invert the color to highlight error messages
400
404
  -d, --dirnames edit given directory names directly, not their
401
405
  contents
402
406
  -F, --files only show/edit files
@@ -411,7 +415,7 @@ options:
411
415
  -Y, --group-dirs-last
412
416
  group directories last (including when sorted)
413
417
  -Z, --no-group-dirs negate the options to group directories
414
- --suffix SUFFIX specify suffix for editor file, default=".sh"
418
+ --suffix SUFFIX specify suffix for temp editor file, default=".sh"
415
419
  -V, --version show edir version
416
420
 
417
421
  Note you can set default starting options in $HOME/.config/edir-
@@ -0,0 +1 @@
1
+ edir
@@ -17,7 +17,7 @@ import sys
17
17
  import tempfile
18
18
  from collections import OrderedDict
19
19
  from pathlib import Path
20
- from typing import List, Optional, TextIO, Tuple
20
+ from typing import List, Optional, Tuple
21
21
 
22
22
  from platformdirs import user_config_path
23
23
 
@@ -30,18 +30,19 @@ SUFFIX = '.sh'
30
30
  # The temp dir we will use in the dir of each target move
31
31
  TEMPDIR = '.tmp-' + PROG
32
32
 
33
- # Define ANSI escape sequences for colors ..
34
- COLOR_red = '\033[31m'
35
- COLOR_green = '\033[32m'
36
- COLOR_yellow = '\033[33m'
37
- COLOR_reset = '\033[39m'
38
-
33
+ # Define action verbs and ANSI escape sequences for action colors
34
+ # Refer https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
39
35
  ACTIONS = {
40
- 'remove': (COLOR_red, ('Removing', 'Remove', 'Removed')),
41
- 'rename': (COLOR_yellow, ('Renaming', 'Rename', 'Renamed')),
42
- 'copy': (COLOR_green, ('Copying', 'Copy', 'Copied')),
36
+ 'remove': (('\033[31m', '\033[30;41m'), # Red
37
+ ('Removing', 'Remove', 'Removed')),
38
+ 'copy': (('\033[32m', '\033[30;42m'), # Green
39
+ ('Copying', 'Copy', 'Copied')),
40
+ 'rename': (('\033[33m', '\033[30;43m'), # Yellow
41
+ ('Renaming', 'Rename', 'Renamed')),
43
42
  }
44
43
 
44
+ COLOR_reset = '\033[39;49m'
45
+
45
46
  args = argparse.Namespace()
46
47
  gitfiles = set()
47
48
  counts = [0, 0]
@@ -53,12 +54,26 @@ def get_default_editor() -> str:
53
54
  from platform import system
54
55
  return EDITORS.get(system(), EDITORS['default'])
55
56
 
57
+ def editfile(filename: Path) -> None:
58
+ 'Run the editor command'
59
+ # Use explicit user defined editor or choose system default
60
+ editor = os.getenv(EDITOR) or os.getenv('EDITOR') or get_default_editor()
61
+ editcmd = shlex.split(editor) + [str(filename)]
62
+
63
+ # Run the editor ..
64
+ with open('/dev/tty') as tty:
65
+ res = subprocess.run(editcmd, stdin=tty)
66
+
67
+ # Check if editor returned error
68
+ if res.returncode != 0:
69
+ sys.exit(f'ERROR: {editor} returned error {res.returncode}')
70
+
56
71
  def log(action: str, msg: str, error: Optional[str] = None, *,
57
72
  prompt: bool = False) -> None:
58
73
  'Output message with appropriate color'
59
74
  counts[bool(error)] += 1
60
75
 
61
- color, tense = ACTIONS[action]
76
+ colors, tense = ACTIONS[action]
62
77
 
63
78
  if error:
64
79
  out = sys.stderr
@@ -69,10 +84,11 @@ def log(action: str, msg: str, error: Optional[str] = None, *,
69
84
  out = sys.stdout
70
85
  msg = f'{tense[0] if prompt else tense[2]} {msg}'
71
86
 
72
- if args.no_color:
73
- print(msg, file=out)
74
- else:
75
- print(color + msg + COLOR_reset, file=out)
87
+ if not args.no_color:
88
+ msg = colors[bool(error and not args.no_invert_color)] \
89
+ + msg + COLOR_reset
90
+
91
+ print(msg, file=out)
76
92
 
77
93
  def run(cmd: str) -> Tuple[str, str]:
78
94
  'Run given command and return (stdout, stderr) strings'
@@ -121,16 +137,25 @@ def remove(path: Path, git: bool = False, trash: bool = False,
121
137
  except Exception as e:
122
138
  return str(e)
123
139
 
124
- def rename(pathsrc: Path, pathdest: Path, is_git: bool = False) -> None:
140
+ def rename(pathsrc: Path, pathdest: Path, is_git: bool = False) -> str:
125
141
  'Rename given pathsrc to pathdest'
126
142
  if is_git:
127
143
  out, err = run(f'git mv -f "{pathsrc}" "{pathdest}"')
128
144
  if err:
129
- log('rename', '"{pathsrc}"', f'git mv error: {err}')
145
+ err = f'git mv error: {err}'
130
146
  else:
131
- shutil.move(str(pathsrc), str(pathdest))
147
+ try:
148
+ shutil.move(str(pathsrc), str(pathdest))
149
+ except Exception as e:
150
+ # Remove any trailing colon and specific file since we may
151
+ # be renaming a temp file.
152
+ err = str(e).split(':')[0]
153
+ else:
154
+ err = ''
155
+
156
+ return err
132
157
 
133
- def create_options(options: str) -> str:
158
+ def create_prompt(options: str) -> str:
134
159
  'Create a string of options + keys for user to choose from'
135
160
  letters = []
136
161
  words = []
@@ -151,7 +176,11 @@ class Fpath:
151
176
  self.temppath = None
152
177
  self.note = ''
153
178
  self.copies: List[Path] = []
154
- self.is_dir = path.is_dir()
179
+ try:
180
+ self.is_dir = path.is_dir()
181
+ except Exception as e:
182
+ sys.exit(f'ERROR: Can not read {path}: {e}')
183
+
155
184
  self.appdash = '/' if self.is_dir else ''
156
185
  self.diagrepr = str(self.path)
157
186
  self.is_git = self.diagrepr in gitfiles
@@ -163,16 +192,18 @@ class Fpath:
163
192
  self.diagrepr += '/'
164
193
 
165
194
  @staticmethod
166
- def inc_path(path: Path) -> Optional[Path]:
195
+ def inc_path(path: Path) -> Path:
167
196
  'Find next unique file name'
168
197
  # Iterate forever, there can only be a finite number of existing
169
198
  # paths
170
199
  name = path.name
171
200
  for c in itertools.count():
172
201
  if not path.is_symlink() and not path.exists():
173
- return path
202
+ break
174
203
  path = path.with_name(name + ('~' if c <= 0 else f'~{c}'))
175
204
 
205
+ return path
206
+
176
207
  def copy(self, pathdest: Path) -> Optional[str]:
177
208
  'Copy given pathsrc to pathdest'
178
209
  func = shutil.copytree if self.is_dir else shutil.copy2
@@ -181,31 +212,32 @@ class Fpath:
181
212
  except Exception as e:
182
213
  return str(e)
183
214
 
184
- def rename_temp(self) -> None:
215
+ def rename_temp(self) -> Optional[str]:
185
216
  'Move this path to a temp place in advance of final move'
186
217
  if not self.newpath:
187
- return
218
+ return None
188
219
  tempdir = self.newpath.parent / TEMPDIR
189
220
  try:
190
221
  tempdir.mkdir(parents=True, exist_ok=True)
191
- except Exception:
192
- print(f'Create dir for {self.diagrepr} ERROR: '
193
- f'Can not write in {tempdir.parent}', file=sys.stderr)
194
- else:
195
- self.temppath = self.inc_path(tempdir / self.newpath.name)
196
- if self.temppath:
197
- self.tempdirs.add(tempdir)
198
- rename(self.path, self.temppath, self.is_git)
222
+ except Exception as e:
223
+ # Remove any trailing colon and specific file since we are
224
+ # renaming a temp file.
225
+ return str(e).split(':')[0]
199
226
 
200
- def restore_temp(self) -> bool:
227
+ self.tempdirs.add(tempdir)
228
+ temppath = self.inc_path(tempdir / self.newpath.name)
229
+ err = rename(self.path, temppath, self.is_git)
230
+ if not err:
231
+ self.temppath = temppath
232
+
233
+ return err
234
+
235
+ def restore_temp(self) -> Optional[str]:
201
236
  'Restore temp path to final destination'
202
237
  if not self.temppath or not self.newpath:
203
- return False
238
+ return None
204
239
  self.newpath = self.inc_path(self.newpath)
205
- if self.newpath:
206
- rename(self.temppath, self.newpath, self.is_git)
207
- return True
208
- return False
240
+ return rename(self.temppath, self.newpath, self.is_git)
209
241
 
210
242
  def sort_name(self) -> str:
211
243
  'Return name for sort'
@@ -279,76 +311,76 @@ class Fpath:
279
311
  cls.append(path)
280
312
 
281
313
  @classmethod
282
- def writefile(cls, fp: TextIO) -> None:
314
+ def writefile(cls, fpath: Path) -> None:
283
315
  'Write the file for user to edit'
284
- # Ensure consistent width for line numbers
285
- num_width = len(str(len(cls.paths)))
286
- fp.writelines(f'{i:0{num_width}} {p.linerepr}\n'
287
- for i, p in enumerate(cls.paths, 1))
316
+ with fpath.open('w') as fp:
317
+ # Ensure consistent width for line numbers
318
+ num_width = len(str(len(cls.paths)))
319
+ fp.writelines(f'{i:0{num_width}} {p.linerepr}\n'
320
+ for i, p in enumerate(cls.paths, 1))
288
321
 
289
322
  @classmethod
290
- def readfile(cls, fp: TextIO) -> None:
323
+ def readfile(cls, fpath: Path) -> None:
291
324
  'Read the list of files/dirs as edited by user'
292
- for count, line in enumerate(fp, 1):
293
- # Skip blank or commented lines
294
- rawline = line.rstrip('\n\r')
295
- line = rawline.lstrip()
296
- if not line or line[0] == '#':
297
- continue
298
-
299
- try:
300
- n, pathstr = line.split(maxsplit=1)
301
- except Exception:
302
- sys.exit(f'ERROR: line {count} invalid:\n{rawline}')
303
- try:
304
- num = int(n)
305
- except Exception:
306
- sys.exit(f'ERROR: line {count} number {n} invalid:\n{rawline}')
307
-
308
- if num <= 0 or num > len(cls.paths):
309
- sys.exit(f'ERROR: line {count} number {num} '
310
- f'out of range:\n{rawline}')
311
-
312
- path = cls.paths[num - 1]
313
-
314
- if len(pathstr) > 1:
315
- pathstr = pathstr.rstrip('/')
316
-
317
- newpath = Path(pathstr)
318
-
319
- if path.newpath:
320
- if newpath != path.path:
321
- path.copies.append(newpath)
322
- else:
323
- path.newpath = newpath
324
-
325
- @classmethod
326
- def reset(cls) -> None:
327
- 'Reset all the read path changes to null state'
325
+ # Reset all the read path changes to null state
328
326
  for p in cls.paths:
329
327
  p.newpath = None
330
328
  p.copies.clear()
331
329
 
330
+ # Now read file and record changes
331
+ with fpath.open() as fp:
332
+ for count, line in enumerate(fp, 1):
333
+ # Skip blank or commented lines
334
+ rawline = line.rstrip('\n\r')
335
+ line = rawline.lstrip()
336
+ if not line or line[0] == '#':
337
+ continue
338
+
339
+ try:
340
+ n, pathstr = line.split(maxsplit=1)
341
+ except Exception:
342
+ sys.exit(f'ERROR: line {count} invalid:\n{rawline}')
343
+ try:
344
+ num = int(n)
345
+ except Exception:
346
+ sys.exit(f'ERROR: line {count} number {n} invalid:'
347
+ f'\n{rawline}')
348
+
349
+ if num <= 0 or num > len(cls.paths):
350
+ sys.exit(f'ERROR: line {count} number {num} '
351
+ f'out of range:\n{rawline}')
352
+
353
+ path = cls.paths[num - 1]
354
+
355
+ if len(pathstr) > 1:
356
+ pathstr = pathstr.rstrip('/')
357
+
358
+ newpath = Path(pathstr)
359
+
360
+ if path.newpath:
361
+ if newpath != path.path and newpath not in path.copies:
362
+ path.copies.append(newpath)
363
+ else:
364
+ path.newpath = newpath
365
+
332
366
  @classmethod
333
367
  def get_path_changes(cls) -> List:
334
368
  'Get a list of change paths from the user'
369
+ options_prompt = create_prompt('proceed edit restart quit[default]')
335
370
  with tempfile.TemporaryDirectory() as fdir:
336
371
  # Create a temp file for the user to edit then read the lines back
337
372
  fpath = Path(fdir, f'{PROG}{args.suffix}')
338
373
  restart = True
339
374
  while True:
340
- cls.reset()
341
375
  if restart:
342
376
  restart = False
343
- with fpath.open('w') as fp:
344
- cls.writefile(fp)
377
+ cls.writefile(fpath)
345
378
 
346
379
  # Invoke editor on list of paths
347
380
  editfile(fpath)
348
381
 
349
382
  # Read the changed paths
350
- with fpath.open() as fp:
351
- cls.readfile(fp)
383
+ cls.readfile(fpath)
352
384
 
353
385
  # Reduce paths to those that were removed or changed by the user
354
386
  paths = [p for p in cls.paths
@@ -368,10 +400,9 @@ class Fpath:
368
400
  for p in paths:
369
401
  p.log_pending_changes()
370
402
 
371
- options = create_options('proceed edit restart quit')
372
403
  while True:
373
404
  try:
374
- ans = input(options).lower()
405
+ ans = input(options_prompt).strip().lower()
375
406
  except KeyboardInterrupt:
376
407
  print()
377
408
  return []
@@ -383,25 +414,11 @@ class Fpath:
383
414
  elif ans == 'r':
384
415
  restart = True
385
416
  break
386
- elif ans == 'q':
417
+ elif not ans or ans == 'q':
387
418
  return []
388
419
  else:
389
420
  print('Invalid answer.')
390
421
 
391
- def editfile(filename: Path) -> None:
392
- 'Run the editor command'
393
- # Use explicit user defined editor or choose system default
394
- editor = os.getenv(EDITOR) or os.getenv('EDITOR') or get_default_editor()
395
- editcmd = shlex.split(editor) + [str(filename)]
396
-
397
- # Run the editor ..
398
- with open('/dev/tty') as tty:
399
- res = subprocess.run(editcmd, stdin=tty)
400
-
401
- # Check if editor returned error
402
- if res.returncode != 0:
403
- sys.exit(f'ERROR: {editor} returned error {res.returncode}')
404
-
405
422
  def main() -> int:
406
423
  'Main code'
407
424
  global args
@@ -413,7 +430,8 @@ def main() -> int:
413
430
  'and their shortforms) allow you to temporarily override your '
414
431
  'defaults.')
415
432
  opt.add_argument('-i', '--interactive', action='store_true',
416
- help='prompt with summary of changes before doing them')
433
+ help='prompt with summary of changes and allow re-edit before '
434
+ 'proceeding')
417
435
  opt.add_argument('-I', '--no-interactive', dest='interactive',
418
436
  action='store_false',
419
437
  help='negate the -i/--interactive option')
@@ -444,6 +462,8 @@ def main() -> int:
444
462
  help='trash program to use, default="%(default)s"')
445
463
  opt.add_argument('-c', '--no-color', action='store_true',
446
464
  help='do not color rename/remove/copy messages')
465
+ opt.add_argument('-C', '--no-invert-color', action='store_true',
466
+ help='do not invert the color to highlight error messages')
447
467
  opt.add_argument('-d', '--dirnames', action='store_true',
448
468
  help='edit given directory names directly, not their contents')
449
469
  grp = opt.add_mutually_exclusive_group()
@@ -474,7 +494,7 @@ def main() -> int:
474
494
  action='store_const', const=-1,
475
495
  help='negate the options to group directories')
476
496
  opt.add_argument('--suffix', default=SUFFIX,
477
- help='specify suffix for editor file, default="%(default)s"')
497
+ help='specify suffix for temp editor file, default="%(default)s"')
478
498
  opt.add_argument('-V', '--version', action='store_true',
479
499
  help=f'show {PROG} version')
480
500
  opt.add_argument('args', nargs='*',
@@ -566,7 +586,10 @@ def main() -> int:
566
586
  for p in paths:
567
587
  if p.newpath:
568
588
  if p.newpath != p.path:
569
- p.rename_temp()
589
+ err = p.rename_temp()
590
+ if err:
591
+ log('rename',
592
+ f'"{p.diagrepr}" to "{p.newpath}{p.appdash}"', err)
570
593
  elif not p.is_dir:
571
594
  err = remove(p.path, p.is_git, args.trash)
572
595
  log('remove', f'"{p.diagrepr}"', err)
@@ -582,8 +605,9 @@ def main() -> int:
582
605
  # Pass 3. Rename all temp files and dirs to final target, and make
583
606
  # copies.
584
607
  for p in paths:
585
- if p.restore_temp():
586
- log('rename', f'"{p.diagrepr}" to "{p.newpath}{p.appdash}"')
608
+ err = p.restore_temp()
609
+ if err is not None:
610
+ log('rename', f'"{p.diagrepr}" to "{p.newpath}{p.appdash}"', err)
587
611
 
588
612
  for c in p.copies:
589
613
  err = p.copy(c)
@@ -1 +0,0 @@
1
- tmp
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes