#!/usr/bin/env ruby

require "curses"
include Curses

###############################################################################
# compile
###############################################################################

$LINES = []
$CURSOR = 0
$SCROLL_TOP = 0

make_name = ""
if ARGV.size == 1
    make_name = ARGV[0]
else
    make_name = "make -k"
end

init_screen()
crmode()
raw()
noecho()
stdscr.keypad(TRUE)

### make ###
clear()
stdscr.setpos(0,0)
refresh()

output = `#{make_name} 2>&1 1>/dev/null`

### split ###
i = 0
tmp = ""
x = 0
while i < output.size()
    if output[i] == ?\n
        if tmp != ""
            $LINES.push(tmp)
        end
        tmp = ""
        x = 0
    elsif x == stdscr.maxx()-1
        tmp = tmp + output[i].chr

        if tmp != ""       
            $LINES.push(tmp)
        end
        tmp = ""
        x = 0
    else
        tmp = tmp + output[i].chr
        x = x + 1
    end

    i = i + 1
end

### loop ###
if $LINES.size > 0 
    key = [0, 0]    
    while true
        ### view ###
        clear()
        n = $SCROLL_TOP
        while n < $LINES.size
            if $CURSOR == n
                stdscr.standout()
            end
            
            stdscr.setpos(n-$SCROLL_TOP, 0)
            stdscr << $LINES[n]
            
            if $CURSOR == n
                stdscr.standend()
            end
            
            n = n + 1
        end
        
        refresh()

        ### input ###
        key = getch()

        if key == 14 || key == Key::DOWN 
            $CURSOR = $CURSOR + 1
        end
        if key == 16 || key == Key::UP 
            $CURSOR = $CURSOR -1
        end
        if key == 4 || key == Key::NPAGE
            $CURSOR = $CURSOR + stdscr.maxy / 2
        end
        if key == 21 || key == Key::PPAGE
            $CURSOR = $CURSOR - stdscr.maxy / 2
        end
        if key == 12
            clear
            refresh
        end

        if key == ?q || key == 3
            $LINES = []
            $CURSOR = 0
            $SCROLL_TOP = 0
            break;
        end
        if key == 27 || key == 7
            break;
        end
        
        if key == 10 || key == 13
            array = $LINES[$CURSOR].split(':')

            if !File.exist?(array[0])
                output = `find . -name #{array[0]}`
                array2 = output.split
                
                if output.size != 0 && array2.size == 1
                    array[0] = output.chomp
                end
                if array2.size >  1
                    clear()                    
                    stdscr.setpos(0,0)
                    stdscr << "find two or more " + array[1]
                    refresh()
                    getch()
                    
                    #array[0] = array2[0].chomp
                end
            end

            if File.exist?(array[0]) && array[1].to_i != 0
                # array[1] == line number
                # array[0] == file name

                close_screen()

                system("#{ENV['EDITOR']} +" + array[1] + " " + array[0])

                init_screen()
                crmode()
                raw()
                noecho()
                stdscr.keypad(TRUE)

            end
        end

        ### modification ###
        if $CURSOR < 0
            $CURSOR = 0
        end
        if $CURSOR >= $LINES.size
            $CURSOR = $LINES.size - 1
        end
        if $CURSOR < $SCROLL_TOP
             $SCROLL_TOP = $CURSOR
        end
        if $CURSOR > $SCROLL_TOP + stdscr.maxy() - 1
            $SCROLL_TOP = $SCROLL_TOP + ($CURSOR-$SCROLL_TOP-stdscr.maxy()) + 1
        end
    end
end

close_screen()

