Add more polish to fragment generation commands.

1. Use heading numbers instead of line numbers in `fragment -h`.
2. Open `fr -h` and `fr -l` output in `less`, if it doesn't fit in the
terminal.
3. A bit of refactoring, for clarity.
This commit is contained in:
nervuri 2021-12-25 11:54:24 +00:00
parent 5ce221ae8b
commit 71ff278c59
1 changed files with 71 additions and 18 deletions

89
av98.py
View File

@ -1484,36 +1484,86 @@ Select Nth occurence of text: -tN."""
else:
fragment_input = ''
if args[0].startswith('-h'): # select heading
# Select heading.
if args[0].startswith('-h'):
if not fragment_input:
# Display all headings and their line numbers.
line_number = 0
# Display all headings, numbered.
heading_number = 0
numbered_headings = ''
with open(self.tmp_filename, 'r') as raw_gemtext_file:
for line in raw_gemtext_file.read().splitlines():
line_number += 1
if line.startswith('#'):
print(str(line_number).rjust(4) + ' ' + line)
heading_number += 1
numbered_headings += str(heading_number)\
.rjust(4) + ' ' + line + '\n'
# Remove trailing line break.
numbered_headings = numbered_headings.rstrip()
print(numbered_headings)
# If output does not fit in terminal, open it in Less.
if shutil.get_terminal_size()[1] < heading_number:
process1 = subprocess.Popen(('echo', numbered_headings),
stdout=subprocess.PIPE)
subprocess.call('less', stdin=process1.stdout)
return
elif fragment_input.isdigit():
# Heading number provided.
# Generate fragment for heading N.
occurrence = 0
selected_heading_number = int(fragment_input)
with open(self.tmp_filename, 'r') as raw_gemtext_file:
raw_gemtext = raw_gemtext_file.read()
# 1st pass - get line text
match_found = False
heading_number = 0
for line in raw_gemtext.splitlines():
if line.startswith('#'):
heading_number += 1
if heading_number == selected_heading_number:
match_found = True
fragment_input = line
break
if not match_found:
print("Heading not found. This page has "\
+ str(heading_number) + " headings.")
return
# 2nd pass - get occurrence number, if more than 1 match.
if raw_gemtext.count(fragment_input) > 1:
heading_number = 0
for line in raw_gemtext.splitlines():
occurrence += line.count(fragment_input)
if line.startswith('#'):
heading_number += 1
if heading_number == selected_heading_number:
break
elif not fragment_input.isdigit():
print("Please provide heading line number (run `fr -h` to list them).")
return
elif args[0].startswith('-l'): # select line
# Select line.
elif args[0].startswith('-l'):
if not fragment_input:
# Display file with line numbers.
cmd_str = self._get_handler_cmd(self.mime)
cmd_str = cmd_str % self.tmp_filename
subprocess.call("%s | less -rGN" % cmd_str, shell=True)
line_number = 0
numbered_lines = ''
with open(self.tmp_filename, 'r') as raw_gemtext_file:
for line in raw_gemtext_file.read().splitlines():
line_number += 1
numbered_lines += str(line_number)\
.rjust(4) + ' ' + line + '\n'
# Remove trailing line break.
numbered_lines = numbered_lines.rstrip()
print(numbered_lines)
# If output does not fit in terminal, open it in Less.
if shutil.get_terminal_size()[1] < line_number:
cmd_str = self._get_handler_cmd(self.mime)
cmd_str = cmd_str % self.tmp_filename
subprocess.call("%s | less -N" % cmd_str, shell=True)
return
elif not fragment_input.isdigit():
print("Please provide line number (run `fr -l` to list them).")
return
if args[0][0:2] in ('-h', '-l'):
if fragment_input.isdigit():
elif fragment_input.isdigit():
# Line number provided.
# Generate fragment for line N.
occurrence = 0
selected_line = int(fragment_input)
selected_line_number = int(fragment_input)
with open(self.tmp_filename, 'r') as raw_gemtext_file:
raw_gemtext = raw_gemtext_file.read()
# 1st pass - get line text
@ -1521,7 +1571,7 @@ Select Nth occurence of text: -tN."""
line_number = 0
for line in raw_gemtext.splitlines():
line_number += 1
if line_number == selected_line:
if line_number == selected_line_number:
match_found = True
fragment_input = line
break
@ -1538,8 +1588,11 @@ Select Nth occurence of text: -tN."""
for line in raw_gemtext.splitlines():
line_number += 1
occurrence += line.count(fragment_input)
if line_number == selected_line:
if line_number == selected_line_number:
break
elif not fragment_input.isdigit():
print("Please provide line number (run `fr -l` to list them).")
return
if not fragment_input:
print(err)