Skip to content

Commit

Permalink
Merge pull request #3 from niyue/feature/custom-style
Browse files Browse the repository at this point in the history
Allow specifying custom styles.
  • Loading branch information
niyue committed Dec 1, 2022
2 parents 35aeb01 + b41ce5c commit eae1ac2
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
pcvis/__pycache__
tests/__pycache__
.vscode
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# 0.2.0 - 2022-12-01
* Add python 3.6 support
* Customizable output styles
# 0.1.0 - 2022-12-01
* Initial release, support parsing and visualizing pcstat's per page status output
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ Visualize a given file's page cache status like below. In the visualized image,
pcstat -json -pps /path/to/my_file | pcvis
```

<img width="1916" alt="image" src="https://user-images.githubusercontent.com/27754/204568345-ecf236d3-3151-4f3e-8c2b-cf30f9833091.png">
## sample outputs
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█░░░░░░░░░░░░░░░░░░░░█░░░░░░░░░█░░░░█░█░██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
Via this visualization, you can easily spot that:
1. this file's header and footer are accessed and loaded in page cache
2. this file is accessed in a random access manner, and you may even vaguely check if the random access is a binary search, etc

## arguments
* `-s` or `--style`: there are over 20 different rendering styles to choose from, you can specify a custom style by passing an integer to this argument. The default style is `0`, e.g. `pcvis -s 24`
🌕🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌕🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌕🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌕🌑🌑🌑🌑🌑🌑🌑🌑🌑🌕🌑🌑🌑🌑🌕🌑🌕🌑🌕🌕🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌑🌕


# notes
Expand Down
54 changes: 49 additions & 5 deletions pcvis/pcvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import sys
import json
from sys import stdin
import argparse

import importlib.metadata


BAR_STYLES = [
"▁▇",
Expand Down Expand Up @@ -35,6 +39,40 @@
]


def get_meta():
version = "NA"
summmary = "NA"
try:
package_metadada = importlib.metadata.metadata("pcvis")
# info from pyproject.toml's `version` and `description`
version = package_metadada.get("Version")
summary = package_metadada.get("Summary")
except:
pass
return version, summary


def _cli_parser():
parser = argparse.ArgumentParser(prog="pcvis")
parser.add_argument(
"-s",
"--style",
default=0,
type=int,
help="the visualization style for rendering",
)

version, summary = get_meta()

parser.add_argument(
"--version",
action="version",
version=f"%(prog)s {version} [{summary}]",
help="show version number",
)
return parser


def read_pps():
lines = ""
for line in stdin:
Expand All @@ -50,17 +88,23 @@ def parse_pps(pps_json, style=0):
return pps_string


def parse_sys_args(sys_args):
parser = _cli_parser()
args = parser.parse_args(sys_args)
return vars(args)


def main():
try:
style = int(sys.argv[1]) if len(sys.argv) > 1 else 0
except:
style = 0
args = parse_sys_args(sys.argv[1:])
style = args["style"]
pps_json = read_pps()
try:
pps_string = parse_pps(pps_json, style % len(BAR_STYLES))
print(pps_string)
except Exception as e:
print(f"[failed to parse per page status from pcstat] pps_json='{pps_json}' error='{str(e)}'")
print(
f"[failed to parse per page status from pcstat] pps_json='{pps_json}' error='{str(e)}'"
)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "pcvis"
version = "0.1.0"
description = ""
version = "0.2.0"
description = "2022-12-01"
authors = ["Yue Ni <niyue.com@gmail.com>"]
readme = "README.md"

Expand Down
22 changes: 22 additions & 0 deletions tests/test_parse_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from pcvis.pcvis import parse_sys_args
from unittest import TestCase


def assert_dict_equal(d1, d2):
TestCase().assertDictEqual(d1, d2)


def test_empty_args_should_use_default_style():
args = parse_sys_args([])
assert_dict_equal(
args,
{"style": 0},
)


def test_custom_style():
args = parse_sys_args(["--style", "1"])
assert_dict_equal(
args,
{"style": 1},
)
File renamed without changes.

0 comments on commit eae1ac2

Please sign in to comment.