Skip to content

Commit

Permalink
0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
a committed Nov 6, 2020
1 parent 36f9ca4 commit 4b93247
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ hist-txt # Text format shortcut

Arguments:
```
usage: hist-format [-h] [-f FORMAT] [-c COUNT] [-l] [-m] [--lines]
usage: hist-format [-h] [-f FORMAT] [-c COMMANDS_COUNT] [-l] [-H [OUTPUT_HEAD_COUNT]] [-T [OUTPUT_TAIL_COUNT]]
[-m] [--lines]
Format xonsh history to post it to Github or another page.
optional arguments:
-h, --help show this help message and exit
-f FORMAT, --format FORMAT
Format: md, txt.
-c COUNT, --count COUNT
-c COMMANDS_COUNT, --commands-count COMMANDS_COUNT
Count of commands
-l, --show-commands-list
Show commands in distinct section.
-l, --commands-list Show commands in distinct section.
-H [OUTPUT_HEAD_COUNT], --output-head-count [OUTPUT_HEAD_COUNT]
Count of lines from output head to show.
-T [OUTPUT_TAIL_COUNT], --output-tail-count [OUTPUT_TAIL_COUNT]
Count of lines from output tail to show.
-m, --min Make block minimized i.e. by adding <details> tag in Markdown.
--lines Add additional lines before and after.
```

Note! The `clear` command is used as marker of the beginning of commands list. If you run commands 1, 2, 3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setuptools.setup(
name='xontrib-hist-format',
version='0.0.7',
version='0.0.8',
license='MIT',
author='anki-code',
author_email='no@no.no',
Expand Down
42 changes: 36 additions & 6 deletions xontrib/hist_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ def _hist_format(args):

argp = argparse.ArgumentParser(prog='hist-format', description="Format xonsh history to post it to Github or another page.")
argp.add_argument('-f', '--format', default='md', help="Format: md, txt.")
argp.add_argument('-c', '--count', default=10, help="Count of commands")
argp.add_argument('-l', '--show-commands-list', action='store_true', help="Show commands in distinct section.")
argp.add_argument('-c', '--commands-count', default=10, help="Count of commands")
argp.add_argument('-l', '--commands-list', action='store_true', help="Show commands in distinct section.")
argp.add_argument('-H', '--output-head-count', nargs='?', const=10, help="Count of lines from output head to show.")
argp.add_argument('-T', '--output-tail-count', nargs='?', const=10, help="Count of lines from output tail to show.")
argp.add_argument('-m', '--min', action='store_true', help="Make block minimized i.e. by adding <details> tag in Markdown.")
argp.add_argument('--lines', action='store_true', help="Add additional lines before and after.")
opt = argp.parse_args(args)

opt.count = int(opt.count)
opt.commands_count = abs(int(opt.commands_count))
if opt.output_head:
opt.output_head = abs(int(opt.output_head))
if opt.output_tail:
opt.output_tail = abs(int(opt.output_tail))

formats = {
'md': {'begin': '```python', 'end': '```', 'comment': '# '},
Expand All @@ -30,7 +36,7 @@ def _hist_format(args):
h = __xonsh__.history[i]
if 'hist-format' in h.cmd or 'hist-md' in h.cmd or 'hist-txt' in h.cmd:
continue
if len(cmds_idx) >= opt.count or h.cmd.rstrip() == 'clear':
if len(cmds_idx) >= opt.commands_count or h.cmd.rstrip() == 'clear':
break
cmds_idx.append(i)

Expand All @@ -54,13 +60,37 @@ def _hist_format(args):
print(h.cmd.rstrip(), end='')
if h.out:
print()
print('\n'.join([format['comment'] + l for l in str(h.out).rstrip().split('\n')]))
output_lines = [format['comment'] + l for l in str(h.out).rstrip().split('\n')]

if opt.output_head and opt.output_tail:
if (opt.output_head + opt.output_tail) < len(output_lines):
print('\n'.join(output_lines[:opt.output_head]))
print(format['comment'])
print(format['comment'] + f'---- Skipped {len(output_lines) - opt.output_head - opt.output_tail} lines of output ----')
print(format['comment'])
print('\n'.join(output_lines[-opt.output_tail:]))
else:
print('\n'.join(output_lines))

elif opt.output_head:
print('\n'.join(output_lines[:opt.output_head]))
skipped = len(output_lines) - opt.output_head
if skipped > 0:
print(format['comment'] + f'---- Skipped next {skipped} lines of output ----')
elif opt.output_tail:
skipped = len(output_lines) - opt.output_tail
if skipped > 0:
print(format['comment'] + f'---- Skipped prev {skipped} lines of output ----')
print('\n'.join(output_lines[-opt.output_tail:]))
else:
print('\n'.join(output_lines))

print()
cmds.append(h.cmd.rstrip())
print(format['comment'] + 'Prepared by xontrib-hist-format')
print(format['end'])

if opt.show_commands_list:
if opt.commands_list:
if opt.format == 'md':
print('\nCommands:\n')
else:
Expand Down

0 comments on commit 4b93247

Please sign in to comment.