Fix screen rendering issues #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build widecharwidth library | |
on: | |
push: | |
branches: [ main ] | |
pull_request: | |
branches: [ main ] | |
workflow_dispatch: # Allow manual triggering | |
jobs: | |
build: | |
name: Build on ${{ matrix.os }} | |
runs-on: ${{ matrix.os }} | |
strategy: | |
fail-fast: false | |
matrix: | |
os: [ ubuntu-latest, macos-latest, windows-latest ] | |
include: | |
- os: ubuntu-latest | |
output_name: libwidecharwidth.so | |
lib_dir: linux | |
- os: macos-latest | |
output_name: libwidecharwidth.dylib | |
lib_dir: macos | |
- os: windows-latest | |
output_name: widecharwidth.dll | |
lib_dir: windows | |
steps: | |
- name: Checkout main repository | |
uses: actions/checkout@v4 | |
- name: Create libs directory structure | |
run: mkdir -p libs/${{ matrix.lib_dir }} | |
- name: Checkout widecharwidth repository | |
uses: actions/checkout@v4 | |
with: | |
repository: ridiculousfish/widecharwidth | |
path: widecharwidth-src | |
- name: Set up GCC (Linux) | |
if: runner.os == 'Linux' | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y build-essential | |
- name: Set up GCC (macOS) | |
if: runner.os == 'macOS' | |
run: | | |
brew install gcc | |
- name: Set up MinGW (Windows) | |
if: runner.os == 'Windows' | |
run: | | |
choco install mingw --version=8.1.0 -y | |
# Create a simple C file that includes the C header | |
- name: Create C wrapper file | |
shell: bash | |
run: | | |
echo '#include "widecharwidth-src/widechar_width_c.h"' > wrapper.c | |
echo '' >> wrapper.c | |
echo '// The function is already available from the header' >> wrapper.c | |
echo '// We just need to compile it into a shared library' >> wrapper.c | |
- name: Build shared library (Linux) | |
if: runner.os == 'Linux' | |
run: | | |
gcc -shared -fPIC -o ${{ matrix.output_name }} wrapper.c | |
- name: Build shared library (macOS) | |
if: runner.os == 'macOS' | |
run: | | |
gcc -shared -fPIC -o ${{ matrix.output_name }} wrapper.c | |
- name: Build shared library (Windows) | |
if: runner.os == 'Windows' | |
run: | | |
gcc -shared -o ${{ matrix.output_name }} wrapper.c | |
- name: Move library to libs directory | |
run: | | |
mv ${{ matrix.output_name }} libs/${{ matrix.lib_dir }}/ | |
- name: Upload library artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ matrix.lib_dir }}-${{ matrix.output_name }} | |
path: libs/${{ matrix.lib_dir }}/${{ matrix.output_name }} | |
# Combine all libraries and commit back to the repo | |
commit: | |
name: Commit Libraries to Repository | |
needs: build | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Create libs directory | |
run: mkdir -p libs/{linux,macos,windows} | |
- name: Download Linux library | |
uses: actions/download-artifact@v4 | |
with: | |
name: linux-libwidecharwidth.so | |
path: libs/linux | |
- name: Download macOS library | |
uses: actions/download-artifact@v4 | |
with: | |
name: macos-libwidecharwidth.dylib | |
path: libs/macos | |
- name: Download Windows library | |
uses: actions/download-artifact@v4 | |
with: | |
name: windows-widecharwidth.dll | |
path: libs/windows | |
- name: List contents of libs directory | |
run: | | |
ls -la libs/ | |
ls -la libs/linux/ || echo "Linux library missing" | |
ls -la libs/macos/ || echo "macOS library missing" | |
ls -la libs/windows/ || echo "Windows library missing" | |
- name: Create README for libs directory | |
run: | | |
cat > libs/README.md << 'EOL' | |
# Widecharwidth Library Binaries | |
This directory contains pre-compiled binaries of the [widecharwidth](https://github.com/ridiculousfish/widecharwidth) library for: | |
- Linux: `linux/libwidecharwidth.so` | |
- macOS: `macos/libwidecharwidth.dylib` | |
- Windows: `windows/widecharwidth.dll` | |
These libraries are automatically compiled by GitHub Actions from the original source code. | |
## Usage with PHP FFI | |
```php | |
<?php | |
// Load the appropriate library based on the OS | |
if (PHP_OS_FAMILY === 'Darwin') { | |
$lib = __DIR__ . '/libs/macos/libwidecharwidth.dylib'; | |
} elseif (PHP_OS_FAMILY === 'Windows') { | |
$lib = __DIR__ . '/libs/windows/widecharwidth.dll'; | |
} else { | |
$lib = __DIR__ . '/libs/linux/libwidecharwidth.so'; | |
} | |
// Define the function signature | |
$ffi = FFI::cdef("int widechar_wcwidth(unsigned int code_point);", $lib); | |
// Example usage - handle special return values appropriately | |
$width = $ffi->widechar_wcwidth(0x1F600); // 😀 | |
echo "Width: " . ($width > 0 ? $width : 0) . "\n"; // Convert negative values to 0 | |
``` | |
EOL | |
- name: Configure Git | |
run: | | |
git config --local user.email "action@github.com" | |
git config --local user.name "GitHub Action" | |
- name: Commit files | |
run: | | |
git add libs/ | |
git commit -m "Update widecharwidth libraries" -m "Automatically compiled by GitHub Actions" || echo "No changes to commit" | |
- name: Push changes | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: ${{ github.ref }} |