Skip to content

Commit

Permalink
Merge pull request #10 from bexxmodd/master_refactor
Browse files Browse the repository at this point in the history
Master refactor
  • Loading branch information
bexxmodd authored Sep 27, 2020
2 parents 255be27 + 0766163 commit 2b64c90
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 131 deletions.
109 changes: 47 additions & 62 deletions src/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class Options:

_graph_color = fg("white")
_header_color = fg("red")
_pre_graph_color = fg("white")
_post_graph_color = fg("white")
_footer_color = fg("white")
_header_style = attr('bold')
_text_color = fg("white")
_fsymbol = "█"
_msymbol = "▒"
_esymbol = "░"
Expand All @@ -24,7 +23,7 @@ def graph_color(self):
return self._graph_color

@graph_color.setter
def graph_color(self, color):
def graph_color(self, color: str):
self._graph_color = fg(color)

@property
Expand All @@ -33,43 +32,34 @@ def header_color(self):
return self._header_color

@header_color.setter
def header_color(self, color):
def header_color(self, color: str):
self._header_color = fg(color)

@property
def pre_graph_color(self):
""" pre_graph_color """
return self._pre_graph_color

@pre_graph_color.setter
def pre_graph_color(self, color):
self._pre_graph_color = fg(color)
def header_style(self):
""" header style """
return self._header_style
@header_style.setter
def header_style(self, style: str):
self._header_style = attr(style)

@property
def post_graph_color(self):
""" post_graph_color """
return self._post_graph_color
def text_color(self):
""" text color """
return self._text_color

@post_graph_color.setter
def post_graph_color(self, color):
self._post_graph_color = fg(color)

@property
def footer_color(self):
""" footer_color """
return self._footer_color

@footer_color.setter
def footer_color(self, color):
self._footer_color = fg(color)
@text_color.setter
def text_color(self, color: str):
self._text_color = fg(color)

@property
def symbol(self):
""" graph symbols to use """
return [self._fsymbol, self._msymbol, self.esymbol]
return (self._fsymbol, self._msymbol, self.esymbol)

@symbol.setter
def symbol(self, symbol):
def symbol(self, symbol: str):
if symbol:
self._fsymbol = symbol
self._msymbol = ">"
Expand All @@ -81,7 +71,7 @@ def symbol(self, symbol):

@property
def fsymbol(self):
""" The final symbol """
""" The full symbol """
return self._fsymbol

@property
Expand All @@ -91,13 +81,12 @@ def esymbol(self):

@property
def msymbol(self):
""" The used symbol """
""" The middle symbol """
return self._msymbol


class Chart:
"""Abstract base object for charts"""
options: Options = None

def __init__(self, options: Options = None):
if options is None:
Expand All @@ -106,64 +95,60 @@ def __init__(self, options: Options = None):
self.options = options


class BarChart(Chart):
"""Draws chart with user selected color and symbol"""
class HorizontalBarChart(Chart):
"""
Draws horizontal chart with user selected color and symbol
"""

def chart(
self,
title: str,
pre_graph_text: str,
post_graph_text: str,
footer: str,
maximum: float,
current: float,
):
print(stylize(title, self.options.header_color))
def chart(self,
title: str,
pre_graph_text: str,
post_graph_text: str,
footer: str,
maximum: float,
current: float) -> None:
print(
stylize(title, self.options.header_color + self.options.header_style)
)

if pre_graph_text:
print(stylize(pre_graph_text, self.options.pre_graph_color))
print(stylize(pre_graph_text, self.options.text_color))

print(
"[%s]"
"%s"
% stylize(
self.draw_horizontal_bar(maximum, current), self.options.graph_color
self.draw_horizontal_bar(maximum, current),
self.options.graph_color
),
end=" ",
)
if post_graph_text:
print(stylize(post_graph_text, self.options.post_graph_color))
print(stylize(post_graph_text, self.options.text_color))
else:
print()

if footer:
print(stylize(footer, self.options.footer_color))
print(stylize(footer, self.options.text_color))

def draw_horizontal_bar(self, maximum: int, current: int) -> str:
"""Draw a horizontal bar chart
Return:
drawn horizontal bar chart
"""
"""Draw a horizontal bar chart"""
textBar = ""
usage = int((current / maximum) * 36)
usage = int((current / maximum) * 38)
for i in range(1, usage + 1):
textBar += self.options.fsymbol
textBar += self.options.msymbol
for i in range(1, 37 - usage):
for i in range(1, 39 - usage):
textBar += self.options.esymbol
# check if the user set up graph color
if "█" not in self.options.fsymbol:
return f"[{textBar}]"
return textBar


class verticalBarChar(Chart):
def draw_vertical_bar(self, capacity: int, used: int) -> str:
"""Draw a vertical bar chart
class VerticalBarChart(Chart):

Returns:
drawn vertical bar chart
"""
def draw_vertical_bar(self, capacity: int, used: int) -> str:
"""Draw a vertical bar chart"""
textBar = "\n"
n = (used / capacity) * 8
# If the usage is below 1% print empty chart
Expand Down
46 changes: 36 additions & 10 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
from charts import Options
from colored import fg, attr, stylize

# Command line arguments for cli
# Command line arguments and options for cli
@click.command()
@click.argument("chart", nargs=1, default="barh", metavar="[CHART_TYPE]")
@click.argument(
"chart",
nargs=1,
default="barh",
metavar="[CHART_TYPE]"
)
@click.option(
"-P",
"--path",
Expand All @@ -23,11 +28,14 @@
multiple=True,
help="Select partition you want to exclude",
)
@click.option("--every", is_flag=True, help="Display information for all the disks")
@click.option(
"--every",
is_flag=True,
help="Display information for all the disks"
)
@click.option(
"--details",
is_flag=True,
default=False,
help="Display additinal details like fstype and mountpoint",
)
@click.option(
Expand Down Expand Up @@ -63,12 +71,30 @@
help="Change the color of the bar graph",
)
@click.option(
"-m", "--mark", default=None, help="Choose the symbols used for the graph"
"-m",
"--mark",
default=None,
help="Choose the symbols used for the graph"
)
def cli(chart, path, every, details, exclude, header, style, text, graph, mark):
""" Displays charts in the terminal, graphically """
chart = "barh"
def cli(chart, path, every, details, exclude,
header, style, text, graph, mark) -> None:
"""
** Displays Disk Usage in the terminal, graphically. **
Customize visuals by setting colors and attributes.
Select one of the available graph types:
barv : Vertical Bars
*barh : Horizontal Bars (buggy)
*pie : Pie Chart (coming soon)
COLORS: light_red, red, dark_red, dark_blue, blue,
cyan, yellow, green, pink, white, black, purple,
neon, grey, beige, orange, magenta, peach.
ATTRIBUTES: bold, dim, underlined, blink, reverse.
"""
chart = "barh"
options: Options = Options()
if mark:
options.symbol = mark
Expand All @@ -78,17 +104,17 @@ def cli(chart, path, every, details, exclude, header, style, text, graph, mark):
options.text_color = text
if graph:
options.graph_color = graph
if style:
options.header_style = style

chart = chart
style = style
exclude_list = list(exclude)

renderer = None
if chart == "barh":
renderer = DiskUsage(
path=path, exclude=exclude_list, details=details, every=every
)

renderer.print_charts(options)


Expand Down
Loading

0 comments on commit 2b64c90

Please sign in to comment.