diff --git a/kglobe.py b/kglobe.py index 9644ab8..bb76c5f 100644 --- a/kglobe.py +++ b/kglobe.py @@ -15,10 +15,20 @@ from terminalplotter import TerminalPlotter # TODO: Color arches based on latency # TODO: Text info (num hops etc.) +# TODO: Mouse support # DONE: Interactive globe (spin w/ keys) # DONE: Image spacing # DONE: Async rendering? +EXAMPLE_ROUTE = [ + (47.996, 7.849), # freiburg + (50.110, 8.682), # ffm + (52.231, 21.006), # warsaw + (12.988, 77.622), # bangalore + (22.350, 114.184), # hong kong + (-33.869, 151.208) # sydney +] + # Convert lat/lon to Cartesian coordinates def latlon_to_xyz(lat: float, lon: float, radius=1.0): lat_rad = np.radians(lat) @@ -83,6 +93,7 @@ def main(): epilog="Requires kitty graphics protocol support in terminal", ) parser.add_argument("-t", "--traceroute", default=None) + parser.add_argument("--example", action="store_true") args = parser.parse_args() locations = [] @@ -91,8 +102,8 @@ def main(): globe = pv.Sphere( radius=1.0, - theta_resolution=120, - phi_resolution=120, + theta_resolution=60, + phi_resolution=60, start_theta=270.001, end_theta=270, ) @@ -107,10 +118,14 @@ def main(): ) # Convert to 3D coordinates - points_3d = [latlon_to_xyz(lat, lon) for lat, lon in locations] + if args.example: + points_3d = [latlon_to_xyz(lat, lon) for lat, lon in EXAMPLE_ROUTE] + else: + points_3d = [latlon_to_xyz(lat, lon) for lat, lon in locations] - plotter = TerminalPlotter(450, 450) - plotter.add_mesh(globe, color="tan", smooth_shading=True, texture=tex, show_edges=False) + height, width = kitty.get_terminal_size_pixel() + plotter = TerminalPlotter(width, height) + plotter.add_mesh(globe, color="tan", smooth_shading=False, texture=tex, show_edges=False) for point in points_3d: city_marker = pv.Sphere(center=point, radius=0.02) @@ -120,7 +135,8 @@ def main(): line = pv.lines_from_points(arch, close=False) plotter.add_mesh(line, color="red", line_width=2) - kitty.hide_cursor() + + #kitty.hide_cursor() try: asyncio.run(plotter.run()) finally: diff --git a/terminalplotter.py b/terminalplotter.py index dbf447e..8f7f493 100644 --- a/terminalplotter.py +++ b/terminalplotter.py @@ -13,17 +13,18 @@ import kitty class TerminalPlotter(pv.Plotter): def __init__(self, width, height, **kwargs): - super().__init__(off_screen=True, window_size=(height, width), **kwargs) + super().__init__(off_screen=True, window_size=(width, height), **kwargs) + self.width = width + self.height = height self._running = True h_pix, _ = kitty.get_terminal_cell_size() self.rows, _ = kitty.get_terminal_size() self.start_y, self.start_x = kitty.get_position() # the image requires height/cell_height lines self.needed_lines = math.ceil(height / h_pix) - self.set_background([0.0, 1.0, 0.0]) + self.set_background([0.0, 0.0, 0.0]) # if we are too close to the bottom of the terminal, create some space. if self.rows - self.start_y < self.needed_lines: - self.set_background([1.0, 0.0, 0.0]) missing = self.needed_lines - (self.rows - self.start_y) self.start_y -= missing print("\n" * self.needed_lines, end="") @@ -32,7 +33,7 @@ class TerminalPlotter(pv.Plotter): def render_to_kitty(self): self.render() buf = io.BytesIO() - self.screenshot(buf, transparent_background=True) + self.screenshot(buf, transparent_background=True, window_size=(self.width, self.height)) kitty.draw_to_terminal(buf) # print("y:", self.start_y, "rows:", self.rows, end="") kitty.set_position(self.start_y, self.start_x)