Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Align selected text lines along occurrences of a given pattern or substring
- // During the alignment process, all the pattern occurrences will be deleted
- /* global UltraEdit, selectedLines, lineEnd */
- /* eslint no-shadow:0 */
- var alignAt = UltraEdit.getString("Align at pattern (e.g. \\t)",1)
- var alignAtPattern = new RegExp( alignAt )
- // Cut array "selectedLines" into clipboard,
- // determine "lineEnd" byte sequence of this document:
- // include cutSelection.js
- var cells = selectedLines.map( function(line) { return line.split(alignAtPattern) } )
- var colwidths = cells.reduce( function( widths, row ) {
- row.forEach( function(cell,i) {
- if (widths.length < i+1) widths.push(0)
- widths[i] = Math.max(widths[i],cell.length)
- })
- return widths
- }, [])
- var resultLines = cells.map(
- pad_to( colwidths )
- )
- // Output - and done
- UltraEdit.activeDocument.write( resultLines.join(lineEnd) )
- //-----------------------------------------------------------------------------------
- function pad_to( colwidths ) {
- var spaces = new Array( Math.max.apply(null,colwidths)+1).join(' ')
- return function( row ) {
- return row.map( function(cell,i){
- return cell+spaces.substr(0,colwidths[i]-cell.length)
- }).join(' ')
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement