lunes, 2 de febrero de 2015

Pharo Smalltalk Scripts, part 1

These are some scripts and tips I used in my daily developement with Pharo Smalltalk in the last years. Hope you find them useful:

Parsing XML with DOM

You can instantiate and parse a XML DOM parser with one line of code:
1
2
3
4
(XMLDOMParser parseFileNamed: 'fao_country_names.xml')
  firstNode
  allElementsSelect: [ : each |
    each localName = 'geographical_region' ].

NeoCSV

You can quickly parse a CSV file using NeoCSV with just a snippet for most tasks:
1
2
3
4
5
6
7
(NeoCSVReader on: 'myfile.csv' asFileReference readStream)
 separator: Character tab;
 do: [ : row | " do something with row "
    row
      first;
      second;
      third ]
or using a one-liner
1
2
'myfile.csv' asFileReference readStreamDo: [ : stream |
  (NeoCSVReader on: stream) upToEnd ]

Lorem Ipsum

You already have the first paragraph of "Lorem ipsum" available in Pharo.
1
String loremIpsum

Profiling

If you are developing UI you can have the Time Profiler (Pharo) opened in any method by enclosing your code between:
1
2
3
TimeProfiler new openOnBlock: [
  " Your code ... "
]

Open File Dialog

Opening a File Dialog for specific file type is a one-liner:
1
UIManager default chooseFileMatching: #('*.xml').

Spec

Spec is a relatively young UI Specification library. You can prototype UI's easily by using Dynamic Composable Models, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| view layout |
" Configure the Spec models "
view := DynamicComposableModel new
        instantiateModels: #(labelA LabelModel textA TextInputFieldModel labelB LabelModel textB TextInputFieldModel);
        extent: 500@200;
        title: 'Title'
        yourself.
" Configure the Spec layout "
layout := SpecLayout composed
        newColumn: [ : r | r
                add: #labelA; add: #textA;
                add: #labelB; add: #textB ];
        yourself.
" Set up the widgets "
view labelA text: 'A'.
view labelB text: 'B'.
" Open the Window "
(view openDialogWithSpecLayout: layout)
        centered;
        modalRelativeTo: World.

Morphic Window and Controls

If you need a basic container Morph window, just evaluate:
1
2
3
4
5
6
7
| s |
(s := SystemWindow labelled: 'Window')
 openInHand.
 s addMorph: (StringMorph new
  contents: 'Comments:';
  color: Color black)
   frame: (0@0.0 corner: 1@0.2).
But you can have a control like a TextArea without container
1
2
3
4
5
| o ptm |
o := Object new.
ptm := PluggableTextMorph on: o text: #printString accept: nil.
ptm height: TextStyle defaultFont height + 6.
ptm acceptOnCR: true; openInHand.
And instantiated differently
1
2
3
4
5
(PluggableTextMorph on: Workspace new
 text: #contents
 accept: #acceptContents:
 readSelection: nil
 menu: #codePaneMenu:shifted:) openInHand.

FTP

Udo Schneider recently wrote an FTP/WebDAV Plugin for the Pharo FileSystem, and you get almost for free a FTP client using the File Browser
1
2
3
fs := FileSystem ftp: 'ftp://ftp.mozilla.org'.
FileList openOn: fs workingDirectory.
fs close.

0 comentarios:

Publicar un comentario