2016-02-10 17:22:34 -05:00
|
|
|
/* 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"
|
2016-03-05 05:19:31 -05:00
|
|
|
#include "powerline.h"
|
2016-02-10 17:22:34 -05:00
|
|
|
|
|
|
|
std::string mem_string( const MemoryStatus & mem_status,
|
|
|
|
MEMORY_MODE mode,
|
2016-03-05 05:19:31 -05:00
|
|
|
bool use_colors,
|
|
|
|
bool use_powerline )
|
2016-02-10 17:22:34 -05:00
|
|
|
{
|
|
|
|
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 )
|
|
|
|
{
|
2016-03-05 05:19:31 -05:00
|
|
|
unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem);
|
|
|
|
powerline(oss, mem_lut[color], use_powerline);
|
2016-02-10 17:22:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ) << '/'
|
2016-03-06 10:57:38 -05:00
|
|
|
<< static_cast< unsigned int >( mem_status.total_mem ) << "MB";
|
2016-02-10 17:22:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if( use_colors )
|
|
|
|
{
|
2016-03-05 05:19:31 -05:00
|
|
|
if( use_powerline )
|
|
|
|
{
|
|
|
|
oss << " ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oss << "#[fg=default,bg=default]";
|
|
|
|
}
|
2016-02-10 17:22:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|