Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
# To install the pre-commit hook: pre-commit install
# To run the command: pre-commit run --all-files

fail_fast: false
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.0
hooks:
- id: gitleaks

- repo: https://github.com/sirosen/texthooks
rev: 0.7.1
hooks:
- id: fix-smartquotes
exclude: assets/models|sponge/deps

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: detect-private-key
- id: detect-aws-credentials
args: ["--allow-missing-credentials"]
- id: mixed-line-ending
exclude: assets/models|sponge/deps
args: ["--fix=lf"]
- id: trailing-whitespace
exclude: assets/models|sponge/deps
- id: end-of-file-fixer
exclude: assets/models|sponge/deps
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-merge-conflict

- repo: https://github.com/crate-ci/typos
rev: v1.44.0
hooks:
- id: typos
exclude: .clang-tidy|assets/models|sponge/deps

- repo: https://github.com/MaratFM/pre-commit-remark
rev: 1.0.0
hooks:
- id: remark
args: ["--output", "--frail"]
files: "\\.md$"
exclude: assets/models|sponge/deps
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

A Go library demonstrating line-breaking algorithms with optimal text formatting.


## Overview

This project implements two line-breaking algorithms to format text within specified width constraints:

- **Greedy Algorithm**: Fast, simple approach that fits as many words as possible on each line
- **Knuth-Plass Algorithm**: Sophisticated dynamic programming solution that optimizes line breaks based on penalty functions
* **Greedy Algorithm**: Fast, simple approach that fits as many words as possible on each line
* **Knuth-Plass Algorithm**: Sophisticated dynamic programming solution that optimizes line breaks based on penalty functions

## Features

- **Multiple Algorithms:** Choose between greedy and Knuth-Plass line breaking
- **Dynamic Programming:** Efficiently computes optimal line breaks. It uses a penalty for trailing spaces to promote balanced text.
- **Unit Tested:** Includes unit tests to ensure quality and to allow future changes with confidence.
* **Multiple Algorithms:** Choose between greedy and Knuth-Plass line breaking
* **Dynamic Programming:** Efficiently computes optimal line breaks. It uses a penalty for trailing spaces to promote balanced text.
* **Unit Tested:** Includes unit tests to ensure quality and to allow future changes with confidence.

## Installation and Running

Expand All @@ -36,11 +35,14 @@ go get github.com/tomconder/linebreak/pkg/linebreak

The [greedy algorithm](https://en.wikipedia.org/wiki/Greedy_algorithm) breaks a sequence of words into lines. At each step it fits as many words as possible on each line within the given width.

For example, the following text with a given width of 14:
For example, the following text with a given width of 14:

```
The lazy yellow dog was caught by the slow red fox as he lay sleeping in the sun
```

gives the following result

```
The lazy
yellow dog was
Expand All @@ -56,10 +58,13 @@ the sun
The [Knuth-Plass line breaking algorithm](https://en.wikipedia.org/wiki/Knuth%E2%80%93Plass_line-breaking_algorithm) breaks a sequence of words into lines that do not exceed the given width. It uses [dynamic programming](https://en.wikipedia.org/wiki/Dynamic_programming) to determine optimal breakpoints based on a penalty function that discourages trailing empty spaces.

For example, the following text with a given width of 14:

```
The lazy yellow dog was caught by the slow red fox as he lay sleeping in the sun
```

gives the following result

```
The lazy
yellow dog
Expand Down
Loading