@@ -2492,6 +2492,35 @@ void Query::view()
24922492 std::wstring wcommand;
24932493#endif
24942494
2495+ Screen::clear ();
2496+ Screen::invert ();
2497+ Screen::put (command.c_str ());
2498+ Screen::put (' ' );
2499+ Screen::put (filename.c_str ());
2500+ if (!partname.empty ())
2501+ {
2502+ Screen::put (' {' );
2503+ Screen::put (partname.c_str ());
2504+ Screen::put (' }' );
2505+ }
2506+ Screen::normal ();
2507+ Screen::setpos (1 , 0 );
2508+
2509+ #ifndef OS_WIN
2510+ // normal tty mode
2511+ VKey::cleanup ();
2512+ #endif
2513+
2514+ // executing the command is OK, unless checks fail, pipe fails, or command fails
2515+ bool ok = true ;
2516+
2517+ // file was changed when viewed, e.g. by an editor?
2518+ bool changed = false ;
2519+
2520+ // track elapsed time: if the command terminates very quickly within 500ms, then let the user press a key
2521+ reflex::timer_type et;
2522+ reflex::timer_start (et);
2523+
24952524 if (flag_stdin && filename == flag_label)
24962525 {
24972526 // standard input is viewed via a pipe to the pager
@@ -2521,8 +2550,7 @@ void Query::view()
25212550 catch (...)
25222551 {
25232552 // this should never happen, but just in case we ignore errors
2524- Screen::alert ();
2525- return ;
2553+ ok = false ;
25262554 }
25272555 }
25282556 }
@@ -2532,66 +2560,54 @@ void Query::view()
25322560 if (filename.empty () || filename.at (0 ) == ' /' || filename.find (' "' ) != std::string::npos)
25332561 {
25342562 // illegal filename in Windows, should never happen, but just in case
2535- Screen::alert ();
2536- return ;
2563+ ok = false ;
2564+ }
2565+ else
2566+ {
2567+ // view file in the pager using Windows _wsystem() call
2568+ command.append (" \" " ).append (filename).append (" \" " );
2569+ // Windows system() does not support non-ASCII, instead we use a wide string with _wsystem()
2570+ wcommand = utf8_decode (command);
2571+ // flush before calling _wsystem(), according to the Window's system API documentation
2572+ // _flushall(); removed because this may cause the TUI to freeze
25372573 }
2538- // view file in the pager using Windows _wsystem() call
2539- command.append (" \" " ).append (filename).append (" \" " );
2540- // Windows system() does not support non-ASCII, instead we use a wide string with _wsystem()
2541- wcommand = utf8_decode (command);
2542- // flush before calling _wsystem(), according to the Window's system API documentation
2543- // _flushall(); removed because this may cause the TUI to freeze
25442574#else
25452575 if (filename.find (' \' ' ) != std::string::npos)
25462576 {
25472577 // incorrect filename
2548- Screen::alert ();
2549- return ;
2578+ ok = false ;
2579+ }
2580+ else
2581+ {
2582+ // view file in the pager using system() call, double -- ends options
2583+ command.append (" -- '" ).append (filename).append (" '" );
25502584 }
2551- // view file in the pager using system() call, double -- ends options
2552- command.append (" -- '" ).append (filename).append (" '" );
2553- #endif
2554- }
2555-
2556- Screen::clear ();
2557- Screen::put (" Waiting on " );
2558- Screen::put (command.c_str ());
2559- Screen::put (" to finish" );
2560- Screen::home ();
2561-
2562- // pipe to pager was OK or executing the command is OK
2563- bool ok;
2564-
2565- if ((flag_stdin && filename == flag_label) || !partname.empty ())
2566- {
2567- ok = (pager != NULL );
2568- }
2569- else
2570- {
2571- #ifdef OS_WIN
2572- ok = (_wsystem (wcommand.c_str ()) == 0 );
2573- #else
2574- ok = (system (command.c_str ()) == 0 );
25752585#endif
25762586 }
25772587
25782588 if (ok)
25792589 {
2580- #ifdef OS_WIN
2581- if (strcmp (flag_view, " more" ) == 0 )
2590+ if ((flag_stdin && filename == flag_label) || !partname.empty ())
25822591 {
2583- Screen::setpos (Screen::rows - 1 , 0 );
2584- Screen::put (" (END) press a key" );
2585- Screen::alert ();
2586- VKey::flush ();
2587- VKey::get ();
2592+ // popen() pipe to pager is OK?
2593+ ok = (pager != NULL );
2594+
2595+ if (ok)
2596+ {
2597+ // close the pipe to the pager
2598+ pclose (pager);
2599+ pager = NULL ;
2600+ }
25882601 }
2602+ else
2603+ {
2604+ // execute command, check if OK
2605+ #ifdef OS_WIN
2606+ ok = (_wsystem (wcommand.c_str ()) == 0 );
2607+ #else
2608+ ok = (system (command.c_str ()) == 0 );
25892609#endif
25902610
2591- bool changed = false ;
2592-
2593- if (pager == NULL )
2594- {
25952611 // check if file was changed by the pager (when it is an editor)
25962612#ifdef OS_WIN
25972613 _WIN32_FILE_ATTRIBUTE_DATA attr_after;
@@ -2609,28 +2625,50 @@ void Query::view()
26092625#endif
26102626#endif
26112627 }
2612- else
2613- {
2614- // close the pipe to the pager
2615- pclose (pager);
2616- pager = NULL ;
2617- }
2628+ }
26182629
2619- if (changed)
2620- {
2621- // file is changed, update the search results
2622- search ();
2623- jump (ref);
2624- }
2625- else
2630+ #ifndef OS_WIN
2631+ // resume RAW tty mode and flush the key buffer
2632+ VKey::setup (VKey::TTYRAW);
2633+ #else
2634+ // flush the key buffer
2635+ VKey::flush ();
2636+ #endif
2637+
2638+ if (ok)
2639+ {
2640+ float ms = reflex::timer_elapsed (et);
2641+
2642+ #ifdef OS_WIN
2643+ if (ms < 500 || strcmp (flag_view, " more" ) == 0 )
2644+ #else
2645+ if (ms < 500 )
2646+ #endif
26262647 {
2627- redraw ();
2648+ // command terminated very quickly within 500ms
2649+ Screen::setpos (Screen::rows - 1 , 0 );
2650+ Screen::invert ();
2651+ Screen::put (" (END)" );
2652+ Screen::normal ();
2653+ Screen::put (" press a key " );
2654+ VKey::get ();
26282655 }
26292656 }
2657+
2658+ if (changed)
2659+ {
2660+ // file is changed, update the search results
2661+ search ();
2662+ jump (ref);
2663+ }
26302664 else
26312665 {
2632- Screen::alert ();
26332666 redraw ();
2667+ }
2668+
2669+ if (!ok)
2670+ {
2671+ Screen::alert ();
26342672 message (std::string (" failed: " ).append (command));
26352673 }
26362674 }
0 commit comments