Skip to content

Commit a5219e0

Browse files
committed
Adds rogueutil example
1 parent 15831d9 commit a5219e0

File tree

13 files changed

+2629
-122
lines changed

13 files changed

+2629
-122
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: c-rogueutil Example
2+
description: Build and run the c-rogueutil example
3+
4+
runs:
5+
using: composite
6+
steps:
7+
- name: Initialize git submodules
8+
shell: bash
9+
run: git submodule update --init --recursive examples/c-rogueutil/rogueutil
10+
11+
- name: Run generation and build script
12+
shell: bash
13+
working-directory: ./examples/c-rogueutil
14+
run: ./generate-and-run.sh

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
[submodule "examples/c-minisat/minisat-c-bindings"]
88
path = examples/c-minisat/minisat-c-bindings
99
url = https://github.com/niklasso/minisat-c-bindings.git
10-
[submodule "c-rogueutil/rogueutil"]
10+
[submodule "examples/c-rogueutil/rogueutil"]
1111
path = examples/c-rogueutil/rogueutil
12-
url = git@github.com:sakhmatd/rogueutil.git
12+
url = https://github.com/sakhmatd/rogueutil.git

examples/c-rogueutil/generate-and-run.sh

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
77
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
88

99
echo "# "
10-
echo "# Building minisat C bindings"
10+
echo "# Building rogueutil C bindings"
1111
echo "# "
1212

13-
cd "$SCRIPT_DIR/minisat-c-bindings"
13+
cd "$SCRIPT_DIR/rogueutil"
1414
make
1515

16-
echo "# "
17-
echo "# Creating symlinks for shared library"
18-
echo "# "
19-
20-
cd "$SCRIPT_DIR/minisat-c-bindings/build/dynamic/lib"
21-
ln -sf libminisat-c.so.1.0.0 libminisat-c.so
22-
ln -sf libminisat-c.so.1.0.0 libminisat-c.so.1
23-
2416
echo "# "
2517
echo "# Generating Haskell bindings"
2618
echo "# "
@@ -29,30 +21,33 @@ cd "$PROJECT_ROOT"
2921

3022
cabal run hs-bindgen-cli -- \
3123
preprocess \
32-
-I "$SCRIPT_DIR/minisat-c-bindings" \
24+
-I "$SCRIPT_DIR/rogueutil" \
3325
--hs-output-dir "$SCRIPT_DIR/hs-project/src" \
34-
--module Minisat.Generated \
35-
"$SCRIPT_DIR/minisat-c-bindings/minisat.h"
26+
--module RogueUtil.Generated \
27+
--clang-option=-D_POSIX_C_SOURCE=200809L \
28+
"$SCRIPT_DIR/rogueutil/rogueutil.h"
3629

3730
echo "# "
3831
echo "# Creating cabal.project.local"
3932
echo "# "
4033

4134
cat > "$SCRIPT_DIR/hs-project/cabal.project.local" <<EOF
42-
package c-minisat
35+
package c-rogueutil
4336
extra-include-dirs:
44-
$SCRIPT_DIR/minisat-c-bindings
45-
, $SCRIPT_DIR/minisat-c-bindings/build/dynamic/lib
37+
$SCRIPT_DIR/rogueutil
4638
extra-lib-dirs:
47-
$SCRIPT_DIR/minisat-c-bindings
48-
, $SCRIPT_DIR/minisat-c-bindings/build/dynamic/lib
39+
$SCRIPT_DIR/rogueutil
40+
41+
shared: False
4942
EOF
5043

5144
echo "# "
5245
echo "# Done!"
5346
echo "# "
5447
echo "Running the project"
48+
5549
cd $SCRIPT_DIR/hs-project
56-
export LD_LIBRARY_PATH=$SCRIPT_DIR/minisat-c-bindings/build/dynamic/lib:\$LD_LIBRARY_PATH
50+
export LD_LIBRARY_PATH=$SCRIPT_DIR/rogueutil/:\$LD_LIBRARY_PATH
51+
5752
cabal build
58-
cabal run c-minisat
53+
cabal run c-rogueutil

examples/c-rogueutil/hs-project/app/Main.hs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@
22

33
module Main where
44

