# Sixteen corners

DevFeed: [Sixteen corners](<https://devfeed.tech/articles/sixteen-corners-20971.md>)

Original publisher: [Read original article](<https://jakewharton.com/sixteen-corners/>)

Published: 2020-08-06T00:00:00Z

Content type: article

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [library](<https://devfeed.tech/tags/library.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [terminal](<https://devfeed.tech/tags/terminal.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

The article explains how the Picnic library calculates terminal table corner characters. It replaces nested conditional logic with four boolean values mapped to binary indices into a string of sixteen corner characters, and discusses testing the logic with a single configuration.

## Source excerpt

Last year I built a library called Picnic for rendering data tables in monospaced environments like your terminal. Part of rendering the table is calculating what character to use for each wall and each corner separating the cells. Here's a representative output with a bunch of different corner styles: │ compressed │ uncompressed ├───────────┬───────────┬───────┼───────────┬───────────┬──────── APK │ old │ new │ diff │ old │ new │ diff ──────────┼───────────┼───────────┼───────┼───────────┼───────────┼──────── dex │ 664.8 KiB │ 664.8 KiB │ -25 B │ 1.5 MiB │ 1.5 MiB │ -112 B arsc │ 201.7 KiB │ 201.7 KiB │ 0 B │ 201.6 KiB │ 201.6 KiB │ 0 B manifest │ 1.4 KiB │ 1.4 KiB │ 0 B │ 4.2 KiB │ 4.2 KiB │ 0 B res │ 418.2 KiB │ 418.2 KiB │ -14 B │ 488.3 KiB │ 488.3 KiB │ 0 B asset │ 0 B │ 0 B │ 0 B │ 0 B │ 0 B │ 0 B other │ 37.1 KiB │ 37.1 KiB │ 0 B │ 36.3 KiB │ 36.3 KiB │ 0 B ──────────┼───────────┼───────────┼───────┼───────────┼───────────┼──────── total │ 1.3 MiB │ 1.3 MiB │ -39 B │ 2.2 MiB │ 2.2 MiB │ -112 B Wall border calculation is straightforward. For a vertical wall, a vertical pipe is used if either or both of the two cells wants a border, otherwise an empty space is used.1 Corner calculation is a bit more involved. A corner has four potential segments for the four cardinal directions that may be drawn. The four adjacent cells each participate in the visibility of two segments. Corner Characters Once the code determines the four boolean values for the four segments of a corner we need to map that to the display character. Four booleans produce sixteen possible values. Initially I started with the naive nesting of conditionals to get it working. return if (left) { if (right) { if (up) { if (down) { '┼' } else { '┴' } } else { if (down) { '┬' } else { /*..*/ } } } else { /*..*/ } } Nesting conditionals is an optimization so that each boolean is only checked once. If we wanted, we could flatten the conditionals by repeatedly checking each boolean. if (left && right && up