def main(): """Since 1 braille chars can represent 2*4 points we have to scale the board accordingly""" width, height = drawille.getTerminalSize() width, height = int((width*2.0)-2), int((height*4.0)-4) game = GameOfLife(width=width, height=height) def fill_board_randomly(game): def set_cell_randomly(): if randrange(10) > randrange(6, 10): return 1 return 0 for y in range(game.height): for x in range(game.width): game.board[y][x] = set_cell_randomly() fill_board_randomly(game) def frame_coordinates(game): while True: game.evolve_board() s = [] for y in range(game.height): for x in range(game.width): if game.board[y][x] == 1: s.append((x, y)) yield s s = drawille.Canvas() drawille.animate(s, frame_coordinates, 1./5, game)
def create(self, *args, **keywords): super(MapForm, self).create(*args, **keywords) self.w_map_selection = self.add( npyscreen.BoxTitle, name="", values=["WORLD"], #values=["WORLD", "ORBIT", "SYSTEM"], max_width=20, rely=self.PADDING_Y, relx=self.PADDING_X, #check_value_change=True ) self.w_map_selection.value = 0 self.w_map_box = self.add( TextBox, name="MAP", #max_width= self.USEABLE_X-self.w_map_selection.max_width - 2*self.PADDING_X, max_width=self.USEABLE_X - self.w_map_selection.max_width - 5, max_height=self.USEABLE_Y - 3, #Relx of the previous + width of the previous - Padding relx=self.w_map_selection.relx + self.w_map_selection.width - 2 * self.PADDING_X, rely=self.PADDING_Y) self.w_map_box.editable = False ##Pixel X,Y Coordinates that limit the size of the Map ## It is updated after setting self.map_pixel_limits = { "min_x": 0, "min_y": 0, "max_x": self.w_map_box.max_width * 2, "max_y": (self.w_map_box.max_height - self.w_map_box.rely) * 4 } ##Canvas with Only the map. Each refresh just re-print ## this canvas instead of redrawing the entire map self.base_map_canvas = drawille.Canvas() draw_image_on_canvas(open(const.MAP_IMAGE_FILEPATH, 'rb'), const.MAP_IMAGE_THRESHOLD, False, self.base_map_canvas, self.map_pixel_limits) #Canvas where the objects will be tracked on self.map_canvas = drawille.Canvas()
def dump_tile(self, address): c = drawille.Canvas() line1 = [] line2 = [] for x in range(8): line1.append(self.mem.read(address + (2 * x))) line2.append(self.mem.read(address + (2 * x) + 1)) print('Tile Data @' + hex(address)) c.set(0, 0) c.set(10, 10) for i, line in enumerate(line1): for pixel in range(8): if self.get_color(line, line2[i], pixel) != 0: c.set(1 + pixel, 1 + i) print(c.frame())
def main(): gb = Pyboi() gb.load_rom('roms/tetris.gb') c = drawille.Canvas() while True: c.clear() c.set(0,0) c.set(159, 143) frame = gb.get_boot_frame() for row in range(144): for col in range(160): if frame[(row * 160) + col] != 0: c.set(col, row) print(chr(27) + "[2J") print(chr(27) + "[3J") print(c.frame())
def dump_tile_to_screen(self, address, x_start): line1 = [] line2 = [] for x in range(8): line1.append(self.mem.read(address + (2 * x))) line2.append(self.mem.read(address + (2 * x) + 1)) for i, line in enumerate(line1): for pixel in range(8): if self.get_color(line, line2[i], pixel) != 0: self.draw_to_buffer(x_start + i, pixel, 1) c = drawille.Canvas() c.clear() c.set(0, 0) c.set(160, 144) for row in range(144): for col in range(160): if self.gb_screen[(row * 160) + col] != 0: c.set(col, row) print(c.frame())
def while_waiting(self): memory_info = self.data['mem']['proc_info']['mem'] processes_info = self.data['proc']['proc_info'] cpu_info = self.data['cpu']['chart'] cpcanv = drawille.Canvas() y = int( math.floor( (float(cpu_info['percentage']) / 100) * self.can_height * 7)) self.cpu_chart.value = (self.create_chart_for_cpu_using(y, cpcanv)) self.cpu_chart.update(clear=True) self.proc_details = self.data['proc']['grid'] sdata = self.proc_details cdata = [] for proc in sdata: cdata.append( "{1: >5} {0: <60} {2: >0.2f}%\ ".format((proc['name'][:60] + '...') if len(proc['name']) > 60 else proc['name'], proc['id'], proc['memory'], " " * int(5 * self.static_x))) self.pGrid.entry_widget.values = cdata self.pGrid.entry_widget.comp_data(self.proc_details) self.pGrid.entry_widget.update(clear=True) self.window.display()
def create(self, *args, **keywords): super(MainForm, self).create(*args, **keywords) self.form_canvas = drawille.Canvas() y, x = self.useable_space() self.dot_pos = (10, 10) obj = self.add(npyscreen.BoxTitle, name="Window Useable Space", values=["X {}".format(x), "Y {}".format(y)], max_width=20, max_height=y - 5, rely=2) self.t = self.add(InputBox, name="CHART", max_width=40, max_height=y - 5, relx=obj.relx + obj.width + 1, rely=2) self.t.editable = False self.t.footer = "Width {} Height {}\tDot X:{} Y:{}".format( self.t.width, self.t.height, self.dot_pos[0], self.dot_pos[1]) self.t2 = self.add( InputBox, name="CHART", max_width=x - (self.t.max_width + self.t.relx + obj.max_width + obj.relx), max_height=y - 5, relx=self.t.width + self.t.relx + 1, rely=2) new_handlers = { "^R": self.handle_update, "^E": self.handle_change_form } self.add_handlers(new_handlers)
def create(self, *args, **keywords): super(MainForm, self).create(*args, **keywords) self.form_canvas = drawille.Canvas() y, x = self.useable_space() self.dot_pos = (10, 10) obj = self.add(npyscreen.BoxTitle, name="", values=["WORLD", "ORBIT"], max_width=20, rely=1) self.t = self.add(InputBox, name="CHART", max_width=x - obj.max_width - 5, max_height=y - 3, relx=obj.relx + obj.width + 1, rely=1) self.t.editable = False self.t.footer = "NOW TRACKING: EXAMPLE | Width {} Height {}\tDot X:{} Y:{}".format( self.t.width, self.t.height, self.dot_pos[0], self.dot_pos[1]) new_handlers = {"^R": self.h_update, curses.KEY_F2: self.h_change_form} self.add_handlers(new_handlers)
def update(self): ''' Update the form in background, this used to be called inside the ThreadJob and but now is getting called automatically in while_waiting ''' try: disk_info = self.statistics['Disk']['text']['/'] swap_info = self.statistics['Memory']['text']['swap_memory'] memory_info = self.statistics['Memory']['text']['memory'] processes_info = self.statistics['Process']['text'] system_info = self.statistics['System']['text'] cpu_info = self.statistics['CPU']['graph'] network_info = self.statistics['Network']['text'] #### Overview information #### row1 = "Disk Usage (/) {4}{0: <7}/{1: >7} MB{4}{2: >2} %{5}Processes{4}{3: <8}".format(disk_info["used"], disk_info["total"], disk_info["percentage"], processes_info["running_processes"], " "*int(4*self.X_SCALING_FACTOR), " "*int(9*self.X_SCALING_FACTOR)) row2 = "Swap Memory {4}{0: <7}/{1: >7} MB{4}{2: >2} %{5}Threads {4}{3: <8}".format(swap_info["active"], swap_info["total"], swap_info["percentage"], processes_info["running_threads"], " "*int(4*self.X_SCALING_FACTOR), " "*int(9*self.X_SCALING_FACTOR)) row3 = "Main Memory {4}{0: <7}/{1: >7} MB{4}{2: >2} %{5}Boot Time{4}{3: <8}".format(memory_info["active"], memory_info["total"], memory_info["percentage"], timeStr, " "*int(4*self.X_SCALING_FACTOR), " "*int(9*self.X_SCALING_FACTOR)) net = psutil.net_io_counters() sentt = net[0]*0.000001 sentt = str(round(sentt,2)) recc = net[1]*0.000001 recc = str(round(recc,2)) row4 = "Network Usage:\nSent/Received {4}{0: <7}/{1: >7} MB{4}{2: >2} {5} {4}{3: <8}".format(sentt, recc, '', '', " "*int(4*self.X_SCALING_FACTOR), " "*int(9*self.X_SCALING_FACTOR)) self.basic_stats.value = row1 + '\n' + row2 + '\n' + row3 + '\n' + row4 # Lazy update to GUI self.basic_stats.update(clear=True) #### CPU Usage information #### cpu_canvas = drawille.Canvas() next_peak_height = int(math.ceil((float(cpu_info['percentage'])/100)*self.CHART_HEIGHT)) self.cpu_chart.value = (self.draw_chart(cpu_canvas,next_peak_height,'cpu')) self.cpu_chart.update(clear=True) #### Memory Usage information #### memory_canvas = drawille.Canvas() next_peak_height = int(math.ceil((float(memory_info['percentage'])/100)*self.CHART_HEIGHT)) self.memory_chart.value = self.draw_chart(memory_canvas,next_peak_height,'memory') self.memory_chart.update(clear=True) #### Network Usage information #### network_canvas = drawille.Canvas() next_peak_height = int(math.ceil((float(network_info['received'])/100)*self.CHART_HEIGHT)) if next_peak_height > 100: next_peak_height = 100 #print(self.cpu_array[100:126]) self.network_chart.value = self.draw_chart(network_canvas,next_peak_height,'network') #lehar.draw(self.cpu_array[100:126]) self.network_chart.update(clear=True) #### Processes table #### self._processes_data = self.statistics['Process']['table'] # check sorting flags if MEMORY_SORT: sorted_processes_data = sorted(self._processes_data,key=lambda k:k['memory'],reverse=True) self._logger.info("Memory sorting done for process table") elif TIME_SORT: sorted_processes_data = sorted(self._processes_data,key=lambda k:k['rawtime'],reverse=True) self._logger.info("Time sorting done for process table") elif PROCESS_RELEVANCE_SORT: sorted_processes_data = sorted(self._processes_data,key=lambda k:k['rawtime']) self._logger.info("Sorting on the basis of relevance") else: sorted_processes_data = self._processes_data self._logger.info("Resetting the sorting behavior") # to keep things pre computed curtailed_processes_data = [] for proc in sorted_processes_data: curtailed_processes_data.append("{0: <30} {1: >5}{6}{2: <10}{6}{3}{6}{4: >6.2f} % {6}{5}\ ".format( (proc['name'][:25] + '...') if len(proc['name']) > 25 else proc['name'], proc['id'], proc['user'], proc['time'], proc['memory'], proc['local_ports'], " "*int(5*self.X_SCALING_FACTOR)) ) if not self.processes_table.entry_widget.is_filtering_on(): self.processes_table.entry_widget.values = curtailed_processes_data # Set the processes data dictionary to uncurtailed processes data self.processes_table.entry_widget.set_uncurtailed_process_data(self._processes_data) self.processes_table.entry_widget.update(clear=True) ''' This will update all the lazy updates at once, instead of .display() [fast] .DISPLAY()[slow] is used to avoid glitches or gibberish text on the terminal ''' self.window.DISPLAY() # catch the f*****g KeyError caused to c # cumbersome point of reading the stats data structures except KeyError: self._logger.info("Some of the stats reading failed",exc_info=True)
try: i_converted = i.tobytes() except AttributeError: raise if invert_threshold: threshold_test = lambda pix_color, color_threshold: pix_color > color_threshold else: threshold_test = lambda pix_color, color_threshold: pix_color < color_threshold x = y = 0 for pix in i_converted: if threshold_test(pix, color_threshold): canvas.set(x, y) x += 1 if x >= image_width: y += 1 x = 0 if __name__ == "__main__": img = requests.get( "https://spacelaunchnow-prod-east.nyc3.cdn.digitaloceanspaces.com/media/logo/national2520aeronautics2520and2520space2520administration_logo_20190207032448.png", stream=True) #img = requests.get("https://spacelaunchnow-prod-east.nyc3.cdn.digitaloceanspaces.com/media/default/cache/21/f1/21f1222a77ab913fea2ef8b2f5ffd953.jpg",stream=True) pixel_limits = {"min_x": 0, "min_y": 0, "max_x": 128, "max_y": 128} canvas = drawille.Canvas() #draw_img_on_canvas(img.raw,145,True,canvas,pixel_limits) draw_img_on_canvas(img.raw, 70, True, canvas, pixel_limits)
def update(self): ''' Update the form in background, this used to be called inside the ThreadJob and but now is getting called automatically in while_waiting ''' try: disk_info = self.statistics['Disk']['text']['/'] swap_info = self.statistics['Memory']['text']['swap_memory'] memory_info = self.statistics['Memory']['text']['memory'] processes_info = self.statistics['Process']['text'] system_info = self.statistics['System']['text'] cpu_info = self.statistics['CPU']['graph'] gpu_info = self.statistics['GPU']['graph'] network_info = self.statistics['Network']['text'] temp_info = self.statistics['Temperature']['text'] #### Overview information #### row1 = "Disk Usage (/) {4}{0: <6}/{1: >6} MB{4}{2: >2} %{5}Processes{4}{3: <8}".format( disk_info["used"], disk_info["total"], disk_info["percentage"], processes_info["running_processes"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row11 = "Disk Usage (/) {3}{0: <6}/{1: >6} MB{3}{2: >2} %".format( disk_info["used"], disk_info["total"], disk_info["percentage"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row12 = "Processes{1}{0: <8}".format( processes_info["running_processes"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row2 = "Swap Memory {4}{0: <6}/{1: >6} MB{4}{2: >2} %{5}Threads {4}{3: <8}".format( swap_info["active"], swap_info["total"], swap_info["percentage"], processes_info["running_threads"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row21 = "Swap Memory {3}{0: <6}/{1: >6} MB{3}{2: >2} %".format( swap_info["active"], swap_info["total"], swap_info["percentage"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row22 = "Threads {1}{0: <8}".format( processes_info["running_threads"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row3 = "Main Memory {4}{0: <6}/{1: >6} MB{4}{2: >2} %{5}Boot Time{4}{3: <8} hours".format( memory_info["active"], memory_info["total"], memory_info["percentage"], str(system_info["running_time"]), " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row31 = "Main Memory {3}{0: <6}/{1: >6} MB{3}{2: >2} %".format( memory_info["active"], memory_info["total"], memory_info["percentage"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row32 = "Boot Time{1}{0: <8} hours".format( str(system_info["running_time"]), " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) row41 = "Network Speed {2}{0: <3}↓ {1: <3}↑ MB/s".format( network_info["download_speed_in_mb"], network_info["upload_speed_in_mb"], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) if (self.statistics['GPU']['text']['number_of_gpus'] == 0): row42 = "Temperatures{1}CPU:{0: <8}".format( temp_info['temp'], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) else: row42 = "Temperatures{2}GPU:{0},CPU:{1: <8}".format( self.statistics['GPU']['text']['temperature'], temp_info['temp'], " " * int(4 * self.X_SCALING_FACTOR), " " * int(9 * self.X_SCALING_FACTOR)) self.basic_stats1.value = row11 + '\n' + row21 + '\n' + row31 + '\n' + row41 self.basic_stats2.value = row12 + '\n' + row22 + '\n' + row32 + '\n' + row42 # Lazy update to GUI self.basic_stats1.update(clear=True) self.basic_stats2.update(clear=True) #### CPU Usage information #### cpu_canvas = drawille.Canvas() next_peak_height = int( math.ceil( (float(cpu_info['percentage']) / 100) * self.CHART_HEIGHT)) self.cpu_chart.value = (self.draw_chart(cpu_canvas, next_peak_height, 'cpu')) self.cpu_chart.update(clear=True) #### Memory Usage information #### memory_canvas = drawille.Canvas() next_peak_height = int( math.ceil((float(memory_info['percentage']) / 100) * self.CHART_HEIGHT)) self.memory_chart.value = self.draw_chart(memory_canvas, next_peak_height, 'memory') self.memory_chart.update(clear=True) #### GPU Usage information #### gpu_canvas = drawille.Canvas() next_peak_height = int( math.ceil( (float(gpu_info['percentage']) / 100) * self.CHART_HEIGHT)) self.gpu_chart.value = (self.draw_chart(gpu_canvas, next_peak_height, 'gpu')) self.gpu_chart.update(clear=True) #### Processes table #### self._processes_data = self.statistics['Process']['table'] # check sorting flags if MEMORY_SORT: sorted_processes_data = sorted(self._processes_data, key=lambda k: k['memory'], reverse=True) self._logger.info("Memory sorting done for process table") elif TIME_SORT: sorted_processes_data = sorted(self._processes_data, key=lambda k: k['rawtime'], reverse=True) self._logger.info("Time sorting done for process table") elif PROCESS_RELEVANCE_SORT: sorted_processes_data = sorted(self._processes_data, key=lambda k: k['rawtime']) self._logger.info("Sorting on the basis of relevance") else: sorted_processes_data = self._processes_data self._logger.info("Resetting the sorting behavior") # to keep things pre computed curtailed_processes_data = [] for proc in sorted_processes_data: curtailed_processes_data.append( "{0: <30} {1: >5}{6}{2: <10}{6}{3}{6}{4: >6.2f} % {6}{7: >6.2f} % {6}{5}\ ".format((proc['name'][:25] + '...') if len(proc['name']) > 25 else proc['name'], proc['id'], proc['user'], proc['time'], proc['memory'], proc['local_ports'], " " * int(3 * self.X_SCALING_FACTOR), proc['cpu'])) if not self.processes_table.entry_widget.is_filtering_on(): self.processes_table.entry_widget.values = curtailed_processes_data # Set the processes data dictionary to uncurtailed processes data self.processes_table.entry_widget.set_uncurtailed_process_data( self._processes_data) self.processes_table.entry_widget.update(clear=True) ''' This will update all the lazy updates at once, instead of .display() [fast] .DISPLAY()[slow] is used to avoid glitches or gibberish text on the terminal ''' self.window.DISPLAY() # catch the f*****g KeyError caused to c # cumbersome point of reading the stats data structures except KeyError: self._logger.info("Some of the stats reading failed", exc_info=True)
def update(self): ''' Update the form in background, this used to be called inside the ThreadJob and but now is getting called automatically in while_waiting ''' try: dask_memory = self.statistics['Dask']['Memory'] dask_cpu = self.statistics['Dask']['CPU'] dask_cluster= self.statistics['Dask']['Cluster'] #### Overview information #### row2 = "Workers: {workers: <6} {long_space} Total Threads: {threads: <8}".format(workers=dask_cluster["n_workers"], threads=dask_cluster["total_threads"], space=" "*int(4*self.X_SCALING_FACTOR), long_space=" "*int(9*self.X_SCALING_FACTOR)) row3 = "Memory (used/available) {space}{used: <6}/{total: >6} MB".format(used = dask_memory['used_memory'], total = dask_memory['total_memory'], space=" "*int(4*self.X_SCALING_FACTOR), long_space=" "*int(9*self.X_SCALING_FACTOR)) self.basic_stats.value = row2 + '\n' + row3 # Lazy update to GUI self.basic_stats.update(clear=True) #### CPU Usage information #### cpu_canvas = drawille.Canvas() next_peak_height = int(math.ceil((float(dask_cpu['cpu_usage'])/100)*self.CHART_HEIGHT)) self.cpu_chart.value = (self.draw_chart(cpu_canvas,next_peak_height,'cpu')) self.cpu_chart.update(clear=True) #### Memory Usage information #### memory_canvas = drawille.Canvas() next_peak_height = int(math.ceil((float(dask_memory['used_memory_percent']))*self.CHART_HEIGHT)) self.memory_chart.value = self.draw_chart(memory_canvas,next_peak_height,'memory') self.memory_chart.update(clear=True) #### Worker table #### self._processes_data = self.statistics['Dask']['Workers'] # check sorting flags if MEMORY_SORT: sorted_processes_data = sorted(self._processes_data,key=lambda k:k['memory'],reverse=True) self._logger.info("Memory sorting done for process table") elif TIME_SORT: sorted_processes_data = sorted(self._processes_data,key=lambda k:k['rawtime'],reverse=True) self._logger.info("Time sorting done for process table") elif PROCESS_RELEVANCE_SORT: sorted_processes_data = sorted(self._processes_data,key=lambda k:k['rawtime']) self._logger.info("Sorting on the basis of relevance") else: sorted_processes_data = self._processes_data self._logger.info("Resetting the sorting behavior") # to keep things pre computed # curtailed_processes_data = [] # for proc in sorted_processes_data: # curtailed_processes_data.append("{0: <30} {1: >5}{6}{2: <10}{6}{3}% {6}{4: >6.2f}{6}{5}\ # ".format( (proc['address'][:25] + '...') if len(proc['address']) > 25 else proc['address'], # 0 # proc['nthreads'], # 1 # proc['user'], # 2 # proc['cpu'], # 3 # proc['memory'], # 4 # proc['memory_limit'], # 5 # " "*int(5*self.X_SCALING_FACTOR)) # 6 # ) # to keep things pre computed curtailed_processes_data = [] for proc in sorted_processes_data: curtailed_processes_data.append("{address}{space}{nthreads}{space}{cpu} % {space}{memory}/{memory_limit}{space}{read}/{write}\ ".format( address = (proc['address'][:25] + '...') if len(proc['address']) > 25 else proc['address'], # 0 nthreads = proc['nthreads'], # 1 cpu = proc['cpu'], # 3 memory = proc['memory'], # 4 memory_limit = proc['memory_limit'], # 5 read = proc['read'], write = proc['write'], space= " "*int(5*self.X_SCALING_FACTOR)) # 6 ) if not self.processes_table.entry_widget.is_filtering_on(): self.processes_table.entry_widget.values = curtailed_processes_data # Set the processes data dictionary to uncurtailed processes data self.processes_table.entry_widget.set_uncurtailed_process_data(self._processes_data) self.processes_table.entry_widget.update(clear=True) ''' This will update all the lazy updates at once, instead of .display() [fast] .DISPLAY()[slow] is used to avoid glitches or gibberish text on the terminal ''' self.window.DISPLAY() # catch the f*****g KeyError caused to c # cumbersome point of reading the stats data structures except KeyError: self._logger.info("Some of the stats reading failed",exc_info=True)