ganonbit/format-text
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
# format-text - Ruby Text Formatter
A command-line tool for formatting text into 80-character paragraphs written in Ruby.
## Quick Start
```bash
# Install dependencies
bundle install
# Run formatter
./bin/format-text input.txt
# Run tests
bundle exec rspec
```
## Requirements
- Ruby 4.0.1
- Bundler
## Usage
```bash
./bin/format-text <filename>
```
The tool reads the specified file and outputs formatted text to stdout.
### Example
**Input** (`input.txt`):
```bash
This is
a badly formatted file.
```
**Output:**
```bash
This is a badly formatted file.
```
## Formatting Rules
- Lines wrapped at 80 characters
- Words are not broken mid-word
- Single blank line between paragraphs
- Multiple spaces/blank lines are collapsed
## Architecture
The tool uses a three-layer design:
```bash
TextFormatter (orchestrator)
├── ParagraphParser (splits into paragraphs)
└── LineWrapper (wraps lines to 80 chars)
```
### Design Decisions
**Why separate classes?**
- Single Responsibility Principle
- Easier to test each component independently
- Clear separation between paragraph detection and line wrapping
**Why word-based approach?**
- Simpler than regex manipulation
- Naturally handles edge cases (long words, multiple spaces)
- More maintainable and readable
**Why fixtures in tests?**
- Keeps test specs clean and focused
- Easy to add new test cases
- Separates test data from test logic
## Testing
```bash
# Run all tests
bundle exec rspec
# Run specific test
bundle exec rspec spec/line_wrapper_spec.rb
```
Test coverage includes:
- Integration test with provided example
- Edge cases (long words, multiple blank lines, empty input)
- Unit tests for each component
## Development
```bash
# Check code style
bundle exec rubocop
```
## Edge Cases Handled
- Words longer than 80 characters (kept on their own line)
- Multiple consecutive spaces (collapsed to single space)
- Multiple blank lines (collapsed to single blank line)
- Empty or whitespace-only input
- No trailing newline in input