diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index d8f35c6..80d5271 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -1003,20 +1003,40 @@ def cmd_hosts(options, persona, objstr, cache): printhost(i) +def genstartstop(cnt, idx): + idx = min(idx, cnt - 10) + idx = max(0, idx) + + maxstart = max(0, cnt - 20) + + startidx = min(max(0, idx - 10), maxstart) + endidx = min(cnt, startidx + 20) + + return startidx, endidx + def getnextfile(files, idx): + # original data incase of abort + origfiles = files origidx = idx - maxstart = max(0, len(files) - 10) - idx = min(maxstart, idx) + # current selection (last file or dir) + curselidx = origidx + + currentcnt = None while True: - startidx = min(max(10, idx - 10), maxstart) - _debprint(len(files), startidx) - subset = files[startidx:min(len(files), idx + 10)] - selfile = -1 if origidx < startidx or origidx >= startidx + \ - 20 else origidx - startidx + if len(files) != currentcnt: + currentcnt = len(files) + maxidx = max(0, currentcnt - 10) + idx = min(maxidx, idx) + + startidx, endidx = genstartstop(currentcnt, idx) + subset = files[startidx:endidx] + selfile = -1 if curselidx < startidx or curselidx >= startidx + \ + 20 else curselidx - startidx + print('%2d) Parent directory' % 0) for i, f in enumerate(subset): - print('%2d)%1s%s' % (i + 1, '*' if i == selfile else '', repr(str(f)))) + print('%2d)%1s%s%s' % (i + 1, '*' if i == selfile else '', repr(str(f)), '/' if f.is_dir() else '')) print('P) Previous page') print('N) Next page') @@ -1025,13 +1045,13 @@ def getnextfile(files, idx): inp = sys.stdin.readline().strip() if inp.lower() == 'p': - idx = max(10, idx - 19) + idx = max(0, idx - 19) continue if inp.lower() == 'n': - idx = min(maxstart, idx + 19) + idx = min(currentcnt - 1, idx + 19) continue if inp.lower() == 'a': - return origidx + return origfiles, origidx try: inp = int(inp) @@ -1039,11 +1059,23 @@ def getnextfile(files, idx): print('Invalid selection.') continue + if inp == 0: + curdir = files[idx].parent + files = sorted(files[idx].parent.parent.iterdir()) + idx = curselidx = files.index(curdir) + continue if inp < 1 or inp > len(subset): print('Invalid selection.') continue - return startidx - 1 + inp + newidx = startidx - 1 + inp + + if files[newidx].is_dir(): + files = sorted(files[newidx].iterdir()) + curselidx = idx = 0 + continue + + return files, newidx def checkforfile(objstr, curfile, ask=False): try: @@ -1144,7 +1176,7 @@ def cmd_interactive(options, persona, objstr, cache): idx = min(len(files) - 1, idx + 1) continue if inp == '3': - idx = getnextfile(files, idx) + files, idx = getnextfile(files, idx) continue if inp == '4': files = sorted(curfile.parent.iterdir()) @@ -1526,6 +1558,18 @@ class _TestCases(unittest.TestCase): os.chdir(self.oldcwd) + def test_genstartstop(self): + self.assertEqual(genstartstop(5, 0), (0, 5)) + self.assertEqual(genstartstop(5, 1), (0, 5)) + self.assertEqual(genstartstop(5, 4), (0, 5)) + self.assertEqual(genstartstop(25, 1), (0, 20)) + self.assertEqual(genstartstop(25, 20), (5, 25)) + self.assertEqual(genstartstop(25, 24), (5, 25)) + self.assertEqual(genstartstop(124, 1), (0, 20)) + self.assertEqual(genstartstop(124, 53), (43, 63)) + self.assertEqual(genstartstop(124, 120), (104, 124)) + self.assertEqual(genstartstop(124, 124), (104, 124)) + def test_fileobject(self): os.chdir(self.tempdir)