Giter Site home page Giter Site logo

ashneyderman / cecho Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mazenharake/cecho

0.0 1.0 0.0 1.73 MB

An ncurses library for Erlang

Home Page: http://mazenharake.wordpress.com

License: BSD 2-Clause "Simplified" License

C 43.73% Erlang 56.27%

cecho's Introduction

cecho

An ncurses library for Erlang

Introduction

Cecho is an ncurses library for Erlang which enabled Erlang applications to create terminal based GUIs. It aims to have an API as close as possible to the original API of ncurses so that a person familiar with ncurses API should be able to immediately use it without any introduction.

Compile

To clean/compile run

./rebar3 clean
./rebar3 compile

Usage

The intention of this library is to have an API as close as possible to the ncurses library. Something things can't be translated and other things return values instead of taking pointer arguments etc. but overall the API should be similar enough to make use of the ncurses documentation that is out on the web.

The following sequence is more or less standard when using the library:

  1. Start the cecho application
  2. Set flags and attributes, create windows and initialize data
  3. Move cursor, add text and refresh window 3.1) Listen for input to be able to terminate the program
  4. Stop the cecho application gracefully

The following is an example hello world which bounces "Hello world" across the screen and terminates when 'q' is pressed. It can be found in the cecho_example.erl file in the src/ directory.

%%
%% helloworld - bounce "Hello World!" on the end of the screen
%%
helloworld() ->
    %% Start application
	application:start(cecho),
	%% Set attributes
	cecho:cbreak(),
	cecho:noecho(),
	cecho:curs_set(?ceCURS_INVISIBLE),
	%% Write initial string...
	cecho:mvaddstr(0, 0, "Hello World!"),
	cecho:refresh(),
	%% Start the process that will "move" the string
	Mover = spawn(fun() -> mvhello() end),
	ctrl(Mover).

ctrl(Mover) ->
    %% get key-input
	C = cecho:getch(),
	case C of
    $q -> 
        %% If we get a 'q' then exit the mover and stop cecho
    	exit(Mover, normal),
    	application:stop(cecho),
    	erlang:halt();
    _ ->
        %% ignore anything else
        ctrl(Mover)
    end.

%% start the mover
mvhello() -> mvhello(0, 0, 1, 1).

%% take previous pos and direction and print out new string
mvhello(PrevY, PrevX, DirY, DirX) ->
    %% "erase" previous position
	cecho:mvaddstr(PrevY, PrevX, "            "),
	%% calculate new position and direction
	{NewY, NewX, NewDirY, NewDirX} =
    calc_new_pos(PrevY, PrevX, DirY, DirX),
	%% "move" the text to new position
	cecho:mvaddstr(NewY, NewX, "Hello World!"),
	%% update the screen to show the change
	cecho:refresh(),
	%% do it again!
	timer:sleep(50),
 mvhello(NewY, NewX, NewDirY, NewDirX).

calc_new_pos(Py, Px, Dy, Dx) ->
    %% get max coords of the screen
    {My, Mx} = cecho:getmaxyx(),
    %% calc new vertical position and new direction
    {NewPy, NewDy} =
    if (Py+(Dy) >= My) orelse (Py+(Dy) < 0) ->
           {Py+(Dy*-1), Dy*-1};
       true ->
           {Py+(Dy), Dy}
        end,
    %% calc new horizontal position and new direction
	%% take string length into account
	{NewPx, NewDx} =
    if (Px+(Dx)+12 >= Mx) orelse (Px+(Dx) < 0) ->
           {Px+(Dx*-1), Dx*-1};
       true ->
               {Px+(Dx), Dx}
    end,
    {NewPy, NewPx, NewDy, NewDx}.

There are several ways to run the examples and own scripts/app with cecho. There are essentially two intended ways which are recommend; either running the whole thing as a script or from a module using the '-eval' flag to erl.

Examples on how to run the helloworld example (make sure you have compiled cecho first):

From the cecho directory do:

$> erl -noinput -pa ../cecho/_build/default/lib/cecho/ebin/ -eval 'cecho_example:helloworld()' +A 50

Or create an escript file as follows:

#!/usr/bin/env escript
%%! -noinput -pa ../cecho/_build/default/lib/cecho/ebin +A 50
-include_lib("cecho/include/cecho.hrl").
main(_) -> cecho_example:helloworld().

and save it, then make it executable and run it from the cecho directory:

$> chmod +x ./helloworld.escript
$> ./helloworld.escript

IMPORTANT: If input will be used (cecho:getch/0) then the two flags '-noinput' and +A must be used. The reason is that if '-noinput' is not specified the erlang VM will interfere with the reading of the keys. The second reason is that in order to asynchronously read the input Erlang need more io threads; however it doesn't seem to be enough to specify e.g. 10 in some cases and the reason is not known. If a "high enough number" is specified then the input will work very well.

Contribute

Should you find yourself using cecho and have issues, comments or feedback please [create an issue!] 1

cecho's People

Contributors

mazenharake avatar djnym avatar erlanger avatar andrewtj avatar msantos avatar systra avatar gleber avatar rafaltrojniak avatar slepher avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.