Skip to content

Commit

Permalink
auto display images
Browse files Browse the repository at this point in the history
  • Loading branch information
Like4Schnitzel committed Mar 2, 2024
1 parent 49a937b commit 9b8137b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/media-viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ int main(int argc, char** argv)
bool viewing = false;
TermUtils tu;
int fileIndex = 0;
cout << "Selected file: " << mv.current()->path << "\n" << flush;
unsigned char keyValue;
cout << "waiting for a keypress...\npress ESC to close the program.\n" << flush;
cout << "Selected file: " << mv.current()->path << "\n" << flush;
tu.hideInput();
while (true)
{
Expand All @@ -43,11 +42,17 @@ int main(int argc, char** argv)
if (!viewing && kp.keyValue == 1792836)
cout << "Selected file: " << mv.prev()->path << "\n";

// V key
if (!viewing && std::toupper(kp.keyValue) == 'V')
// displaying a file
if ((!viewing && std::toupper(kp.keyValue) == 'V') || mv.current()->type == FileType::IMG)
{
viewing = true;
mv.view();
if (mv.current()->type != FileType::IMG)
{
viewing = true;
}

auto sizeToUse = TermUtils::getTerminalDimensions();
sizeToUse[1] -= 2; // leave space for the file name line
mv.view(sizeToUse);
}

// ESC
Expand Down
9 changes: 4 additions & 5 deletions src/media-viewer/mediaviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ File* MediaViewer::prev()
return &files[filesIndex];
}

void MediaViewer::view()
void MediaViewer::view(std::array<int, 2> maxDims)
{
auto currentFile = current();
switch (currentFile->type)
Expand All @@ -142,19 +142,18 @@ void MediaViewer::view()
auto pDims = std::array<int, 2>();
pDims[0] = viewer.getPixelWidth();
pDims[1] = viewer.getPixelHeight();
auto tMax = TermUtils::getTerminalDimensions();

double aspectRatio = (double) pDims[0] / pDims[1];
std::array<double, 2> tDims;

// try maxing out height
tDims[1] = tMax[1];
tDims[1] = maxDims[1];
tDims[0] = 2 * aspectRatio * tDims[1];

// if that doesn't work, max out width
if (tDims[0] > tMax[0])
if (tDims[0] > maxDims[0])
{
tDims[0] = tMax[0];
tDims[0] = maxDims[0];
tDims[1] = 0.5 * tDims[0] / aspectRatio;
}

Expand Down
4 changes: 3 additions & 1 deletion src/media-viewer/mediaviewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class MediaViewer {
/// @brief Move to the previous file in the list.
/// @return A pointer to the previous file in the list, or nullptr if the directory is empty.
File* prev();
void view();
/// @brief Displays the currently selected file's contents in the terminal.
/// @param maxDims The maximum allowed size to use for the displaying. The image will be scaled to this as far as possible while staying close to the original aspect ratio.
void view(std::array<int, 2> maxDims = TermUtils::getTerminalDimensions());
};

}

0 comments on commit 9b8137b

Please sign in to comment.