Skip to content

Commit bda2ee4

Browse files
committed
feat: Support reading from stdin in stylish-haskell-lhs.
1 parent 00aec25 commit bda2ee4

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

bin/stylish-haskell-lhs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use v5.10.1;
88

99
use Data::Dumper;
1010
use File::Find;
11+
use File::Temp qw/tempfile/;
1112
use IPC::Open2;
1213

1314
$Data::Dumper::Sortkeys = 1;
@@ -19,8 +20,6 @@ shift @ARGV if $INPLACE;
1920

2021
my $STYLISH_HASKELL = ($0 =~ /stylish-haskell$/ ? "stylish-haskell.distrib" : "stylish-haskell");
2122

22-
my @FILES = @ARGV or die "Usage: $0 [-i] <file|dir>...\n(will invoke $STYLISH_HASKELL after preprocessing)\n";
23-
2423
sub parse_file {
2524
my ($file) = @_;
2625

@@ -67,8 +66,23 @@ sub stylish_haskell {
6766
sub format_file {
6867
my ($file) = @_;
6968

70-
if ($file =~ /\.hs$/) {
71-
system $STYLISH_HASKELL, "-i", $file;
69+
my $is_lhs = 0;
70+
if (not defined $file) {
71+
die "-i cannot be used with stdin" if $INPLACE;
72+
# No file => read stdin.
73+
(my $fh, $file) = tempfile SUFFIX => ".hs";
74+
while (<>) {
75+
print $fh $_;
76+
$is_lhs = 1 if /^\\begin/;
77+
}
78+
}
79+
80+
if (not $is_lhs and $file =~ /\.hs$/) {
81+
if ($INPLACE) {
82+
system $STYLISH_HASKELL, "-i", $file;
83+
} else {
84+
system $STYLISH_HASKELL, $file;
85+
}
7286
} else {
7387
my ($lines, $docs, @code) = parse_file $file;
7488
@code = stylish_haskell @code;
@@ -105,7 +119,11 @@ sub format_file {
105119
}
106120
}
107121

108-
for my $file (@FILES) {
122+
if (not @ARGV) {
123+
format_file undef;
124+
}
125+
126+
for my $file (@ARGV) {
109127
if (-f $file) {
110128
format_file $file;
111129
} else {

0 commit comments

Comments
 (0)