5-
import qualified RogueUtil as RU
6-
import Foreign.C (newCString)
7-
import Formatting
5+
import Control.Monad (forM_)
6+
import Foreign.C (newCString, withCString)
7+
8+
import RogueUtil.Generated qualified as RU
9+
import RogueUtil.Generated.Safe qualified as Safe
810

911
main :: IO ()
1012
main = do
11-
RU.setColor (fromIntegral $ RU.un_Color_code RU.YELLOW)
12-
pressAnyKeyString <- newCString "Press any key to start"
13-
RU.anykey (RU.RUTIL_STRING pressAnyKeyString)
14-
RU.resetColor
15-
RU.colorPrint RU.RED RU.WHITE ("\nThis is the manual colorPrint binding: " % string % int) "ola" 2
13+
_ <- Safe.saveDefaultColor
14+
15+
Safe.cls
16+
Safe.hidecursor
17+
18+
Safe.setColor (fromIntegral $ RU.un_Color_code RU.YELLOW)
19+
Safe.setBackgroundColor (fromIntegral $ RU.un_Color_code RU.BLUE)
20+
titleStr <- newCString "=== RogueUtil Haskell Bindings Demo ==="
21+
Safe.printXY 20 2 (RU.RUTIL_STRING titleStr)
22+
Safe.resetColor
23+
24+
rows <- Safe.trows
25+
cols <- Safe.tcols
26+
Safe.setColor (fromIntegral $ RU.un_Color_code RU.CYAN)
27+
sizeMsg <- newCString $ "Terminal size: " ++ show cols ++ "x" ++ show rows
28+
Safe.printXY 20 4 (RU.RUTIL_STRING sizeMsg)
29+
Safe.resetColor
30+
31+
let colors = [RU.RED, RU.YELLOW, RU.GREEN, RU.CYAN, RU.BLUE, RU.MAGENTA]
32+
forM_ (zip [0..] colors) $ \(i, color) -> do
33+
Safe.setColor (fromIntegral $ RU.un_Color_code color)
34+
withCString ("█████ " ++ show color) $ \str ->
35+
Safe.printXY 20 (6 + fromIntegral i) (RU.RUTIL_STRING str)
36+
Safe.resetColor
37+
38+
Safe.locate 20 14
39+
Safe.showcursor
40+
pressKeyMsg <- newCString "Press any key to exit..."
41+
Safe.anykey (RU.RUTIL_STRING pressKeyMsg)
42+
43+
Safe.cls
44+
Safe.resetColor

examples/c-rogueutil/hs-project/c-rogueutil.cabal

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ library
5959

6060
-- Modules exported by the library.
6161
exposed-modules: RogueUtil.Generated
62-
RogueUtil
62+
RogueUtil.Generated.Safe
63+
RogueUtil.Generated.Unsafe
64+
RogueUtil.Generated.Global
65+
RogueUtil.Generated.FunPtr
6366

6467
-- Modules included in this library but not exported.
6568
-- other-modules:
@@ -69,7 +72,6 @@ library
6972

7073
-- Other library packages from which modules are imported.
7174
build-depends: base ^>=4.19.2.0,
72-
formatting,
7375
hs-bindgen-runtime
7476

7577
-- Directories containing source files.
@@ -94,7 +96,6 @@ executable c-rogueutil
9496
-- Other library packages from which modules are imported.
9597
build-depends:
9698
base ^>=4.19.2.0,
97-
formatting,
9899
c-rogueutil
99100

100101
-- Directories containing source files.
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
packages: .
2-
../ansi-diff
3-
../c-expr
4-
../clang
5-
../hs-bindgen-runtime
6-
../hs-bindgen
2+
../../../ansi-diff
3+
../../../hs-bindgen-runtime
4+
../../../c-expr-runtime
5+
../../../c-expr-dsl
6+
../../../hs-bindgen
77

8+
source-repository-package
9+
type: git
10+
location: https://github.com/well-typed/libclang
11+
tag: d4ae9a5fcca9ae71fcb4df2c0b0ea37c6b15c48c

examples/c-rogueutil/hs-project/src/RogueUtil.hs

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

0 commit comments

Comments
 (0)