Skip to content

Commit

Permalink
fix input being eaten after viewing an image
Browse files Browse the repository at this point in the history
  • Loading branch information
Like4Schnitzel committed Mar 2, 2024
1 parent 96c563e commit 49a937b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
20 changes: 20 additions & 0 deletions src/img-viewer/imgviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,24 @@ void ImgViewer::print()
}
}

int ImgViewer::getPixelWidth()
{
return file.size().width;
}

int ImgViewer::getPixelHeight()
{
return file.size().height;
}

int ImgViewer::getTerminalWidth()
{
return width;
}

int ImgViewer::getTerminalHeight()
{
return height;
}

}
10 changes: 10 additions & 0 deletions src/img-viewer/imgviewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class ImgViewer {
ImgViewer(const std::string path);
void transcode(const int width, const int height);
void print();

/// @brief Get the width of the original image file.
int getPixelWidth();
/// @brief Get the height of the original image file.
int getPixelHeight();

/// @brief Get the width of the transcoded image.
int getTerminalWidth();
/// @brief Get the height of the transcoded image.
int getTerminalHeight();
};

}
6 changes: 3 additions & 3 deletions src/media-viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ int main(int argc, char** argv)
{
filesystem::path path;
if (argc > 1)
path = filesystem::absolute(argv[1]);
path = filesystem::relative(argv[1]);
else
path = filesystem::current_path();
path = filesystem::relative(filesystem::current_path());

auto mv = MediaViewer(path);

Expand Down Expand Up @@ -47,7 +47,7 @@ int main(int argc, char** argv)
if (!viewing && std::toupper(kp.keyValue) == 'V')
{
viewing = true;
mv.view(&tu);
mv.view();
}

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

void MediaViewer::view(TermUtils* tu)
void MediaViewer::view()
{
auto currentFile = current();
switch (currentFile->type)
{
case FileType::IMG:
{
int width, height;
auto viewer = ImgViewer(currentFile->path);
if (tu != nullptr)
(*tu).showInput();
std::cout << "Enter width: ";
std::cin >> width;
std::cout << "Enter height: ";
std::cin >> height;
std::cout << "\n";
if (tu != nullptr)
(*tu).hideInput();
viewer.transcode(width, height);
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[0] = 2 * aspectRatio * tDims[1];

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

viewer.transcode(tDims[0], tDims[1]);
viewer.print();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/media-viewer/mediaviewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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(TermUtils* tu = nullptr);
void view();
};

}

0 comments on commit 49a937b

Please sign in to comment.