Skip to content

Commit

Permalink
add simargs (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 authored Oct 21, 2023
1 parent 87e7159 commit b6fcc93
Show file tree
Hide file tree
Showing 25 changed files with 1,299 additions and 242 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Bug Report
labels: ['bug']
labels: ["bug"]
description: Report to help us improve
body:
- type: markdown
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ jobs:
zig fmt --check .
zig build test -Dis_ci=true
zig build -Dis_ci=true
cross-compile:
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
targets: [x86_64-windows, x86_64-linux, x86_64-macos, aarch64-macos]
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Build
run: |
zig build -Dtarget=${{ matrix.targets }} -Dis_ci
12 changes: 4 additions & 8 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
name: Deploy docs

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
paths:
- "docs/**"
- ".github/workflows/docs.yml"

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

# Default to bash
defaults:
run:
shell: bash

jobs:
# Build job
build:
runs-on: ubuntu-latest
env:
Expand All @@ -42,7 +38,7 @@ jobs:
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.111.3'
hugo-version: '0.119.0'
extended: true
- name: Build with Hugo
working-directory: docs
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/simargs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Simargs CI

on:
workflow_dispatch:
pull_request:
paths:
- 'src/mod/simargs.zig'
- ".github/workflows/simargs.yml"
push:
branches:
- main
paths:
- 'src/mod/simargs.zig'
- ".github/workflows/simargs.yml"

jobs:
memory-leak:
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
- name: Run Test
run: zig build test
- name: Memory leak detect
run: |
sudo apt update && sudo apt install -y valgrind
zig build -Dcpu=baseline --verbose
TEST_BINARY=./zig-out/bin/simargs-demo
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${TEST_BINARY} --output a.out \
hello world
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
build:
zig build -Doptimize=ReleaseFast \
-Dbuild_date=$(shell date +"%Y-%m-%dT%H:%M:%S%z") \
-Dgit_commit=$(shell git rev-parse --short HEAD)
-Dgit_commit=$(shell git rev-parse --short HEAD) \
--summary all

fmt:
zig fmt --check .

test:
zig build test
zig build test --summary all

ci: fmt test

Expand Down
119 changes: 45 additions & 74 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
#+TITLE: Zigcli
#+DATE: 2022-09-20T22:55:17+0800
#+LASTMOD: 2023-07-14T23:52:55+0800
#+AUTHOR: Jiacai Liu
#+EMAIL: [email protected]
#+OPTIONS: toc:nil num:nil
#+STARTUP: content
#+TITLE: Introduction
#+DATE: 2023-10-21T12:09:48+0800
#+LASTMOD: 2023-10-21T12:54:48+0800
#+TYPE: docs

* Zigcli
[[https://github.com/jiacai2050/loc/actions/workflows/CI.yml][https://github.com/jiacai2050/loc/actions/workflows/CI.yml/badge.svg]]
[[https://github.com/jiacai2050/loc/actions/workflows/binary.yml][https://github.com/jiacai2050/loc/actions/workflows/binary.yml/badge.svg]]

Command line programs written in Zig. Currently there are:
- =loc=, lines of code.
- =tree=, list contents of directories in a tree-like format.
- =yes=, output a string repeatedly until killed.
- =pidof=, like [[https://man7.org/linux/man-pages/man1/pidof.1.html][pidof]], but for macOS.
- =night-shift=, control [[https://support.apple.com/guide/mac-help/use-night-shift-mchl97bc676d/mac][Night Shift]] in cli, build for macOS.
- =repeat=, repeat a command until it succeeds.

[[https://github.com/jiacai2050/zigcli/][Zigcli]] is a toolkit for building command lines programs in Zig. This package provides:
- Modules :: Developers can use to build their awesome cli programs.
- Binaries :: Various practical command line programs which can be used directly.
* Install
** Binaries
Prebuilt binaries can be found in [[https://github.com/jiacai2050/loc/actions/workflows/binary.yml][CI's artifacts]], or you can build from source:
#+begin_src bash
git clone https://github.com/jiacai2050/zigcli.git
Expand All @@ -26,77 +21,53 @@ Then build with
make build
#+end_src

#+RESULTS:

#+begin_quote
To build zigcli locally, Zig master is required, which can be downloaded [[https://ziglang.org/download/][here]].
#+end_quote
* Tree
#+begin_src bash :results verbatim code :exports both
./zig-out/bin/tree zig-out
#+end_src

#+RESULTS:
#+begin_src bash
zig-out
└──bin
├──loc
├──pidof
├──tree
└──yes
Currently there are:
- =loc=, lines of code.
- =tree=, list contents of directories in a tree-like format.
- =yes=, output a string repeatedly until killed.
- =pidof=, like [[https://man7.org/linux/man-pages/man1/pidof.1.html][pidof]], but for macOS.
- =night-shift=, control [[https://support.apple.com/guide/mac-help/use-night-shift-mchl97bc676d/mac][Night Shift]] in cli, build for macOS.
- =repeat=, repeat a command until it succeeds.

1 directories, 4 files
#+end_src
* Loc
#+begin_src bash :results verbatim code :exports both
./zig-out/bin/loc
#+end_src
** Modules
=zigcli= support [[https://ziglang.org/download/0.11.0/release-notes.html#Package-Management][module]] introduced in Zig 0.11.

#+RESULTS:
#+begin_src bash
Language File Line Code Comment Blank Size
-------- ---- ---- ---- ------- ----- -------
Zig 8 1427 1210 45 172 41.19K
YAML 6 195 182 5 8 5.03K
Makefile 1 12 8 0 4 206.00B
Python 1 10 7 2 1 166.00B
C 1 9 2 4 3 34.00B
Ruby 1 8 5 2 1 201.00B
-------- ---- ---- ---- ------- ----- -------
Total 18 1661 1414 58 189 46.81K
#+end_src
* Night shift
#+begin_src bash :results verbatim code :exports both
./zig-out/bin/night-shift -h
First add it to =build.zig.zon= of your project like this:
#+begin_src zig
.{
.dependencies = .{
.zigcli = .{
.url = "https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz",
.hash = "${HASH}",
},
},
}
#+end_src
Replace =${COMMIT}= with a real one, =.hash= field is difficult to get now, you can fill in a fake one like =1220b5dafa54081ce54f108d0b7fa9451149bde7dc7c20f4e14e322fdeeb7a1dfd9d=, then run =zig build=, it should throw following similar error:
#+begin_example
Fetch Packages [1/1] zigcli... ~/demo/build.zig.zon:7:21: error: hash mismatch: expected: 1220b5dafa54081ce54f108d0b7fa9451149bde7dc7c20f4e14e322fdeeb7a1dfd9d, found: 122079741f4df43f426c87585058123be0e0fa3fda3985d9d88341d3d03f8484e105
.hash = "1220b5dafa54081ce54f108d0b7fa9451149bde7dc7c20f4e14e322fdeeb7a1dfd9d",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#+end_example

#+RESULTS:
#+begin_src bash
USAGE:
./zig-out/bin/night-shift [OPTIONS] [--] <command>

Available commands by category:
Manual on/off control:
status View current Night Shift status
on Turn Night Shift on
off Turn Night Shift off
toggle Toggle Night Shift

Color temperature:
temp View temperature preference
temp <0-100> Set temperature preference
The =found:= section output the real hash, copy that to =build.zig.zon=.

Schedule:
schedule View current schedule
schedule sun Start schedule from sunset to sunrise
schedule off Stop the current schedule
schedule <from> <to> Start a custom schedule(HH:mm, 24-hour format)
Then in your =build.zig=, access =zigcli= like this:
#+begin_src zig
const zigcli = b.dependency("zigcli", .{});

OPTIONS:
-v, --version Print version
-h, --help Print help information
// Currently zigcli provide two modules
exe.addModule("simargs", zigcli.module("simargs"));
exe.addModule("pretty-table", zigcli.module("pretty-table"));
#+end_src

* Who's Using
If you’d like to be added to this list, welcome to [[https://github.com/jiacai2050/zigcli/pulls][open a PR]].

* Roadmap
** Loc
- Performance, at least comparable with [[https://github.com/cgag/loc][cgag/loc]] [[https://github.com/jiacai2050/loc/issues/1][#1]]
Expand Down
Loading

0 comments on commit b6fcc93

Please sign in to comment.