Skip to content

Instantly share code, notes, and snippets.

@fintanmm
Created February 21, 2024 16:24
Show Gist options
  • Save fintanmm/ab75db64db0b325c82714d18912a4aa6 to your computer and use it in GitHub Desktop.
Save fintanmm/ab75db64db0b325c82714d18912a4aa6 to your computer and use it in GitHub Desktop.
Parse output from 'zypper ps -s' and display using FlatLaf GUI framework.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.6.1
//DEPS com.formdev:flatlaf:0.45
import com.formdev.flatlaf.FlatDarkLaf;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@Command(name = "ZypperPS", mixinStandardHelpOptions = true, version = "ZypperPS 1.0", description = "Parse output from 'zypper ps -s' and display using FlatLaf GUI framework.")
class ZypperPS implements Runnable {
public void run() {
// Run 'sudo zypper ps -s' command
List<String> command = new ArrayList<>();
command.add("sudo");
command.add("zypper");
command.add("ps");
command.add("-s");
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
try {
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// Parse the output
List<String[]> data = new ArrayList<>();
String line;
boolean skipHeaders = true;
while ((line = reader.readLine()) != null) {
if (line.startsWith("The following running processes use deleted files:")) {
continue; // skip this line and the following ones
}
if (line.startsWith("------+-------+------+------------+------------------+-----------------")) {
continue; // skip this line and the following ones
}
if (line.startsWith("You")) {
continue; // skip this line and the following ones
}
if (line.startsWith("PID")) { // skip headers
skipHeaders = false;
continue;
}
if (skipHeaders) {
continue;
}
String[] fields = line.trim().split("\\s*\\|\\s*");
data.add(fields);
}
// Create table model
String[] columnNames = { "PID", "PPID", "UID", "User", "Command", "Service" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (String[] row : data) {
model.addRow(row);
}
// Display in GUI
FlatDarkLaf.install();
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Zypper PS Output");
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
// Add text outside of table
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea(
"You may wish to restart these processes.\n" +
"See 'man zypper' for information about the meaning of values in the above table.\n\n" +
"Since the last system boot core libraries or services have been updated.\n" +
"Reboot is suggested to ensure that your system benefits from these updates.");
textArea.setEditable(false);
panel.add(textArea, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
} catch (IOException e) {
System.err.println("Error executing command: " + e.getMessage());
System.exit(1);
}
}
public static void main(String[] args) {
CommandLine.run(new ZypperPS(), args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment