Skip to content

Commit

Permalink
#5108: Add file size to FileSystemView.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 19, 2020
1 parent d110939 commit c438870
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
29 changes: 29 additions & 0 deletions libs/os/filesize.h
@@ -0,0 +1,29 @@
#pragma once

#include <fmt/format.h>

namespace os
{

// Formats the given number in bytes/kB/MB/GB
inline std::string getFormattedFileSize(std::size_t size)
{
if (size > 1024 * 1024 * 1024)
{
return fmt::format("{0:0.2f} GB", (static_cast<double>(size) / (1024 * 1024 * 1024)));
}
else if (size > 1024 * 1024)
{
return fmt::format("{0:0.1f} MB", (static_cast<double>(size) / (1024 * 1024)));
}
else if (size > 1024)
{
return fmt::format("{0:0.0f} kB", (static_cast<double>(size) / 1024));
}
else
{
return fmt::format("{0:d} bytes", size);
}
}

}
3 changes: 3 additions & 0 deletions libs/wxutil/fsview/FileSystemView.cpp
Expand Up @@ -50,6 +50,9 @@ FileSystemView::FileSystemView(wxWindow* parent, const TreeModel::Ptr& model, lo
AppendIconTextColumn(_("File"), Columns().filename.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);

AppendTextColumn(_("Size"), Columns().size.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_RIGHT, wxDATAVIEW_COL_SORTABLE);

// Get selection and connect the changed callback
Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &FileSystemView::OnSelectionChanged, this);
Bind(EV_TREEMODEL_POPULATION_FINISHED, &FileSystemView::OnTreeStorePopulationFinished, this);
Expand Down
6 changes: 6 additions & 0 deletions libs/wxutil/fsview/Populator.cpp
@@ -1,7 +1,9 @@
#include "Populator.h"

#include "iuimanager.h"
#include "iarchive.h"
#include "os/path.h"
#include "os/filesize.h"

#include <wx/artprov.h>

Expand Down Expand Up @@ -123,6 +125,10 @@ void Populator::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& ro
isExplicit ? _fileIcon : _folderIcon));
row[_columns.vfspath] = _basePath + path;
row[_columns.isFolder] = !isExplicit;

// Get the file size if possible
auto file = GlobalFileSystem().openFile(_basePath + path);
row[_columns.size] = os::getFormattedFileSize(file ? file->size() : -1);
}

}
Expand Down
14 changes: 8 additions & 6 deletions libs/wxutil/fsview/Populator.h
Expand Up @@ -15,14 +15,16 @@ struct TreeColumns :
public TreeModel::ColumnRecord
{
TreeColumns() :
filename(add(wxutil::TreeModel::Column::IconText)),
vfspath(add(wxutil::TreeModel::Column::String)),
isFolder(add(wxutil::TreeModel::Column::Boolean))
filename(add(TreeModel::Column::IconText)),
vfspath(add(TreeModel::Column::String)),
isFolder(add(TreeModel::Column::Boolean)),
size(add(TreeModel::Column::String))
{}

wxutil::TreeModel::Column filename; // e.g. "chair1.pfb"
wxutil::TreeModel::Column vfspath; // e.g. "prefabs/chair1.pfb"
wxutil::TreeModel::Column isFolder; // whether this is a folder
TreeModel::Column filename; // e.g. "chair1.pfb"
TreeModel::Column vfspath; // e.g. "prefabs/chair1.pfb"
TreeModel::Column isFolder; // whether this is a folder
TreeModel::Column size; // file size string
};

class Populator :
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -174,6 +174,7 @@
<ClInclude Include="..\..\libs\ObservedUndoable.h" />
<ClInclude Include="..\..\libs\os\dir.h" />
<ClInclude Include="..\..\libs\os\file.h" />
<ClInclude Include="..\..\libs\os\filesize.h" />
<ClInclude Include="..\..\libs\os\fs.h" />
<ClInclude Include="..\..\libs\os\path.h" />
<ClInclude Include="..\..\libs\parser\CodeTokeniser.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -265,6 +265,9 @@
<ClInclude Include="..\..\libs\stream\ExportStream.h">
<Filter>stream</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\os\filesize.h">
<Filter>os</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit c438870

Please sign in to comment.