Made the roster command a bit more useful: since the main thread and the

team IDs are now equivalent in Haiku, we don't need to print both.
Also, team paths are now truncated to fit into the list to give a better
visual appearance.
Added two options to turn off truncating and to show the team name
(instead of full path) only.
Applied our style guide, replaced C++ streams with nicer stdio stuff
(formatted output is not that nice in C++).


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11638 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-03-10 16:10:44 +00:00
parent b9b6724617
commit a49279b1e0
2 changed files with 128 additions and 75 deletions
+1 -6
View File
@@ -64,6 +64,7 @@ StdBinCommands
play.cpp
query.cpp
quit.cpp
roster.cpp
setversion.cpp
shutdown.cpp
version.cpp
@@ -83,12 +84,6 @@ StdBinCommands
: be libmedia.so
;
# standard commands that need libbe.so, libstdc++.r4.so
StdBinCommands
roster.cpp
: be stdc++.r4
;
# standard commands that need libbe.so, libmail.so
StdBinCommands
mail.cpp
+116 -58
View File
@@ -1,87 +1,145 @@
/*********************************************************************
** Roster Applet
**********************************************************************
** 2002-04-30 Mathew Hounsell Created.
*/
/*
* Copyright 2002-2005 Haiku Inc.
* Distributed under the terms of the MIT license
*
* Authors:
* Mathew Hounsell
* Axel Dörfler, [email protected].
*/
#include <Roster.h>
#include <Path.h>
#include <Entry.h>
#include <List.h>
#include <String.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <getopt.h>
#include <cstring>
#include <cstdlib>
#include <cassert>
/*********************************************************************
*/
bool OutputTeam( void * item_p )
static struct option const kLongOptions[] = {
{"name", no_argument, 0, 'n'},
{"no-trunc", no_argument, 0, 't'},
{"help", no_argument, 0, 'h'},
{NULL}
};
extern const char *__progname;
static const char *kProgramName = __progname;
static const int32 kNameFieldWidth = 34;
// view modes
static const int32 kStandardMode = 0x0;
static const int32 kNameOnlyMode = 0x1;
static const int32 kNoTruncateMode = 0x2;
void
truncate_string(BString &name, int32 length)
{
static uint32 i = 0u; // counter
if (name.Length() <= length)
return;
if (length < 6)
length = 6;
app_info info;
int32 beginLength = length / 3 - 1;
int32 endLength = length - 3 - beginLength;
// Roster stores team_id not team_id *
team_id id = reinterpret_cast<team_id>( item_p );
BString begin, end;
name.CopyInto(begin, 0, beginLength);
name.CopyInto(end, name.Length() - endLength, endLength);
name = begin;
name.Append("...").Append(end);
}
void
output_team(team_id id, int32 mode)
{
// Get info on team
if( be_roster->GetRunningAppInfo( id, &info ) == B_OK ) {
app_info info;
if (be_roster->GetRunningAppInfo(id, &info) != B_OK)
return;
// Allocate a entry and get it's path.
// - works as they are independant (?)
BEntry entry( & info.ref );
BPath path( & entry );
BEntry entry(&info.ref);
BPath path(&entry);
// Output - leave the work to ostream
std::cout
<< setw(2) << i << ' '
// short << setw(32) << info.ref.name << ' '
<< setw(32) << path.Path() << ' '
<< setw(6) << info.thread << ' '
<< setw(4) << info.team << ' '
<< setw(4) << info.port << ' '
<< setw(4)
;
// Go hex, output, the go dec
hex( std::cout );
std::cout << info.flags;
dec( std::cout );
BString name;
if (mode & kNameOnlyMode)
name = info.ref.name;
else
name = path.Path();
// Output a signature
std::cout
<< " (" << info.signature << ')'
<< std::endl
;
if ((mode & kNoTruncateMode) == 0)
truncate_string(name, kNameFieldWidth);
/* NOW */ ++i;
}
return false;
printf("%6ld %*s %5ld %5lx (%s)\n",
id, (int)kNameFieldWidth, name.String(),
info.port, info.flags, info.signature);
}
/*********************************************************************
*/
int main( int argc, char ** argv )
{
// Don't have an BApplication as it is a waste.
cout
<< " path thread team port flags\n"
"-- -------------------------------- ------ ---- ---- -----"
<< std::endl
;
void
usage(int exitCode)
{
fprintf(stderr, "usage: %s [-nt]\n"
" -n, --name\t\tInstead of the full path, only the name of the teams are written\n"
" -t, --no-trunc\tDon't truncate the path name\n",
kProgramName);
exit(exitCode);
}
int
main(int argc, char **argv)
{
int32 mode = kStandardMode;
// Don't have an BApplication as it is not needed for what we do
int c;
while ((c = getopt_long(argc, argv, "nt", kLongOptions, NULL)) != -1) {
switch (c) {
case 'n':
mode |= kNameOnlyMode;
break;
case 't':
mode |= kNoTruncateMode;
break;
case 0:
break;
case 'h':
usage(0);
break;
default:
usage(1);
break;
}
}
// print title line
printf(" team %*s port flags signature\n",
(int)kNameFieldWidth, mode & kNameOnlyMode ? "name" : "path");
printf("------ ");
for (int32 i = 0; i < kNameFieldWidth; i++)
putchar('-');
puts(" ----- ----- ---------");
// Retrieve the running list.
BList applist( 0 );
be_roster->GetAppList( & applist );
BList applicationList;
be_roster->GetAppList(&applicationList);
// Iterate through the list.
// void DoForEach(bool (*func)(void *) )
applist.DoForEach( OutputTeam );
for (int32 i = 0; i < applicationList.CountItems(); i++)
output_team((team_id)applicationList.ItemAt(i), mode);
return 0;
}