display content in npyscreen

133 Views Asked by At

I start to play around with npyscreen. I want the output of def ping() displayed within a box in npyscreen. At this moment I have the following code:

#!/usr/bin/env python
# encoding: utf-8

import sys
import curses
import npyscreen
import os.path

from os import system, name
from pathlib import Path

class bcolors:
    OK      = '\033[92m'
    FAIL    = '\033[91m'
    RESET   = '\u001b[0m'
    DEFAULT = '\033[35m'
    CHECK   = '\033[33m'
    PINK    = '\033[95m'

def clear():
    if name == 'nt':
        _ = system('cls')
    else:
        _ = system('clear')


hostnames = ["1.1.1.1", "0.0.0.0", "2.2.2.2"]

class dashboard(npyscreen.NPSApp):
    def main(self):
        F  = npyscreen.Form(name = "DASHBOARD")
        column_height = terminal_dimensions()[0] -9
        widget_top = F.add(
            Column,
            name        = "SERVER",
            relx        = 2,
            rely        = 2,
            max_width   = 40,
        )

        widget_top.values = [print(re)]
        F.edit()

class Column(npyscreen.BoxTitle):
    def resize(self):
        self.max_height = int(0.73 * terminal_dimensions()[0])

def terminal_dimensions():
    return curses.initscr().getmaxyx()

def ping():
    for i in hostnames:
        response = os.system("ping -c 1 " + i + "> /dev/null 2>&1")
        if response == 0:
            print(bcolors.OK + "" + bcolors.RESET + f" {i}")
        else:
            print(bcolors.FAIL + "" + bcolors.RESET + f" {i}")

re = ping()

if __name__ == "__main__":
    clear()
    App = dashboard()
    App.run()

At this point the def ping() brings the right output but totally destroy the layout from npyscreen. How do I display the output within the box named "SERVER"?

Thanks

1

There are 1 best solutions below

0
larsks On

There are several issues here.

print doesn't know about your widgets

You can't use print to output into your npyscreen widgets. The print function doesn't know anything about npyscreen; it just prints stuff out at the current cursor position.

If you want to display text, pick an appropriate widget and then set the value accordingly.

There are a number of examples in the npyscreen repository that demonstrate things like this.

print doesn't return a value

print doesn't return a value, so this makes no sense:

widget_top.values = [print(re)]

That will only ever show result in a list containing the single value None. Maybe you want something like this?

widget_top.values = hostnames

ping doesn't return a value

Your ping method doesn't return a value, so this makes no sense:

re = ping()