Debian Codegraph @

bin/data-viewer

raw
1 #!/usr/bin/env perl
2 #
3 # This file is part of Tk-ObjScanner
4 #
5 # This software is copyright (c) 1997-2004,2007,2014,2017,2023 by Dominique Dumont.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10
11 use 5.016;
12 use strict;
13 use warnings;
14 use Tk;
15 use Tk::ObjScanner;
16 use YAML::PP;
17 use JSON;
18 use Path::Tiny;
19 use warnings qw(FATAL utf8); # fatalize encoding glitches
20 use open qw(:std :utf8); # undeclared streams in UTF-8
21
22 use Getopt::Long;
23
24 my $opt = {};
25
26 GetOptions($opt, 'yaml!', 'json!')
27 or die("Error in command line arguments\n");
28
29 my $file = shift @ARGV // '';
30 my $format = $opt->{yaml} ? 'yaml'
31 : $opt->{json} ? 'json'
32 : $file =~ /\.ya?ml$/i ? 'yaml'
33 : $file =~ /\.json/i ? 'json'
34 : die "Unknown format for ", $file ? "file $file" : "standard input. Please use -yaml or -json option";
35
36 my $string = $file ? path($file)->slurp_utf8($file) : join('', <> );
37 my $ypp = YAML::PP->new;
38
39 my $data = $format eq 'yaml' ? $ypp->load_string( $string )
40 : decode_json ($string) ;
41
42 my $mw = MainWindow->new;
43 # $mw->geometry('+10+10');
44 my $s = $mw->ObjScanner(
45 '-caller' => $data,
46 -destroyable => 1,
47 -destroy_label => "quit",
48 -show_tied => 0,
49 -title => 'data viewer'
50 );
51
52 $s->pack( -expand => 1, -fill => 'both' );
53 $s->OnDestroy( sub { $mw->destroy; } );
54
55 MainLoop; # Tk's
56
57
58 __END__
59
60 =pod
61
62 =encoding UTF-8
63
64 =head1 NAME
65
66 data_viewer - Open a data viewer widget for you JSON or YAML data
67
68 =head1 SYNOPSIS
69
70 # view json file
71 data_viewer file.json
72
73 # viewn yaml file
74 data_viewer file.yml
75
76 # pipe json data
77 data_viewer -json < file.json
78
79 # pipe yaml data
80 data_viewer -yaml < file.yml
81
82 =head1 DESCRIPTION
83
84 This command opens a L<Tk::ObjScanner> widget for the data stored in
85 YAML or JSON file.
86
87 This command can be applied on a file passed as argument or data can
88 be piped to C<data_viewer>. Yaml file suffix can be C<yaml> or C<yml>.
89
90 =head1 OPTIONS
91
92 =head2 -json
93
94 Parse the data as JSON.
95
96 =head2 -yaml
97
98 Parse the data as YAML.
99
100 =head1 BUGS
101
102 =over
103
104 =item *
105
106 YAML anchor and aliases are ignored. Data is seen as duplicated in the widget.
107
108 =item *
109
110 JSON object are showed as C<HASH>, which is Perl terminology.
111
112 =back
113
114 =head1 SEE ALSO
115
116 L<Tk::ObjScanner>
117
118 =cut
119
120 =cut
121
122
123
124