How to write an interactive program, for example, Top?

When you start top , you enter the interactive display located in the terminal, but the command line is missing.

I want to create a program with this type of display, but I don’t even know what to do.

  • Where to begin?
+4
source share
2 answers

topused curses when it was first released, but later switched to its own screen control code due to the overhead associated with curses.

Read more about the top at the following link:


, , TUI ( ).


ncurses goto , "" .

, google . :

+5

, , bash :

#!/bin/bash 

# Put terminal into canonical mode with noecho 
# (not required for this example but perhaps useful nonethless )
MYTERMRESTORE=$(stty --save)
stty icanon -echo

# Obtain terminal dimensions 
columns=$(tput cols)
lines=$(tput lines)

# Populate a buffer and store its size
buffer="$(ps aux)"
scroll="${#buffer}"

# Set a top bar and scrolling region (printf "\033[2;${lines}")
tput csr 1 "${lines}"
while [ "${#x}" -lt "$columns" ]
do x="$x="
done
printf "$x\n"

# Set up a continuos loop
while [ 1 ]
do  printf "%.*s"  $scroll  "$buffer"
    printf "\n\nUse arrow keys to toggle through output, q to quit\n"
    read -n 1 i
    case "$i" in
        '[')
             read  -n 1 j
             case "$j" in
                  "A") # Up arrow
                       scroll=$(( scroll - $columns ))
                   ;;
                  "B") # Down arrow
                       scroll=$(( scroll + $columns ))
                   ;;
             esac
             ;;
        'q') break
             ;;
    esac
done


stty "$MYTERMRESTORE"

, . select().

+3

Source: https://habr.com/ru/post/1610309/


All Articles