Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/usbtop/console_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ConsoleOutput
private:
static void clear_screen();
static void print_stats();
static std::string format_speed(double bytes_per_second);
static void print_stats_bus(UsbBus const& bus);
};

Expand Down
46 changes: 41 additions & 5 deletions src/console_output.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012 Adrien Guinet <adrien@guinet.me>
* 2025 Thomas Lienbacher <usbtop@lienbacher.dev>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -37,6 +38,7 @@

#include <iostream>
#include <iomanip>
#include <sstream>

#include <unistd.h>

Expand Down Expand Up @@ -65,18 +67,52 @@ void usbtop::ConsoleOutput::print_stats()
{ print_stats_bus(*bus); });
}

std::string usbtop::ConsoleOutput::format_speed(double bytes_per_second)
{
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << std::setw(3);

// usb probably won't reach TiB/s in the next couple of years ;)
if (bytes_per_second > 1024.0 * 1024.0 * 1024.0)
{
// format as GiB/s
ss << bytes_per_second / 1024.0 / 1024.0 / 1024.0;
ss << " GiB/s";
}
else if (bytes_per_second > 1024.0 * 1024.0)
{
// format as NiB/s
ss << bytes_per_second / 1024.0 / 1024.0;
ss << " MiB/s";
}
else if (bytes_per_second > 1024.0)
{
// format as KiB/s
ss << bytes_per_second / 1024.0;
ss << " KiB/s";
}
else
{
// format as B/s
ss << bytes_per_second;
ss << " B/s";
}

return ss.str();
}

void usbtop::ConsoleOutput::print_stats_bus(UsbBus const& bus)
{
std::cout << "Bus ID " << bus.id() << " (" << bus.desc() << ")";
std::cout << "Bus ID " << bus.id() << " (" << bus.desc() << ")";
std::cout << "\tTo device\tFrom device" << std::endl;
UsbBus::list_devices_t const& devs = bus.devices();
UsbBus::list_devices_t::const_iterator it;
for (it = devs.begin(); it != devs.end(); it++) {
for (it = devs.begin(); it != devs.end(); ++it) {
UsbDevice const& dev(*it->second);
UsbStats const& stats(dev.stats());
std::cout << " Device ID " << std::setw(3) << it->first << " :\t";
double stats_to = stats.stats_to_device().bw_instant()/1024.0;
double stats_from = stats.stats_from_device().bw_instant()/1024.0;
std::cout << "\t\t\t" << stats_to << " KiB/s\t" << stats_from << " KiB/s" << std::endl;
double stats_to = stats.stats_to_device().bw_instant();
double stats_from = stats.stats_from_device().bw_instant();
std::cout << "\t\t\t" << format_speed(stats_to) << "\t" << format_speed(stats_from) << std::endl;
}
}