Skip to content

shellhistory ¤

Shell History package.

Visualize your usage of Bash/Zsh through a web app thanks to Flask and Highcharts!

Functions:

  • get_parser –

    Return the CLI argument parser.

  • import_legacy –

    Import a legacy text history file into the database.

  • location –

    Print the path of the shell script to source.

  • main –

    Run the main program.

  • migrate –

    Convert a legacy single-table database to the sessions + history split.

  • web –

    Run the web application.

get_parser ¤

get_parser() -> ArgumentParser

Return the CLI argument parser.

Returns:

Source code in src/shellhistory/_internal/cli.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def get_parser() -> argparse.ArgumentParser:
    """Return the CLI argument parser.

    Returns:
        An argparse parser.
    """
    parser = argparse.ArgumentParser(
        prog="shellhistory",
        epilog="subcommands: secrets (scan command history for credentials)",
    )
    parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug._get_version()}")
    parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.")

    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--location",
        dest="location",
        action="store_true",
        help="Print the path of the shell script to source.",
    )
    group.add_argument("--web", dest="web", action="store_true", help="Run the web application.")
    group.add_argument(
        "--import",
        dest="import_file",
        action="store_true",
        help="Import a legacy text history file into the database.",
    )
    group.add_argument(
        "--migrate",
        dest="migrate",
        action="store_true",
        help="Convert a legacy single-table database to the current schema.",
    )
    parser.add_argument("file", nargs="?", help="History file to import (defaults to $SHELLHISTORY_FILE).")

    return parser

import_legacy ¤

import_legacy(path: str | None = None) -> int

Import a legacy text history file into the database.

Parameters:

  • path ¤

    (str | None, default: None ) –

    The file to import. Defaults to $SHELLHISTORY_FILE.

Returns:

  • int –

    An exit code.

Source code in src/shellhistory/_internal/cli.py
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def import_legacy(path: str | None = None) -> int:
    """Import a legacy text history file into the database.

    Parameters:
        path: The file to import. Defaults to `$SHELLHISTORY_FILE`.

    Returns:
        An exit code.
    """
    from shellhistory._internal import _migrations as migrations  # noqa: PLC0415

    report = migrations.import_file(path) if path else migrations.import_history()
    print(f"imported {report.inserted} records ({report.duplicates} already present)")
    return 0

location ¤

location() -> int

Print the path of the shell script to source.

Returns:

  • int –

    An exit code.

Source code in src/shellhistory/_internal/cli.py
201
202
203
204
205
206
207
208
def location() -> int:
    """Print the path of the shell script to source.

    Returns:
        An exit code.
    """
    print(Path(__file__).parent.parent / "shellhistory.sh")
    return 0

main ¤

main(args: list[str] | None = None) -> int

Run the main program.

This function is executed when you type shellhistory or python -m shellhistory.

Parameters:

  • args ¤

    (list[str] | None, default: None ) –

    Arguments passed from the command line.

Returns:

  • int –

    An exit code.

Source code in src/shellhistory/_internal/cli.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def main(args: list[str] | None = None) -> int:
    """Run the main program.

    This function is executed when you type `shellhistory` or `python -m shellhistory`.

    Parameters:
        args: Arguments passed from the command line.

    Returns:
        An exit code.
    """
    args = sys.argv[1:] if args is None else args
    if args and args[0] == "secrets":
        return _secrets_command(args[1:])

    parser = get_parser()
    opts = parser.parse_args(args=args)

    if opts.location:
        return location()
    if opts.web:
        return web()
    if opts.migrate:
        return migrate()
    if opts.import_file:
        return import_legacy(opts.file)

    parser.print_help()
    return 0

migrate ¤

migrate() -> int

Convert a legacy single-table database to the sessions + history split.

Returns:

  • int –

    An exit code.

Source code in src/shellhistory/_internal/cli.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def migrate() -> int:
    """Convert a legacy single-table database to the sessions + history split.

    Returns:
        An exit code.
    """
    from shellhistory._internal import _db as db  # noqa: PLC0415
    from shellhistory._internal import _migrations as migrations  # noqa: PLC0415

    if not db.is_legacy_schema():
        print(f"{db.DB_PATH} is already on the current schema, nothing to do.")
        return 0
    result = migrations.migrate_schema()
    print(
        f"migrated {result['rows']} records into {result['sessions']} sessions\noriginal kept at {result['backup']}",
    )
    return 0

web ¤

web() -> int

Run the web application.

Returns:

  • int –

    An exit code.

Source code in src/shellhistory/_internal/cli.py
211
212
213
214
215
216
217
218
219
220
221
222
def web() -> int:
    """Run the web application.

    Returns:
        An exit code.
    """
    # Imported here, not at module level: `shellhistory-location` runs from every
    # shell's startup file, and it must not pay for importing Flask.
    from shellhistory._internal._app import app  # noqa: PLC0415

    app.run()
    return 0