Class: CoMe_UI

Inherits:
Object
  • Object
show all
Includes:
Glimmer
Defined in:
lib/come_ui.rb

Overview

GUI for CoMe_NG

Author:

  • PRV

Version:

  • 1.2.3

Defined Under Namespace

Classes: LogEntry

Constant Summary collapse

BG_COLOR =
:ghost_white
COLORS =
{
  red: "\e[31m",
  green: "\e[32m",
  blue: "\e[34m"
}.freeze
@@logtext =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoMe_UI

Initialize vars and launch the UI



40
41
42
43
44
45
46
47
48
# File 'lib/come_ui.rb', line 40

def initialize
  @filename = ''
  @outfile = ''
  @logs = [LogEntry.new(['⬆️ Logs will appear above ⬆️', :black], BG_COLOR)]
  @logs << LogEntry.new(['=======================', :black], BG_COLOR)
  @logs = @logs.reverse

  launch
end

Instance Attribute Details

#logsObject

Returns the value of attribute logs.



37
38
39
# File 'lib/come_ui.rb', line 37

def logs
  @logs
end

Instance Method Details

#create_windowUI::Window

Note:

It follows the Glimmer DSL syntax.

Create the UI window with its elements and logic.

Returns:

  • (UI::Window)

    the UI window

See Also:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/come_ui.rb', line 63

def create_window
  window('CoMe', 720, 480) do
    margined true

    vertical_box do
      @label = label('Aucun fichier ouvert...')
      horizontal_box do
        button('Ouvrir une SITAC') do
          on_clicked do
            @filename = open_file
            next unless @filename

            # update label
            @label.text = "📖 Fichier ouvert : #{@filename}"
          end
        end
        button('Générer le KML') do
          on_clicked do
            next unless @filename

            # lexer
            lexer = XMLLexer.new(@filename, 'ntk')
            tokens = lexer.tokenize

            # parser
            parser = NorthropParser.new(tokens)
            parser.parse_figures

            # make kml
            kml = KMLMaker.new
            kml.build(parser.figures, parser.name)
            # ask for output file
            output = open_folder
            next unless output

            # output += '.kml' unless output.include?('.kml')
            # write kml to file
            output = kml.export(output)
            @outfile = output

            # update label
            @label.text = "✅ Fichier généré : #{output}"
            @openbtn.enabled = true
          end
        end
        @openbtn = button('Afficher') do
          next unless @outfile

          # get system type (Darwin, Linux, Windows)
          sysname = RbConfig::CONFIG['host_os']

          on_clicked do
            case sysname
            when /darwin/
              system("open -R #{@outfile}") # open file in finder
            when /linux/
              system("xdg-open #{@outfile}") # open file in file manager
            when /mswin|mingw|cygwin/
              system("explorer #{@outfile}") # open file in explorer
            else
              Log.err('Unsupported OS', 'CoMe')
            end
          end
        end
        @openbtn.enabled = false
      end
      # display $stdout in a pretty colored table
      table do
        background_color_column
        text_color_column("Logs")
        editable false
        # bind to @logs
        cell_rows <=> [self, :logs]
      end
    end

    get_stdout

    # observe @@logtext for changes
    DataBinding::Observer.proc do |value|
      next if value.last.nil?

      @logs.unshift(value.last)
    end.observe(@@logtext)

    puts 'CoMe UI initialized'
  end

end

#get_stdoutObject

Redirect $stdout and write to @@logtext



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/come_ui.rb', line 154

def get_stdout
  $stdout = StringIO.new
  $stdout.sync = true

  # data stream to @logtext on every write
  $stdout.extend(Module.new do
    def write(str)
      # get color key from color codes
      color = COLORS.key(str.match(/\e\[(\d+)m/).to_s)
      # delete color codes
      str = str.gsub(/\e\[(\d+)m/, '')

      # add to @@logtext
      @@logtext << LogEntry.new([str, color], BG_COLOR)
    end
  end)
end

#launchObject

Create a UI window and display it



51
52
53
54
55
56
57
# File 'lib/come_ui.rb', line 51

def launch
  @window = create_window
  @window.show
ensure
  # redirect $stdout to original stream upon closing
  $stdout = STDOUT
end