Skip to content

Commit

Permalink
chafa: Assume square grid tiles if only one dimension specified
Browse files Browse the repository at this point in the history
  • Loading branch information
hpjansson committed Dec 23, 2024
1 parent 4ef09f9 commit 506c0d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
7 changes: 4 additions & 3 deletions tools/chafa/chafa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,11 +1238,11 @@ parse_grid_arg (G_GNUC_UNUSED const gchar *option_name, const gchar *value, G_GN
}
else if (width < 0)
{
width = height;
width = -1;
}
else if (height < 0)
{
height = width;
height = -1;
}

options.grid_width = width;
Expand Down Expand Up @@ -3276,7 +3276,8 @@ run_grid (GList *filenames)

tty_options_init ();

canvas_config = build_config (options.width, options.height, FALSE);
/* The prototype canvas' size isn't used for anything; set it to a legal value */
canvas_config = build_config (1, 1, FALSE);

grid_layout = grid_layout_new ();
grid_layout_set_view_size (grid_layout, options.width, options.height);
Expand Down
43 changes: 37 additions & 6 deletions tools/chafa/grid-layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,48 @@ update_geometry (GridLayout *grid)
gint view_width, view_height;
gint n_cols, n_rows;
gint item_width, item_height;
gint cell_width_px, cell_height_px;
gint font_ratio;

if (!grid->canvas_config)
return;

chafa_canvas_config_get_cell_geometry (grid->canvas_config, &cell_width_px, &cell_height_px);
if (cell_width_px < 1 || cell_height_px < 1)
{
cell_width_px = 10;
cell_height_px = 20;
}

view_width = MAX (grid->view_width, 1);
view_height = MAX (grid->view_height, 1);
n_cols = MAX (grid->n_cols, 1);
n_rows = MAX (grid->n_rows, 1);
n_cols = grid->n_cols;
n_rows = grid->n_rows;

item_width = MAX (view_width / n_cols - 1, 1);
item_height = MAX (view_height / n_rows - 1, 1);
if (n_cols < 1 && n_rows < 1)
n_cols = n_rows = 1;

if (grid->canvas_config)
chafa_canvas_config_set_geometry (grid->canvas_config, item_width, item_height);
/* If one dimension is not provided, make square tiles */

if (n_cols < 1)
{
item_height = view_height / n_rows - 1;
item_width = (item_height * cell_height_px) / cell_width_px;
}
else if (n_rows < 1)
{
item_width = view_width / n_cols - 1;
item_height = (item_width * cell_width_px) / cell_height_px;
}
else
{
item_width = view_width / n_cols - 1;
item_height = view_height / n_rows - 1;
}

item_width = MAX (item_width, 1);
item_height = MAX (item_height, 1);
chafa_canvas_config_set_geometry (grid->canvas_config, item_width, item_height);
}

static ChafaCanvas *
Expand Down

0 comments on commit 506c0d9

Please sign in to comment.