1#! /usr/bin/python3
2
3#########################################################################
4#       This Python script is Copyright (c) 2002, Peter J Billam        #
5#               c/o P J B Computing, www.pjb.com.au                     #
6#                                                                       #
7#     This script is free software; you can redistribute it and/or      #
8#           modify it under the same terms as Python itself.            #
9#########################################################################
10from TermClui import *
11import sys
12# import TermClui::FileSelect
13colour = ""
14paint = ""
15name = ""
16text = ""
17
18def main():
19    while True:
20        task = choose('Test which TermClui.py function ?',
21            ['ask','choose','confirm','edit','view','select_file']
22        );
23        if not task:
24            sys.exit()
25        eval('test_'+task+'()')
26
27def test_choose():
28    colours = ['Red','Orange','Black','Grey','Blue']
29    paints  = [
30     'Bizzare extremely long name that certainly will never occur on any real artist pallette',
31     'Alizarin Crimson', 'Burnt Sienna', 'Cadmium Yellow', 'Cobalt Blue',
32     'Flake White', 'Indian Red', 'Indian Yellow', 'Ivory Black',
33     'Lemon Yellow',
34     'Naples Yellow', 'Prussian Blue', 'Raw Sienna', 'Raw Umber', 'Red Ochre',
35     'Rose Madder', 'Ultramarine Blue', 'Vandyke Brown', 'Viridian Green',
36     'Yellow Ochre'
37    ]
38    scientists = [
39    'Luis Alvarez', 'Alain Aspect', 'Michael Barnsley', 'Johann Bernouilli',
40    'Nicolas Bernouilli', 'Friedrich Wilhelm Bessel', 'John Bell',
41    'Antoine Becquerel', 'Hans Bethe', 'David Bohm', 'Niels Bohr',
42    'Ludwig Boltzmann', 'Hermann Bondi', 'George Boole', 'Max Born',
43    'Satyendra Bose', 'Robert Boyle', 'Léon Brillouin', 'Eugenio Calabi',
44    'Georg Cantor', 'James Chadwick', 'Gregory Chaitin',
45    'Subrahmanyan Chandrasekar', 'Geoffrey Chew', 'Alonzo Church',
46    'John Horton Conway', 'Francis Crick', 'Marie Curie', 'Charles Darwin',
47    'Humphrey Davy', 'Richard Dawkins', 'Louis de Broglie', 'Max Delbrück',
48    'René Descartes', 'Willem de Sitter', 'Bruce DeWitt', 'Paul Dirac',
49    'Freeman Dyson', 'Arthur Stanley Eddington', 'Albert Einstein',
50    'Leonhard Euler', 'Hugh Everett', 'Michael Faraday', 'Pierre Fatou',
51    'Mitchell Feigenbaum', 'Pierre de Fermat', 'Enrico Fermi', 'Richard Feynman',
52    'Joseph Fraunhofer', 'Galileo Galilei', 'Evariste Galois', 'George Gamov',
53    'Carl Friedrich Gauss', 'Murray Gell-Mann', 'Kurt Gödel', 'Alan Guth',
54    'Stephen Hawking', 'Felix Hausdorff', 'Werner Heisenberg', 'Charles Hermite',
55    'Peter Higgs', 'David Hilbert', 'Fred Hoyle', 'Edwin Hubble',
56    'Christian Huygens', 'David Hilbert', 'Edwin Hubble', 'Pascual Jordan',
57    'Gaston Julia', 'Marc Kac', 'Theodor Kaluza', 'Stuart Kauffman',
58    'William Lord Kelvin', 'Gustav Robert Kirchhoff', 'Oskar Klein',
59    'Helge von Kock', 'Willis Lamb', 'Lev Davidovich Landau', 'Paul Langevin',
60    'Pierre Simon de Laplace', 'Gottfried Wilhelm Leibnitz', 'Paul Lévy',
61    'Hendrik Lorentz', 'James Clark Maxwell', 'Marston Morse',
62    'Benoit Mandelbrot', 'Gregor Mendel', 'Dmitri Mendeleev', 'Robert Millikan',
63    'Hermann Minkowski', 'John von Neumann', 'Isaac Newton', 'Emmy Noether',
64    'Hans Christian Oersted', 'Lars Onsager', 'Robert Oppenheimer',
65    'Abraham Pais', 'Heinz Pagels', 'Vilfredo Pareto', 'Louis Pasteur',
66    'Wolfgang Pauli', 'Linus Pauling', 'Guiseppe Peano', 'Rudolf Peierls',
67    'Roger Penrose', 'Arno Penzias', 'Jean Perrin', 'Max Planck',
68    'Boris Podolsky', 'Henri Poincaré', 'Isidor Rabi', 'Srinivasa Ramanujan',
69    'Lord Rayleigh', 'Lewis Fry Richardson', 'B. Riemann', 'Nathan Rosen',
70    'Ernest Rutherford', 'Abdus Salam', 'Erwin Schrödinger',
71    'Karl Schwarzschild', 'Julian Schwinger', 'Claude Shannon',
72    'Waclaw Sierpinski', 'Leo Szilard', 'Kip Thorne', 'Alan Turning',
73    'Sin-itro Tomonaga', 'Stanislaw Ulam', 'James Watson', 'Karl Weierstrauss',
74    'Hermann Weyl', 'Steven Weinberg', 'John Wheeler', 'Charles Weiner',
75    'Norbert Wiener', 'Eugene Wigner', 'Robert Wilson', 'Edward Witten',
76    'Shing-Tung Yau', 'Chen-Ning Yang', 'Hideki Yukawa', 'George Kingsley Zipf',
77    ]
78
79    multi = choose('Mode ?', ['Single-choice', 'Multi-choice'])
80    if not multi:
81        return False
82    if (multi == 'Single-choice'):
83        paint = choose ("Your favourite paint ?\n"+help_text(''), paints)
84        scientist = choose ("Your favourite scientist ?", scientists)
85        multiline_question = r'''Your favourite colour ?
86
87This tests how the 'choose' subroutine handles multi-line
88questions. After you choose, all but the first line should disappear,
89leaving the question and answer on the screen as a record of the dialogue.
90The other lines should only get displayed if there is room.
91'''
92        colour = choose (multiline_question, colours);
93        print('paint='+str(paint)+', scientist='+str(scientist)+', colour='+str(colour))
94    else:
95        fav_paints = choose("Your favourite paints ?\n"+help_text('multi'), paints, multichoice=True);
96        fav_scientists = choose("Your favourite scientists ?", scientists, multichoice=True);
97        print("paints = "+', '.join(fav_paints) +
98            "\nscientists = "+', '.join(fav_scientists))
99    return
100
101
102def test_confirm():
103    multiline_question = r'''OK to proceed with the test ?
104
105This step checks the 'confirm' subroutine and whether it handles
106a multiline question OK.  After you choose Yes or No all but the
107first line should disappear,  leaving the question and answer on
108the screen as a record of the dialogue.
109'''
110    if not confirm (multiline_question):
111        return False
112    confirm('Did the text vanish except for the 1st line ?')
113
114name = ''
115colour = ''
116paint = ''
117text = ''
118def test_ask():
119    global name, colour, paint
120    multiline_question = r'''Enter a string :
121
122The point of this test is to check out the behaviour of &ask
123with multi-line questions; subsequent lines after the initial
124question should be formatted within the window width ...
125
126'''
127    string = ask(multiline_question+help_text('ask'))
128
129    colours = ['Red','Orange','Black','Grey','Blue']
130    colour = choose('Your favourite colour ?', colours)
131    if not colour:
132        return False
133    names = dict(
134        Red='Fred', Orange='Solange', Black='Jack', Grey='May', Blue='Sue'
135    )
136    name = ask("Choose a name which rhymes with "+colour+" :", names[colour])
137    print('string='+string+', name='+name)
138
139def test_edit():
140    global name, colour, paint, text
141    text = r'''
142 There was a brave soul called {0},
143 Whose favourite colour was {1};
144   But some {2}
145   ...
146 And that was the end of {0}.
147'''.format(name, colour, paint)
148    text = edit('Your limerick', text);
149    print('text='+text)
150
151def test_view():
152    global text
153    view('Your limerick:', text or 'try testing "ask" and "edit" first :-)')
154
155def test_select_file():
156    bool_opts = ['Chdir','Create','ShowAll','DisableShowAll',
157        'SelDir','TextFile','Readable','Writeable','Executable','Owned',
158        'Directory']
159    text_opts = ('FPat','File','Path','Title','TopDir')
160    multiple = choose('Select',['Single file','Multiple files'])
161    if multiple == 'Multiple files':
162        bool_opts.pop(0)
163        bool_opts.pop(0)
164    opts = dict()
165    for bool_opt in bool_opts:
166        opts[bool_opt] = choose("option "+bool_opt+" ?",['default','0','1'])
167        if not opts[bool_opt]:
168            return False
169        if opts[bool_opt] == 'default':
170            del(opts[bool_opt])
171        elif opts[bool_opt] == '0':
172            opts[bool_opt] = False
173        elif opts[bool_opt] == '1':
174            opts[bool_opt] = True
175    for text_opt in text_opts:
176        opts[text_opt] = ask("option "+text_opt+" ?", opts.get(text_opt, ''));
177        if not opts[text_opt]:
178            del(opts[text_opt])
179    if multiple == 'Multiple files':
180        # files = select_file(-Chdir=0, opts);
181        opts['Chdir'] = False
182        opts['multichoice'] = True
183        files = select_file(**opts)
184        print("You selected "+str(files), file=sys.stderr)
185    else:
186        # given the dict, how can I put together an arg-by-name call ?
187        print("You selected " +str(select_file(**opts)), file=sys.stderr)
188
189main()
190