Skip to content

Commit bd3a35f

Browse files
Merge pull request #19 from richardwilkes/v2-wip
Sweeping changes to reorganize the packages, fix some things, improve others, and drop some old functionality that was no longer needed by any of my projects. Going forward, I'll try to adhere to the normal version compatibility guidelines for Go code, unlike with the v1 code base. I hope to find time in the near future to add a V1-TO-V2-MIGRATION.md file to the repo to help existing users of v1 make the transition. If you need help finding something or understanding a change, feel free to reach out to me in the meantime.
2 parents f744225 + 652ff15 commit bd3a35f

File tree

299 files changed

+24462
-13457
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

299 files changed

+24462
-13457
lines changed

.github/copilot-instructions.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copilot Instructions for Toolbox
2+
3+
## Project Overview
4+
5+
This is a comprehensive Go utility library (v2) providing extended functionality for common patterns. The codebase follows a strict "x-prefix" convention where packages extending standard library functionality are prefixed with "x" (e.g., `xmath`, `xio`, `xhttp`).
6+
7+
## Key Architecture Patterns
8+
9+
### Generics-First Design
10+
11+
- Heavy use of Go generics with type constraints (see `xmath.Numeric` interface)
12+
- Generic data structures like `Point[T xmath.Numeric]`, `Tree[K, V any]`, `Int[T fixed.Dx]`
13+
- Type conversion functions like `ConvertPoint[T, F xmath.Numeric](pt Point[F]) Point[T]`
14+
15+
### Error Handling Convention
16+
17+
- Use `errs.Error` for structured errors with stack traces instead of basic Go errors
18+
- All errors include source location and can be chained: `errs.NewWithCause("message", causeErr)`
19+
- The `errs` package provides `ErrorWrapper` and `StackError` interfaces
20+
- Use `xos.PanicRecovery()` for panic handling in defer statements
21+
22+
### Testing Patterns
23+
24+
- Use the internal `check` package instead of raw testing.T: `c := check.New(t)`
25+
- Testing methods: `c.Equal(expected, actual)`, `c.HasPrefix()`, `c.NoError()`, `c.HasError()`
26+
- Example tests follow `ExampleFunction` naming for documentation
27+
- All test files use `package_test` naming convention
28+
29+
### Fixed-Point Arithmetic
30+
31+
- `fixed64.Int[T]` and `fixed128.Int[T]` for precise decimal calculations
32+
- Always use `.Mul()` and `.Div()` methods, never direct operators for multiplication/division
33+
- Different precision types via type parameters: `fixed64.Int[fixed.D2]` for 2 decimal places
34+
35+
### Command-Line Applications
36+
37+
- Use `xflag.SetUsage()` for rich CLI help with version info and formatting
38+
- Set app metadata in main(): `xos.AppIdentifier`, `xos.CopyrightStartYear`, etc.
39+
- Use `i18n.Text()` for all user-facing strings for localization support
40+
41+
## Build and Development
42+
43+
### Build System
44+
45+
```bash
46+
./build.sh --all # Full build with linting and race-checked tests
47+
./build.sh --lint # Run golangci-lint (auto-installs latest version)
48+
./build.sh --test # Run tests
49+
./build.sh --race # Run tests with race detection
50+
```
51+
52+
### Dependencies
53+
54+
- Minimal external dependencies: only `golang.org/x/*` and `gopkg.in/yaml.v3`
55+
- Self-contained utilities - avoid adding new external deps without strong justification
56+
57+
## Package-Specific Notes
58+
59+
### Geometry (`geom/`)
60+
61+
- All geometric types are generic: `Point[T]`, `Rect[T]`, `Size[T]` with `xmath.Numeric` constraint
62+
- Conversion between numeric types via `ConvertPoint`, `ConvertRect`, etc.
63+
64+
### Collections (`collection/`)
65+
66+
- Red-black tree: `redblack.New[K, V any](compareFunc)` - requires comparison function
67+
- Bitset and quadtree implementations available
68+
69+
### Extended Standard Library
70+
71+
- `xmath`: Generic math functions with proper type constraints
72+
- `xio`: BOM stripping, safe file operations
73+
- `xstrings`: Enhanced string utilities
74+
- `xhttp`: HTTP utilities with metadata and compression helpers
75+
76+
### 128-bit Numbers (`num128/`)
77+
78+
- `Int` and `UInt` types for 128-bit arithmetic
79+
- Always check for overflow in mathematical operations
80+
- String conversion methods handle different bases
81+
82+
## File Organization
83+
84+
- Each package should have comprehensive tests (`*_test.go`)
85+
- Example tests for public APIs to serve as documentation
86+
- License header on all files (Mozilla Public License 2.0)
87+
- Internal utilities in appropriate x-prefixed packages

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ linters:
3737
- fmt.Fprintf
3838
- fmt.Fprintln
3939
- (fmt.State).Write
40+
- (*github.com/richardwilkes/toolbox/v2/xterm.AnsiWriter).Write
41+
- (*github.com/richardwilkes/toolbox/v2/xterm.AnsiWriter).WriteByte
42+
- (*github.com/richardwilkes/toolbox/v2/xterm.AnsiWriter).WriteString
4043
goconst:
4144
min-len: 3
4245
min-occurrences: 3
@@ -52,6 +55,7 @@ linters:
5255
gosec:
5356
excludes:
5457
- G103
58+
- G104
5559
- G115
5660
- G204
5761
- G301

