Skip to content

Instantly share code, notes, and snippets.

@ulidtko
Created March 15, 2025 00:51
Show Gist options
  • Save ulidtko/624ff0a503f94e76a7f5e79e28265979 to your computer and use it in GitHub Desktop.
Save ulidtko/624ff0a503f94e76a7f5e79e28265979 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Quote stdin line containing the Nth character, in absolute offset,
counting each newline as a character too.
Inspired by https://stackoverflow.com/a/79399803/531179
In the name José João in this file, accents are on offsets +210 and on +214.
"""
from argparse import ArgumentParser
import sys
if __name__ == "__main__":
argP = ArgumentParser()
argP.add_argument('ABSOLUTE_OFFSET', type=int, help="which character")
argP.add_argument('FILE', nargs='?', type=lambda fp: open(fp), default=sys.stdin, help="optional file path")
opts = argP.parse_args()
#print(opts)
n = opts.ABSOLUTE_OFFSET
lineno = 1
for line in opts.FILE.readlines():
if len(line) > n:
# found, print out
print(f"{lineno}: {line}")
charp = len(str(lineno)) + 2 + n
caret = '-' * charp + '^'
print(caret)
break
lineno += 1
n -= len(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment