• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[Rebol School] REBOL School

caelum
22-Apr-2012
[91]
Is it possible to change the background color of the view led? Or 
are the only states available 'true' and 'false' which are green 
and red? I want different background colors for 'true' and 'false' 
states.

view center-face layout [
	across
	l1: led
	label "State" state: field

	btn "Change led state" [
		either l1/data [
			l1/data: false
			state/text: "False"
		][
			l1/data: true
			l1/data: true
			state/text: "True"
		]
		show [l1 state]
	]
]
Henrik
22-Apr-2012
[92x3]
caelum, take a look at the style code. the colors are likely embedded 
in it:

>> get in get-style 'led 'colors
== [0.255.0 255.0.0]

Then something like this:

stylize [
	led: led with [colors: [<your colors here>]]
]
should probably be STYLIZE/MASTER, sorry.
you can also do this:

view layout [led with [colors: [<your colors here>]]]
caelum
22-Apr-2012
[95]
Got it, exactly what I was looking for. Thanks.
Gregg
22-Apr-2012
[96x2]
You're ramping up fast sujoy. Most people don't get to using BIND 
for a long time. :-)
As Steeve has pointed out "it can be done" is *almost* always the 
answer with REBOL, and usually done in many ways. REBOL is great 
for practical use, as well as learning to think about problems in 
new ways.
Sujoy
24-Apr-2012
[98]
thanks Gregg. am loving rebol... :)
Endo
24-Apr-2012
[99]
you took the red pill then :)
Sujoy
24-Apr-2012
[100]
haha! looks like it Endo!
Gregg
24-Apr-2012
[101]
He's one of us now Endo. Next thing, he'll b3 wondering why every 
other programming language has only 6 or 8 datatypes and why they 
don't have a PARSE function. :-)
GrahamC
24-Apr-2012
[102x3]
has anyone written parse rules for phone numbers?   I need to setup 
a set of rules for international numbers, local numbers, cell numbers, 
1800 etc for use in asterisk and prevent users from making toll calls 
unless authorise
To make it easier I will strip out alll non-digits first
I guess i should just make a rule for each type of call and then 
combine them.
Gregg
24-Apr-2012
[105x4]
Here's something for US phone numbers I did a long time ago
set 'parse-phone-num func [
        num [string!]
        /local  digit digits sep _ext_  ch nums pin ext
    ] [
        digit:  charset "0123456798"
        digits: [some digit]
        sep:    charset "()-._"
        _ext_:  ["ext" opt "." | "x"]

        nums: copy ""
        rules: [
            any [
                some [sep | copy ch digit (append nums ch)]
                | _ext_ copy ext digits
                | "pin" copy pin digits
            ]
            end
        ]
        either parse trim num rules 
            [reduce ['num nums 'ext ext 'pin pin]]
            [none]
    ]

    set 'well-formed-phone-number? func [num /local data] [
        either none? data: parse-phone-num num [false] [
            any [
                found? find [7 10] length? data/num
                all [11 = length? data/num  data/num/1 = #"1"]
            ]
        ]
    ]
And a formatter that goes the other way:
set 'format-phone-number func [
        num [string! object!] "String or object with /text value"
        /def-area-code area-code [string! integer!]
        /local left right mid obj res
    ] [
        left:  func [s len][copy/part s len]
        right: func [s len] [copy skip tail s negate len]
        mid:   func [s start len][copy/part at s start len]

        if object? num [obj: num  num: obj/text]

        res: either data: parse-phone-num num [
            ; discard leader if it's there.
            if all [11 = length? data/num  data/num/1 = #"1"] [
                data/num: right data/num 10
            ]
            rejoin [
                rejoin switch/default length? data/num [
                    7  [ compose [

                        (either area-code [rejoin ["(" area-code ") "]][])
                        left data/num 3 "-" right data/num 4
                    ]]
                    10 [[
                        "(" left data/num 3 ") "
                        mid data/num 4 3 "-" right data/num 4
                    ]]
                ][[data/num]]

                reduce either data/ext [[" ext" trim data/ext]] [""]

                reduce either data/pin [[" pin" trim data/pin]] [""]
            ]
        ][num]

        if obj [
            obj/text: res
            attempt [if 'face = obj/type [show obj]]
        ]
        res
    ]
GrahamC
24-Apr-2012
[109x2]
Ok, so this checks for legally formatted numbers.  Or formats numbers 
into the common format.
need to reboot .. clipboard not working.  When is openme coming :(
Gregg
24-Apr-2012
[111]
Yes. :-)
GrahamC
24-Apr-2012
[112x3]
p: format-phone-number "1800-123456"
== "(180) 012-3456"
might need a ittle tweaking for international numbers
How are international numbers formatted?  +181812345678  ?
Gregg
24-Apr-2012
[115x2]
As I said, it was just for US phone numbers. The +1 was not included, 
since it wasn't meant for dialing.
If you're asking how they're formatted in general, it depends.
GrahamC
24-Apr-2012
[117]
ok, so always 3 numbers in the first group if not international?
Gregg
24-Apr-2012
[118]
Yes.
GrahamC
24-Apr-2012
[119x2]
( we use 2 numbers for the first group unless it's cellular  when 
it's 3 or 4 )
can a number start with 1 ?
Gregg
24-Apr-2012
[121]
IIRC, some places use 4, grouping them into 2 sets of 2 for the area 
code.
GrahamC
24-Apr-2012
[122x2]
ohh ....
I guess users are going to have to make their own parse rules then
Gregg
24-Apr-2012
[124x2]
The code I posted doesn't do any validation according to what might 
work in the real world when dialed.
http://msdn.microsoft.com/en-us/goglobal/bb688129
http://en.wikipedia.org/wiki/Telephone_numbering_plan


I think the number of options are limited enough that you could get 
pretty far, but a custom system would be great. If you could template-ize 
things, users could submit their templates for others to use, or 
to be integrated into the default system.
GrahamC
24-Apr-2012
[126]
These rules are going into a cgi script to allow local and national 
dialiing (and emergency) but stop international
Gregg
24-Apr-2012
[127]
Ah, there you go. Validate against your local and national rules 
and fail everything else.
GrahamC
24-Apr-2012
[128]
yep
Gregg
24-Apr-2012
[129x4]
Not sure if this will help, but maybe:
parse-int-values: func [

    "Parses and returns integer values, each <n> chars long in a string."
    input [any-string!]

    spec [block!] "Dialected block of commands: <n>, skip <n>, done, 
    char, or string"
    /local
        gen'd-rules ; generated rules
        result      ; what we return to the caller

        emit emit-data-rule emit-skip-rule emit-literal-rule emit-data
        digit= n= literal=
        int-rule= skip-rule= literal-rule= done= build-rule=
        data-rule skip-rule
][

    ; This is where we put the rules we build; our gernated parse rules.
    gen'd-rules: copy []
    ; This is where we put the integer results
    result: copy []

    ; helper functions

    emit: func [rule n] [append gen'd-rules replace copy rule 'n n]
    emit-data-rule: func [n] [emit data-rule n]
    emit-skip-rule: func [n] [emit skip-rule n]
    emit-literal-rule: func [value] [append gen'd-rules value]
    emit-data: does [append result to integer! =chars]

    ; Rule templates; used to generate rules

    ;data-rule: [copy =chars n digit= (append result to integer! =chars)]
    data-rule: [copy =chars n digit= (emit-data)]
    skip-rule: [n skip]

    ; helper parse rules
	digit=: charset [#"0" - #"9"]
    n=: [set n integer!]
    literal=: [set lit-val [char! | any-string!]]

    ; Rule generation helper parse rules
    int-rule=: [n= (emit-data-rule n)]
    skip-rule=: ['skip n= (emit-skip-rule n)]
    literal-rule=: [literal= (emit-literal-rule lit-val)]
    done=: ['done (append gen'd-rules [to end])]

    ; This generates the parse rules used against the input

    build-rule=: [some [skip-rule= | int-rule= | literal-rule=] opt done=]


    ; We parse the spec they give us, and use that to generate the

    ; parse rules used against the actual input. If the spec parse

    ; fails, we return none (maybe we should throw an error though);

    ; if the data parse fails, we return false; otherwise they get
    ; back a block of integers. Have to decide what to do if they
    ; give us negative numbers as well.
    either parse spec build-rule= [
        either parse input gen'd-rules [result] [false]
    ] [none]
]
Example: parse-int-values "1234567890" [2 4 skip 2 2]
parse-int-values "(123) 456-7890" [#"(" 3 ") " 3 #"-" 4]
GrahamC
24-Apr-2012
[133]
http://en.wikipedia.org/wiki/Telephone_numbers_in_New_Zealand

learnt something new .. 02 is for Scott Base in Antarctica ;)
Gregg
24-Apr-2012
[134x2]
We had a REBOLer who worked there some years back. ...what was his 
name...
G. Scott Jones? Now it's gonna bother me. :-\
GrahamC
24-Apr-2012
[136]
yeah... though he won't be in Scott Base but the USA base there
Gregg
24-Apr-2012
[137]
Still probably a local call from Scott Base though. ;-)
GrahamC
24-Apr-2012
[138x3]
maybe not .. it may be routed thru the USA :)
zero: charset [ #"0" ]
digit: charset [ #"0" - #"9" ]
digits: [ some digit ]
local-number: charset [ #"3" #"4" #"6" #"9" ]
not01-number: charset [ #"2" - #"9" ]

national-rule: [ zero local-number not01-number digits]
local-rule: [ not01-number digits ]
emergency-rule: [ "111" ]

; toll free numbers are 0800 and 0508
toll-free: [ zero [ "800" | "508" ] digits ]

; directory assistance 018, 0172,
directory-assistance: [ "01" [ "8" | "72" ]]
 
; faults 120, 125 and business 126
faults: [ "12" [ "0" | "5" | "6" ]]

; cellular are 021, 022, 027, 028, 029
cellular-rule: [ "02" digits ]

; international 00 - country code - number
international-rule: [[ "00" | "+" ] digits ]

allowed-phone-rule: [
	local-rule |
	national-rule |
	emergency-rule |
	toll-free |
	directory-assistance |
	faults |
	cellular-rule
]

all-phone-rule: [ allowed-phone-rule | international-rule ]
hope that covers all of my dialling rules