README.md

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,108 @@
11
# toolbox
22

3-
[![Go Reference](https://pkg.go.dev/badge/github.com/richardwilkes/toolbox.svg)](https://pkg.go.dev/github.com/richardwilkes/toolbox)
4-
[![Go Report Card](https://goreportcard.com/badge/github.com/richardwilkes/toolbox)](https://goreportcard.com/report/github.com/richardwilkes/toolbox)
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/richardwilkes/toolbox/v2.svg)](https://pkg.go.dev/github.com/richardwilkes/toolbox/v2)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/richardwilkes/toolbox/v2)](https://goreportcard.com/report/github.com/richardwilkes/toolbox/v2)
55

6-
Toolbox for Go. Contains a wide variety of packages I've found useful in my own projects over the years.
6+
Toolbox for Go.
77

8-
> [!NOTE]
9-
> This library already had a v1.x.y when Go modules were first introduced. Due to this, it doesn't follow the
10-
> normal convention and instead treats its releases as if they are of the v0.x.y variety (i.e. it could introduce
11-
> breaking API changes). Keep this in mind when deciding whether or not to use it.
8+
Contains a wide variety of code I've found useful in my own projects over the years. For cases where code exists to help
9+
use standard library code, the package has been named the same as the standard library one, but with a preceeding "x"
10+
(for extended). This allows both to be used in the same file without having to do import renaming.
11+
12+
## Package Overview
13+
14+
### Core Utilities
15+
16+
- **`check`** - Enhanced testing utilities that wrap Go's standard testing interface with more informative error messages and convenient assertion methods.
17+
18+
- **`errs`** - Structured error handling with stack traces, error chaining, and detailed error objects that provide source locations and nested causes for better debugging.
19+
20+
- **`i18n`** - Internationalization support for applications, providing localization capabilities for user-facing text and messages.
21+
22+
- **`notifier`** - Event notification system for implementing the observer pattern, allowing objects to register for and receive notifications about events.
23+
24+
- **`tid`** - Thread-safe unique identifier generation using cryptographically secure random values encoded in base64.
25+
26+
### Mathematical and Numerical
27+
28+
- **`xmath`** - Extended mathematical functions with generic type constraints (e.g., `Numeric` interface) that work across integer and floating-point types.
29+
30+
- **`num128`** - 128-bit integer arithmetic with signed (`Int`) and unsigned (`UInt`) types for high-precision calculations that exceed native Go integer limits.
31+
32+
- **`fixed`** - Fixed-point decimal arithmetic for precise financial and monetary calculations, with separate packages for 64-bit (`fixed64`) and 128-bit (`fixed128`) precision.
33+
34+
### Geometry and Graphics
35+
36+
- **`geom`** - Generic geometric primitives including `Point[T]`, `Rect[T]`, `Size[T]`, `Line[T]`, `Matrix[T]`, and `Insets[T]` with type conversion utilities.
37+
38+
- **`geom/poly`** - Provides polygon operations using fixed-point arithmetic for reliable geometric computations.
39+
40+
- **`geom/visibility`** - 2D visibility polygon computation for line-of-sight calculations.
41+
42+
### Data Structures and Collections
43+
44+
- **`collection/bitset`** - Efficient bitset implementation for working with sets of bits.
45+
46+
- **`collection/quadtree`** - Spatial data structure for efficient 2D range queries and collision detection.
47+
48+
- **`collection/redblack`** - Generic red-black tree implementation providing balanced binary search tree functionality.
49+
50+
### Extended Standard Library
51+
52+
- **`xbytes`** - Extended byte manipulation utilities including BOM (Byte Order Mark) handling and buffer utilities.
53+
54+
- **`xcrc64`** - Extended CRC64 checksum utilities for data integrity verification.
55+
56+
- **`xcrypto`** - Cryptographic utilities including stream encryption/decryption helpers.
57+
58+
- **`xfilepath`** - Extended file path utilities with filename manipulation, root detection, and cross-platform path splitting.
59+
60+
- **`xflag`** - Enhanced command-line flag parsing with rich usage formatting, automatic version flags, and post-parse function handling.
61+
62+
- **`xhash`** - Extended hashing utilities for creating consistent hash values across different data types.
63+
64+
- **`xhttp`** - HTTP utilities including basic authentication, gzip handling, metadata management, file retrieval, and server helpers.
65+
66+
- **`ximage`** - Image processing utilities with support for various image formats.
67+
68+
- **`xio`** - Extended I/O utilities including BOM stripping, safe file closing, and specialized readers/writers.
69+
70+
- **`xjson`** - Enhanced JSON handling utilities for parsing and marshaling with additional features.
71+
72+
- **`xnet`** - Network utilities for address manipulation and network-related operations.
73+
74+
- **`xos`** - Operating system utilities including application information, browser launching, filesystem operations, panic recovery, safe file handling, task queues, and user information.
75+
76+
- **`xrand`** - Extended random number generation utilities for cryptographically secure randomness.
77+
78+
- **`xreflect`** - Reflection utilities for working with Go's reflection API more effectively.
79+
80+
- **`xruntime`** - Runtime utilities including detailed stack trace generation with source location information.
81+
82+
- **`xslices`** - Enhanced slice manipulation utilities including column-based sorting and other advanced slice operations.
83+
84+
- **`xslog`** - Enhanced structured logging with a "pretty" formatter that provides colorful output, stack trace formatting, and improved readability.
85+
86+
- **`xstrings`** - String manipulation utilities including case conversion, text wrapping, natural sorting, emoji handling, capitalization, space collapsing, and various string processing functions.
87+
88+
- **`xsync`** - Synchronization utilities extending Go's sync package with additional concurrent programming tools.
89+
90+
- **`xterm`** - Terminal utilities for ANSI color codes, terminal detection, and formatted output with cross-platform compatibility.
91+
92+
- **`xtime`** - Time manipulation utilities extending Go's time package with additional date/time functionality.
93+
94+
- **`xunicode`** - Unicode utilities for advanced text processing and character manipulation.
95+
96+
- **`xyaml`** - YAML processing utilities for parsing and marshaling YAML data with enhanced features.
97+
98+
### Specialized Utilities
99+
100+
- **`rate`** - Rate limiting with hierarchical limiters, where each limiter can be capped by its parent, useful for implementing tiered rate limiting.
101+
102+
- **`softref`** - Soft reference implementation for memory management, allowing resources to be garbage collected when memory pressure occurs.
103+
104+
### Command Line Tools
105+
106+
- **`cmd/i18n`** - Command-line tool for extracting and managing internationalization strings from Go source code.
107+
108+
All packages follow consistent patterns with use of Go generics for type safety where appropriate, comprehensive error handling with the `errs` package, and thorough testing with the `check` package. The "x" prefix convention allows seamless use alongside standard library packages without import conflicts.

appdir.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

atexit/atexit.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ fi
6565

6666
# Install executables
6767
echo -e "\033[33mInstalling executables...\033[0m"
68-
go install -v ./i18n/i18n
68+
go install -v ./cmd/i18n

0 commit comments

Comments
 (0)