Skip to main content
better implementation
Source Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405

With this implementation the option ColumnWidths in fact determines the maximum column width, because when we reduce the window width, columns will become even narrower. Hence the minimal column width is actually determined by the minimal width of the whole Grid.

With this implementation the option ColumnWidths in fact determines the maximum column width, because when we reduce the window width, columns will become even narrower. Hence the minimal column width is actually determined by the minimal width of the whole Grid.

better implementation
Source Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405
PrettyPrint[args__] := Print@Module[{tab, newline}, Grid[Map[RowGrid[Map[TextCell[Row[#]] &, Split[#, # =!= tab &] & /@ Split[Flatten[ Replace[{args}, {s_String :> StringSplit[s, {"\t" -> tab, "\n" -> newline}]}, 1], 1], # =!= newline &], {2}] /. {tab | newline -> Nothing}, Alignment -> Left]] 

output

If you reduce the width of the Notebook window, columns widths are automatically adapted and proper word-wrapping is applied to long rows of text:

output

With an example TSV file imported as "Text":

Options[PrettyPrint] = {ColumnWidths -> Automatic}; PrettyPrint[args__] := Print@Module[{tab, newline}, Grid[Map[Row[#Grid[Map[TextCell[Row[#], ImageSize  PageWidth -> OptionValue[Options[PrettyPrint], ColumnWidths]] &, Split[#, # =!= tab &] & /@ Split[Flatten[ Replace[{args}, {s_String :> StringSplit[s, {"\t" -> tab, "\n" -> newline}]}, 1], 1], # =!= newline &], {2}] /. {tab | newline -> Nothing}, Alignment -> Left]] 

outputoutput

PrettyPrint[args__] := Print@Module[{tab, newline}, Grid[Map[Row, Split[#, # =!= tab &] & /@ Split[Flatten[ Replace[{args}, {s_String :> StringSplit[s, {"\t" -> tab, "\n" -> newline}]}, 1], 1], # =!= newline &], {2}] /. {tab | newline -> Nothing}, Alignment -> Left]] 

output

With an example TSV file:

Options[PrettyPrint] = {ColumnWidths -> Automatic}; PrettyPrint[args__] := Print@Module[{tab, newline}, Grid[Map[Row[#, ImageSize -> OptionValue[Options[PrettyPrint], ColumnWidths]] &, Split[#, # =!= tab &] & /@ Split[Flatten[ Replace[{args}, {s_String :> StringSplit[s, {"\t" -> tab, "\n" -> newline}]}, 1], 1], # =!= newline &], {2}] /. {tab | newline -> Nothing}, Alignment -> Left]] 

output

PrettyPrint[args__] := Print@Module[{tab, newline}, Grid[Map[TextCell[Row[#]] &, Split[#, # =!= tab &] & /@ Split[Flatten[ Replace[{args}, {s_String :> StringSplit[s, {"\t" -> tab, "\n" -> newline}]}, 1], 1], # =!= newline &], {2}] /. {tab | newline -> Nothing}, Alignment -> Left]] 

output

If you reduce the width of the Notebook window, columns widths are automatically adapted and proper word-wrapping is applied to long rows of text:

output

With an example TSV file imported as "Text":

Options[PrettyPrint] = {ColumnWidths -> Automatic}; PrettyPrint[args__] := Print@Module[{tab, newline}, Grid[Map[TextCell[Row[#],   PageWidth -> OptionValue[Options[PrettyPrint], ColumnWidths]] &, Split[#, # =!= tab &] & /@ Split[Flatten[ Replace[{args}, {s_String :> StringSplit[s, {"\t" -> tab, "\n" -> newline}]}, 1], 1], # =!= newline &], {2}] /. {tab | newline -> Nothing}, Alignment -> Left]] 

output

added 4 characters in body
Source Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405

You know that Print doesn't have options, because itsit must print arguments can be anythingof any type, including options. Hence if you want to handle options, you need either other syntax, or specify options separately.

You know that Print doesn't have options, because its arguments can be anything, including options. Hence if you want to handle options, you need either other syntax, or specify options separately.

You know that Print doesn't have options, because it must print arguments of any type, including options. Hence if you want to handle options, you need either other syntax, or specify options separately.

better implementation
Source Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405
Loading
Source Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405
Loading