#!/usr/bin/tclsh
#

package require Tk


set cmdRun ""
# Color for window
set colors { {image white red} {stack gray50 white} {volume gray50 orange} }
set fp1 ""

proc addID { id } {
	.e insert end $id
}

proc eDocker { cmd } {
	set app [ auto_execok docker ]
	set run [string trim "$app $cmd" ]

	return [ string trim [ eval exec -keepnewline -- $run ] ]
}

proc oDocker { cmd } {
	global fp1

	if { $cmd == "" } { set cmd "--help" }

	set app [ auto_execok docker ]

	set fp1 [open "| $app  $cmd" r]
	fileevent $fp1 readable display
}

proc display { } {
	global fp1

  	if { [ gets $fp1 line ] >=0 } {
		.txt insert end "$line\n"
	} else { 
  		close $fp1
		displayListing
	}
}


proc toField { txt } {
	set out [ string map {"  "  "\t" } $txt]
	set out [ string map {"\t "  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string map {"\t\t"  "\t" } $out]
	set out [ string trim $out ] 

	return [ split $out '\t\n' ]
}

proc toList { result } {
	set out [ list ]
       	set lst [ split $result '\n']
	
	foreach line $lst {
		lappend out [ toField $line ]
	}
	return $out
}

proc displayDocker { cmd } {
	global colors

	set w [ lindex [ split $cmd " "] 0 ]

	# run docker command
	set lst [ toList [ eDocker $cmd ] ]
	# Get title from docker return
	set titles [ lindex $lst 0 ]
	set nbf  [ llength $titles ]
	# Get datas form docker return
	set datas  [ lrange $lst 1 end ]

	

	set col 0
	set row 0
	if { [ winfo exists .$w ] == 0 } {
		toplevel .$w
		focus .$w
		foreach title $titles {
			set name [ string map { " " "_" } $title ]
			button .${w}.t_$name -text "$title"
			grid .${w}.t_$name -row $row -column $col -sticky ew
			incr col
		}
	} else {
		focus .$w
		foreach children [ winfo children .$w ] {
			set tokill [ regexp {\.[lb]d_} $children ]
			if { $tokill == 1 } {
				grid remove $children
				destroy $children
			}
		}

	}
	set row 1
	# fo every line of data
	foreach data $datas {
		set col 0
		# for every field
		foreach field $data {
			set name [ string map { " " "_" } $field ]
			if { [ string first ID [ lindex $titles $col ]] == -1 } {
				label  .${w}.ld_$row$col -text "$field"
				grid .${w}.ld_$row$col -row $row -column $col -sticky ew
			} else {
				button .${w}.bd_$row$col -text "$field" -command "addID $field" -fg orange -activebackground orange -activeforeground white
				grid .${w}.bd_$row$col -row $row -column $col -sticky ew
			}
			incr col
		}
		incr row
	}

}

proc runDockerCommand { } {
	global cmdRun

	.txt delete 0.0 end
	.txt insert end [ oDocker $cmdRun ]
	.txt insert end "\n"
	set cmdRun ""
	update
	displayListing
}

proc displayListing { } {
	foreach cmd [ list "image list" "volume ls" "container ps"  "app ls" "network ls" "ps" "context ls" "plugin ls" "service ls" "stack ls" ] {
		displayDocker $cmd
	}
}

text .txt 
pack .txt -fill both -expand 1
entry .e -textvariable cmdRun
button .b -text "Run" -command runDockerCommand
pack .b -side right 
pack .e -side right -padx 0 -pady 0 -fill x -expand 1

displayListing

bind . <Control-q> { exit }
bind .e <Return> { runDockerCommand }
