Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
htop
Commits
6f58fbc5
Commit
6f58fbc5
authored
Jun 12, 2015
by
Christian Hesse
Browse files
make units more dynamic
Signed-off-by:
Christian Hesse
<
mail@eworm.de
>
parent
f3a9f540
Changes
4
Hide whitespace changes
Inline
Side-by-side
MemoryMeter.c
View file @
6f58fbc5
...
...
@@ -25,8 +25,16 @@ int MemoryMeter_attributes[] = {
};
static
void
MemoryMeter_setValues
(
Meter
*
this
,
char
*
buffer
,
int
size
)
{
int
written
;
Platform_setMemoryValues
(
this
);
snprintf
(
buffer
,
size
,
"%ld/%ldM"
,
(
long
int
)
this
->
values
[
0
]
/
1024
,
(
long
int
)
this
->
total
/
1024
);
written
=
Meter_humanUnit
(
buffer
,
this
->
values
[
0
],
size
);
buffer
+=
written
;
if
((
size
-=
written
)
>
0
)
{
*
buffer
++
=
'/'
;
size
--
;
Meter_humanUnit
(
buffer
,
this
->
total
,
size
);
}
}
static
void
MemoryMeter_display
(
Object
*
cast
,
RichString
*
out
)
{
...
...
Meter.c
View file @
6f58fbc5
...
...
@@ -141,6 +141,34 @@ Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) {
return
this
;
}
int
Meter_humanUnit
(
char
*
buffer
,
unsigned
long
int
value
,
int
size
)
{
const
char
*
prefix
=
"KMGTPEZY"
;
unsigned
long
int
powi
=
1
;
unsigned
int
written
,
powj
=
1
,
precision
=
2
;
for
(;;)
{
if
(
value
/
1024
<
powi
)
break
;
if
(
prefix
[
1
]
==
0
)
break
;
powi
*=
1024
;
++
prefix
;
}
for
(;
precision
>
0
;
precision
--
)
{
powj
*=
10
;
if
(
value
/
powi
<
powj
)
break
;
}
written
=
snprintf
(
buffer
,
size
,
"%.*f%c"
,
precision
,
(
double
)
value
/
powi
,
*
prefix
);
return
written
;
}
void
Meter_delete
(
Object
*
cast
)
{
if
(
!
cast
)
return
;
...
...
Meter.h
View file @
6f58fbc5
...
...
@@ -105,6 +105,8 @@ extern MeterClass Meter_class;
Meter
*
Meter_new
(
struct
ProcessList_
*
pl
,
int
param
,
MeterClass
*
type
);
int
Meter_humanUnit
(
char
*
buffer
,
unsigned
long
int
value
,
int
size
);
void
Meter_delete
(
Object
*
cast
);
void
Meter_setCaption
(
Meter
*
this
,
const
char
*
caption
);
...
...
SwapMeter.c
View file @
6f58fbc5
...
...
@@ -33,9 +33,17 @@ static void SwapMeter_humanNumber(char* buffer, const long int* value) {
sprintf
(
buffer
,
"%ldM "
,
*
value
/
MEGABYTE
);
}
static
void
SwapMeter_setValues
(
Meter
*
this
,
char
*
buffer
,
int
len
)
{
static
void
SwapMeter_setValues
(
Meter
*
this
,
char
*
buffer
,
int
size
)
{
int
written
;
Platform_setSwapValues
(
this
);
snprintf
(
buffer
,
len
,
"%ld/%ldM"
,
(
long
int
)
this
->
values
[
0
]
/
MEGABYTE
,
(
long
int
)
this
->
total
/
MEGABYTE
);
written
=
Meter_humanUnit
(
buffer
,
this
->
values
[
0
],
size
);
buffer
+=
written
;
if
((
size
-=
written
)
>
0
)
{
*
buffer
++
=
'/'
;
size
--
;
Meter_humanUnit
(
buffer
,
this
->
total
,
size
);
}
}
static
void
SwapMeter_display
(
Object
*
cast
,
RichString
*
out
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment