-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDataFrameTextWriter.class.st
More file actions
84 lines (67 loc) · 2.06 KB
/
DataFrameTextWriter.class.st
File metadata and controls
84 lines (67 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"
Implements a Text writer for DataFrame contents, including setting the alignment of each cell to the left and right.
# Example
```language=Pharo
(DataFrameCsvReader new
readFromString: 'Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$''dollar''$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
column$are$separated$by$at$least$one$space.
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column.'
withSeparator: $$
withHeader: false)
writeToTextAlignedRight
```
"
Class {
#name : #DataFrameTextWriter,
#superclass : #DataFrameCsvWriter,
#instVars : [
'padDirection'
],
#category : #'DataFrame-IO-Core'
}
{ #category : #accessing }
DataFrameTextWriter >> padDirection [
^ padDirection
]
{ #category : #accessing }
DataFrameTextWriter >> padDirection: anObject [
padDirection := anObject
]
{ #category : #accessing }
DataFrameTextWriter >> padLeft [
"Set the receiver to pad elements to left"
self padDirection: #padLeftTo:
]
{ #category : #accessing }
DataFrameTextWriter >> padRight [
"Set the receiver to pad elements to left"
self padDirection: #padRightTo:
]
{ #category : #writing }
DataFrameTextWriter >> rowPairsFor: colWidths using: row [
| gen |
gen := Generator on: [ : g | colWidths do: [ : k | g yield: k ] ].
^ row collect: [ : w | { w . gen next } ]
]
{ #category : #writing }
DataFrameTextWriter >> write: aDataFrame [
"Answer a <String> representation of aDataFrame, aligned according to the receiver's padDirection"
^ String streamContents: [ : stream |
| colWidths |
colWidths := aDataFrame applySize applyToAllColumns: #max.
(aDataFrame collect: [ : row | self rowPairsFor: colWidths using: row ])
do: [ : line |
self writeLine: line to: stream.
stream cr ] ]
]
{ #category : #writing }
DataFrameTextWriter >> writeLine: line to: stream [
line do: [ : pair |
pair first ifNotNil: [ : pf |
stream
<< (pf perform: self padDirection with: pair second);
space ] ]
]