42275e094d
Refactor status string generation into a common function.
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
|
|
*
|
|
* Copyright 2012 Matthew McCormick
|
|
* Copyright 2015 Pawel 'l0ner' Soltys
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include <sstream>
|
|
|
|
#include "memory.h"
|
|
#include "luts.h"
|
|
#include "conversions.h"
|
|
|
|
std::string mem_string( const MemoryStatus & mem_status,
|
|
MEMORY_MODE mode,
|
|
bool use_colors )
|
|
{
|
|
std::ostringstream oss;
|
|
// Change the percision for floats, for a pretty output
|
|
oss.precision( 2 );
|
|
oss.setf( std::ios::fixed | std::ios::right );
|
|
|
|
if( use_colors )
|
|
{
|
|
oss << mem_lut[static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem)];
|
|
}
|
|
|
|
switch( mode )
|
|
{
|
|
case MEMORY_MODE_FREE_MEMORY: // Show free memory in MB or GB
|
|
{
|
|
const float free_mem = mem_status.total_mem - mem_status.used_mem;
|
|
const float free_mem_in_gigabytes = convert_unit( free_mem, GIGABYTES, MEGABYTES );
|
|
|
|
// if free memory is less than 1 GB, use MB instead
|
|
if( free_mem_in_gigabytes < 1.0f )
|
|
{
|
|
oss << free_mem << "MB";
|
|
}
|
|
else
|
|
{
|
|
oss << free_mem_in_gigabytes << "GB";
|
|
}
|
|
break;
|
|
}
|
|
case MEMORY_MODE_USAGE_PERCENTAGE:
|
|
{
|
|
// Calculate the percentage of used memory
|
|
const float percentage_mem = mem_status.used_mem /
|
|
static_cast<float>( mem_status.total_mem ) * 100.0;
|
|
|
|
oss << percentage_mem << '%';
|
|
break;
|
|
}
|
|
default: // Default mode, just show the used/total memory in MB
|
|
oss << static_cast< unsigned int >( mem_status.used_mem ) << '/'
|
|
<< static_cast< unsigned int >( mem_status.used_mem ) << "MB";
|
|
}
|
|
|
|
if( use_colors )
|
|
{
|
|
oss << "#[fg=default,bg=default]";
|
|
}
|
|
|
|
return oss.str();
|
|
}
|
|
|